CEPS  24.01
Cardiac ElectroPhysiology Simulator
DistributedVector.hpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
31 #pragma once
32 
33 #include <cstring>
34 #include <map>
35 #include <memory>
36 
37 // Bug with PETSc and VTK: need to put this file using macro HZ first
38 #include <vtkPointLocator.h>
39 
40 #ifdef CEPS_USE_PETSC
41  #include <petscvec.h>
42 // We will manipulate a PETSc vector
43 using PetscVector = Vec;
44 #endif // CEPS_USE_PETSC
45 
46 #include "common/CepsCommon.hpp"
48 
56 {
57 
58  public:
59 
60  friend class DistributedMatrix;
61 
64 
67 
70  operator=(const DistributedVector& v);
71 
77 
85  DistributedVector& templateVector,
86  CepsBool copyValues,
87  CepsBool isSequential = false
88  );
89 
97  std::shared_ptr<DistributedVector> templateVector,
98  CepsBool copyValues,
99  CepsBool isSequential = false
100  );
101 
107 
109  virtual ~DistributedVector();
110 
112  inline CepsString
113  getUnit() const;
114 
116  inline void
117  setUnit(const CepsString& u);
118 
127  localData();
128 
133  void
134  setSize(CepsInt M, CepsInt m);
135 
139  void
140  getSize(CepsInt* globalSize) const;
141 
143  CepsInt
144  getSize() const;
145 
149  void
150  getLocalSize(CepsInt* localSize) const;
151 
153  CepsInt
154  getLocalSize() const;
155 
160  void
162 
171  virtual void
172  getLocalData();
173 
177  virtual void
179 
182  const PetscVector&
183  getVector() const;
184 
189  void
191 
196  void
198 
208  void
210 
216  void
217  setValues(const CepsMathScalar* values, CepsInt n, const CepsGlobalIndex* indices);
218 
225  void
226  setValuesLocal(DistributedVector& values, CepsInt n, const CepsGlobalIndex* indices = nullptr);
227 
234  void
236  const CepsMathScalar* values,
237  CepsInt n,
238  const CepsGlobalIndex* indices = nullptr
239  );
240 
245  void
247  const CepsVector<CepsMathScalar>& values,
248  const CepsVector<CepsGlobalIndex>& indices
249  );
250 
256  void
257  addValues(const CepsMathScalar* values, CepsInt n, const CepsGlobalIndex* indices);
258 
265  void
266  addValuesLocal(DistributedVector& values, CepsInt n, const CepsGlobalIndex* indices = nullptr);
267 
274  void
276  const CepsMathScalar* values,
277  CepsInt n,
278  const CepsGlobalIndex* indices = nullptr
279  );
280 
288  virtual void
289  getValues(CepsMathScalar* values, CepsInt n, const CepsGlobalIndex* indices);
290 
298  void
299  addSubVector(const CepsMathDynamic1D& subVector, const CepsVector<CepsGlobalIndex>& rowIndices);
300 
307  void
309  const CepsMathDynamic1D& subVector,
310  const CepsVector<CepsGlobalIndex>& rowIndices
311  );
312 
317  getSubVector(const CepsVector<CepsGlobalIndex>& rowIndices);
318 
320  void
321  zero ();
322 
324  void
325  fill(CepsMathScalar value);
326 
331  void
332  duplicate(DistributedVector& dest, CepsBool copyValues) const;
333 
340  void
341  beginAssembly();
342 
346  void
347  endAssembly();
348 
350  void
351  finalize();
352 
360  CepsBool
361  isAssembled() const;
362 
367  CepsBool
368  sameSize(const DistributedVector& vec) const;
369 
375  CepsBool
376  sameLocalRange(const DistributedVector& vec) const;
377 
385  CepsBool
386  equals(const DistributedVector& vec, CepsMathScalar errorFactor = 1.0) const;
387 
394  CepsBool
395  approxEquals(const DistributedVector& vec, CepsMathScalar epsilon) const;
396 
398  void
399  checkNanOrInf(CepsString message="");
400 
409  void
410  add(const DistributedVector& x);
411 
420  void
421  addScaled(const DistributedVector& x, CepsMathScalar alpha);
422 
426  virtual void
427  scale(CepsMathScalar alpha);
428 
432  virtual void
433  pointWiseScale(const DistributedVector& x, CepsBool divide=false);
434 
445  virtual void
446  mult(const DistributedMatrix& A, const DistributedVector& x);
447 
449  virtual void
450  abs();
451 
458 
461  lInfNorm() const;
462 
465  l1Norm() const;
466 
469  l2Norm() const;
470 
472  void
473  view() const;
474 
479  virtual CepsMathScalar&
480  operator[](CepsGlobalIndex globalIndex);
481 
486  virtual CepsMathScalar
487  operator[](CepsGlobalIndex globalIndex) const;
488 
491  operator+=(const DistributedVector& v);
492 
495  operator+=(const CepsMathScalar& scalar);
496 
499  operator-=(const DistributedVector& v);
500 
503  operator-=(const CepsMathScalar& scalar);
504 
507  operator*=(const CepsMathScalar& scalar);
508 
510  void
512 
513  friend class TestDistributedVector;
514 
515 
516  protected:
517 
522  void
523  checkAndSetSizes(CepsInt gSize, CepsInt lSize);
524 
526  void
528 
529 
530  protected:
531 
533  PetscVector m_v;
538 
547 
554 
555  #ifdef PROFILING_VECTOR
556  CepsMathScalar m_totalAssembly;
557  CepsMathScalar m_tickAssembly;
558  #endif
559 
562 
563 };
564 
566 using DVecPtr = std::shared_ptr<DistributedVector>;
567 
570 operator+(const DistributedVector& lhs, const DistributedVector& rhs);
571 
574 operator+(const DistributedVector& lhs, const CepsMathScalar& scalar);
575 
578 operator-(const DistributedVector& lhs, const DistributedVector& rhs);
579 
582 operator-(const DistributedVector& lhs, const CepsMathScalar& scalar);
583 
586 operator*(const DistributedVector& lhs, const CepsMathScalar& scalar);
587 
590 operator*(const CepsMathScalar& scalar, const DistributedVector& rhs);
591 
594 operator*(const DistributedMatrix& A, const DistributedVector& x);
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
Eigen::Matrix< CepsScalar, Eigen::Dynamic, 1 > CepsMathDynamic1D
Dynamic 1D array, eigen format.
Definition: CepsTypes.hpp:139
CepsScalar CepsMathScalar
Real numbers.
Definition: CepsTypes.hpp:133
std::vector< _Type, _Alloc > CepsVector
C++ vector.
Definition: CepsTypes.hpp:155
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
CepsIndex CepsGlobalIndex
Many uses. Has to be signed for PETSc.
Definition: CepsTypes.hpp:218
int32_t CepsInt
Need 32 bit integer.
Definition: CepsTypes.hpp:106
std::shared_ptr< DistributedMatrix > DMatPtr
Short typedef for pointer on dist matrix.
DistributedVector operator-(const DistributedVector &lhs, const DistributedVector &rhs)
vector-vector
DistributedVector operator+(const DistributedVector &lhs, const DistributedVector &rhs)
Addition of vectors.
std::shared_ptr< DistributedVector > DVecPtr
Short typedef for pointer on distributed vector.
DistributedVector operator*(const DistributedVector &lhs, const CepsMathScalar &scalar)
vector*scalar
Sparse matrix distributed between process.
Structure to hold spatially dependant data and distribute it between process.
CepsMathScalar * m_localData
void insertSubVector(const CepsMathDynamic1D &subVector, const CepsVector< CepsGlobalIndex > &rowIndices)
Insert (i.e. set) a vector in a distributed vector.
CepsString getUnit() const
Units of data.
virtual CepsMathScalar & operator[](CepsGlobalIndex globalIndex)
Direct access to the values of a vector.
DistributedVector()
Default Constructor.
Definition: PETScVector.cpp:31
void setValuesLocal(DistributedVector &values, CepsInt n, const CepsGlobalIndex *indices=nullptr)
Sets n multiple local values.
virtual ~DistributedVector()
Destructor.
CepsBool equals(const DistributedVector &vec, CepsMathScalar errorFactor=1.0) const
Whether two vector have the (exact!) same values on all process.
void finalize()
Calls both beginAssembly() and endAssembly()
void view() const
Display vector on standard output.
virtual void getLocalData()
Enables direct access to the stored local values.
CepsBool m_initialized
Whether this vector is ready to be used or not.
CepsBool m_isAssembled
Whether this vector is assembled or not.
virtual void getValues(CepsMathScalar *values, CepsInt n, const CepsGlobalIndex *indices)
Read values in the vector.
PetscVector m_v
The underlying vector.
CepsGlobalIndex m_lo
Index of first owned row.
CepsBool sameLocalRange(const DistributedVector &vec) const
Compares local ranges.
CepsBool approxEquals(const DistributedVector &vec, CepsMathScalar epsilon) const
Whether two vector have the approximately the same values on all process, with epsilon tolerance.
DistributedVector & operator*=(const CepsMathScalar &scalar)
Short mult by scalar.
CepsMathScalar l1Norm() const
-norm of vector
void setValue(CepsMathScalar value, CepsGlobalIndex i)
Sets or replaces a single value.
void createEmptyVector()
Inits the underlying vector. Use setSize afterwards.
void zero()
Fills vector with zeros.
void add(const DistributedVector &x)
Adds x to current vector.
void addSubVector(const CepsMathDynamic1D &subVector, const CepsVector< CepsGlobalIndex > &rowIndices)
Adds a vector in a distributed vector.
CepsMathScalar l2Norm() const
-norm of vector
friend class TestDistributedVector
void endAssembly()
Wait for the distributed vector assembly to finish.
DistributedVector & operator+=(const DistributedVector &v)
Short addition with other vector.
DistributedVector & operator-=(const DistributedVector &v)
Short substraction with other vector.
CepsString m_unit
Unit of the data.
CepsMathScalar lInfNorm() const
-norm of vector
void addValues(const CepsMathScalar *values, CepsInt n, const CepsGlobalIndex *indices)
Add multiple values to already existing values.
const PetscVector & getVector() const
The underlying vector.
CepsMathScalar dot(DistributedVector &x)
Computes the inner product of current vector by x.
CepsMathScalar * localData()
Point on the local data.
void addValuesLocal(DistributedVector &values, CepsInt n, const CepsGlobalIndex *indices=nullptr)
Add multiple local values to already existing values.
void duplicate(DistributedVector &dest, CepsBool copyValues) const
Shares the non-zero structure, and optionally values.
CepsGlobalIndex m_hi
Index of row right after last owned row.
void ignoreOffProcEntries(CepsBool ignore)
Sets whether the values on rows that are not owned by current process will be ignored or not.
void fillFromVector(const CepsVector< CepsMathScalar > &data)
Fills the distributed vector with data stored in a vector on process 0.
CepsInt getSize() const
Get the global size of the vector.
virtual void releaseLocalData()
Release the pointer on the local data.
void checkAndSetSizes(CepsInt gSize, CepsInt lSize)
Check if calling vector has had its sizes set. If not, set sizes.
CepsVector< CepsMathScalar > getSubVector(const CepsVector< CepsGlobalIndex > &rowIndices)
Get some local data from the distributed vector.
CepsBool m_vecCreated
Underlying vec created.
CepsBool isAssembled() const
true if vector has been assembled
void setUnit(const CepsString &u)
Units of data.
CepsInt m_globalSize
Global size of vector.
void getLocalRange(CepsGlobalIndex *lo, CepsGlobalIndex *hi) const
Get the range of rows owned by current process.
void fill(CepsMathScalar value)
Fills vector with given value.
virtual void pointWiseScale(const DistributedVector &x, CepsBool divide=false)
Perform a point wise multiplication or division on this vector by another vector.
void setSize(CepsInt M, CepsInt m)
Set distributed vector size.
virtual void abs()
Every component of the vector is replaced by its absolute value.
virtual void mult(const DistributedMatrix &A, const DistributedVector &x)
Sets current vector to result of product A*x.
virtual void scale(CepsMathScalar alpha)
Multiplies self by alpha.
CepsInt m_localSize
Local size of vector.
void beginAssembly()
Start distributed vector assembly.
CepsInt getLocalSize() const
Get the local size of the vector.
DistributedVector & operator=(const DistributedVector &v)
Assignment operator.
void addValue(CepsMathScalar value, CepsGlobalIndex i)
Adds given scalar to the currently existing value or set it if not.
CepsBool sameSize(const DistributedVector &vec) const
Compares global sizes.
void setValues(const CepsMathScalar *values, CepsInt n, const CepsGlobalIndex *indices)
Sets n multiple values.
void checkNanOrInf(CepsString message="")
Stops if there are coefficients that are nan or inf.
void addScaled(const DistributedVector &x, CepsMathScalar alpha)
Adds alpha*x to current vector.