13 #if !defined(KRATOS_CSR_MATRIX_H_INCLUDED )
14 #define KRATOS_CSR_MATRIX_H_INCLUDED
20 #include <span/span.hpp>
58 template<
class TDataType=
double,
class TIndexType=std::
size_t>
65 typedef std::unordered_map<std::pair<IndexType, IndexType>,
85 KRATOS_ERROR <<
"Attempting to construct a serial CsrMatrix with a distributed communicator" << std::endl;
92 template<
class TGraphType>
95 mpComm = rSparseGraph.pGetComm();
96 TIndexType row_data_size=0;
97 TIndexType col_data_size=0;
98 rSparseGraph.ExportCSRArrays(mpRowIndicesData,row_data_size,mpColIndicesData, col_data_size);
99 mRowIndices = Kratos::span<TIndexType>(mpRowIndicesData, row_data_size);
100 mColIndices = Kratos::span<TIndexType>(mpColIndicesData, col_data_size);
114 for(
const auto& item : matrix_map){
122 for(
const auto item : matrix_map){
125 TDataType value = item.second;
133 mpComm = rOtherMatrix.mpComm;
138 mNrows = rOtherMatrix.mNrows;
139 mNcols = rOtherMatrix.mNcols;
143 mRowIndices[
i] = rOtherMatrix.mRowIndices[
i];
148 mColIndices[
i] = rOtherMatrix.mColIndices[
i];
153 mValuesVector[
i] = rOtherMatrix.mValuesVector[
i];
160 mIsOwnerOfData=rOtherMatrix.mIsOwnerOfData;
161 rOtherMatrix.mIsOwnerOfData=
false;
164 mpRowIndicesData = rOtherMatrix.mpRowIndicesData;
165 mpColIndicesData = rOtherMatrix.mpColIndicesData;
166 mpValuesVectorData = rOtherMatrix.mpValuesVectorData;
169 mRowIndices = rOtherMatrix.mRowIndices;
170 mColIndices = rOtherMatrix.mColIndices;
171 mValuesVector = rOtherMatrix.mValuesVector;
173 mNrows = rOtherMatrix.mNrows;
174 mNcols = rOtherMatrix.mNcols;
192 mpComm = rOtherMatrix.mpComm;
193 mIsOwnerOfData=rOtherMatrix.mIsOwnerOfData;
194 rOtherMatrix.mIsOwnerOfData=
false;
197 mpRowIndicesData = rOtherMatrix.mpRowIndicesData;
198 mpColIndicesData = rOtherMatrix.mpColIndicesData;
199 mpValuesVectorData = rOtherMatrix.mpValuesVectorData;
202 mRowIndices = rOtherMatrix.mRowIndices;
203 mColIndices = rOtherMatrix.mColIndices;
204 mValuesVector = rOtherMatrix.mValuesVector;
206 mNrows = rOtherMatrix.mNrows;
207 mNcols = rOtherMatrix.mNcols;
236 mValuesVector[
i] = value;
242 return mRowIndices.size()-1;
257 return mIsOwnerOfData;
262 mIsOwnerOfData = IsOwner;
275 return mValuesVector;
288 return mValuesVector;
306 return mColIndices[
i];
315 delete [] mpRowIndicesData;
316 mpRowIndicesData = pExternalData;
318 mRowIndices = Kratos::span<TIndexType>(mpRowIndicesData, DataSize);
320 mRowIndices = Kratos::span<TIndexType>();
326 delete [] mpColIndicesData;
327 mpColIndicesData = pExternalData;
329 mColIndices = Kratos::span<TIndexType>(mpColIndicesData, DataSize);
331 mColIndices = Kratos::span<TIndexType>();
337 delete [] mpValuesVectorData;
338 mpValuesVectorData = pExternalData;
340 mValuesVector = Kratos::span<TDataType>(mpValuesVectorData, DataSize);
342 mValuesVector = Kratos::span<TDataType>();
348 if(mpRowIndicesData !=
nullptr)
349 delete [] mpRowIndicesData;
350 mpRowIndicesData =
new TIndexType[DataSize];
351 mRowIndices = Kratos::span<TIndexType>(mpRowIndicesData, DataSize);
357 if(mpColIndicesData !=
nullptr)
358 delete [] mpColIndicesData;
359 mpColIndicesData =
new TIndexType[DataSize];
360 mColIndices = Kratos::span<TIndexType>(mpColIndicesData, DataSize);
366 if(mpValuesVectorData !=
nullptr)
367 delete [] mpValuesVectorData;
368 mpValuesVectorData =
new TDataType[DataSize];
369 mValuesVector = Kratos::span<TDataType>(mpValuesVectorData, DataSize);
378 max_col =
std::max(max_col,mColIndices[
i]);
380 KRATOS_ERROR <<
" max column index : " << max_col <<
" exceeds mNcols :" << mNcols << std::endl;
410 template<
class TInputVectorType,
class TOutputVectorType>
411 void SpMV(
const TInputVectorType&
x, TOutputVectorType&
y)
const
413 KRATOS_ERROR_IF(
size1() !=
y.size() ) <<
"SpMV: mismatch between row sizes : " <<
size1() <<
" and destination vector size " <<
y.size() << std::endl;
414 KRATOS_ERROR_IF(
size2() !=
x.size() ) <<
"SpmV: mismatch between col sizes : " <<
size2() <<
" and input vector size " <<
x.size() << std::endl;
431 template<
class TInputVectorType,
class TOutputVectorType>
433 const TInputVectorType&
x,
434 const TDataType beta,
435 TOutputVectorType&
y)
const
437 KRATOS_ERROR_IF(
size1() !=
y.size() ) <<
"SpMV: mismatch between matrix sizes : " <<
size1() <<
" " <<
size2() <<
" and destination vector size " <<
y.size() << std::endl;
438 KRATOS_ERROR_IF(
size2() !=
x.size() ) <<
"SpmV: mismatch between matrix sizes : " <<
size1() <<
" " <<
size2() <<
" and input vector size " <<
x.size() << std::endl;
443 TDataType aux = TDataType();
454 template<
class TInputVectorType,
class TOutputVectorType>
457 KRATOS_ERROR_IF(
size2() !=
y.size() ) <<
"TransposeSpMV: mismatch between transpose matrix sizes : " <<
size2() <<
" " <<
size1() <<
" and destination vector size " <<
y.size() << std::endl;
458 KRATOS_ERROR_IF(
size1() !=
x.size() ) <<
"TransposeSpMV: mismatch between transpose matrix sizes : " <<
size2() <<
" " <<
size1() <<
" and input vector size " <<
x.size() << std::endl;
472 template<
class TInputVectorType,
class TOutputVectorType>
474 const TInputVectorType&
x,
475 const TDataType beta,
476 TOutputVectorType&
y)
const
478 KRATOS_ERROR_IF(
size2() !=
y.size() ) <<
"TransposeSpMV: mismatch between transpose matrix sizes : " <<
size2() <<
" " <<
size1() <<
" and destination vector size " <<
y.size() << std::endl;
479 KRATOS_ERROR_IF(
size1() !=
x.size() ) <<
"TransposeSpMV: mismatch between transpose matrix sizes : " <<
size2() <<
" " <<
size1() <<
" and input vector size " <<
x.size() << std::endl;
500 return std::sqrt(sum2);
528 for(
unsigned int i=0;
i<
size1(); ++
i)
536 value_map[ {
i,
j}] =
v;
547 template<
class TMatrixType,
class TIndexVectorType >
549 const TMatrixType& rMatrixInput,
550 const TIndexVectorType& EquationId
553 KRATOS_DEBUG_ERROR_IF(rMatrixInput.size1() != EquationId.size()) <<
"sizes of matrix and equation id do not match in Assemble" << std::endl;
554 KRATOS_DEBUG_ERROR_IF(rMatrixInput.size2() != EquationId.size()) <<
"sizes of matrix and equation id do not match in Assemble" << std::endl;
556 const unsigned int local_size = rMatrixInput.size1();
558 for (
unsigned int i_local = 0; i_local <
local_size; ++i_local) {
571 for(
unsigned int j_local=1; j_local<
local_size; ++j_local) {
572 J = EquationId[j_local];
576 }
else if(
J > lastJ) {
578 }
else if(
J < lastJ) {
590 template<
class TMatrixType,
class TIndexVectorType >
592 const TMatrixType& rMatrixInput,
593 const TIndexVectorType& RowEquationId,
594 const TIndexVectorType& ColEquationId
597 KRATOS_DEBUG_ERROR_IF(rMatrixInput.size1() != RowEquationId.size()) <<
"sizes of matrix and equation id do not match in Assemble" << std::endl;
598 KRATOS_DEBUG_ERROR_IF(rMatrixInput.size2() != ColEquationId.size()) <<
"sizes of matrix and equation id do not match in Assemble" << std::endl;
600 const unsigned int local_size = rMatrixInput.size1();
601 const unsigned int col_size = rMatrixInput.size2();
603 for (
unsigned int i_local = 0; i_local <
local_size; ++i_local) {
604 const IndexType I = RowEquationId[i_local];
616 for(
unsigned int j_local=1; j_local<col_size; ++j_local) {
617 J = ColEquationId[j_local];
621 }
else if(
J > lastJ) {
623 }
else if(
J < lastJ) {
645 template<
class TVectorType1,
class TVectorType2=TVectorType1>
647 const TDataType DiagonalValue,
651 <<
" and free_dofs_vector size " << rFreeDofsVector.size() << std::endl;
653 <<
" and free_dofs_vector size " << rFreeDofsVector.size() << std::endl;
658 rRHS[
i] *= rFreeDofsVector[
i];
662 if(std::abs(rFreeDofsVector[
i]-1.0) < 1
e-14) {
700 std::stringstream buffer;
701 buffer <<
"CsrMatrix" ;
708 rOStream <<
"CsrMatrix" << std::endl;
715 rOStream <<
"size1 : " <<
size1() <<std::endl;
716 rOStream <<
"size2 : " <<
size2() <<std::endl;
717 rOStream <<
"nnz : " <<
nnz() <<std::endl;
718 rOStream <<
"index1_data : " << std::endl;
720 rOStream << item <<
",";
721 rOStream << std::endl;
722 rOStream <<
"index2_data : " << std::endl;
724 rOStream << item <<
",";
725 rOStream << std::endl;
726 rOStream <<
"value_data : " << std::endl;
728 rOStream << item <<
",";
729 rOStream << std::endl;
755 template<
class TVectorType >
761 int m =
l + (r -
l) / 2;
811 bool mIsOwnerOfData =
true;
814 TDataType* mpValuesVectorData =
nullptr;
815 Kratos::span<IndexType> mRowIndices;
816 Kratos::span<IndexType> mColIndices;
817 Kratos::span<TDataType> mValuesVector;
828 rSerializer.
save(
"IsOwnerOfData",mIsOwnerOfData);
830 rSerializer.
save(
"nrows",mRowIndices.size());
832 rSerializer.
save(
"i",mRowIndices[
i]);
834 rSerializer.
save(
"cols_size",mColIndices.size());
836 rSerializer.
save(
"i",mColIndices[
i]);
838 rSerializer.
save(
"val_size",mValuesVector.size());
840 rSerializer.
save(
"d",mValuesVector[
i]);
842 rSerializer.
save(
"Nrow",mNrows);
843 rSerializer.
save(
"Ncol",mNcols);
848 rSerializer.
load(
"IsOwnerOfData",mIsOwnerOfData);
849 if(mIsOwnerOfData ==
false)
852 KRATOS_WARNING(
"csr_matrix becomes owner of a copy of data after serialization");
856 rSerializer.
load(
"nrows",rows_size);
857 mpRowIndicesData =
new TIndexType[rows_size];
858 mRowIndices = Kratos::span<TIndexType>(mpRowIndicesData, rows_size);
860 rSerializer.
load(
"i",mRowIndices[
i]);
863 rSerializer.
load(
"cols_size",cols_size);
864 mpColIndicesData =
new TIndexType[cols_size];
865 mColIndices = Kratos::span<TIndexType>(mpRowIndicesData, cols_size);
867 rSerializer.
load(
"i",mColIndices[
i]);
870 rSerializer.
load(
"val_size",vals_size);
871 mpValuesVectorData =
new TDataType[vals_size];
872 mValuesVector = Kratos::span<TDataType>(mpValuesVectorData, vals_size);
874 rSerializer.
load(
"d",mValuesVector[
i]);
876 rSerializer.
load(
"Nrow",mNrows);
877 rSerializer.
load(
"Ncol",mNcols);
918 template<
class TDataType>
926 template<
class TDataType>
931 rOStream << std::endl;
This class implements "serial" CSR matrix, including capabilities for FEM assembly.
Definition: csr_matrix.h:60
IndexType BinarySearch(const TVectorType &arr, IndexType l, IndexType r, IndexType x) const
Definition: csr_matrix.h:756
Kratos::span< TDataType > & value_data()
Definition: csr_matrix.h:273
CsrMatrix(const MatrixMapType &matrix_map)
Definition: csr_matrix.h:111
void AssignValueData(TDataType *pExternalData, TIndexType DataSize)
Definition: csr_matrix.h:334
void BeginAssemble()
Definition: csr_matrix.h:542
void CheckColSize()
Definition: csr_matrix.h:374
void TransposeSpMV(const TDataType alpha, const TInputVectorType &x, const TDataType beta, TOutputVectorType &y) const
Definition: csr_matrix.h:473
IndexType nnz() const
Definition: csr_matrix.h:250
bool IsOwnerOfData() const
Definition: csr_matrix.h:255
IndexType size1() const
Definition: csr_matrix.h:240
IndexType size2() const
Definition: csr_matrix.h:245
CsrMatrix(const DataCommunicator &rComm)
Definition: csr_matrix.h:82
const DataCommunicator & GetComm() const
Definition: csr_matrix.h:222
CsrMatrix()
Definition: csr_matrix.h:77
void SetRowSize(IndexType Nrows)
Definition: csr_matrix.h:296
TDataType & operator()(IndexType I, IndexType J)
Definition: csr_matrix.h:383
void ComputeColSize()
Definition: csr_matrix.h:301
const Kratos::span< TDataType > & value_data() const
Definition: csr_matrix.h:286
void SpMV(const TDataType alpha, const TInputVectorType &x, const TDataType beta, TOutputVectorType &y) const
Definition: csr_matrix.h:432
void SetValue(const TDataType value)
Definition: csr_matrix.h:232
void SpMV(const TInputVectorType &x, TOutputVectorType &y) const
Definition: csr_matrix.h:411
void AssignIndex1Data(TIndexType *pExternalData, TIndexType DataSize)
Definition: csr_matrix.h:312
void Assemble(const TMatrixType &rMatrixInput, const TIndexVectorType &RowEquationId, const TIndexVectorType &ColEquationId)
Definition: csr_matrix.h:591
CsrMatrix & operator=(CsrMatrix const &rOtherMatrix)=delete
Assignment operator.
void AssembleEntry(const TDataType Value, const IndexType GlobalI, const IndexType GlobalJ)
Definition: csr_matrix.h:634
const Kratos::span< IndexType > & index1_data() const
Definition: csr_matrix.h:278
void Clear()
Definition: csr_matrix.h:213
const DataCommunicator * pGetComm() const
Definition: csr_matrix.h:227
TIndexType IndexType
Definition: csr_matrix.h:64
void FinalizeAssemble()
Definition: csr_matrix.h:544
void SetColSize(IndexType Ncols)
Definition: csr_matrix.h:291
CsrMatrix(const TGraphType &rSparseGraph)
constructor.
Definition: csr_matrix.h:93
void ResizeValueData(TIndexType DataSize)
Definition: csr_matrix.h:363
TDataType NormFrobenius() const
Definition: csr_matrix.h:494
void Assemble(const TMatrixType &rMatrixInput, const TIndexVectorType &EquationId)
Definition: csr_matrix.h:548
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: csr_matrix.h:713
void TransposeSpMV(const TInputVectorType &x, TOutputVectorType &y) const
Definition: csr_matrix.h:455
Kratos::span< IndexType > & index2_data()
Definition: csr_matrix.h:269
std::unordered_map< std::pair< IndexType, IndexType >, double, PairHasher< IndexType, IndexType >, PairComparor< IndexType, IndexType > > MatrixMapType
Definition: csr_matrix.h:69
const TDataType & operator()(IndexType I, IndexType J) const
Definition: csr_matrix.h:392
bool Has(IndexType I, IndexType J) const
Definition: csr_matrix.h:401
CsrMatrix & operator=(CsrMatrix &&rOtherMatrix)
Definition: csr_matrix.h:190
KRATOS_CLASS_POINTER_DEFINITION(CsrMatrix)
Pointer definition of CsrMatrix.
CsrMatrix(CsrMatrix< TDataType, TIndexType > &&rOtherMatrix)
Definition: csr_matrix.h:158
void SetIsOwnerOfData(bool IsOwner)
Definition: csr_matrix.h:260
std::string Info() const
Turn back information as a string.
Definition: csr_matrix.h:698
void ApplyHomogeneousDirichlet(const TVectorType1 &rFreeDofsVector, const TDataType DiagonalValue, TVectorType2 &rRHS)
Definition: csr_matrix.h:646
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: csr_matrix.h:706
void ResizeIndex2Data(TIndexType DataSize)
Definition: csr_matrix.h:354
void AssignIndex2Data(TIndexType *pExternalData, TIndexType DataSize)
Definition: csr_matrix.h:323
void reserve(IndexType NRows, IndexType nnz)
Definition: csr_matrix.h:513
Kratos::span< IndexType > & index1_data()
Definition: csr_matrix.h:265
void ResizeIndex1Data(TIndexType DataSize)
Definition: csr_matrix.h:345
const Kratos::span< IndexType > & index2_data() const
Definition: csr_matrix.h:282
CsrMatrix(const CsrMatrix< TDataType, TIndexType > &rOtherMatrix)
Definition: csr_matrix.h:131
~CsrMatrix()
Destructor.
Definition: csr_matrix.h:179
MatrixMapType ToMap() const
Definition: csr_matrix.h:525
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
virtual bool IsDistributed() const
Check whether this DataCommunicator is aware of parallelism.
Definition: data_communicator.h:606
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
static DataCommunicator & GetDataCommunicator(const std::string &rName)
Retrieve a registered DataCommunicator instance.
Definition: parallel_environment.cpp:26
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
void load(std::string const &rTag, TDataType &rObject)
Definition: serializer.h:207
void save(std::string const &rTag, std::array< TDataType, TDataSize > const &rObject)
Definition: serializer.h:545
Short class definition.
Definition: sparse_graph.h:66
void Finalize()
Definition: sparse_graph.h:204
void AddEntry(const IndexType RowIndex, const IndexType ColIndex)
Definition: sparse_graph.h:161
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
#define KRATOS_WARNING(label)
Definition: logger.h:265
static double max(double a, double b)
Definition: GeometryFunctions.h:79
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void AtomicAdd(TDataType &target, const TDataType &value)
Definition: atomic_utilities.h:55
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
TABLE_NUMBER_ANGULAR_VELOCITY TABLE_NUMBER_MOMENT I33 BEAM_INERTIA_ROT_UNIT_LENGHT_Y KRATOS_DEFINE_APPLICATION_VARIABLE(DEM_APPLICATION, double, BEAM_INERTIA_ROT_UNIT_LENGHT_Z) typedef std double
Definition: DEM_application_variables.h:182
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
y
Other simbols definition.
Definition: generate_axisymmetric_navier_stokes_element.py:54
v
Definition: generate_convection_diffusion_explicit_element.py:114
alpha
Definition: generate_convection_diffusion_explicit_element.py:113
int local_size
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:17
def load(f)
Definition: ode_solve.py:307
int k
Definition: quadrature.py:595
int j
Definition: quadrature.py:648
int m
Definition: run_marine_rain_substepping.py:8
J
Definition: sensitivityMatrix.py:58
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17
integer l
Definition: TensorModule.f:17
e
Definition: run_cpp_mpi_tests.py:31
This is a key comparer between two indexes pairs.
Definition: key_hash.h:432
This is a hasher for pairs.
Definition: key_hash.h:412