| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
#ifndef DUNE_ISTL_SCALARPRODUCTS_HH |
| 6 |
#define DUNE_ISTL_SCALARPRODUCTS_HH |
| 7 |
|
| 8 |
#include <cmath> |
| 9 |
#include <complex> |
| 10 |
#include <iostream> |
| 11 |
#include <iomanip> |
| 12 |
#include <string> |
| 13 |
#include <memory> |
| 14 |
|
| 15 |
#include <dune/common/exceptions.hh> |
| 16 |
#include <dune/common/shared_ptr.hh> |
| 17 |
|
| 18 |
#include "bvector.hh" |
| 19 |
#include "solvercategory.hh" |
| 20 |
|
| 21 |
|
| 22 |
namespace Dune { |
| 23 |
/** |
| 24 |
* @defgroup ISTL_SP Scalar products |
| 25 |
* @ingroup ISTL_Solvers |
| 26 |
* @brief Scalar products for the use in iterative solvers |
| 27 |
*/ |
| 28 |
/** @addtogroup ISTL_SP |
| 29 |
@{ |
| 30 |
*/ |
| 31 |
|
| 32 |
/** \file |
| 33 |
|
| 34 |
\brief Define base class for scalar product and norm. |
| 35 |
|
| 36 |
These classes have to be implemented differently for different |
| 37 |
data partitioning strategies. Default implementations for the |
| 38 |
sequential case are provided. |
| 39 |
|
| 40 |
*/ |
| 41 |
|
| 42 |
/*! \brief Base class for scalar product and norm computation |
| 43 |
|
| 44 |
Krylov space methods need to compute scalar products and norms |
| 45 |
(for convergence test only). These methods have to know about the |
| 46 |
underlying data decomposition. For the sequential case a default implementation |
| 47 |
is provided. |
| 48 |
|
| 49 |
by default the scalar product is sequential |
| 50 |
*/ |
| 51 |
template<class X> |
| 52 |
class ScalarProduct { |
| 53 |
public: |
| 54 |
//! export types, they come from the derived class |
| 55 |
typedef X domain_type; |
| 56 |
typedef typename X::field_type field_type; |
| 57 |
typedef typename FieldTraits<field_type>::real_type real_type; |
| 58 |
|
| 59 |
/*! \brief Dot product of two vectors. |
| 60 |
It is assumed that the vectors are consistent on the interior+border |
| 61 |
partition. |
| 62 |
*/ |
| 63 |
virtual field_type dot (const X& x, const X& y) const |
| 64 |
{ |
| 65 |
return x.dot(y); |
| 66 |
} |
| 67 |
|
| 68 |
/*! \brief Norm of a right-hand side vector. |
| 69 |
The vector must be consistent on the interior+border partition |
| 70 |
*/ |
| 71 |
virtual real_type norm (const X& x) const |
| 72 |
{ |
| 73 |
return x.two_norm(); |
| 74 |
} |
| 75 |
|
| 76 |
//! Category of the scalar product (see SolverCategory::Category) |
| 77 |
virtual SolverCategory::Category category() const |
| 78 |
{ |
| 79 |
return SolverCategory::sequential; |
| 80 |
} |
| 81 |
|
| 82 |
//! every abstract base class has a virtual destructor |
| 83 |
virtual ~ScalarProduct () {} |
| 84 |
}; |
| 85 |
|
| 86 |
/** |
| 87 |
* \brief Scalar product for overlapping Schwarz methods. |
| 88 |
* |
| 89 |
* Consistent vectors in interior and border are assumed. |
| 90 |
* \tparam X The type of the sequential vector to use for the left hand side, |
| 91 |
* e.g. BlockVector or another type fulfilling the ISTL |
| 92 |
* vector interface. |
| 93 |
* \tparam C The type of the communication object. |
| 94 |
* This must either be OwnerOverlapCopyCommunication or a type |
| 95 |
* implementing the same interface. |
| 96 |
*/ |
| 97 |
template<class X, class C> |
| 98 |
class ParallelScalarProduct : public ScalarProduct<X> |
| 99 |
{ |
| 100 |
public: |
| 101 |
//! \brief The type of the vector to compute the scalar product on. |
| 102 |
//! |
| 103 |
//! E.g. BlockVector or another type fulfilling the ISTL |
| 104 |
//! vector interface. |
| 105 |
typedef X domain_type; |
| 106 |
//! \brief The field type used by the vector type domain_type. |
| 107 |
typedef typename X::field_type field_type; |
| 108 |
typedef typename FieldTraits<field_type>::real_type real_type; |
| 109 |
//! \brief The type of the communication object. |
| 110 |
//! |
| 111 |
//! This must either be OwnerOverlapCopyCommunication or a type |
| 112 |
//! implementing the same interface. |
| 113 |
typedef C communication_type; |
| 114 |
|
| 115 |
/*! |
| 116 |
* \param com The communication object for syncing overlap and copy |
| 117 |
* data points. |
| 118 |
* \param cat parallel solver category (nonoverlapping or overlapping) |
| 119 |
*/ |
| 120 |
ParallelScalarProduct (std::shared_ptr<const communication_type> com, SolverCategory::Category cat) |
| 121 |
: _communication(com), _category(cat) |
| 122 |
{} |
| 123 |
|
| 124 |
/*! |
| 125 |
* \param com The communication object for syncing overlap and copy |
| 126 |
* data points. |
| 127 |
* \param cat parallel solver category (nonoverlapping or overlapping) |
| 128 |
* \note if you use this constructor you have to make sure com stays alive |
| 129 |
*/ |
| 130 |
ParallelScalarProduct (const communication_type& com, SolverCategory::Category cat) |
| 131 |
: ParallelScalarProduct(stackobject_to_shared_ptr(com), cat) |
| 132 |
{} |
| 133 |
|
| 134 |
|
| 135 |
/*! \brief Dot product of two vectors. |
| 136 |
It is assumed that the vectors are consistent on the interior+border |
| 137 |
partition. |
| 138 |
*/ |
| 139 |
field_type dot (const X& x, const X& y) const override |
| 140 |
{ |
| 141 |
field_type result(0); |
| 142 |
_communication->dot(x,y,result); // explicitly loop and apply masking |
| 143 |
return result; |
| 144 |
} |
| 145 |
|
| 146 |
/*! \brief Norm of a right-hand side vector. |
| 147 |
The vector must be consistent on the interior+border partition |
| 148 |
*/ |
| 149 |
real_type norm (const X& x) const override |
| 150 |
{ |
| 151 |
return _communication->norm(x); |
| 152 |
} |
| 153 |
|
| 154 |
//! Category of the scalar product (see SolverCategory::Category) |
| 155 |
SolverCategory::Category category() const override |
| 156 |
{ |
| 157 |
return _category; |
| 158 |
} |
| 159 |
|
| 160 |
private: |
| 161 |
std::shared_ptr<const communication_type> _communication; |
| 162 |
SolverCategory::Category _category; |
| 163 |
}; |
| 164 |
|
| 165 |
//! Default implementation for the scalar case |
| 166 |
template<class X> |
| 167 |
class SeqScalarProduct : public ScalarProduct<X> |
| 168 |
{ |
| 169 |
using ScalarProduct<X>::ScalarProduct; |
| 170 |
}; |
| 171 |
|
| 172 |
/** |
| 173 |
* \brief Nonoverlapping Scalar Product with communication object. |
| 174 |
* |
| 175 |
* Consistent vectors in interior and border are assumed. |
| 176 |
*/ |
| 177 |
template<class X, class C> |
| 178 |
class NonoverlappingSchwarzScalarProduct : public ParallelScalarProduct<X,C> |
| 179 |
{ |
| 180 |
public: |
| 181 |
NonoverlappingSchwarzScalarProduct (std::shared_ptr<const C> comm) : |
| 182 |
ParallelScalarProduct<X,C>(comm,SolverCategory::nonoverlapping) {} |
| 183 |
|
| 184 |
NonoverlappingSchwarzScalarProduct (const C& comm) : |
| 185 |
ParallelScalarProduct<X,C>(comm,SolverCategory::nonoverlapping) {} |
| 186 |
}; |
| 187 |
|
| 188 |
/** |
| 189 |
* \brief Scalar product for overlapping Schwarz methods. |
| 190 |
* |
| 191 |
* Consistent vectors in interior and border are assumed. |
| 192 |
* \tparam X The type of the sequential vector to use for the left hand side, |
| 193 |
* e.g. BlockVector or another type fulfilling the ISTL |
| 194 |
* vector interface. |
| 195 |
* \tparam C The type of the communication object. |
| 196 |
* This must either be OwnerOverlapCopyCommunication or a type |
| 197 |
* implementing the same interface. |
| 198 |
*/ |
| 199 |
template<class X, class C> |
| 200 |
class OverlappingSchwarzScalarProduct : public ParallelScalarProduct<X,C> |
| 201 |
{ |
| 202 |
public: |
| 203 |
OverlappingSchwarzScalarProduct (std::shared_ptr<const C> comm) : |
| 204 |
ParallelScalarProduct<X,C>(comm, SolverCategory::overlapping) {} |
| 205 |
|
| 206 |
OverlappingSchwarzScalarProduct (const C& comm) : |
| 207 |
ParallelScalarProduct<X,C>(comm,SolverCategory::overlapping) {} |
| 208 |
}; |
| 209 |
|
| 210 |
/** @} end documentation */ |
| 211 |
|
| 212 |
/** |
| 213 |
* \brief Choose the appropriate scalar product for a solver category. |
| 214 |
* |
| 215 |
* \todo this helper function should be replaced by a proper factory |
| 216 |
* |
| 217 |
* As there is only one scalar product for each solver category it is |
| 218 |
* possible to choose the appropriate product at compile time. |
| 219 |
* |
| 220 |
* In each specialization of the this struct there will be a typedef ScalarProduct |
| 221 |
* available the defines the type of the scalar product. |
| 222 |
*/ |
| 223 |
template<class X, class Comm> |
| 224 |
std::shared_ptr<ScalarProduct<X>> makeScalarProduct(std::shared_ptr<const Comm> comm, SolverCategory::Category category) |
| 225 |
{ |
| 226 |
switch(category) |
| 227 |
{ |
| 228 |
case SolverCategory::sequential: |
| 229 |
return |
| 230 |
std::make_shared<ScalarProduct<X>>(); |
| 231 |
default: |
| 232 |
return |
| 233 |
std::make_shared<ParallelScalarProduct<X,Comm>>(comm,category); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* \copydoc createScalarProduct(std::shared_ptr<const Comm>,SolverCategory::Category) |
| 239 |
* \note Using this helper, you are responsible for the life-time management of comm |
| 240 |
*/ |
| 241 |
template<class X, class Comm> |
| 242 |
std::shared_ptr<ScalarProduct<X>> createScalarProduct(const Comm& comm, SolverCategory::Category category) |
| 243 |
{ return makeScalarProduct<X>(stackobject_to_shared_ptr(comm), category); } |
| 244 |
|
| 245 |
} // end namespace Dune |
| 246 |
|
| 247 |
#endif |