CEPS  24.01
Cardiac ElectroPhysiology Simulator
DistributedMatrix.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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
30 #pragma once
31 
32 // #include "CepsConfig.h"
33 #include <memory>
34 
35 #include "common/CepsCommon.hpp"
36 
37 #ifdef CEPS_USE_PETSC
38  #include <petscmat.h>
39  #include <petscsys.h>
40 // We will manipulate a PETSc matrix
41 using PetscMatrix = Mat;
42 #endif // CEPS_USE_PETSC
43 
44 #include <Eigen/Dense>
45 #include <iostream>
46 
47 class DistributedVector;
48 
76 {
77 public:
86 
99 
105  explicit DistributedMatrix (const DistributedMatrix &templateMatrix, CepsBool copyValues = false);
106 
110  explicit DistributedMatrix (const CepsString &file);
111 
114 
123  void
125 
127  void
128  setSymmetric (CepsBool flag = true);
129 
140  void
141  setSize (CepsInt M, CepsInt N, CepsInt m, CepsInt n);
142 
151  void
152  setNonZeroStructure (const CepsInt d_nnz[], const CepsInt o_nnz[]);
153 
164  void
165  setNonZeroStructure (CepsInt d_nz, CepsInt o_nz);
166 
172  void
174 
180  void
182 
194  void
195  getValues (
196  CepsMathScalar *values,
197  CepsInt nbRows,
198  CepsInt nbColumns,
199  const CepsGlobalIndex *rowIndices,
200  const CepsGlobalIndex *columnIndices
201  );
202 
217  void
218  setValues (
219  const CepsMathScalar *values,
220  CepsInt nbRows,
221  CepsInt nbColumns,
222  const CepsGlobalIndex *rowIndices,
223  const CepsGlobalIndex *columnIndices
224  );
225 
231  void
232  addValues (
233  const CepsMathScalar *values,
234  CepsInt nbRows,
235  CepsInt nbColumns,
236  const CepsGlobalIndex *rowIndices,
237  const CepsGlobalIndex *columnIndices
238  );
239 
251  void
252  addSubMatrix (
253  const CepsMathDynamic2D &subMat,
254  const CepsVector<CepsGlobalIndex> &indicesI,
255  const CepsVector<CepsGlobalIndex> &indicesJ
256  );
257 
266  void
267  getSubMatrix (
268  const CepsVector<CepsGlobalIndex> &indicesI,
269  const CepsVector<CepsGlobalIndex> &indicesJ,
270  CepsMathDynamic2D &subMat
271  );
272 
278  void
279  zeroRowsAndColumns (CepsInt nbRows, CepsGlobalIndex *rows, CepsMathScalar diagonalValue = 1.0);
280 
286  void
287  zeroRows (CepsInt nbRows, CepsGlobalIndex *rows, CepsMathScalar diagonalValue = 1.0);
288 
290  void
291  zero ();
292 
294  void
295  identity ();
296 
303  void
304  add (DistributedMatrix &X, CepsMathScalar a, CepsBool sameNonZeroStructure = true);
305 
308  lInfNorm () const;
309 
312  l1Norm () const;
313 
316  l2Norm () const;
317 
323  void
324  copy (DistributedMatrix &A) const;
325 
335  void
336  duplicate (DistributedMatrix &dest, CepsBool copyValues) const;
337 
342  void
343  getSize (CepsInt *M, CepsInt *N) const;
344 
349  void
350  getLocalSize (CepsInt *m, CepsInt *n) const;
351 
356  void
358 
372  void
373  getLocalRow (
374  const CepsMathScalar **values,
375  CepsGlobalIndex row,
376  CepsInt *nbNonZero,
377  const CepsGlobalIndex **nonZeroCols
378  ) const;
379 
382  void
384  const CepsMathScalar **values,
385  CepsGlobalIndex row,
386  CepsInt *nbNonZero,
387  const CepsGlobalIndex **nonZeroCols
388  );
389 
402  void
404 
408  void
410 
418  void
420 
429  void
431 
438  void
440 
448  void
450 
451 
459  void
461 
463  const PetscMatrix &
464  getMatrix () const;
465 
471  void
472  save (const CepsString &filename);
473 
478  void
479  load (const CepsString &filename);
480 
486  void
487  beginAssembly ();
488 
495  void
496  endAssembly ();
497 
499  void
500  finalize ();
501 
503  void
504  flush ();
505 
507  CepsBool
508  isAssembled () const;
509 
512  void
513  assertAssembled(CepsString info="") const;
514 
527  CepsBool
528  isSymmetric () const;
529 
531  void
532  checkProperlySet () const;
533 
535  void
536  view () const;
537 
540  operator*= (const CepsMathScalar &scalar);
541 
542  friend class TestDistributedMatrix;
543 
544 private:
545  PetscMatrix m_A;
549 };
550 
552 using DMatPtr = std::shared_ptr<DistributedMatrix>;
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
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
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
int32_t CepsInt
Need 32 bit integer.
Definition: CepsTypes.hpp:106
Eigen::Matrix< CepsScalar, Eigen::Dynamic, Eigen::Dynamic > CepsMathDynamic2D
Dynamic 2D array, eigen format.
Definition: CepsTypes.hpp:140
std::shared_ptr< DistributedMatrix > DMatPtr
Short typedef for pointer on dist matrix.
Sparse matrix distributed between process.
void zero()
Zeroes all entries.
CepsMathScalar lInfNorm() const
-norm of matrix
CepsMathScalar l2Norm() const
-norm of matrix
void add(DistributedMatrix &X, CepsMathScalar a, CepsBool sameNonZeroStructure=true)
Adds a*X to this.
void getEntireLocalRow(CepsVector< CepsMathScalar > &rowValues, CepsGlobalIndex row) const
Get a copy of all values stored in a local row, including zeros.
void zeroRows(CepsInt nbRows, CepsGlobalIndex *rows, CepsMathScalar diagonalValue=1.0)
Sets the indicated rows to zero.
void releaseLocalRow(const CepsMathScalar **values, CepsGlobalIndex row, CepsInt *nbNonZero, const CepsGlobalIndex **nonZeroCols)
Release access to the values of matrix row after reading.
CepsBool isSymmetric() const
True if matrix is symmetric.
void setValue(CepsMathScalar value, CepsGlobalIndex i, CepsGlobalIndex j)
Sets A(i,j) (replace if existing)
void identity()
Sets to identity matrix. Fails if matrix is not square.
void setSymmetric(CepsBool flag=true)
Set whether this matrix is symmetric or not.
Definition: PETScMatrix.cpp:98
void getLocalSize(CepsInt *m, CepsInt *n) const
Get the local size of the matrix.
CepsGlobalIndex m_lo
Index of first local row.
void ignoreOffProcEntries(CepsBool flag)
Choose to ignore entries destined to other processes when setting or adding values in the matrix.
Definition: PETScMatrix.cpp:90
void setNonZeroStructure(const CepsInt d_nnz[], const CepsInt o_nnz[])
Set the matrix non-zero structure.
void getDiagonalAsDistributedVector(DistributedVector &v) const
Fills a DistributedVector with values of diagonal.
void flush()
Use when switching between calls to adding and setting values.
void beginAssembly()
Begin matrix assembly.
friend class TestDistributedMatrix
CepsBool isAssembled() const
Safety check.
void addValue(CepsMathScalar value, CepsGlobalIndex i, CepsGlobalIndex j)
Adds value to A(i,j) (sets if not existing already)
void checkProperlySet() const
Will fail if matrix is not properly set (correct sizes and memory preallocation).
void setDiagonalAsDistributedVector(DistributedVector &v)
Sets the values of a distributed vector in matrix diagonal.
DistributedMatrix & operator*=(const CepsMathScalar &scalar)
Short mult by scalar.
void copy(DistributedMatrix &A) const
Copy values from this matrix to matrix A of same non-zero structure.
void getLocalRow(const CepsMathScalar **values, CepsGlobalIndex row, CepsInt *nbNonZero, const CepsGlobalIndex **nonZeroCols) const
Read-only access to the values of matrix row.
void assertAssembled(CepsString info="") const
Safetier check.
DistributedMatrix()
Empty constructor.
Definition: PETScMatrix.cpp:33
void setSize(CepsInt M, CepsInt N, CepsInt m, CepsInt n)
Set distributed matrix global and local size.
void setValues(const CepsMathScalar *values, CepsInt nbRows, CepsInt nbColumns, const CepsGlobalIndex *rowIndices, const CepsGlobalIndex *columnIndices)
Set multiple values in the matrix at once.
void getDiagonalFootPrintAsDistributedVector(DistributedVector &v, CepsReal epsilon=1E-12) const
Fills a DistributedVector with the footprint (zero or non-zero) of diagonal.
void getSubMatrix(const CepsVector< CepsGlobalIndex > &indicesI, const CepsVector< CepsGlobalIndex > &indicesJ, CepsMathDynamic2D &subMat)
Insert each element of a distributed matrix in a small Eigen submatrix.
void setColumnAsDistributedVector(DistributedVector &v, CepsGlobalIndex column)
Sets the values of a distributed vector in matrix column.
const PetscMatrix & getMatrix() const
The underlying matrix.
void getLocalRange(CepsGlobalIndex *lo, CepsGlobalIndex *hi) const
Get the range of rows owned by current process.
CepsGlobalIndex m_hi
Index of last local row +1.
void addSubMatrix(const CepsMathDynamic2D &subMat, const CepsVector< CepsGlobalIndex > &indicesI, const CepsVector< CepsGlobalIndex > &indicesJ)
Add each element of a small square matrix in a distributed matrix.
void addValues(const CepsMathScalar *values, CepsInt nbRows, CepsInt nbColumns, const CepsGlobalIndex *rowIndices, const CepsGlobalIndex *columnIndices)
Add multiple values in the matrix at once.
void duplicate(DistributedMatrix &dest, CepsBool copyValues) const
Copy non-zero structure to dest matrix. Optionally values.
CepsBool m_properlySet
Safety flag to check for sizes and memory preallocation.
PetscMatrix m_A
The main object.
void getColumnAsDistributedVector(DistributedVector &v, CepsGlobalIndex column) const
Fills a DistributedVector with values of column.
void save(const CepsString &filename)
Matrix is saved in binary format.
void getSize(CepsInt *M, CepsInt *N) const
Get the global size of the matrix.
void load(const CepsString &filename)
Matrix is loaded from specified file in binary format.
void endAssembly()
Wait for the end of matrix assembly.
~DistributedMatrix()
Destructor. The underlying object is cleaned.
Definition: PETScMatrix.cpp:83
void zeroRowsAndColumns(CepsInt nbRows, CepsGlobalIndex *rows, CepsMathScalar diagonalValue=1.0)
Sets the indicated rows and columns values to zero for symmetric matrices.
void finalize()
Performs assembly.
CepsMathScalar l1Norm() const
-norm of matrix
void getValues(CepsMathScalar *values, CepsInt nbRows, CepsInt nbColumns, const CepsGlobalIndex *rowIndices, const CepsGlobalIndex *columnIndices)
Get multiple values in the matrix.
void view() const
Displays the matrix on standard output.
Structure to hold spatially dependant data and distribute it between process.