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.
global_pointer.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: Carlos A. Roig
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <iostream>
17 
18 // External includes
19 
20 // Project includes
21 #include "includes/define.h"
22 #include "includes/serializer.h"
23 #include "includes/key_hash.h"
24 
25 namespace Kratos
26 {
29 
32 
42 template<class TDataType>
44 {
45 public:
48 
50  using element_type = TDataType;
51 
55 
61  mDataPointer = nullptr;
62 #ifdef KRATOS_USING_MPI
63  this->mRank = 0;
64 #endif
65  };
66 
71  GlobalPointer(TDataType Data) = delete;
72 
78  TDataType* DataPointer,
79  int Rank = 0
80  ) : mDataPointer(DataPointer)
81 #ifdef KRATOS_USING_MPI
82  , mRank(Rank)
83 #endif
84  {
85 #ifndef KRATOS_USING_MPI
86  KRATOS_DEBUG_ERROR_IF(Rank != 0) << "Trying to construct a global pointer with rank different from zero when kratos is not in MPI mode " << std::endl;
87 #endif
88  }
89 
96  int Rank = 0
97  ) : mDataPointer(DataPointer.get())
98 #ifdef KRATOS_USING_MPI
99  , mRank(Rank)
100 #endif
101  {
102 #ifndef KRATOS_USING_MPI
103  KRATOS_DEBUG_ERROR_IF(Rank != 0) << "Trying to construct a global pointer with rank different from zero when kratos is not in MPI mode " << std::endl;
104 #endif
105  }
106 
112  Kratos::intrusive_ptr<TDataType>& DataPointer,
113  int Rank = 0
114  ) : mDataPointer(DataPointer.get())
115  #ifdef KRATOS_USING_MPI
116  , mRank(Rank)
117  #endif
118  {
119 #ifndef KRATOS_USING_MPI
120  KRATOS_DEBUG_ERROR_IF(Rank != 0) << "Trying to construct a global pointer with rank different from zero when kratos is not in MPI mode " << std::endl;
121 #endif
122  }
123 
129  Kratos::weak_ptr<TDataType> DataPointer,
130  int Rank = 0
131  ) : mDataPointer(DataPointer.lock().get())
132 #ifdef KRATOS_USING_MPI
133  , mRank(Rank)
134 #endif
135  {
136  #ifndef KRATOS_USING_MPI
137  KRATOS_DEBUG_ERROR_IF(Rank != 0) << "Trying to construct a global pointer with rank different from zero when kratos is not in MPI mode " << std::endl;
138  #endif
139  }
140 
145  GlobalPointer(std::unique_ptr<TDataType> DataPointer, int Rank = 0) = delete;
146 
152  : mDataPointer(rOther.mDataPointer)
153 #ifdef KRATOS_USING_MPI
154  , mRank(rOther.mRank)
155 #endif
156  {
157  }
158 
163  GlobalPointer(const GlobalPointer&& rOther)
164  : mDataPointer(std::move(rOther.mDataPointer))
165 #ifdef KRATOS_USING_MPI
166  , mRank(std::move(rOther.mRank))
167 #endif
168  {
169  }
170 
172  ~GlobalPointer() = default;
173 
177 
184  mDataPointer = rOther.mDataPointer;
185 #ifdef KRATOS_USING_MPI
186  mRank = rOther.mRank;
187 #endif
188  return *this;
189  }
190 
195  TDataType& operator*() {
196  return *mDataPointer;
197  }
198 
203  TDataType const& operator*() const {
204  return *mDataPointer;
205  }
206 
211  TDataType* operator->() {
212  return mDataPointer;
213  }
214 
219  TDataType const* operator->() const {
220  return mDataPointer;
221  }
222 
228  bool operator==(const GlobalPointer& rOther)
229  {
230 #ifdef KRATOS_USING_MPI
231  return this->get() == rOther.get() && this->GetRank() == rOther.GetRank();
232 #else
233  return this->get() == rOther.get();
234 #endif
235  }
236 
240 
245  TDataType* get() {
246  return mDataPointer;
247  }
248 
253  TDataType const* get() const {
254  return mDataPointer;
255  }
256 
262  int GetRank() const
263  {
264 #ifdef KRATOS_USING_MPI
265  return this->mRank;
266 #else
267  return 0;
268 #endif
269  }
270 
276  void SetRank(const int Rank)
277  {
278 #ifdef KRATOS_USING_MPI
279  this->mRank = Rank;
280 #endif
281  }
282 
286 
291  void save(char * buffer) const {
292  memcpy(buffer, this, sizeof(GlobalPointer));
293  }
294 
299  void load(char * buffer) {
300  memcpy(this, buffer, sizeof(GlobalPointer));
301  }
302 
306 
310 
314 
316  std::string Info() const
317  {
318  std::stringstream buffer;
319  buffer << "GlobalPointer" ;
320  return buffer.str();
321  }
322 
324  void PrintInfo(std::ostream& rOStream) const
325  {
326  rOStream << "GlobalPointer";
327  }
328 
330  void PrintData(std::ostream& rOStream) const
331  {
332  rOStream << "GlobalPointer from Rank: " << GetRank() << " contains: \n";
333  mDataPointer->PrintData(rOStream);
334  }
335 
339 
341 private:
344 
348 
349  TDataType* mDataPointer;
350 #ifdef KRATOS_USING_MPI
351  int mRank;
352 #endif
353 
357 
361 
365 
369 
373  friend class Serializer;
374 
379  void save(Serializer& rSerializer) const
380  {
381  if(rSerializer.Is(Serializer::SHALLOW_GLOBAL_POINTERS_SERIALIZATION)) {
382  rSerializer.save("D", reinterpret_cast<std::size_t>(mDataPointer));
383  } else {
384  rSerializer.save("D", mDataPointer);
385  }
386  #ifdef KRATOS_USING_MPI
387  rSerializer.save("R", mRank);
388  #endif
389  }
390 
395  void load(Serializer& rSerializer)
396  {
397  if(rSerializer.Is(Serializer::SHALLOW_GLOBAL_POINTERS_SERIALIZATION)) {
398  std::size_t tmp;
399  rSerializer.load("D", tmp);
400  mDataPointer = reinterpret_cast<TDataType*>(tmp);
401  } else {
402  rSerializer.load("D", mDataPointer);
403  }
404  #ifdef KRATOS_USING_MPI
405  rSerializer.load("R", mRank);
406  #endif
407  }
408 
410 };
411 
416 template <class TDataType>
418 {
424  std::size_t operator()(const GlobalPointer<TDataType>& pGp) const
425  {
426  std::size_t seed = 0;
427  HashCombine(seed, &(*pGp));
428 #ifdef KRATOS_USING_MPI
429  HashCombine(seed, pGp.GetRank());
430 #endif
431  return seed;
432  }
433 };
434 
439 template< class TDataType >
441 {
447  bool operator()(const GlobalPointer<TDataType>& pGp1, const GlobalPointer<TDataType>& pGp2) const
448  {
449 #ifdef KRATOS_USING_MPI
450  return ( &(*pGp1) == &(*pGp2) && pGp1.GetRank() == pGp2.GetRank() );
451 #else
452  return ( &(*pGp1) == &(*pGp2) );
453 #endif
454  }
455 };
456 
462 template< class TDataType >
464 {
470  bool operator()(const GlobalPointer<TDataType>& pGp1, const GlobalPointer<TDataType>& pGp2) const
471  {
472 #ifdef KRATOS_USING_MPI
473  return (pGp1.GetRank() == pGp2.GetRank()) ? (pGp1.get() < pGp2.get()) : (pGp1.GetRank() < pGp2.GetRank());
474 #else
475  return (pGp1.get() < pGp2.get());
476 #endif
477  }
478 };
479 
481 template< class TDataType >
482 inline std::istream& operator >> (std::istream& rIStream,
484  {return rIStream;};
485 
487 template< class TDataType >
488 inline std::ostream& operator << (std::ostream& rOStream,
489  const GlobalPointer<TDataType>& rThis)
490 {
491 
492  rOStream << reinterpret_cast<const std::size_t>(&*rThis) << " : " << rThis.GetRank();
493 
494  return rOStream;
495 }
496 
497 } // namespace Kratos
This class is a wrapper for a pointer to a data that is located in a different rank.
Definition: global_pointer.h:44
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
bool Is(Flags const &rOtherFlag) const
Definition: serializer.h:946
void save(std::string const &rTag, std::array< TDataType, TDataSize > const &rObject)
Definition: serializer.h:545
TDataType const * operator->() const
Arrow operator for const GlobalPointer.
Definition: global_pointer.h:219
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: global_pointer.h:330
std::string Info() const
Turn back information as a string.
Definition: global_pointer.h:316
GlobalPointer(const GlobalPointer &rOther)
Copy constructor for GlobalPointer.
Definition: global_pointer.h:151
int GetRank() const
Returns the rank of the global pointer.
Definition: global_pointer.h:262
TDataType const * get() const
Get a const pointer to the data.
Definition: global_pointer.h:253
GlobalPointer(Kratos::weak_ptr< TDataType > DataPointer, int Rank=0)
Constructor by Kratos::weak_ptr.
Definition: global_pointer.h:128
TDataType element_type
Definition of element type.
Definition: global_pointer.h:50
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: global_pointer.h:324
void SetRank(const int Rank)
Sets the rank of the global pointer.
Definition: global_pointer.h:276
TDataType & operator*()
Dereference operator for non-const GlobalPointer.
Definition: global_pointer.h:195
bool operator==(const GlobalPointer &rOther)
Overloads the '==' operator to compare two GlobalPointer objects of the same template type....
Definition: global_pointer.h:228
bool operator()(const GlobalPointer< TDataType > &pGp1, const GlobalPointer< TDataType > &pGp2) const
The () operator.
Definition: global_pointer.h:470
void HashCombine(HashType &Seed, const TClassType &Value)
This method creates an "unique" hash for the input value.
Definition: key_hash.h:56
std::size_t operator()(const GlobalPointer< TDataType > &pGp) const
The () operator for hashing a GlobalPointer.
Definition: global_pointer.h:424
GlobalPointer(TDataType Data)=delete
Constructor by Data.
bool operator()(const GlobalPointer< TDataType > &pGp1, const GlobalPointer< TDataType > &pGp2) const
The () operator.
Definition: global_pointer.h:447
void load(char *buffer)
Restores the GlobalPoiter with the data from the buffer.
Definition: global_pointer.h:299
GlobalPointer & operator=(const GlobalPointer &rOther)
Assignment operator for GlobalPointer.
Definition: global_pointer.h:183
GlobalPointer(Kratos::intrusive_ptr< TDataType > &DataPointer, int Rank=0)
Constructor by Kratos::intrusive_ptr.
Definition: global_pointer.h:111
GlobalPointer(Kratos::shared_ptr< TDataType > DataPointer, int Rank=0)
Constructor by Kratos::shared_ptr.
Definition: global_pointer.h:94
GlobalPointer(std::unique_ptr< TDataType > DataPointer, int Rank=0)=delete
Constructor by std::unique_ptr.
GlobalPointer()
Default constructor.
Definition: global_pointer.h:60
TDataType const & operator*() const
Dereference operator for const GlobalPointer.
Definition: global_pointer.h:203
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
TDataType * operator->()
Arrow operator for non-const GlobalPointer.
Definition: global_pointer.h:211
friend class Serializer
Friend class Serializer for handling serialization.
Definition: global_pointer.h:373
GlobalPointer(const GlobalPointer &&rOther)
Move constructor for GlobalPointer.
Definition: global_pointer.h:163
TDataType * get()
Get a non-const pointer to the data.
Definition: global_pointer.h:245
void save(char *buffer) const
Fills buffer with the GlobalPoiter data.
Definition: global_pointer.h:291
GlobalPointer(TDataType *DataPointer, int Rank=0)
Constructor by DataPointer.
Definition: global_pointer.h:77
~GlobalPointer()=default
Destructor.
int seed
Definition: GenerateWind.py:138
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::weak_ptr< T > weak_ptr
Definition: smart_pointers.h:30
std::shared_ptr< T > shared_ptr
Definition: smart_pointers.h:27
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
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98
namespace
Definition: array_1d.h:793
This is a key compare between two pointers to the object object.
Definition: global_pointer.h:464
This is a key comparer between two dof pointers checking for equal keys.
Definition: global_pointer.h:441
Template struct for hashing GlobalPointer instances.
Definition: global_pointer.h:418