1
#ifndef _RHEO_DIS_INNER_PRODUCT_H
2
#define _RHEO_DIS_INNER_PRODUCT_H
4
/// This file is part of Rheolef.
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
8
/// Rheolef is free software; you can redistribute it and/or modify
9
/// it under the terms of the GNU General Public License as published by
10
/// the Free Software Foundation; either version 2 of the License, or
11
/// (at your option) any later version.
13
/// Rheolef is distributed in the hope that it will be useful,
14
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
/// GNU General Public License for more details.
18
/// You should have received a copy of the GNU General Public License
19
/// along with Rheolef; if not, write to the Free Software
20
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
/// =========================================================================
24
#include "rheolef/promote.h"
25
#include "rheolef/distributed.h"
30
NAME: dis_inner_product - distributed inner product algorithm (@PACKAGE@-@VERSION@)
32
STL-like inner product for distributed containers
36
template<class InputIterator1, class InputIterator2, class Size>
38
InputIterator1 first1, InputIterator2 first2,
42
A sample usage writes:
44
# include "rheolef/array.h"
45
# include "rheolef/dis_inner_product.h"
46
int main(int argc, char**argv) {
47
environment distributed(argc, argv);
49
array<double> x(n, 2.0);
50
double norme2_x = dis_inner_product(x.begin(),x.begin(),x.size(),x.comm());
51
dcout << "dot(x,x) = " << norme2_x << endl;
55
The std::inner_product(first1,first2,last1) function is similar but not used here.
56
Use here both two "first" iterators and the size "n", since
57
expression template approach generates iterators as expression
58
tree and a comparison like "first1 != last1" becomes complex and
59
requires a recursive inspection.
62
LMC-IMAG, 38041 Grenoble cedex 9, France
63
| Pierre.Saramito@imag.fr
64
DATE: 24 november 1998
67
template <class InputIterator1, class InputIterator2, class Size>
69
typename std::iterator_traits<InputIterator1>::value_type,
70
typename std::iterator_traits<InputIterator2>::value_type
73
InputIterator1 first1,
74
InputIterator2 first2,
76
const distributor::communicator_type& comm,
77
sequential /* memory_model */)
79
typedef typename std::iterator_traits<InputIterator1>::value_type T;
81
for (Size i = 0; i < n; ++i, ++first1, ++first2) {
82
sum = sum + (*first1)*(*first2);
86
#ifdef _RHEOLEF_HAVE_MPI
87
template <class InputIterator1, class InputIterator2, class Size>
90
typename std::iterator_traits<InputIterator1>::value_type,
91
typename std::iterator_traits<InputIterator2>::value_type
94
InputIterator1 first1,
95
InputIterator2 first2,
97
const distributor::communicator_type& comm,
98
distributed /* memory_model */)
100
typedef typename std::iterator_traits<InputIterator1>::value_type T;
101
T local_sum = dis_inner_product (first1, first2, n, comm, sequential());
102
return mpi::all_reduce (comm, local_sum, std::plus<T>());
104
#endif // _RHEOLEF_HAVE_MPI
106
template <class InputIterator1, class InputIterator2, class Size>
109
typename std::iterator_traits<InputIterator1>::value_type,
110
typename std::iterator_traits<InputIterator2>::value_type
113
InputIterator1 first1,
114
InputIterator2 first2,
116
const distributor::communicator_type& comm)
118
return dis_inner_product (first1, first2, n, comm, rheo_default_memory_model());
121
} // namespace rheolef
122
#endif // _RHEO_DIS_INNER_PRODUCT_H