linear_algebra_distribution.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-2022 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 #ifndef OOMPH_LINEAR_ALGEBRA_DISTRIBUTION_CLASS_HEADER
27 #define OOMPH_LINEAR_ALGEBRA_DISTRIBUTION_CLASS_HEADER
28 
29 // Config header generated by autoconfig
30 #ifdef HAVE_CONFIG_H
31 #include <oomph-lib-config.h>
32 #endif
33 
34 // MPI headers
35 #ifdef OOMPH_HAS_MPI
36 #include "mpi.h"
37 #endif
38 
39 #include <ostream>
40 
41 // General headers
42 #include "Vector.h"
43 #include "communicator.h"
44 #include "oomph_utilities.h"
45 
46 
47 namespace oomph
48 {
49  //=============================================================================
62  //=============================================================================
64  {
65  public:
69 
74  const unsigned& first_row_,
75  const unsigned& n_row_local,
76  const unsigned& n_row = 0)
77  : Comm_pt(0)
78  {
79  this->build(&comm, first_row_, n_row_local, n_row);
80  };
81 
86  const unsigned& n_row,
87  const bool& distributed_ = true)
88  : Comm_pt(0)
89  {
90  this->build(&comm, n_row, distributed_);
91  };
92 
97  const unsigned& first_row_,
98  const unsigned& n_row_local,
99  const unsigned& n_row = 0)
100  : Comm_pt(0)
101  {
102  this->build(comm_pt, first_row_, n_row_local, n_row);
103  };
104 
109  const unsigned& n_row,
110  const bool& distributed_ = true)
111  : Comm_pt(0)
112  {
113  this->build(comm_pt, n_row, distributed_);
114  };
115 
118  : Comm_pt(0)
119  {
120  this->build(old_dist);
121  }
122 
125  : Comm_pt(0)
126  {
127  this->build(old_dist_pt);
128  }
129 
132  {
133  delete Comm_pt;
134  }
135 
137  void operator=(const LinearAlgebraDistribution& old_dist)
138  {
139  this->build(old_dist);
140  }
141 
145  void build(const OomphCommunicator* const comm_pt,
146  const unsigned& first_row,
147  const unsigned& nrow_local,
148  const unsigned& nrow = 0);
149 
155  void build(const OomphCommunicator* const comm_pt,
156  const unsigned& nrow,
157  const bool& distributed = true);
158 
161  void build(const LinearAlgebraDistribution& new_dist);
162 
165  void build(const LinearAlgebraDistribution* new_dist_pt)
166  {
167  this->build(*new_dist_pt);
168  }
169 
171  void clear()
172  {
173  // delete the communicator
174  delete Comm_pt;
175  Comm_pt = 0;
176 
177  // delete first_row and nrow_local
178  First_row.clear();
179  Nrow_local.clear();
180 
181  // zero Nrow
182  Nrow = 0;
183  }
184 
186  unsigned nrow() const
187  {
188  return Nrow;
189  }
190 
193  unsigned nrow_local() const
194  {
195  // return the nrow_local
196 #ifdef OOMPH_HAS_MPI
197  if (Distributed)
198  {
199 #ifdef PARANOID
200  if (Comm_pt == 0)
201  {
202  throw OomphLibError(
203  "LinearAlgebraDistribution has not been built : Comm_pt == 0.",
204  "LinearAlgebraDistribution::nrow_local()",
206  }
207 #endif
208 
209  return Nrow_local[Comm_pt->my_rank()];
210  }
211  else
212  {
213  return Nrow;
214  }
215 #else
216  return Nrow;
217 #endif
218  }
219 
222  unsigned nrow_local(const unsigned& p) const
223  {
224 #ifdef PARANOID
225  if (Comm_pt == 0)
226  {
227  throw OomphLibError(
228  "LinearAlgebraDistribution has not been built : Comm_pt == 0.",
231  }
232  if (p >= unsigned(Comm_pt->nproc()))
233  {
234  std::ostringstream error_message;
235  error_message << "Requested nrow_local(" << p
236  << "), but this distribution is defined "
237  << "on " << Comm_pt->nproc() << "processors.";
238  throw OomphLibError(error_message.str(),
241  }
242 #endif
243 
244  // return the nrow_local
245 #ifdef OOMPH_HAS_MPI
246  if (Distributed)
247  {
248  return Nrow_local[p];
249  }
250  else
251  {
252  return Nrow;
253  }
254 #else
255  return Nrow;
256 #endif
257  }
258 
261  unsigned first_row() const
262  {
263  // return the first row
264 #ifdef OOMPH_HAS_MPI
265  if (Distributed)
266  {
267 #ifdef PARANOID
268  if (Comm_pt == 0)
269  {
270  throw OomphLibError(
271  "LinearAlgebraDistribution has not been built : Comm_pt == 0.",
274  }
275 #endif
276 
277  return First_row[Comm_pt->my_rank()];
278  }
279  else
280  {
281  return 0;
282  }
283 #else
284  return 0;
285 #endif
286  }
287 
289  unsigned first_row(const unsigned& p) const
290  {
291 #ifdef PARANOID
292  if (Comm_pt == 0)
293  {
294  throw OomphLibError(
295  "LinearAlgebraDistribution has not been built : Comm_pt == 0.",
298  }
299  if (p >= unsigned(Comm_pt->nproc()))
300  {
301  std::ostringstream error_message;
302  error_message << "Requested first_row(" << p
303  << "), but this distribution is defined "
304  << "on " << Comm_pt->nproc() << "processors.";
305  throw OomphLibError(error_message.str(),
308  }
309 
310 #endif
311 
312  // return the first row
313 #ifdef OOMPH_HAS_MPI
314  if (Distributed)
315  {
316  return First_row[p];
317  }
318  else
319  {
320  return 0;
321  }
322 #else
323  return 0;
324 #endif
325  }
326 
329  bool distributed() const
330  {
331  return Distributed;
332  }
333 
336  {
337  return Comm_pt;
338  }
339 
342  bool built() const
343  {
344  if (Comm_pt == 0)
345  {
346  return false;
347  }
348  return true;
349  }
350 
352  bool operator==(const LinearAlgebraDistribution& other_dist) const;
353 
355  bool operator!=(const LinearAlgebraDistribution& other_dist) const
356  {
357  return !(*this == other_dist);
358  }
359 
361  friend std::ostream& operator<<(std::ostream& stream,
363 
365  unsigned global_to_local_row_map(const unsigned& global_i) const
366  {
367 #ifdef PARANOID
368  if (global_i >= Nrow)
369  {
370  throw OomphLibError(
371  "Requested global row outside the number of global rows",
374  }
375 #endif
376  int local_i = static_cast<int>(global_i);
377  int p = 0;
378  while ((int)(local_i - (int)nrow_local(p)) >= 0)
379  {
380  local_i -= (int)nrow_local(p);
381  p++;
382  }
383  return (unsigned)local_i;
384  }
385 
387  unsigned rank_of_global_row(const unsigned i) const
388  {
389  unsigned p = 0;
390  while (i < first_row(p) || i >= first_row(p) + nrow_local(p))
391  {
392  p++;
393  }
394  return p;
395  }
396 
399  {
400  return Nrow_local;
401  }
402 
405  {
406  return First_row;
407  }
408 
409  private:
411  unsigned Nrow;
412 
415 
418 
423 
426  }; // end of LinearAlgebraDistribution
427 
428 
429  //=============================================================================
433  //=============================================================================
435  {
436  public:
439  {
441  }
442 
445  const DistributableLinearAlgebraObject& matrix) = delete;
446 
449 
452  {
453  delete Distribution_pt;
454  }
455 
458  {
459  return Distribution_pt;
460  }
461 
463  unsigned nrow() const
464  {
465  return Distribution_pt->nrow();
466  }
467 
469  unsigned nrow_local() const
470  {
471  return Distribution_pt->nrow_local();
472  }
473 
475  unsigned nrow_local(const unsigned& p) const
476  {
477  return Distribution_pt->nrow_local(p);
478  }
479 
481  unsigned first_row() const
482  {
483  return Distribution_pt->first_row();
484  }
485 
487  unsigned first_row(const unsigned& p) const
488  {
489  return Distribution_pt->first_row(p);
490  }
491 
493  bool distributed() const
494  {
495  return Distribution_pt->distributed();
496  }
497 
500  bool distribution_built() const
501  {
502  return Distribution_pt->built();
503  }
504 
508  {
509  Distribution_pt->build(dist_pt);
510  }
511 
515  {
516  Distribution_pt->build(dist);
517  }
518 
519  protected:
523  {
525  }
526 
527  private:
530  }; // end of DistributableLinearAlgebraObject
531 
532  //=============================================================================
534  //=============================================================================
535  namespace LinearAlgebraDistributionHelpers
536  {
570  void concatenate(
571  const Vector<LinearAlgebraDistribution*>& in_distribution_pt,
572  LinearAlgebraDistribution& out_distribution);
573  } // namespace LinearAlgebraDistributionHelpers
574 } // namespace oomph
575 #endif
int i
Definition: BiCGSTAB_step_by_step.cpp:9
float * p
Definition: Tutorial_Map_using.cpp:9
Definition: linear_algebra_distribution.h:435
unsigned nrow_local(const unsigned &p) const
access function for the num of local rows on this processor.
Definition: linear_algebra_distribution.h:475
void clear_distribution()
Definition: linear_algebra_distribution.h:522
virtual ~DistributableLinearAlgebraObject()
Destructor.
Definition: linear_algebra_distribution.h:451
unsigned first_row(const unsigned &p) const
access function for the first row on this processor
Definition: linear_algebra_distribution.h:487
bool distributed() const
distribution is serial or distributed
Definition: linear_algebra_distribution.h:493
LinearAlgebraDistribution * distribution_pt() const
access to the LinearAlgebraDistribution
Definition: linear_algebra_distribution.h:457
DistributableLinearAlgebraObject(const DistributableLinearAlgebraObject &matrix)=delete
Broken copy constructor.
unsigned nrow() const
access function to the number of global rows.
Definition: linear_algebra_distribution.h:463
bool distribution_built() const
Definition: linear_algebra_distribution.h:500
LinearAlgebraDistribution * Distribution_pt
the LinearAlgebraDistribution object
Definition: linear_algebra_distribution.h:529
unsigned nrow_local() const
access function for the num of local rows on this processor.
Definition: linear_algebra_distribution.h:469
unsigned first_row() const
access function for the first row on this processor
Definition: linear_algebra_distribution.h:481
void build_distribution(const LinearAlgebraDistribution *const dist_pt)
Definition: linear_algebra_distribution.h:507
DistributableLinearAlgebraObject()
Default constructor - create a distribution.
Definition: linear_algebra_distribution.h:438
void operator=(const DistributableLinearAlgebraObject &)=delete
Broken assignment operator.
void build_distribution(const LinearAlgebraDistribution &dist)
Definition: linear_algebra_distribution.h:514
Definition: linear_algebra_distribution.h:64
void operator=(const LinearAlgebraDistribution &old_dist)
Assignment Operator.
Definition: linear_algebra_distribution.h:137
bool distributed() const
Definition: linear_algebra_distribution.h:329
Vector< unsigned > first_row_vector() const
return the first_row Vector
Definition: linear_algebra_distribution.h:404
unsigned first_row(const unsigned &p) const
access function for the first row on the p-th processor
Definition: linear_algebra_distribution.h:289
LinearAlgebraDistribution(const OomphCommunicator *const comm_pt, const unsigned &first_row_, const unsigned &n_row_local, const unsigned &n_row=0)
Definition: linear_algebra_distribution.h:96
unsigned first_row() const
Definition: linear_algebra_distribution.h:261
bool operator==(const LinearAlgebraDistribution &other_dist) const
== Operator
Definition: linear_algebra_distribution.cc:268
OomphCommunicator * communicator_pt() const
const access to the communicator pointer
Definition: linear_algebra_distribution.h:335
friend std::ostream & operator<<(std::ostream &stream, LinearAlgebraDistribution &dist)
<< operator
Definition: linear_algebra_distribution.cc:318
Vector< unsigned > Nrow_local
the number of local rows on the processor
Definition: linear_algebra_distribution.h:414
void build(const OomphCommunicator *const comm_pt, const unsigned &first_row, const unsigned &nrow_local, const unsigned &nrow=0)
Definition: linear_algebra_distribution.cc:35
~LinearAlgebraDistribution()
Destructor.
Definition: linear_algebra_distribution.h:131
bool operator!=(const LinearAlgebraDistribution &other_dist) const
!= operator
Definition: linear_algebra_distribution.h:355
OomphCommunicator * Comm_pt
the pointer to the MPI communicator object in this distribution
Definition: linear_algebra_distribution.h:425
unsigned Nrow
the number of global rows
Definition: linear_algebra_distribution.h:411
LinearAlgebraDistribution(const OomphCommunicator *const comm_pt, const unsigned &n_row, const bool &distributed_=true)
Definition: linear_algebra_distribution.h:108
LinearAlgebraDistribution(const OomphCommunicator &comm, const unsigned &n_row, const bool &distributed_=true)
Definition: linear_algebra_distribution.h:85
Vector< unsigned > First_row
the first row on this processor
Definition: linear_algebra_distribution.h:417
unsigned global_to_local_row_map(const unsigned &global_i) const
return the local index corresponding to the global index
Definition: linear_algebra_distribution.h:365
Vector< unsigned > nrow_local_vector() const
return the nrow_local Vector
Definition: linear_algebra_distribution.h:398
bool built() const
Definition: linear_algebra_distribution.h:342
bool Distributed
Definition: linear_algebra_distribution.h:422
unsigned nrow_local(const unsigned &p) const
Definition: linear_algebra_distribution.h:222
unsigned nrow() const
access function to the number of global rows.
Definition: linear_algebra_distribution.h:186
void clear()
clears the distribution
Definition: linear_algebra_distribution.h:171
unsigned nrow_local() const
Definition: linear_algebra_distribution.h:193
unsigned rank_of_global_row(const unsigned i) const
return the processor rank of the global row number i
Definition: linear_algebra_distribution.h:387
LinearAlgebraDistribution(const LinearAlgebraDistribution &old_dist)
Copy Constructor.
Definition: linear_algebra_distribution.h:117
LinearAlgebraDistribution()
Definition: linear_algebra_distribution.h:68
LinearAlgebraDistribution(const LinearAlgebraDistribution *old_dist_pt)
pointer based copy constructor
Definition: linear_algebra_distribution.h:124
void build(const LinearAlgebraDistribution *new_dist_pt)
Definition: linear_algebra_distribution.h:165
LinearAlgebraDistribution(const OomphCommunicator &comm, const unsigned &first_row_, const unsigned &n_row_local, const unsigned &n_row=0)
Definition: linear_algebra_distribution.h:73
Definition: communicator.h:54
int my_rank() const
my rank
Definition: communicator.h:176
int nproc() const
number of processors
Definition: communicator.h:157
Definition: oomph_definitions.h:222
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
return int(ret)+1
void concatenate(const Vector< LinearAlgebraDistribution * > &in_distribution_pt, LinearAlgebraDistribution &out_distribution)
Definition: linear_algebra_distribution.cc:367
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).
Definition: AnisotropicHookean.h:10
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86