43 MatSetType(
m_A, MATAIJ);
57 MatSetType(
m_A, MATAIJ);
67 templateMatrix.
duplicate(*
this, copyValues);
92 PetscBool pflag = flag ? PETSC_TRUE : PETSC_FALSE;
93 MatSetOption (
m_A, MAT_IGNORE_OFF_PROC_ENTRIES, pflag);
100 PetscBool pflag = flag ? PETSC_TRUE : PETSC_FALSE;
102 MatSetOption(
m_A, MAT_SYMMETRIC, pflag);
103 MatSetOption(
m_A, MAT_SYMMETRY_ETERNAL, pflag);
110 CEPS_ABORT_IF( not ((M >= 0 && N >= 0) or (m >= 0 && n >= 0)),
113 MatSetSizes(
m_A, m, n, M, N);
122 MatMPIAIJSetPreallocation(
m_A, 0, d_nnz, 0, o_nnz);
125 MatSeqAIJSetPreallocation(
m_A, 0, d_nnz);
136 MatMPIAIJSetPreallocation(
m_A, d_nz,
nullptr, o_nz,
nullptr);
138 MatSeqAIJSetPreallocation(
m_A, d_nz,
nullptr);
148 MatSetValue(
m_A, row, column, value, INSERT_VALUES);
155 MatSetValue(
m_A, row, column, value, ADD_VALUES);
168 MatGetValues(
m_A, nbRows, rowIndices, nbColumns, columnIndices, values);
181 MatSetValues(
m_A, nbRows, rowIndices, nbColumns, columnIndices, values, INSERT_VALUES);
194 MatSetValues(
m_A, nbRows, rowIndices, nbColumns, columnIndices, values, ADD_VALUES);
205 MatSetOption(
m_A, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
206 MatZeroRowsColumns(
m_A, nbRows, rows, diagonalValue,
nullptr,
nullptr);
213 MatSetOption(
m_A, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
214 MatSetOption(
m_A, MAT_NO_OFF_PROC_ZERO_ROWS, PETSC_FALSE);
215 MatZeroRows (
m_A, nbRows, rows, diagonalValue, PETSC_NULL, PETSC_NULL);
230 MatGetSize(
m_A, &mGlobal, &nGlobal);
232 "cannot set non-square matrix to identity.\nCurrent size is ("
233 << mGlobal <<
"," << nGlobal <<
")"
248 if (sameNonZeroStructure)
249 MatAXPY(
m_A, a, X.
m_A, SAME_NONZERO_PATTERN);
251 MatAXPY(
m_A, a, X.
m_A, DIFFERENT_NONZERO_PATTERN);
260 MatNorm(
m_A, NORM_INFINITY, &res);
269 MatNorm(
m_A, NORM_1, &res);
278 MatNorm(
m_A, NORM_FROBENIUS, &res);
288 MatCopy (
m_A, A.
m_A, DIFFERENT_NONZERO_PATTERN);
298 MatDuplicate(
m_A, MAT_COPY_VALUES, &(A.
m_A));
300 MatDuplicate(
m_A, MAT_DO_NOT_COPY_VALUES, &(A.
m_A));
311 MatGetSize (
m_A, M, N);
319 MatGetLocalSize (
m_A, m, n);
340 "Trying to access row " << row <<
"on process that owns range " <<
m_lo <<
"to " <<
m_hi
342 MatGetRow (
m_A, row, nbNonZero, cols, values);
354 MatRestoreRow (
m_A, row, nbNonZero, cols, values);
376 "Trying to access row " << row <<
"on process that owns range " <<
m_lo <<
"to " <<
m_hi
385 MatGetRow(
m_A, row, &nbNonZero,
nullptr, &vals);
387 rowValues.reserve(nbNonZero);
389 rowValues.insert(rowValues.begin(),vals,vals+nbNonZero);
392 MatRestoreRow(
m_A, row, &nbNonZero,
nullptr, &vals);
403 "Trying to access row " << row <<
" on process that owns range " <<
m_lo <<
" to " <<
m_hi
411 rowValues.resize(N, 0.0);
416 MatGetRow(
m_A, row, &nbNonZero, &cols, &vals);
420 rowValues[cols[i]] = vals[i];
423 MatRestoreRow(
m_A, row, &nbNonZero, &cols, &vals);
435 "Incompatible decompositions of vector and matrix for column "
436 <<
"insertion." << std::endl <<
"Matrix has local range (" << loA <<
"," << hiA
437 <<
") whereas vector has local range (" << loV <<
"," << hiV <<
")"
444 rowIndices[i - loA] = i;
448 MatGetValues(
m_A, nbRows, rowIndices, nbCols, colIndices, v.
m_localData);
462 "Incompatible decompositions of vector and matrix for column "
463 <<
"insertion." << std::endl <<
"Matrix has local range (" << loA <<
"," << hiA
464 <<
") whereas vector has local range (" << loV <<
"," << hiV <<
")"
471 rowIndices[i - loA] = i;
475 MatSetValues(
m_A, nbRows, rowIndices, nbCols, colIndices, v.
m_localData, INSERT_VALUES);
490 "Incompatible decompositions of vector and matrix for column "
491 <<
"insertion." << std::endl <<
"Matrix has local range (" << loA <<
"," << hiA
492 <<
") whereas vector has local range (" << loV <<
"," << hiV <<
")"
496 MatGetDiagonal(
m_A, v.
m_v);
507 for (
CepsInt i = lo; i < hi; i++)
508 v[i] =
static_cast<CepsReal>(std::abs(v[i]) > epsilon);
518 PetscViewerDestroy(&fd);
528 PetscViewerDestroy(&fd);
537 MatAssemblyBegin (
m_A, MAT_FINAL_ASSEMBLY);
544 MatAssemblyEnd(
m_A, MAT_FINAL_ASSEMBLY);
559 MatAssemblyBegin(
m_A, MAT_FLUSH_ASSEMBLY);
560 MatAssemblyEnd (
m_A, MAT_FLUSH_ASSEMBLY);
567 PetscBool set, flag = PETSC_FALSE;
568 MatIsSymmetricKnown(
m_A, &set, &flag);
570 return (flag == PETSC_FALSE ?
false :
true);
578 MatAssembled(
m_A, &assembled);
580 return (assembled == PETSC_FALSE ?
false :
true);
586 CepsString message =
"Not to be used on a non-finalized matrix. ";
587 if (not info.empty())
588 message +=
"Call DistributedMatrix::finalize() before " + info;
597 MatView(
m_A, PETSC_VIEWER_STDOUT_WORLD);
605 "canot use this matrix as it is not properly set.\nCheck if "
606 "sizes and memory preallocation are correct"
614 MatScale (
m_A, scalar);
#define CEPS_DISPLAY_VAL(...)
Generates a string with the names and values of the arguments of the macro, supports up to ten argume...
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
std::basic_string< CepsChar > CepsString
C++ format string.
CepsScalar CepsMathScalar
Real numbers.
std::vector< _Type, _Alloc > CepsVector
C++ vector.
CepsIndex CepsGlobalIndex
Many uses. Has to be signed for PETSc.
float CepsReal
Need single precision floating point.
int32_t CepsInt
Need 32 bit integer.
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.
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.
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.
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).
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.
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 setColumnAsDistributedVector(DistributedVector &v, CepsGlobalIndex column)
Sets the values of a distributed vector in matrix column.
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 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.
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.
CepsMathScalar * m_localData
virtual void getLocalData()
Enables direct access to the stored local values.
PetscVector m_v
The underlying vector.
virtual void releaseLocalData()
Release the pointer on the local data.
void getLocalRange(CepsGlobalIndex *lo, CepsGlobalIndex *hi) const
Get the range of rows owned by current process.
void destroyTabular(_Type &)
Destroy[delete] any type.
MPI_Comm getCommunicator()
Get the communicator.
CepsBool isValidPtr(_Type *ptr)
Tells if pointer is not null.
CepsBool isParallel()
Is there more than 1 process currently working ?