37 m_localData (nullptr),
38 m_mayReadData (false ),
39 m_isAssembled (false ),
40 #ifdef PROFILING_VECTOR
44 m_initialized (false ),
64 std::shared_ptr<DistributedVector> templateVector,
73 templateVector->duplicate(*
this,copyValues);
79 VecScatterCreateToAll(templateVector->getVector(),&context,&
m_v);
80 VecScatterBegin (context,templateVector->getVector(),
m_v,INSERT_VALUES,SCATTER_FORWARD);
81 VecScatterEnd (context,templateVector->getVector(),
m_v,INSERT_VALUES,SCATTER_FORWARD);
82 VecScatterDestroy (&context);
84 #ifdef PROFILING_VECTOR
85 m_totalAssembly = m_tickAssembly = 0.0;
102 if (not isSequential)
105 templateVector.
duplicate(*
this,copyValues);
111 VecScatterCreateToAll(templateVector.
getVector(),&context,&
m_v);
112 VecScatterBegin (context,templateVector.
getVector(),
m_v,INSERT_VALUES,SCATTER_FORWARD);
113 VecScatterEnd (context,templateVector.
getVector(),
m_v,INSERT_VALUES,SCATTER_FORWARD);
114 VecScatterDestroy (&context);
116 #ifdef PROFILING_VECTOR
117 m_totalAssembly = m_tickAssembly = 0.0;
133 mat->getLocalRange(&lo,&hi);
136 CepsBool isOwner = lo <= row and row < hi;
140 const PetscScalar *values;
145 MatGetRow(mat->getMatrix(),row,&nbEntries,&columnIndices,&values);
146 VecSetValues(
m_v,nbEntries,columnIndices,values,INSERT_VALUES);
153 MatRestoreRow(mat->getMatrix(),row,&nbEntries,&columnIndices,&values);
167 #ifdef PROFILING_VECTOR
169 std::cout <<
"/* PROFILING: Distributed vector - time spent assembling = " << m_totalAssembly
170 <<
" seconds. *\\" << std::endl;
173 VecDestroy(&(this->
m_v));
192 CEPS_ABORT_IF(M < 0 or m < 0,
"(M = " << M <<
", n = " << m <<
"): cannot set negative size!");
193 VecSetSizes(
m_v,m,M);
221 VecGetLocalSize(
m_v, &s);
230 VecGetLocalSize(
m_v,&s);
261 VecSetValue(
m_v,row,value,INSERT_VALUES);
268 VecSetValue(
m_v,row,value,ADD_VALUES);
275 PetscBool value = flag ? PETSC_TRUE : PETSC_FALSE;
276 VecSetOption(
m_v,VEC_IGNORE_OFF_PROC_ENTRIES,value);
287 VecSetValues(
m_v,n,indices,values,INSERT_VALUES);
346 "Given arrays of values and row indices have different size."
362 VecSetValues(
m_v,n,indices,values,ADD_VALUES);
421 "invalid access to local data.\nMake sure you called "
422 "getLocalData() beforehand."
440 "trying to access row " << row <<
"which is out of local range ("
487 #ifdef PROFILING_VECTOR
488 m_tickAssembly = ceps::tick();
491 VecAssemblyBegin(
m_v);
500 #ifdef PROFILING_VECTOR
501 m_totalAssembly += ceps::tock(m_tickAssembly);
509 #ifdef PROFILING_VECTOR
510 m_tickAssembly = ceps::tick();
514 #ifdef PROFILING_VECTOR
515 m_totalAssembly += ceps::tock(m_tickAssembly);
543 "DistributedVector::equals: trying to compare vectors of sizes "
548 "DistributedVector:equals: trying to compare two vectors with different data repartition."
556 for (
CepsInt i=0; i<(hi-lo); i++)
586 "trying to compare two vectors with different data repartition."
594 for (
CepsInt i=0; i<(hi-lo); i++)
622 "cannot compute x = x + a.x when using PETSc."
627 "added vector is not properly initialized."
644 "Trying to multiply vectors with different local ranges"
649 VecGetArrayRead(v.
m_v,&src);
660 VecRestoreArrayRead(v.
m_v,&src);
670 "cannot compute x = A.x when using PETSc."
675 "x vector is not properly initialized."
679 CepsInt matrixMSize, matrixNSize;
680 A.
getSize(&matrixMSize, &matrixNSize);
682 "cannot compute A.x as A has size (" << matrixMSize <<
"," << matrixNSize
690 "vector and matrix have different local ranges ("
691 <<
m_lo <<
"," <<
m_hi <<
") and (" << matrixLo
692 <<
"," << matrixHi <<
") respectively."
719 VecNorm(
m_v,NORM_INFINITY,&norm);
727 VecNorm (
m_v,NORM_1,&norm);
735 VecNorm (
m_v,NORM_2,&norm);
742 VecView(
m_v,PETSC_VIEWER_STDOUT_WORLD);
752 "Trying to add vectors with different local ranges"
757 VecGetArrayRead (v.
m_v,&src);
760 VecRestoreArrayRead(v.
m_v,&src);
769 VecShift(
m_v,scalar);
777 "Trying to add vectors with different local ranges"
782 VecGetArrayRead(v.
m_v,&src);
786 VecRestoreArrayRead(v.
m_v,&src);
801 VecScale(
m_v,scalar);
815 "DistributedVector::checkAndSetSizes(): calling vector is not of proper sizes."
832 VecSetType(
m_v,VECSTANDARD);
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
CepsScalar CepsMathScalar
Real numbers.
std::vector< _Type, _Alloc > CepsVector
C++ vector.
CepsIndex CepsGlobalIndex
Many uses. Has to be signed for PETSc.
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
float CepsReal
Need single precision floating point.
int32_t CepsInt
Need 32 bit integer.
std::shared_ptr< DistributedMatrix > DMatPtr
Short typedef for pointer on dist matrix.
Sparse matrix distributed between process.
const PetscMatrix & getMatrix() const
The underlying matrix.
void getLocalRange(CepsGlobalIndex *lo, CepsGlobalIndex *hi) const
Get the range of rows owned by current process.
void getSize(CepsInt *M, CepsInt *N) const
Get the global size of the matrix.
Structure to hold spatially dependant data and distribute it between process.
CepsMathScalar * m_localData
DistributedVector()
Default Constructor.
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.
CepsMathScalar l2Norm() const
-norm of vector
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.
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.
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.
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.
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 addScaled(const DistributedVector &x, CepsMathScalar alpha)
Adds alpha*x to current vector.
MPI_Comm getCommunicator()
Get the communicator.
CepsBool approxEquals(CepsReal a, CepsReal b, CepsReal epsilon)
Approximate equality with epsilon tolerance.
CepsBool isValidPtr(_Type *ptr)
Tells if pointer is not null.
CepsBool isMaster()
Is calling process the master ?
CepsBool equals(CepsReal a, CepsReal b, CepsReal error=1.0)
CepsReal equality up to machine precision.
CepsBool isSamePtr(_Type *xptr, U *yptr)
Tells if two pointers designate the same address.