Rheolef  7.2
an efficient C++ finite element environment
 
Loading...
Searching...
No Matches
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// This file is part of Rheolef.
5//
6// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
7//
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.
12//
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.
17//
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
21//
22// =========================================================================
23
24#include "rheolef/promote.h"
25#include "rheolef/communicator.h"
26#include <iterator>
27namespace rheolef {
28
29/*D:dis_inner_product
30NAME: dis_inner_product - distributed inner product algorithm (@PACKAGE@-@VERSION@)
31DESCRIPTION:
32 STL-like inner product for distributed containers
33 environment.
34SYNOPSIS:
35 @example
36 template<class InputIterator1, class InputIterator2, class Size>
37 T dis_inner_product (
38 InputIterator1 first1, InputIterator2 first2,
39 Size n);
40 @end example
41EXAMPLE:
42 A sample usage writes:
43 @example
44 # include "rheolef/disarray.h"
45 # include "rheolef/dis_inner_product.h"
46 int main(int argc, char**argv) {
47 environment distributed(argc, argv);
48 unsigned int n = 100;
49 disarray<double> x(n, 2.0);
50 double norme2_x = dis_inner_product(x.begin(),x.begin(),x.size(),x.comm());
51 dout << "dot(x,x) = " << norme2_x << endl;
52 }
53 @end example
54IMPLEMENTATION NOTE:
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.
60SEE ALSO: "disarray"(1)
61AUTHORS:
62 LMC-IMAG, 38041 Grenoble cedex 9, France
63 | Pierre.Saramito@imag.fr
64DATE: 24 november 1998
65End:
66*/
67template <class InputIterator1, class InputIterator2, class Size>
68typename promote<
69 typename std::iterator_traits<InputIterator1>::value_type,
70 typename std::iterator_traits<InputIterator2>::value_type
71>::type
73 InputIterator1 first1,
74 InputIterator2 first2,
75 Size n,
77 sequential /* memory_model */)
78{
79 typedef typename std::iterator_traits<InputIterator1>::value_type T;
80 T sum = T(0);
81 for (Size i = 0; i < n; ++i, ++first1, ++first2) {
82 sum = sum + (*first1)*(*first2);
83 }
84 return sum;
85}
86#ifdef _RHEOLEF_HAVE_MPI
87template <class InputIterator1, class InputIterator2, class Size>
88inline
89typename promote<
90 typename std::iterator_traits<InputIterator1>::value_type,
91 typename std::iterator_traits<InputIterator2>::value_type
92>::type
94 InputIterator1 first1,
95 InputIterator2 first2,
96 Size n,
98 distributed /* memory_model */)
99{
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>());
103}
104#endif // _RHEOLEF_HAVE_MPI
105
106template <class InputIterator1, class InputIterator2, class Size>
107inline
108typename promote<
109 typename std::iterator_traits<InputIterator1>::value_type,
110 typename std::iterator_traits<InputIterator2>::value_type
111>::type
113 InputIterator1 first1,
114 InputIterator2 first2,
115 Size n,
117{
118 return dis_inner_product (first1, first2, n, comm, rheo_default_memory_model());
119}
120
121} // namespace rheolef
122#endif // _RHEO_DIS_INNER_PRODUCT_H
communicator communicator_type
Definition distributor.h:79
#define rheo_default_memory_model
Expr1::float_type T
Definition field_expr.h:230
This file is part of Rheolef.
promote< typenamestd::iterator_traits< InputIterator1 >::value_type, typenamestd::iterator_traits< InputIterator2 >::value_type >::type dis_inner_product(InputIterator1 first1, InputIterator2 first2, Size n, const distributor::communicator_type &comm, sequential)