CEPS  24.01
Cardiac ElectroPhysiology Simulator
DistributedVector.cpp
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
32 
33 #ifdef CEPS_USE_PETSC
34  // Use the wrapper around PETSc Vec
36 #endif // CEPS_USE_PETSC
37 
40 {
41  return m_unit;
42 }
43 
44 void
46 {
47  m_unit = u;
48 }
49 
52 {
53  return m_isAssembled;
54 }
55 
56 void
58 {
59  this->getLocalData();
60  for (CepsGlobalIndex i=m_lo;i<m_hi;i++)
61  ceps::checkNanOrInf(this->operator[](i),message+" coefficient #"+ceps::toString(i)+" ");
62  this->releaseLocalData();
63 }
64 
65 // -------------------------------------------------------------------------====
66 // Subvectors operations
67 // -------------------------------------------------------------------------====
68 
69 void
71  const CepsMathDynamic1D &subVec,
72  const CepsVector<CepsGlobalIndex> &rowIndices
73 )
74 {
75  addValues(subVec.data(), rowIndices.size(), rowIndices.data());
76  return;
77 }
78 
79 void
81  const CepsMathDynamic1D &subVec,
82  const CepsVector<CepsGlobalIndex> &rowIndices
83 )
84 {
85  setValues(subVec.data(), rowIndices.size(), rowIndices.data());
86  return;
87 }
88 
89 // -------------------------------------------------------------------------====
90 // OPERATORS
91 // -------------------------------------------------------------------------====
92 
95 {
96  v.duplicate(*this,true);
97  return *this;
98 }
99 
100 // Access: non const version
101 inline CepsReal &
103 {
105  "trying to access to memory without required called to getLocalData()."
106  );
107 
108  CEPS_ABORT_IF((globalIndex < m_lo) or (globalIndex >= m_hi),
109  "out-of-bounds index " << globalIndex << " while range of vector is ( low-high " << m_lo << ","
110  << m_hi << ")"
111  );
112 
113  return m_localData[globalIndex-m_lo];
114 }
115 
116 // Access: const version
117 inline CepsReal
119 {
120  //CEPS_ABORT_IF (
121  // not m_mayReadData, "trying to access to memory without required called to getLocalData()."
122  //);
123  // get/realeaseLocalData is not compatible with const...
124 
125  CEPS_ABORT_IF((globalIndex < m_lo) or (globalIndex >= m_hi),
126  "out-of-bounds index " << globalIndex << " while range of vector is ( low-high " << m_lo << ","
127  << m_hi << ")"
128  );
129 
130  return m_localData[globalIndex-m_lo];
131 }
132 
135 {
136  return DistributedVector(lhs) += rhs;
137 }
138 
140 operator+ (const DistributedVector &lhs, const CepsReal &scalar)
141 {
142  return DistributedVector(lhs) += scalar;
143 }
144 
147 {
148  return DistributedVector(lhs) -= rhs;
149 }
150 
152 operator-(const DistributedVector &lhs, const CepsReal &scalar)
153 {
154  return DistributedVector(lhs) -= scalar;
155 }
156 
158 operator*(const DistributedVector &lhs, const CepsReal &scalar)
159 {
160  return DistributedVector(lhs) *= scalar;
161 }
162 
164 operator*(const CepsReal &scalar, const DistributedVector &rhs)
165 {
166  return DistributedVector(rhs) *= scalar;
167 }
168 
171 {
172  DistributedVector res(x);
173  res.mult(A,x);
174  return res;
175 }
176 
177 void
179 {
180  // Gather local sizes of vectors
182  int *sizes = nullptr;
183 
184  if (ceps::isMaster())
185  sizes = new int[M];
186  MPI_Gather(
188  );
189 
190  // Create offset array
191  int *displ = nullptr;
192  if (ceps::isMaster())
193  {
194  displ = new int[M];
195  displ[0] = 0;
196  for (CepsUInt i = 1; i < M; ++i)
197  displ[i] = displ[i - 1] + sizes[i - 1];
198  }
199 
200  // Send values directly into the vector
201  getLocalData();
202  MPI_Scatterv(
203  src.data(), sizes, displ, CEPS_MPI_REAL, m_localData, m_localSize, CEPS_MPI_REAL,
205  );
207 
208  if (ceps::isMaster())
209  {
210  ceps::destroyTabular(sizes);
211  ceps::destroyTabular(displ);
212  }
213  return;
214 }
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
#define CEPS_MASTER_PROC
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
#define CEPS_MPI_INT
Definition: CepsTypes.hpp:320
std::vector< _Type, _Alloc > CepsVector
C++ vector.
Definition: CepsTypes.hpp:155
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
#define CEPS_MPI_REAL
Definition: CepsTypes.hpp:315
CepsIndex CepsGlobalIndex
Many uses. Has to be signed for PETSc.
Definition: CepsTypes.hpp:218
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
CepsInt CepsIndex
Index rowid etc.
Definition: CepsTypes.hpp:111
DistributedVector operator-(const DistributedVector &lhs, const DistributedVector &rhs)
vector-vector
DistributedVector operator*(const DistributedVector &lhs, const CepsReal &scalar)
vector*scalar
DistributedVector operator+(const DistributedVector &lhs, const DistributedVector &rhs)
Addition of vectors.
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.
virtual void getLocalData()
Enables direct access to the stored local values.
CepsBool m_isAssembled
Whether this vector is assembled or not.
CepsGlobalIndex m_lo
Index of first owned row.
void addSubVector(const CepsMathDynamic1D &subVector, const CepsVector< CepsGlobalIndex > &rowIndices)
Adds a vector in a distributed vector.
CepsString m_unit
Unit of the data.
void addValues(const CepsMathScalar *values, CepsInt n, const CepsGlobalIndex *indices)
Add multiple 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 fillFromVector(const CepsVector< CepsMathScalar > &data)
Fills the distributed vector with data stored in a vector on process 0.
virtual void releaseLocalData()
Release the pointer on the local data.
CepsBool isAssembled() const
true if vector has been assembled
void setUnit(const CepsString &u)
Units of data.
virtual void mult(const DistributedMatrix &A, const DistributedVector &x)
Sets current vector to result of product A*x.
CepsInt m_localSize
Local size of vector.
DistributedVector & operator=(const DistributedVector &v)
Assignment operator.
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.
CepsString toString(_Tp value)
Convert a given value to a string (input has to be compatible with std::to_string)
Definition: CepsString.hpp:409
void destroyTabular(_Type &)
Destroy[delete] any type.
Definition: CepsMemory.hpp:149
MPI_Comm getCommunicator()
Get the communicator.
CepsUInt getGridSize()
Returns the number of process on the computing grid.
void checkNanOrInf(CepsReal v, CepsString message="")
Stops if value is NaN or infty.
CepsBool isMaster()
Is calling process the master ?