overlappingscalarproduct.hh
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  Copyright (C) 2011-2013 by Andreas Lauser
5  Copyright (C) 2012 by Bernd Flemisch
6 
7  This file is part of the Open Porous Media project (OPM).
8 
9  OPM is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 2 of the License, or
12  (at your option) any later version.
13 
14  OPM is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OPM. If not, see <http://www.gnu.org/licenses/>.
21 */
26 #ifndef EWOMS_OVERLAPPING_SCALAR_PRODUCT_HH
27 #define EWOMS_OVERLAPPING_SCALAR_PRODUCT_HH
28 
29 #if HAVE_MPI
30 #include <mpi.h>
31 #endif
32 
33 #include <dune/istl/scalarproducts.hh>
34 
35 namespace Ewoms {
36 namespace Linear {
37 
41 template <class OverlappingBlockVector, class Overlap>
43  : public Dune::ScalarProduct<OverlappingBlockVector>
44 {
45 public:
46  typedef typename OverlappingBlockVector::field_type field_type;
47 
48  enum { category = Dune::SolverCategory::overlapping };
49 
50  OverlappingScalarProduct(const Overlap &overlap) : overlap_(overlap)
51  {}
52 
53  field_type dot(const OverlappingBlockVector &x,
54  const OverlappingBlockVector &y)
55  {
56  double sum = 0;
57  int numLocal = overlap_.numLocal();
58  for (int localIdx = 0; localIdx < numLocal; ++localIdx) {
59  if (overlap_.iAmMasterOf(localIdx))
60  sum += x[localIdx] * y[localIdx];
61  }
62 
63  // compute the global sum
64  double sumGlobal = 0.0;
65 #if HAVE_MPI
66  MPI_Allreduce(&sum, // source buffer
67  &sumGlobal, // destination buffer
68  1, // number of objects in buffers
69  MPI_DOUBLE, // data type
70  MPI_SUM, // operation
71  MPI_COMM_WORLD); // communicator
72 #else
73  sumGlobal = sum;
74 #endif // HAVE_MPI
75 
76  return sumGlobal;
77  }
78 
79  double norm(const OverlappingBlockVector &x)
80  { return std::sqrt(dot(x, x)); }
81 
82 private:
83  const Overlap &overlap_;
84 };
85 
86 } // namespace Linear
87 } // namespace Ewoms
88 
89 #endif
An overlap aware ISTL scalar product.
Definition: overlappingscalarproduct.hh:42
double norm(const OverlappingBlockVector &x)
Definition: overlappingscalarproduct.hh:79
An overlap aware block vector.
Definition: overlappingblockvector.hh:47
OverlappingScalarProduct(const Overlap &overlap)
Definition: overlappingscalarproduct.hh:50
Definition: baseauxiliarymodule.hh:35
Definition: overlappingscalarproduct.hh:48
field_type dot(const OverlappingBlockVector &x, const OverlappingBlockVector &y)
Definition: overlappingscalarproduct.hh:53
OverlappingBlockVector::field_type field_type
Definition: overlappingscalarproduct.hh:46