SCOREC core
Parallel unstructured mesh tools
PCU.h
Go to the documentation of this file.
1 #ifndef SCOREC_PCU_H
2 #define SCOREC_PCU_H
3 
9 #include <memory>
10 #include <cstdlib>
11 #include <cstdarg> //va_list
12 #include "pcu_defines.h"
13 
14 struct pcu_msg_struct;
15 struct pcu_mpi_struct;
16 
22 namespace pcu {
26 class PCU {
27 public:
28  PCU();
29  explicit PCU(PCU_Comm comm);
30  ~PCU() noexcept;
31  PCU(PCU const &) = delete;
32  PCU(PCU &&) noexcept;
33  PCU &operator=(PCU const &) = delete;
34  PCU &operator=(PCU &&) noexcept;
38  [[nodiscard]] int Self() const noexcept;
42  [[nodiscard]] int Peers() const noexcept;
43 
44  [[nodiscard]] PCU_t GetCHandle() {PCU_t h; h.ptr=this; return h;}
45  /*recommended message passing API*/
46  void Begin() noexcept;
47  int Pack(int to_rank, const void *data, size_t size) noexcept;
48  template<typename T> int Pack(int to_rank, const T& data) noexcept {
49  return Pack(to_rank, &(data), sizeof(data));
50  }
51 
52  int Send() noexcept;
53  bool Receive() noexcept;
54  bool Listen() noexcept;
55  int Sender() noexcept;
56  bool Unpacked() noexcept;
57  int Unpack(void *data, size_t size) noexcept;
58  template<typename T> int Unpack(T& data) noexcept {
59  return Unpack(&(data), sizeof(data));
60  }
61  /*IPComMan replacement API*/
62  int Write(int to_rank, const void *data, size_t size) noexcept;
63  bool Read(int *from_rank, void **data, size_t *size) noexcept;
64 
65  /*turns deterministic ordering for the
66  above API on/off*/
67  void Order(bool on);
68 
69  /*collective operations*/
70  void Barrier();
71  template <typename T> void Add(T *p, size_t n) noexcept;
72  template <typename T> [[nodiscard]] T Add(T p) noexcept;
73  template <typename T> void Min(T *p, size_t n) noexcept;
74  template <typename T> [[nodiscard]] T Min(T p) noexcept;
75  template <typename T> void Max(T *p, size_t n) noexcept;
76  template <typename T> [[nodiscard]] T Max(T p) noexcept;
77  template <typename T> void Exscan(T *p, size_t n) noexcept;
78  template <typename T> [[nodiscard]] T Exscan(T p) noexcept;
79  template <typename T>
80  void Allgather(const T *send, T *recv, size_t n) noexcept;
81 
82  /*bitwise operations*/
83  [[nodiscard]] int Or(int c) noexcept;
84  [[nodiscard]] int And(int c) noexcept;
85 
97  std::unique_ptr<PCU> Split(int color, int key) noexcept;
98 
112  int DupComm(PCU_Comm* newcomm) const noexcept;
113 
114  /*lesser-used APIs*/
115  int Packed(int to_rank, size_t *size) noexcept;
116  int From(int *from_rank) noexcept;
117  int Received(size_t *size) noexcept;
118  void *Extract(size_t size) noexcept;
119 
120  /*
121  * Debug print function with printf-style formatting
122  * This function is excluded from SWIG parsing due to complex macro usage
123  * with variadic argument. Also, such functionalities is not needed for the Python API.
124  */
125  #ifndef SWIG
126  void DebugPrint(const char* format, ...) noexcept PCU_FORMAT_ATTRIBUTE(2, 3)
127  void DebugPrint(const char* format, va_list args) noexcept;
128  #endif // SWIG
129  /* Debug functions */
130  void DebugOpen() noexcept;
131 
132 private:
133  pcu_msg_struct *msg_;
134  pcu_mpi_struct *mpi_;
135 };
136 /*stack trace helpers using GNU/Linux*/
137 void Protect() noexcept;
138 /*Memory usage*/
139 [[nodiscard]] double GetMem() noexcept;
140 /*MPI_Wtime() equivalent*/
141 [[nodiscard]] double Time() noexcept;
142 
149 void Init(int *argc, char ***argv);
156 void Finalize();
157 
158 /*
159  * Explicit instantiations of template functions,
160  * ignored by SWIG to avoid difficulties when parsing macros involving
161  * template functions. Templates are initialized manually in the interface file.
162  */
163 #ifndef SWIG
164 #define PCU_EXPL_INST_DECL(T) \
165  extern template void PCU::Add<T>(T * p, size_t n) noexcept; \
166  extern template T PCU::Add<T>(T p) noexcept; \
167  extern template void PCU::Min<T>(T * p, size_t n) noexcept; \
168  extern template T PCU::Min<T>(T p) noexcept; \
169  extern template void PCU::Max<T>(T * p, size_t n) noexcept; \
170  extern template T PCU::Max<T>(T p) noexcept; \
171  extern template void PCU::Exscan<T>(T * p, size_t n) noexcept; \
172  extern template T PCU::Exscan<T>(T p) noexcept; \
173  extern template \
174  void PCU::Allgather<T>(const T *send, T *recv, size_t n) noexcept;
175 PCU_EXPL_INST_DECL(int)
176 PCU_EXPL_INST_DECL(size_t)
177 PCU_EXPL_INST_DECL(long)
178 PCU_EXPL_INST_DECL(double)
179 #undef PCU_EXPL_INST_DECL
180 #endif // SWIG
181 
182 } // namespace pcu
183 
184 #endif // PCUOBJ_H
185 
186 
The Parallel Contrul Unit class encapsulates parallel communication.
Definition: PCU.h:26
int Self() const noexcept
Returns the rank of the current process.
int DupComm(PCU_Comm *newcomm) const noexcept
Duplicate the underlying communicator.
std::unique_ptr< PCU > Split(int color, int key) noexcept
Split a communicator into distinct subgroups.
int Peers() const noexcept
Returns the number of ranks in the communicator.
Definition: PCU.h:15
void Finalize()
Finalize the underlying parallel library.
void Init(int *argc, char ***argv)
Initialize the underlying parallel library.