QMCPACK
inner_product.hpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Raymond Clay III, j.k.rofling@gmail.com, Lawrence Livermore National Laboratory
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 //
12 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 /** @file inner_product.h
17  *
18  * Inline dot and gemv functions.
19  * On modern processors with good compilers, there is no need to use blas1, e.g., ddot, but
20  * strange things can happen on some machines.
21  * SIMD specialization can be implemented here.
22  */
23 #ifndef QMCPLUSPLUS_INNER_PRODUCT_HPP
24 #define QMCPLUSPLUS_INNER_PRODUCT_HPP
25 
26 #include "OhmmsPETE/TinyVector.h"
27 #include "OhmmsPETE/Tensor.h"
28 
29 namespace qmcplusplus
30 {
31 namespace simd
32 {
33 /** dot product
34  * @param a starting address of an array of type T
35  * @param b starting address of an array of type T
36  * @param n size
37  * @param res initial value, can be used to override precision in reduction
38  * @return \f$ res = \sum_i a[i] b[i]\f$
39  */
40 template<typename T, typename TRES = T>
41 inline T dot(const T* restrict a, const T* restrict b, int n, TRES res = TRES())
42 {
43  for (int i = 0; i < n; i++)
44  res += a[i] * b[i];
45  return res;
46 }
47 
48 /** inline dot product
49  * @param a starting address of an array of type T
50  * @param b starting address of an array of type TinyVector<T,D>
51  * @param n size
52  * @param res initial value, can be used to override precision in reduction
53  */
54 template<class T, unsigned D, class TRES = T>
55 inline TinyVector<T, D> dot(const T* a, const TinyVector<T, D>* b, int n, TinyVector<TRES, D> res = TinyVector<T, D>())
56 {
57  for (int i = 0; i < n; i++)
58  res += a[i] * b[i];
59  return res;
60 }
61 
62 /** inline dot product
63  * @param a starting address of an array of type T
64  * @param b starting address of an array of type Tensor<T, D>
65  * @param n size
66  * @param res initial value, can be used to override precision in reduction
67  * @return \f$ {\bf v} = \sum_i a[i] {\bf b}[i]\f$
68  */
69 template<class T, unsigned D, class TRES = T>
70 inline Tensor<T, D> dot(const T* a, const Tensor<T, D>* b, int n, Tensor<TRES, D> res = Tensor<TRES, D>())
71 {
72  for (int i = 0; i < n; i++)
73  res += a[i] * b[i];
74  return res;
75 }
76 
77 /** x*y dot product of two vectors using the same argument list for blas::dot
78  * @param n size
79  * @param x starting address of x
80  * @param incx stride of x
81  * @param y starting address of y
82  * @param incx stride of y
83  * @param return \f$\sum_i x[i+=incx]*y[i+=incy]\f$
84  */
85 template<typename T>
86 inline T dot(int n, const T* restrict x, int incx, const T* restrict y, int incy)
87 {
88  const int xmax = incx * n;
89  T res = 0.0;
90  for (int ic = 0, jc = 0; ic < xmax; ic += incx, jc += incy)
91  res += x[ic] * y[jc];
92  return res;
93 }
94 
95 } // namespace simd
96 } // namespace qmcplusplus
97 #endif
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
T dot(const T *restrict a, const T *restrict b, int n, TRES res=TRES())
dot product
Tensor<T,D> class for D by D tensor.
Definition: OhmmsTinyMeta.h:32