KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
distributed_vector_importer.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Riccardo Rossi
11 //
12 #if !defined(KRATOS_DISTRIBUTED_VECTOR_IMPORTER_H_INCLUDED )
13 #define KRATOS_DISTRIBUTED_VECTOR_IMPORTER_H_INCLUDED
14 
15 
16 // System includes
17 #include <string>
18 #include <iostream>
19 
20 
21 // External includes
22 
23 
24 // Project includes
25 #include "includes/define.h"
29 
30 namespace Kratos
31 {
34 
37 
41 
45 
49 
53 
55 template<class TDataType=double, class TIndexType=std::size_t>
57 {
58 public:
61  typedef TIndexType IndexType;
62  typedef int MpiIndexType;
63 
66 
70 
72  template< class TGlobalIndicesVectorType>
74  const DataCommunicator& rComm,
75  const TGlobalIndicesVectorType& rGlobalIndices,
76  const DistributedNumbering<IndexType>& rNumbering)
77  :
78  mrComm(rComm)
79  {
80  mpNumbering = Kratos::make_unique< DistributedNumbering<IndexType> >(rNumbering);
81  mImportedDataSize = rGlobalIndices.size();
82  std::unordered_map<int, std::vector<IndexType>> to_recv_by_color; //do not need to store this
83 
84  for(unsigned int local_i=0; local_i<rGlobalIndices.size(); ++local_i)
85  {
86  IndexType global_i = rGlobalIndices[local_i];
87  MpiIndexType owner_rank = mpNumbering->OwnerRank(global_i);
88  IndexType remote_local_i = mpNumbering->RemoteLocalId(global_i, owner_rank);
89 
90  mLocalIByColor[owner_rank].push_back(local_i);
91  to_recv_by_color[owner_rank].push_back(remote_local_i);
92  }
93 
94  mIdOfLocallyOwnedTerms = mLocalIByColor[GetComm().Rank()];
95  mLocallyOwnedIds = to_recv_by_color[GetComm().Rank()];
96 
97  //compute communication plan
98  std::vector<MpiIndexType> send_list;
99  for(const auto& item : to_recv_by_color)
100  {
101  MpiIndexType cpu_id = item.first;
102  if(cpu_id != GetComm().Rank())
103  send_list.push_back(cpu_id);
104  }
105  mVectorCommColors = MPIColoringUtilities::ComputeCommunicationScheduling(send_list, rComm);
106 
107  //ensure that we have lists for all colors;
108  for(auto color : mVectorCommColors)
109  {
110  if(color >= 0) //-1 would imply no communication
111  {
112  mLocalIByColor[color]; //note that here we touch the entry and we create it if not existing
113  to_recv_by_color[color];
114  }
115  }
116 
117  //communicate the remote_local_id so that the other node knows what to send
118  for(auto color : mVectorCommColors)
119  {
120  if(color >= 0) //-1 would imply no communication
121  {
122  //NOTE: this can be made nonblocking
123  mToSendByColor[color] = rComm.SendRecv(to_recv_by_color[color], color, color); //TODO, we know all the sizes, we shall use that!
124  }
125  }
126  }
127 
130  :
131  mrComm(rOther.mrComm),
132  mpNumbering(make_unique<DistributedNumbering<IndexType>>(*rOther.mpNumbering)),
133  mLocalIByColor(rOther.mLocalIByColor),
134  mLocallyOwnedIds(rOther.mLocallyOwnedIds),
135  mIdOfLocallyOwnedTerms(rOther.mIdOfLocallyOwnedTerms),
136  mVectorCommColors(rOther.mVectorCommColors)
137  {}
138 
141  {
142  DenseVector<TDataType> ImportedData(mImportedDataSize);
143 
144  std::vector<TDataType> send_buffer;
145  std::vector<TDataType> recv_buffer;
146  for(auto color : mVectorCommColors)
147  {
148  if(color >= 0) //-1 would imply no communication
149  {
150  const auto& local_ids = mLocalIByColor.find(color)->second;
151  const auto& to_send_ids = mToSendByColor.find(color)->second;
152 
153  send_buffer.resize(to_send_ids.size());
154  recv_buffer.resize(local_ids.size());
155 
156  for(IndexType i=0; i<to_send_ids.size(); ++i)
157  send_buffer[i] = data_vector(to_send_ids[i]);
158 
159  //NOTE: this can be made nonblocking
160  GetComm().SendRecv(send_buffer, color, 0, recv_buffer, color, 0); //TODO, we know all the sizes, we shall use that!
161 
162  //write the recv_data onto the output
163  for(IndexType i=0; i<recv_buffer.size(); ++i)
164  ImportedData(local_ids[i]) = recv_buffer[i];
165  }
166  }
167  //treat local data
168  for(IndexType i=0; i<mLocallyOwnedIds.size(); ++i )
169  ImportedData(mIdOfLocallyOwnedTerms[i]) = data_vector( mLocallyOwnedIds[i] );
170 
171  return ImportedData;
172 
173  }
174 
177 
181  const DataCommunicator& GetComm() const{
182  return mrComm;
183  }
184 
185 
189 
193 
194 
198 
199 
203 
205  std::string Info() const
206  {
207  std::stringstream buffer;
208  buffer << "DistributedVectorImporter" ;
209  return buffer.str();
210  }
211 
213  void PrintInfo(std::ostream& rOStream) const {rOStream << "DistributedVectorImporter";}
214 
216  void PrintData(std::ostream& rOStream) const {}
217 
221 
222 
224 
225 protected:
228 
229 
233 
234 
238 
239 
243 
244 
248 
249 
253 
254 
258 
259 
261 
262 private:
265 
266 
270  const DataCommunicator& mrComm;
272 
273 
274  IndexType mImportedDataSize;
275  std::unordered_map<int, std::vector<IndexType>> mToSendByColor;
276  std::unordered_map<MpiIndexType, std::vector<IndexType>> mLocalIByColor;
277  std::vector<IndexType> mLocallyOwnedIds;
278  std::vector<IndexType> mIdOfLocallyOwnedTerms;
279  std::vector<int> mVectorCommColors;
280 
284 
285 
289 
290 
294 
295 
299 
300 
304 
306  DistributedVectorImporter& operator=(DistributedVectorImporter const& rOther){}
307 
308 
309 
311 
312 }; // Class DistributedVectorImporter
313 
315 
318 
319 
323 
324 
326 template<class TDataType, class TIndexType>
327 inline std::istream& operator >> (std::istream& rIStream,
329  {
330  return rIStream;
331  }
332 
334 template<class TDataType, class TIndexType>
335 inline std::ostream& operator << (std::ostream& rOStream,
337 {
338  rThis.PrintInfo(rOStream);
339  rOStream << std::endl;
340  rThis.PrintData(rOStream);
341 
342  return rOStream;
343 }
345 
347 
348 } // namespace Kratos.
349 
350 #endif // KRATOS_DISTRIBUTED_VECTOR_IMPORTER_H_INCLUDED defined
351 
352 
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
TObject SendRecv(const TObject &rSendObject, const int SendDestination, const int SendTag, const int RecvSource, const int RecvTag) const
Exchange data with other ranks.
Definition: data_communicator.h:492
virtual int Rank() const
Get the parallel rank for this DataCommunicator.
Definition: data_communicator.h:587
IndexType OwnerRank(const IndexType RowIndex) const
Definition: distributed_numbering.h:200
IndexType RemoteLocalId(const IndexType rGlobalId, const IndexType rOwnerRank) const
Definition: distributed_numbering.h:190
Provides a DistributedSystemVector which implements FEM assemble capabilities.
Definition: distributed_system_vector.h:61
Provides a DistributedVectorImporter which implements FEM assemble capabilities.
Definition: distributed_vector_importer.h:57
const DataCommunicator & GetComm() const
Definition: distributed_vector_importer.h:181
DenseVector< TDataType > ImportData(const DistributedSystemVector< TDataType, TIndexType > &data_vector) const
this function returns a local array containing the values identified by the rGlobalIndices list passe...
Definition: distributed_vector_importer.h:140
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: distributed_vector_importer.h:216
~DistributedVectorImporter()
Destructor.
Definition: distributed_vector_importer.h:176
KRATOS_CLASS_POINTER_DEFINITION(DistributedVectorImporter)
Pointer definition of DistributedVectorImporter.
int MpiIndexType
Definition: distributed_vector_importer.h:62
DistributedVectorImporter(const DataCommunicator &rComm, const TGlobalIndicesVectorType &rGlobalIndices, const DistributedNumbering< IndexType > &rNumbering)
Default constructor.
Definition: distributed_vector_importer.h:73
DistributedVectorImporter(DistributedVectorImporter const &rOther)
Copy constructor.
Definition: distributed_vector_importer.h:129
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: distributed_vector_importer.h:213
TIndexType IndexType
Definition: distributed_vector_importer.h:61
std::string Info() const
Turn back information as a string.
Definition: distributed_vector_importer.h:205
Definition: amatrix_interface.h:41
static std::vector< int > ComputeCommunicationScheduling(const std::vector< int > &rLocalDestinationIds, const DataCommunicator &rComm)
Definition: communication_coloring_utilities.cpp:56
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
unique_ptr< C > make_unique(Args &&...args)
Definition: smart_pointers.h:45
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
color
Definition: all_t_win_vs_m_fixed_error.py:230
integer i
Definition: TensorModule.f:17