~ubuntu-branches/ubuntu/quantal/rheolef/quantal

« back to all changes in this revision

Viewing changes to skit/plib2/dis_inner_product.h

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Saramito
  • Date: 2011-03-26 08:10:11 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110326081011-378s3slxnhwhn1da
Tags: 5.93-2
* debian/control:
  - "vtk-tcl" dependency changed to "tcl-vtk|vtk-tcl" (closes: #619917)
  - "doxygen" dependency removed                      (closes: #616276)
* debian/rheolef-doc.install : add refman in .info format
* debian/rheolef-doc.doc-base.refman: created for .pdf .html & .info
* debian/rules:
  - clean some obsolete Makefile commandes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/distributed.h"
 
26
#include <iterator>
 
27
namespace rheolef {
 
28
 
 
29
/*D:dis_inner_product
 
30
NAME:  dis_inner_product - distributed inner product algorithm (@PACKAGE@-@VERSION@)
 
31
DESCRIPTION:       
 
32
 STL-like inner product for distributed containers
 
33
 environment.
 
34
SYNOPSIS:
 
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
 
41
EXAMPLE:
 
42
   A sample usage writes:
 
43
   @example
 
44
     # include "rheolef/array.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
         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;
 
52
     }
 
53
   @end example
 
54
IMPLEMENTATION 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.
 
60
SEE ALSO: "array"(1)
 
61
AUTHORS:
 
62
    LMC-IMAG, 38041 Grenoble cedex 9, France
 
63
   | Pierre.Saramito@imag.fr
 
64
DATE:   24 november 1998
 
65
End:
 
66
*/
 
67
template <class InputIterator1, class InputIterator2, class Size>
 
68
typename promote<
 
69
  typename std::iterator_traits<InputIterator1>::value_type,
 
70
  typename std::iterator_traits<InputIterator2>::value_type
 
71
>::type
 
72
dis_inner_product (
 
73
        InputIterator1 first1,
 
74
        InputIterator2 first2,
 
75
        Size n,
 
76
        const distributor::communicator_type& comm,
 
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
 
87
template <class InputIterator1, class InputIterator2, class Size>
 
88
inline
 
89
typename promote<
 
90
  typename std::iterator_traits<InputIterator1>::value_type,
 
91
  typename std::iterator_traits<InputIterator2>::value_type
 
92
>::type
 
93
dis_inner_product (
 
94
        InputIterator1 first1,
 
95
        InputIterator2 first2,
 
96
        Size n,
 
97
        const distributor::communicator_type& comm,
 
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
 
 
106
template <class InputIterator1, class InputIterator2, class Size>
 
107
inline
 
108
typename promote<
 
109
  typename std::iterator_traits<InputIterator1>::value_type,
 
110
  typename std::iterator_traits<InputIterator2>::value_type
 
111
>::type
 
112
dis_inner_product (
 
113
        InputIterator1 first1,
 
114
        InputIterator2 first2,
 
115
        Size n,
 
116
        const distributor::communicator_type& comm)
 
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