rheolef  6.5
dis_inner_product.h
Go to the documentation of this file.
1 #ifndef _RHEO_DIS_INNER_PRODUCT_H
2 #define _RHEO_DIS_INNER_PRODUCT_H
3 
4 #include "rheolef/promote.h"
5 #include "rheolef/distributed.h"
6 #include <iterator>
7 namespace rheolef {
8 
49 template <class InputIterator1, class InputIterator2, class Size>
50 typename promote<
51  typename std::iterator_traits<InputIterator1>::value_type,
52  typename std::iterator_traits<InputIterator2>::value_type
53 >::type
55  InputIterator1 first1,
56  InputIterator2 first2,
57  Size n,
59  sequential /* memory_model */)
60 {
61  typedef typename std::iterator_traits<InputIterator1>::value_type T;
62  T sum = T(0);
63  for (Size i = 0; i < n; ++i, ++first1, ++first2) {
64  sum = sum + (*first1)*(*first2);
65  }
66  return sum;
67 }
68 #ifdef _RHEOLEF_HAVE_MPI
69 template <class InputIterator1, class InputIterator2, class Size>
70 inline
71 typename promote<
72  typename std::iterator_traits<InputIterator1>::value_type,
73  typename std::iterator_traits<InputIterator2>::value_type
74 >::type
76  InputIterator1 first1,
77  InputIterator2 first2,
78  Size n,
80  distributed /* memory_model */)
81 {
82  typedef typename std::iterator_traits<InputIterator1>::value_type T;
83  T local_sum = dis_inner_product (first1, first2, n, comm, sequential());
84  return mpi::all_reduce (comm, local_sum, std::plus<T>());
85 }
86 #endif // _RHEOLEF_HAVE_MPI
87 
88 template <class InputIterator1, class InputIterator2, class Size>
89 inline
90 typename promote<
91  typename std::iterator_traits<InputIterator1>::value_type,
92  typename std::iterator_traits<InputIterator2>::value_type
93 >::type
95  InputIterator1 first1,
96  InputIterator2 first2,
97  Size n,
99 {
100  return dis_inner_product (first1, first2, n, comm, rheo_default_memory_model());
101 }
102 
103 } // namespace rheolef
104 #endif // _RHEO_DIS_INNER_PRODUCT_H
105