13 #if !defined(KRATOS_SPARSE_GRAPH_H_INCLUDED )
14 #define KRATOS_SPARSE_GRAPH_H_INCLUDED
19 #include <unordered_map>
20 #include <unordered_set>
23 #include <span/span.hpp>
64 template<
class TIndexType=std::
size_t>
71 typedef std::map<IndexType, std::unordered_set<IndexType> >
GraphType;
95 KRATOS_ERROR <<
"Attempting to construct a serial CsrMatrix with a distributed communicator" << std::endl;
106 mpComm(rOther.mpComm),
107 mGraph(rOther.mGraph)
111 SparseGraph(
const std::vector<IndexType>& rSingleVectorRepresentation)
138 return mGraph.empty();
143 const auto& row_it = mGraph.find(I);
144 if(row_it != mGraph.end() ) {
145 if((row_it->second).find(
J) != (row_it->second).end())
153 return (mGraph.find(Key))->second;
163 mGraph[RowIndex].insert(ColIndex);
166 template<
class TContainerType>
169 mGraph[RowIndex].insert(rColIndices.begin(), rColIndices.end());
172 template<
class TIteratorType>
174 const TIteratorType& rColBegin,
175 const TIteratorType& rColEnd
178 mGraph[RowIndex].insert(rColBegin, rColEnd);
181 template<
class TContainerType>
184 for(
auto I : rIndices)
185 mGraph[I].insert(rIndices.begin(), rIndices.end());
188 template<
class TContainerType>
189 void AddEntries(
const TContainerType& rRowIndices,
const TContainerType& rColIndices)
191 for(
auto I : rRowIndices)
198 for(
const auto& item: rOtherGraph.
GetGraph())
214 template<
class TVectorType=DenseVector<IndexType>>
216 TVectorType& rRowIndices,
217 TVectorType& rColIndices
224 ExportCSRArrays(pRowIndicesData,RowIndicesDataSize,pColIndicesData,ColIndicesDataSize);
225 if(rRowIndices.size() != RowIndicesDataSize)
226 rRowIndices.resize(RowIndicesDataSize);
231 delete [] pRowIndicesData;
232 if(rColIndices.size() != ColIndicesDataSize)
233 rColIndices.resize(ColIndicesDataSize);
237 delete [] pColIndicesData;
239 return rRowIndices.size();
243 Kratos::span<IndexType>& rRowIndices,
244 Kratos::span<IndexType>& rColIndices
259 pRowIndicesData =
new IndexType[nrows+1];
260 rRowDataSize = nrows+1;
261 Kratos::span<IndexType> row_indices(pRowIndicesData, nrows+1);
269 for(
const auto& item : this->
GetGraph())
271 row_indices[item.first+1] = item.second.size();
275 for(
int i = 1; i<static_cast<int>(row_indices.size()); ++
i){
276 row_indices[
i] += row_indices[
i-1];
283 Kratos::span<IndexType> col_indices(pColIndicesData, nnz);
291 for(
const auto& item : this->
GetGraph()){
292 IndexType
start = row_indices[item.first];
295 for(
auto index : item.second){
303 std::sort(col_indices.begin()+row_indices[
i], col_indices.begin()+row_indices[
i+1]);
314 std::vector< IndexType > single_vector_representation;
317 single_vector_representation.push_back(nrows);
319 for(
const auto& item : this->
GetGraph()){
320 IndexType I = item.first;
321 single_vector_representation.push_back(I);
322 single_vector_representation.push_back(item.second.size());
323 for(
auto J : item.second)
324 single_vector_representation.push_back(
J);
326 return single_vector_representation;
337 while(
counter < rSingleVectorRepresentation.size())
339 auto I = rSingleVectorRepresentation[
counter++];
340 auto nrow = rSingleVectorRepresentation[
counter++];
365 using pointer =
typename GraphType::value_type*;
370 : map_iterator(it.map_iterator) {}
374 {
return map_iterator ==
rhs.map_iterator; }
376 {
return map_iterator !=
rhs.map_iterator; }
378 const typename GraphType::mapped_type&
operator*()
const {
return (map_iterator->second); }
379 const typename GraphType::mapped_type&
operator->()
const {
return map_iterator->second; }
383 return map_iterator->first;
409 std::stringstream buffer;
410 buffer <<
"SparseGraph" ;
415 void PrintInfo(std::ostream& rOStream)
const {rOStream <<
"SparseGraph";}
484 rSerializer.
save(
"IJ",IJ);
489 std::vector< IndexType > IJ;
490 rSerializer.
load(
"IJ",IJ);
535 template<
class TIndexType=std::
size_t>
542 template<
class TIndexType=std::
size_t>
547 rOStream << std::endl;
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
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
Definition: sparse_graph.h:359
typename GraphType::value_type & reference
Definition: sparse_graph.h:366
std::ptrdiff_t difference_type
Definition: sparse_graph.h:363
IndexType GetRowIndex() const
Definition: sparse_graph.h:382
const_row_iterator & base()
Definition: sparse_graph.h:380
const GraphType::mapped_type & operator*() const
Definition: sparse_graph.h:378
const_iterator_adaptor(const const_iterator_adaptor &it)
Definition: sparse_graph.h:369
typename GraphType::value_type value_type
Definition: sparse_graph.h:364
typename GraphType::value_type * pointer
Definition: sparse_graph.h:365
const_iterator_adaptor & operator++()
Definition: sparse_graph.h:371
const GraphType::mapped_type & operator->() const
Definition: sparse_graph.h:379
bool operator!=(const const_iterator_adaptor &rhs) const
Definition: sparse_graph.h:375
const_row_iterator const & base() const
Definition: sparse_graph.h:381
const_iterator_adaptor operator++(int)
Definition: sparse_graph.h:372
bool operator==(const const_iterator_adaptor &rhs) const
Definition: sparse_graph.h:373
const_iterator_adaptor(const_row_iterator it)
Definition: sparse_graph.h:368
std::forward_iterator_tag iterator_category
Definition: sparse_graph.h:362
Short class definition.
Definition: sparse_graph.h:66
SparseGraph(DataCommunicator &rComm)
Definition: sparse_graph.h:92
IndexType ExportCSRArrays(TVectorType &rRowIndices, TVectorType &rColIndices) const
Definition: sparse_graph.h:215
TIndexType IndexType
Definition: sparse_graph.h:70
SparseGraph(IndexType N)
Definition: sparse_graph.h:81
bool IsEmpty() const
Definition: sparse_graph.h:136
void AddEntries(const TContainerType &rIndices)
Definition: sparse_graph.h:182
void Clear()
Definition: sparse_graph.h:156
const_iterator_adaptor begin() const
Definition: sparse_graph.h:387
const DataCommunicator & GetComm() const
Definition: sparse_graph.h:119
void AddEntries(const IndexType RowIndex, const TIteratorType &rColBegin, const TIteratorType &rColEnd)
Definition: sparse_graph.h:173
IndexType Size() const
Definition: sparse_graph.h:129
KRATOS_CLASS_POINTER_DEFINITION(SparseGraph)
Pointer definition of SparseGraph.
const GraphType & GetGraph() const
Definition: sparse_graph.h:208
std::vector< IndexType > ExportSingleVectorRepresentation() const
Definition: sparse_graph.h:312
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: sparse_graph.h:418
void AddEntries(const TContainerType &rRowIndices, const TContainerType &rColIndices)
Definition: sparse_graph.h:189
SparseGraph()
Default constructor.
Definition: sparse_graph.h:87
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: sparse_graph.h:415
void AddEntries(const IndexType RowIndex, const TContainerType &rColIndices)
Definition: sparse_graph.h:167
IndexType ExportCSRArrays(Kratos::span< IndexType > &rRowIndices, Kratos::span< IndexType > &rColIndices) const =delete
void Finalize()
Definition: sparse_graph.h:204
~SparseGraph()
Destructor.
Definition: sparse_graph.h:101
SparseGraph(const std::vector< IndexType > &rSingleVectorRepresentation)
Definition: sparse_graph.h:111
void AddFromSingleVectorRepresentation(const std::vector< IndexType > &rSingleVectorRepresentation)
Definition: sparse_graph.h:329
void AddEntries(SparseGraph &rOtherGraph)
Definition: sparse_graph.h:196
const DataCommunicator * pGetComm() const
Definition: sparse_graph.h:124
GraphType::const_iterator const_row_iterator
Definition: sparse_graph.h:72
IndexType ExportCSRArrays(IndexType *&pRowIndicesData, IndexType &rRowDataSize, IndexType *&pColIndicesData, IndexType &rColDataSize) const
Definition: sparse_graph.h:249
bool Has(const IndexType I, const IndexType J) const
Definition: sparse_graph.h:141
std::string Info() const
Turn back information as a string.
Definition: sparse_graph.h:407
void AddEntry(const IndexType RowIndex, const IndexType ColIndex)
Definition: sparse_graph.h:161
const GraphType::mapped_type & operator[](const IndexType &Key) const
Definition: sparse_graph.h:151
const_iterator_adaptor end() const
Definition: sparse_graph.h:391
SparseGraph(const SparseGraph &rOther)
Copy constructor.
Definition: sparse_graph.h:104
std::map< IndexType, std::unordered_set< IndexType > > GraphType
Definition: sparse_graph.h:71
#define KRATOS_ERROR
Definition: exception.h:161
start
Definition: DEM_benchmarks.py:17
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
rhs
Definition: generate_frictional_mortar_condition.py:297
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98
def load(f)
Definition: ode_solve.py:307
int counter
Definition: script_THERMAL_CORRECT.py:218
J
Definition: sensitivityMatrix.py:58
N
Definition: sensitivityMatrix.py:29
integer i
Definition: TensorModule.f:17