preconditioner_array.h
Go to the documentation of this file.
1 // LIC// ====================================================================
2 // LIC// This file forms part of oomph-lib, the object-oriented,
3 // LIC// multi-physics finite-element library, available
4 // LIC// at http://www.oomph-lib.org.
5 // LIC//
6 // LIC// Copyright (C) 2006-2024 Matthias Heil and Andrew Hazel
7 // LIC//
8 // LIC// This library is free software; you can redistribute it and/or
9 // LIC// modify it under the terms of the GNU Lesser General Public
10 // LIC// License as published by the Free Software Foundation; either
11 // LIC// version 2.1 of the License, or (at your option) any later version.
12 // LIC//
13 // LIC// This library is distributed in the hope that it will be useful,
14 // LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // LIC// Lesser General Public License for more details.
17 // LIC//
18 // LIC// You should have received a copy of the GNU Lesser General Public
19 // LIC// License along with this library; if not, write to the Free Software
20 // LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 // LIC// 02110-1301 USA.
22 // LIC//
23 // LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
24 // LIC//
25 // LIC//====================================================================
26 // Include guards
27 #ifndef OOMPH_PRECONDITION_ARRAY_HEADER
28 #define OOMPH_PRECONDITION_ARRAY_HEADER
29 
30 // Config header generated by autoconfig
31 #ifdef HAVE_CONFIG_H
32 #include <oomph-lib-config.h>
33 #endif
34 
35 // oomph-lib includes
36 #include "Vector.h"
37 #include "double_vector.h"
38 #include "matrices.h"
39 #include "preconditioner.h"
40 
41 
42 // Preconditioner array is only really possible and useful if we have MPI
43 // (and it uses a lot of MPI functions in the .cc file so changing that
44 // would be hard). So if we don't have MPI just define a dummy
45 // implementation that throws an error if you try to use it.
46 #ifndef OOMPH_HAS_MPI
47 namespace oomph
48 {
50  {
51  public:
54  const OomphCommunicator* comm_pt)
55  {
56  throw OomphLibError("PreconditionerArray requires MPI",
57  OOMPH_CURRENT_FUNCTION,
58  OOMPH_EXCEPTION_LOCATION);
59  }
60 
63  {
64  throw OomphLibError("PreconditionerArray requires MPI",
65  OOMPH_CURRENT_FUNCTION,
66  OOMPH_EXCEPTION_LOCATION);
67  }
68  };
69 } // namespace oomph
70 
71 // Otherwise (if we have MPI do the real implementation)
72 #else
73 
74 #include "mpi.h"
75 
76 
77 namespace oomph
78 {
79  //=============================================================================
80  /// PreconditionerArray -
81  /// NOTE - first implementation, a number of assumptions / simplifications
82  /// were made:
83  /// 1. Only works with CRDoubleMatrices
84  /// 2. The number of processors must be greater than the number of
85  /// preconditioners
86  /// 3. Currently only very crude load balancing - each preconditioner will
87  /// be setup and applied with the same number of processors (or as near to
88  /// as possible to the same number of processors)
89  /// 4. This class will, at the appropriate time, delete the all the
90  /// Preconditioners passed setup_preconditioners(...)
91  /// 5. (but) Deletion of matrices passed to setup_preconditioners(...) is NOT
92  /// performed by this class
93  /// 6. It is assumed that preconditioners do not require access to matrix
94  /// once setup(...) is called
95  /// 7. The matrix on the subset of processors will be the same type
96  /// (distributed or global) as the matrix passed to
97  /// setup_preconditioners(...)
98  /// 8. If the matrix is a distributed matrix - it will be assembled with
99  /// a uniform distribution on the subset of processors.
100  //=============================================================================
101  class PreconditionerArray
102  {
103  public:
104  /// Constructor (empty)
106  : Preconditioner_pt(0),
109  {
110  Method = 0;
111  Nprec = 0;
112  };
113 
114  /// Broken copy constructor
116 
117  /// Broken assignment operator
118  void operator=(const PreconditionerArray&) = delete;
119 
120  /// Destructor (empty)
122  {
123  this->clean_up_memory();
124  }
125 
126  /// Setup the preconditioners. Sets up each preconditioner in the
127  /// array for the corresponding matrix in the vector matrix_pt.
128  /// The number of preconditioners in the array is taken to be the length of
129  /// prec_pt
130  /// The preconditioners that are not used on this processor are deleted.
132  Vector<Preconditioner*> prec_pt,
133  const OomphCommunicator* comm_pt);
134 
135  /// Applies each preconditioner to the corresponding vector in
136  /// r and z
139 
140  /// Clean up memory.
142  {
143  // delete the preconditioner pt
144  delete Preconditioner_pt;
145  Preconditioner_pt = 0;
146 
147  // delete the communicators
148  delete Global_communicator_pt;
150  delete Local_communicator_pt;
152 
153  // clear vectors
154  First_row_for_proc.clear();
155  Nrow_local_for_proc.clear();
156  First_row_from_proc.clear();
157  Nrow_local_from_proc.clear();
158  First_proc_for_prec.clear();
159  Nproc_for_prec.clear();
160 
161  // zero
162  Color = 0;
163 
164 #ifdef PARANOID
165  // delete PARANOID check distribution pts
166  for (unsigned i = 0; i < Nprec; i++)
167  {
168  delete Distribution_pt[i];
169  }
170  Distribution_pt.resize(0);
171 #endif
172  }
173 
174  // access function to Method
175  unsigned& method()
176  {
177  return Method;
178  }
179 
180  private:
181  /// helper method for computing the MPI_Isend and MPI_Irecv tags
182  int compute_tag(const int& nproc,
183  const int& source,
184  const int& dest,
185  const int& type)
186  {
187  return source + (nproc * dest) + (nproc * nproc * type);
188  }
189 
190  /// the number of preconditioner in the array
191  unsigned Nprec;
192 
193  /// The pointer to the local preconditioner on this processor
195 
196  /// The first_row component of the distribution of the processors
197  /// over the preconditioners
199 
200  /// The nrow_local component of the distribution of the processors
201  /// over the preconditioners
203 
204  /// Storage (indexed [i][j]) for the first row that will be sent
205  /// from this processor to processor j for preconditioner i
207 
208  /// Storage (indexed [i][j]) for the nrow_local that will be sent
209  /// from this processor to processor j for preconditioner i
211 
212  /// Storage (indexed [i][j]) for the first row that will be received
213  /// by this processor from processor j for preconditioner i
215 
216  /// Storage (indexed [i][j]) for the nrow_local that will be
217  /// received by this processor from processor j for preconditioner i
219 
220  /// the Color of this processor (or the preconditioner number)
221  unsigned Color;
222 
223  /// pointer to the global communicator for this preconditioner array
225 
226  /// Vector of communicators for the preconditioners
228 
229 #ifdef PARANOID
230  // Vector of distribution of each preconditioner - for PARANOID checks only
232 #endif
233 
234  /// the communication method in the setup_preconditioners(...) method
235  /// 1. Non-blocking Send with Blocking Recv
236  /// 2. MPI_Datatypes with Non-blocking sends and receives
237  unsigned Method;
238 
239  }; // PreconditionerArray
240 } // namespace oomph
241 
242 // End of "if we have MPI"
243 #endif
244 
245 // End of include guard
246 #endif
cstr elem_len * i
Definition: cfortran.h:603
An oomph-lib wrapper to the MPI_Comm communicator object. Just contains an MPI_Comm object (which is ...
Definition: communicator.h:54
An OomphLibError object which should be thrown when an run-time error is encountered....
PreconditionerArray - NOTE - first implementation, a number of assumptions / simplifications were mad...
void solve_preconditioners(const Vector< DoubleVector > &r, Vector< DoubleVector > &z)
Applies each preconditioner to the corresponding vector in r and z.
unsigned Method
the communication method in the setup_preconditioners(...) method
Vector< Vector< unsigned > > First_row_for_proc
Storage (indexed [i][j]) for the first row that will be sent from this processor to processor j for p...
Vector< unsigned > First_proc_for_prec
The first_row component of the distribution of the processors over the preconditioners.
unsigned Color
the Color of this processor (or the preconditioner number)
Vector< Vector< unsigned > > Nrow_local_for_proc
Storage (indexed [i][j]) for the nrow_local that will be sent from this processor to processor j for ...
Vector< Vector< unsigned > > First_row_from_proc
Storage (indexed [i][j]) for the first row that will be received by this processor from processor j f...
int compute_tag(const int &nproc, const int &source, const int &dest, const int &type)
helper method for computing the MPI_Isend and MPI_Irecv tags
OomphCommunicator * Global_communicator_pt
pointer to the global communicator for this preconditioner array
void setup_preconditioners(Vector< CRDoubleMatrix * > matrix_pt, Vector< Preconditioner * > prec_pt, const OomphCommunicator *comm_pt)
Setup the preconditioners. Sets up each preconditioner in the array for the corresponding matrix in t...
PreconditionerArray(const PreconditionerArray &)=delete
Broken copy constructor.
OomphCommunicator * Local_communicator_pt
Vector of communicators for the preconditioners.
Preconditioner * Preconditioner_pt
The pointer to the local preconditioner on this processor.
void clean_up_memory()
Clean up memory.
unsigned Nprec
the number of preconditioner in the array
~PreconditionerArray()
Destructor (empty)
Vector< Vector< unsigned > > Nrow_local_from_proc
Storage (indexed [i][j]) for the nrow_local that will be received by this processor from processor j ...
Vector< LinearAlgebraDistribution * > Distribution_pt
Vector< unsigned > Nproc_for_prec
The nrow_local component of the distribution of the processors over the preconditioners.
void operator=(const PreconditionerArray &)=delete
Broken assignment operator.
PreconditionerArray()
Constructor (empty)
Preconditioner base class. Gives an interface to call all other preconditioners through and stores th...
A slight extension to the standard template vector class so that we can include "graceful" array rang...
Definition: Vector.h:58
//////////////////////////////////////////////////////////////////// ////////////////////////////////...