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.
pointer_communicator.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 
13 #pragma once
14 
15 // System includes
16 #include <string>
17 #include <iostream>
18 #include <type_traits>
19 
20 // External includes
21 
22 // Project includes
23 #include "includes/define.h"
31 
32 namespace Kratos
33 {
36 
39 
43 
47 
51 
55 template< class TPointerDataType >
56 class GlobalPointerCommunicator; //fully defined at the end of the file
57 
67 template< class TPointerDataType, class TFunctorType >
69 {
70 public:
72  using TSendType = std::invoke_result_t<TFunctorType,GlobalPointer<TPointerDataType>&>;
73 
82  int current_rank,
84  TFunctorType UserFunctor,
86  ):
87  mCurrentRank(current_rank), mNonLocalData(NonLocalData), mUserFunctor(UserFunctor), mpPointerComm(pPointerComm)
88  {}
89 
91  virtual ~ResultsProxy() {}
92 
99  {
100  if(rGlobalPointer.GetRank() == mCurrentRank)
101  return mUserFunctor(rGlobalPointer);
102  else {
103  auto non_local_gp = mNonLocalData.find(rGlobalPointer);
104  KRATOS_DEBUG_ERROR_IF(non_local_gp == mNonLocalData.end()) << "Missing entry in NonLocalData" << std::endl;
105  return non_local_gp->second;
106  }
107  }
108 
114  TSendType Get(const GlobalPointer<TPointerDataType>& rGlobalPointer) const
115  {
116  if(rGlobalPointer.GetRank() == mCurrentRank)
117  return mUserFunctor(rGlobalPointer);
118  else {
119  auto non_local_gp = mNonLocalData.find(rGlobalPointer);
120  KRATOS_DEBUG_ERROR_IF(non_local_gp == mNonLocalData.end()) << "Missing entry in NonLocalData" << std::endl;
121  return non_local_gp->second;
122  }
123  }
124 
130  bool Has(GlobalPointer<TPointerDataType>& rGlobalPointer) const
131  {
132  if(rGlobalPointer.GetRank() == mCurrentRank)
133  return true;
134  else
135  return mNonLocalData.find(rGlobalPointer) != mNonLocalData.end();
136  }
137 
143  bool Has(const GlobalPointer<TPointerDataType>& rGlobalPointer) const
144  {
145  if(rGlobalPointer.GetRank() == mCurrentRank)
146  return true;
147  else
148  return mNonLocalData.find(rGlobalPointer) != mNonLocalData.end();
149  }
150 
154  void Update()
155  {
156  mpPointerComm->Update(mUserFunctor, mNonLocalData);
157  }
158 
159 private:
160  const int mCurrentRank; // The current rank of the node.
161  GlobalPointersUnorderedMap< TPointerDataType, TSendType > mNonLocalData; // Map holding data not local to the node.
162  TFunctorType mUserFunctor; // Functor to be used for computation.
163  GlobalPointerCommunicator<TPointerDataType>* mpPointerComm; // Pointer to the communicator.
164 
165 };
166 
167 
176 template< class TPointerDataType >
178 {
179 public:
182 
185 
189 
196  mrDataCommunicator(rComm)
197  {
198  AddPointers(rGpList.ptr_begin(),rGpList.ptr_end());
199 
201  {
203  }
204  }
205 
213  template< class TIteratorType >
214  GlobalPointerCommunicator(const DataCommunicator& rComm, TIteratorType itBegin, TIteratorType itEnd):
215  mrDataCommunicator(rComm)
216  {
217  AddPointers(itBegin,itEnd);
218 
220  {
222  }
223  }
224 
231  template< class TFunctorType >
232  GlobalPointerCommunicator(const DataCommunicator& rComm, TFunctorType rFunctor):
233  mrDataCommunicator(rComm)
234  {
235  if(rComm.IsDistributed())
236  {
237  auto gps = rFunctor(rComm);
238  AddPointers(gps.ptr_begin(), gps.ptr_end());
240  }
241  }
242 
248  template< class TFunctorType >
249  GlobalPointerCommunicator(TFunctorType rFunctor)
251  {}
252 
255 
262  template< class TFunctorType >
263  ResultsProxy<
264  TPointerDataType,
265  TFunctorType // TODO: Unfortunately this is deprecated in c++17, so we will have to change this call in the future
266  > Apply(TFunctorType&& UserFunctor)
267  {
269 
270  const int current_rank = mrDataCommunicator.Rank();
271 
273 
275  {
276  Update(UserFunctor, non_local_data);
277  }
278 
279  return ResultsProxy<TPointerDataType, TFunctorType>(current_rank,non_local_data,UserFunctor, this );
280  }
281 
288  template< class TFunctorType >
289  void Update(
290  TFunctorType& rUserFunctor,
292  {
293  //sendrecv data
294  for(auto color : mColors)
295  {
296  if(color >= 0) //-1 would imply no communication
297  {
298  auto& gps_to_be_sent = mNonLocalPointers[color];
299 
300  auto recv_global_pointers = mrDataCommunicator.SendRecv(gps_to_be_sent, color, color );
301 
302  std::vector< typename ResultsProxy<TPointerDataType, TFunctorType >::TSendType > locally_gathered_data; //this is local but needs to be sent to the remote node
303  for(auto& gp : recv_global_pointers.GetContainer())
304  locally_gathered_data.push_back( rUserFunctor(gp) );
305 
306  auto remote_data = mrDataCommunicator.SendRecv(locally_gathered_data, color, color );
307 
308  for(unsigned int i=0; i<remote_data.size(); ++i)
309  rNonLocalData[gps_to_be_sent(i)] = remote_data[i];
310  }
311  }
312  }
313 
317 
321 
325 
331  {
332  return mrDataCommunicator;
333  }
334 
338 
342 
344  virtual std::string Info() const
345  {
346  std::stringstream buffer;
347  buffer << "GlobalPointerCommunicator" ;
348  return buffer.str();
349  }
350 
352  virtual void PrintInfo(std::ostream& rOStream) const
353  {
354  rOStream << "GlobalPointerCommunicator";
355  }
356 
358  virtual void PrintData(std::ostream& rOStream) const {}
359 
363 
365 protected:
368 
369  std::unordered_map<int, GlobalPointersVector< TPointerDataType > > mNonLocalPointers;
370 
372 
376 
380 
390  template< class TIteratorType >
391  void AddPointers( TIteratorType begin, TIteratorType end)
392  {
394  {
395  const int current_rank = mrDataCommunicator.Rank();
396  for(auto it = begin; it != end; ++it)
397  {
398  auto& gp = *it;
399  if(gp.GetRank() != current_rank)
400  {
401  mNonLocalPointers[gp.GetRank()].push_back(gp);
402  }
403  }
404 
405  //ensure that no repeated info is stored
406  for(auto& non_local : mNonLocalPointers)
407  non_local.second.Unique();
408  }
409  }
410 
417  {
418  std::vector<int> send_list;
419  send_list.reserve( mNonLocalPointers.size() );
420  for(auto& it : mNonLocalPointers)
421  send_list.push_back( it.first );
422 
423  std::sort(send_list.begin(), send_list.end());
425  }
426 
430 
434 
438 
442 
444 private:
447 
451 
452  std::vector<int> mColors;
453 
457 
461 
465 
469 
473 
475  GlobalPointerCommunicator& operator=(GlobalPointerCommunicator const& rOther) {}
476 
479 
481 
482 }; // Class GlobalPointerCommunicator
483 
485 
488 
492 
494 template< class TPointerDataType, class TSendType >
495 inline std::istream& operator >> (std::istream& rIStream,
497 {
498  return rIStream;
499 }
500 
502 template< class TPointerDataType, class TSendType >
503 inline std::ostream& operator << (std::ostream& rOStream,
505 {
506  rThis.PrintInfo(rOStream);
507  rOStream << std::endl;
508  rThis.PrintData(rOStream);
509 
510  return rOStream;
511 }
513 
515 
516 } // namespace Kratos.
517 
518 
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
virtual bool IsDistributed() const
Check whether this DataCommunicator is aware of parallelism.
Definition: data_communicator.h:606
A template class for handling communication related to global pointers.
Definition: pointer_communicator.h:178
const DataCommunicator & GetDataCommunicator() const
Returns the data communicator.
Definition: pointer_communicator.h:330
ResultsProxy< TPointerDataType, TFunctorType > Apply(TFunctorType &&UserFunctor)
Applies a user-provided function to the global pointers and return a proxy to the results.
Definition: pointer_communicator.h:266
std::unordered_map< int, GlobalPointersVector< TPointerDataType > > mNonLocalPointers
Definition: pointer_communicator.h:369
void AddPointers(TIteratorType begin, TIteratorType end)
Adds global pointers to the communicator.
Definition: pointer_communicator.h:391
GlobalPointerCommunicator(TFunctorType rFunctor)
Constructor with functor and default data communicator.
Definition: pointer_communicator.h:249
GlobalPointerCommunicator(const DataCommunicator &rComm, GlobalPointersVector< TPointerDataType > &rGpList)
Default constructor.
Definition: pointer_communicator.h:195
GlobalPointerCommunicator(const DataCommunicator &rComm, TFunctorType rFunctor)
Constructor with functor for generating global pointers.
Definition: pointer_communicator.h:232
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: pointer_communicator.h:358
void ComputeCommunicationPlan()
Computes the communication plan for the communicator.
Definition: pointer_communicator.h:416
virtual ~GlobalPointerCommunicator()
Destructor.
Definition: pointer_communicator.h:254
virtual std::string Info() const
Turn back information as a string.
Definition: pointer_communicator.h:344
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: pointer_communicator.h:352
KRATOS_CLASS_POINTER_DEFINITION(GlobalPointerCommunicator)
Pointer definition of GlobalPointerCommunicator.
void Update(TFunctorType &rUserFunctor, GlobalPointersUnorderedMap< TPointerDataType, typename ResultsProxy< TPointerDataType, TFunctorType >::TSendType > &rNonLocalData)
Updates the non-local data using a user-provided function.
Definition: pointer_communicator.h:289
GlobalPointerCommunicator(const DataCommunicator &rComm, TIteratorType itBegin, TIteratorType itEnd)
Constructor with iterator range for global pointers.
Definition: pointer_communicator.h:214
const DataCommunicator & mrDataCommunicator
Non local pointers stored as map with rank as key.
Definition: pointer_communicator.h:371
This class is a wrapper for a pointer to a data that is located in a different rank.
Definition: global_pointer.h:44
This class is a vector which stores global pointers.
Definition: global_pointers_vector.h:61
ptr_iterator ptr_begin()
Definition: global_pointers_vector.h:253
ptr_iterator ptr_end()
Definition: global_pointers_vector.h:261
static std::vector< int > ComputeCommunicationScheduling(const std::vector< int > &rLocalDestinationIds, const DataCommunicator &rComm)
Definition: communication_coloring_utilities.cpp:56
Holder for general data related to MPI (or suitable serial equivalents for non-MPI runs).
Definition: parallel_environment.h:57
A template class to proxy results, whether they are locally or remotely owned.
Definition: pointer_communicator.h:69
void Update()
Updates the NonLocalData using the user functor and communicator.
Definition: pointer_communicator.h:154
ResultsProxy(int current_rank, GlobalPointersUnorderedMap< TPointerDataType, TSendType > NonLocalData, TFunctorType UserFunctor, GlobalPointerCommunicator< TPointerDataType > *pPointerComm)
Constructor.
Definition: pointer_communicator.h:81
TSendType Get(const GlobalPointer< TPointerDataType > &rGlobalPointer) const
Overload for constant GlobalPointer.
Definition: pointer_communicator.h:114
TSendType Get(GlobalPointer< TPointerDataType > &rGlobalPointer) const
Returns the effect of "user_function(rGlobalPointer)" whether the rGlobalPointer is locally or remote...
Definition: pointer_communicator.h:98
std::invoke_result_t< TFunctorType, GlobalPointer< TPointerDataType > & > TSendType
Type alias for the result of applying the functor to a global pointer of TPointerDataType.
Definition: pointer_communicator.h:72
bool Has(GlobalPointer< TPointerDataType > &rGlobalPointer) const
Checks if the node has the GlobalPointer.
Definition: pointer_communicator.h:130
bool Has(const GlobalPointer< TPointerDataType > &rGlobalPointer) const
Overload for constant GlobalPointer.
Definition: pointer_communicator.h:143
virtual ~ResultsProxy()
Destructor.
Definition: pointer_communicator.h:91
int GetRank() const
Returns the rank of the global pointer.
Definition: global_pointer.h:262
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
end
Definition: DEM_benchmarks.py:180
DataCommunicator & GetDefaultDataCommunicator()
Definition: testing.cpp:24
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
color
Definition: all_t_win_vs_m_fixed_error.py:230
integer i
Definition: TensorModule.f:17