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_exporter.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_EXPORTER_H_INCLUDED )
13 #define KRATOS_DISTRIBUTED_VECTOR_EXPORTER_H_INCLUDED
14 
15 // System includes
16 #include <string>
17 #include <iostream>
18 #include <functional>
19 
20 // External includes
21 
22 // Project includes
23 #include "includes/define.h"
26 
27 namespace Kratos
28 {
31 
34 
38 
42 
46 
50 
52 template<class TIndexType=std::size_t>
54 {
55 public:
58  typedef TIndexType IndexType;
59  typedef int MpiIndexType;
60 
63 
67 
69  template< class TGlobalIndicesVectorType>
71  const DataCommunicator& rComm,
72  const TGlobalIndicesVectorType& rGlobalIndices,
73  const DistributedNumbering<IndexType>& rNumbering)
74  :
75  mrComm(rComm)
76  {
77  mpNumbering = Kratos::make_unique< DistributedNumbering<IndexType> >(rNumbering);
78  std::unordered_map<int, std::vector<IndexType>> to_send_remote_local_id; //do not need to store this
79  for(unsigned int local_i=0; local_i<rGlobalIndices.size(); ++local_i)
80  {
81  IndexType global_i = rGlobalIndices[local_i];
82  MpiIndexType owner_rank = mpNumbering->OwnerRank(global_i);
83  IndexType remote_local_i = mpNumbering->RemoteLocalId(global_i, owner_rank);
84  mPositionWithinData[owner_rank].push_back(local_i);
85  to_send_remote_local_id[owner_rank].push_back(remote_local_i);
86  }
87  mToRecvLocalId[GetComm().Rank()] = std::move(to_send_remote_local_id[GetComm().Rank()]); //for the current rank do a memcopy
88 
89 
90 
91  //compute communication plan
92  std::vector<MpiIndexType> send_list;
93  for(const auto& item : to_send_remote_local_id)
94  {
95  MpiIndexType cpu_id = item.first;
96  if(cpu_id != GetComm().Rank())
97  send_list.push_back(cpu_id);
98  }
99  mVectorCommColors = MPIColoringUtilities::ComputeCommunicationScheduling(send_list, rComm);
100 
101  //communicate the remote_local_id so that the other node knows what to send
102  for(auto color : mVectorCommColors)
103  {
104  if(color >= 0) //-1 would imply no communication
105  {
106  //NOTE: this can be made nonblocking
107  mToRecvLocalId[color] = rComm.SendRecv(to_send_remote_local_id[color], color, color); //TODO, we know all the sizes, we shall use that!
108  }
109  }
110 
111  //ensure that entries exist for each color involved
112  for(auto color : mVectorCommColors)
113  {
114  if(color >= 0)
115  {
116  //create if they do not exist
117  mToRecvLocalId[color];
118  mPositionWithinData[color];
119  }
120  }
121  mToRecvLocalId[GetComm().Rank()]; //create if it does not exist
122  mPositionWithinData[GetComm().Rank()]; //create if it does not exist
123  }
124 
127  :
128  mrComm(rOther.mrComm),
129  mpNumbering(Kratos::make_unique<DistributedNumbering<TIndexType>>(*rOther.mpNumbering)),
130  mToRecvLocalId(rOther.mToRecvLocalId),
131  mPositionWithinData(rOther.mPositionWithinData),
132  mVectorCommColors(rOther.mVectorCommColors)
133  {
134  }
135 
137  template< class TDistributedVectorType, class TLocalVectorType, class TApplyFunctorType=std::plus<typename TLocalVectorType::value_type>>
138  void Apply(TDistributedVectorType& rDestinationVector,
139  const TLocalVectorType& rLocalDataVector
140  ) const
141  {
142  std::vector<typename TLocalVectorType::value_type> send_buffer;
143  std::vector<typename TLocalVectorType::value_type> recv_buffer;
144  for(auto color : mVectorCommColors)
145  {
146  if(color >= 0) //-1 would imply no communication
147  {
148  const auto& local_ids = mToRecvLocalId.find(color)->second;
149  const auto& position_within_data = mPositionWithinData.find(color)->second;
150  recv_buffer.resize(local_ids.size());
151  send_buffer.resize(0);
152  for(IndexType i=0; i<position_within_data.size(); ++i)
153  send_buffer.push_back(rLocalDataVector[position_within_data[i]]);
154 
155  //NOTE: this can be made nonblocking
156  GetComm().SendRecv(send_buffer, color, 0, recv_buffer, color, 0);
157 
158  //Apply the remotely received data
159  for(IndexType i=0; i<recv_buffer.size(); ++i)
160  {
161  rDestinationVector[local_ids[i]] = TApplyFunctorType()(rDestinationVector[local_ids[i]],recv_buffer[i]);
162  }
163  }
164  }
165  //treat local datas (no communication is needed)
166  const auto& local_ids = mToRecvLocalId.find(GetComm().Rank())->second;
167  const auto& position_within_data = mPositionWithinData.find(GetComm().Rank())->second;
168  for(IndexType i=0; i<position_within_data.size(); ++i)
169  rDestinationVector[local_ids[i]] = TApplyFunctorType()( rDestinationVector[local_ids[i]], rLocalDataVector[position_within_data[i]] );
170  }
171 
174 
178 
182 
186 
187  const DataCommunicator& GetComm() const{
188  return mrComm;
189  }
190 
194 
195 
199 
201  std::string Info() const
202  {
203  std::stringstream buffer;
204  buffer << "DistributedVectorExporter" ;
205  return buffer.str();
206  }
207 
209  void PrintInfo(std::ostream& rOStream) const {rOStream << "DistributedVectorExporter";}
210 
212  void PrintData(std::ostream& rOStream) const {}
213 
217 
218 
220 
221 protected:
224 
225 
229 
230 
234 
235 
239 
240 
244 
245 
249 
250 
254 
255 
257 
258 private:
261 
262 
266  const DataCommunicator& mrComm;
268  std::unordered_map<int, std::vector<IndexType>> mToRecvLocalId;
269  std::unordered_map<int, std::vector<IndexType>> mPositionWithinData;
270  std::vector<int> mVectorCommColors;
271 
275 
276 
280 
281 
285 
286 
290 
291 
295 
297  DistributedVectorExporter& operator=(DistributedVectorExporter const& rOther){}
298 
300 
301 }; // Class DistributedVectorExporter
302 
304 
307 
308 
312 
313 
315 template<class TIndexType>
316 inline std::istream& operator >> (std::istream& rIStream,
318  {
319  return rIStream;
320  }
321 
323 template<class TIndexType>
324 inline std::ostream& operator << (std::ostream& rOStream,
326 {
327  rThis.PrintInfo(rOStream);
328  rOStream << std::endl;
329  rThis.PrintData(rOStream);
330 
331  return rOStream;
332 }
334 
336 
337 } // namespace Kratos.
338 
339 #endif // KRATOS_DISTRIBUTED_VECTOR_EXPORTER_H_INCLUDED defined
340 
341 
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 DistributedVectorExporter which implements FEM assemble capabilities.
Definition: distributed_vector_exporter.h:54
~DistributedVectorExporter()
Destructor.
Definition: distributed_vector_exporter.h:173
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: distributed_vector_exporter.h:212
const DataCommunicator & GetComm() const
Definition: distributed_vector_exporter.h:187
KRATOS_CLASS_POINTER_DEFINITION(DistributedVectorExporter)
Pointer definition of DistributedVectorExporter.
void Apply(TDistributedVectorType &rDestinationVector, const TLocalVectorType &rLocalDataVector) const
this function "returns" (writes onto rDestinationVector) a local array containing the values identifi...
Definition: distributed_vector_exporter.h:138
TIndexType IndexType
Definition: distributed_vector_exporter.h:58
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: distributed_vector_exporter.h:209
DistributedVectorExporter(DistributedVectorExporter const &rOther)
Copy constructor.
Definition: distributed_vector_exporter.h:126
DistributedVectorExporter(const DataCommunicator &rComm, const TGlobalIndicesVectorType &rGlobalIndices, const DistributedNumbering< IndexType > &rNumbering)
Default constructor.
Definition: distributed_vector_exporter.h:70
std::string Info() const
Turn back information as a string.
Definition: distributed_vector_exporter.h:201
int MpiIndexType
Definition: distributed_vector_exporter.h:59
static std::vector< int > ComputeCommunicationScheduling(const std::vector< int > &rLocalDestinationIds, const DataCommunicator &rComm)
Definition: communication_coloring_utilities.cpp:56
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