seapodym-parallel
Fish dynamics models with parameter estimations
Loading...
Searching...
No Matches
DistDataCollector.h
1#include <mpi.h>
2#include <set>
3#include <vector>
4#include <limits>
5
6#ifndef DIST_DATA_COLLECTOR
7#define DIST_DATA_COLLECTOR
8
9
15
16 private:
17
18 // MPI communicator to use for communication
19 MPI_Comm comm;
20
21 // number of chunks
22 std::size_t numChunks;
23
24 // local size of the data
25 std::size_t numSize;
26
27 // the array that collects the data of size numChunks * numSize on rootRank
28 double* collectedData;
29
30 // MPI window for remote memory access
31 MPI_Win win;
32
33 // MPI rank that holds the collected data
34 int rootRank;
35
36 public:
37
38 // initial values
39 const double BAD_VALUE = std::numeric_limits<double>::quiet_NaN();
40
48 DistDataCollector(MPI_Comm comm, int numChunks, int numSize, int rootRank = 0);
49
54
58 MPI_Win &getWin(){
59 return this->win;
60 }
61
67 void fence();
68
72 void inline startEpoch() {
73 // Start a passive target shared local access epoch for all processes in the communicator
74 MPI_Win_lock_all(MPI_MODE_NOCHECK, this->win);
75 }
76
80 void inline flush() {
81 MPI_Win_flush(this->rootRank, this->win);
82 }
83
87 void inline endEpoch() {
88 MPI_Win_unlock_all(this->win);
89 }
90
97 void put(int chunkId, const double* data);
98
106 void inline putAsync(int chunkId, const double* data) {
107 MPI_Put(data, this->numSize, MPI_DOUBLE, this->rootRank, chunkId * this->numSize, this->numSize, MPI_DOUBLE, this->win);
108 }
109
118 void accumulate(int chunkId, const double* data);
119
126 std::vector<double> get(int chunkId);
127
134 void get(int chunkId, double* buffer);
135
143 void inline getAsync(int chunkId, double* buffer) {
144 MPI_Get(buffer, this->numSize, MPI_DOUBLE, this->rootRank, chunkId * this->numSize, this->numSize, MPI_DOUBLE, this->win);
145 }
146
153 return this->collectedData;
154 }
155
160 int getNumChunks() const {
161 return this->numChunks;
162 }
163
168 int getNumSize() const {
169 return this->numSize;
170 }
171
175 void free() { // Should this be removed and just implemented in the destructor?
176 if (this->win != MPI_WIN_NULL) {
177 MPI_Win_free(&this->win);
178 }
179 //No need to free the data, MPI_Win_free will free the pointer
180 //MPI_Free_mem(this->collectedData);
181 }
182
183 // Disable copy and assignment operations
184 // to prevent accidental copying of the DistDataCollector instance
185 // This is important because the class manages an MPI window and data pointer
186 // which should not be copied or assigned.
187 DistDataCollector(const DistDataCollector&) = delete; // Disable copy constructor
188 DistDataCollector& operator=(const DistDataCollector&) = delete; // Disable assignment operator
189 DistDataCollector(DistDataCollector&& other) noexcept; // Move constructor
190 DistDataCollector& operator=(DistDataCollector&& other) noexcept; // Move assignment operator
191};
192
193#endif // DIST_DATA_COLLECTOR
DistDataCollector is a class that collects data stored on multiple MPI processes into a large array s...
Definition DistDataCollector.h:14
void fence()
Definition DistDataCollector.cpp:48
void endEpoch()
End an epoch for RMA operations.
Definition DistDataCollector.h:87
void put(int chunkId, const double *data)
Put the local data into the collected array.
Definition DistDataCollector.cpp:53
int getNumSize() const
Definition DistDataCollector.h:168
std::vector< double > get(int chunkId)
Get a slice of the remote, collected array to the local worker.
Definition DistDataCollector.cpp:69
void startEpoch()
Start an epoch for RMA operations.
Definition DistDataCollector.h:72
double * getCollectedDataPtr()
Definition DistDataCollector.h:152
void putAsync(int chunkId, const double *data)
Put the local data into the collected array (non-blocking).
Definition DistDataCollector.h:106
void free()
Free the MPI window and empty the collected data.
Definition DistDataCollector.h:175
void flush()
Ensure that the RMA operation is completed and the data are visible to the manager.
Definition DistDataCollector.h:80
int getNumChunks() const
Definition DistDataCollector.h:160
void getAsync(int chunkId, double *buffer)
Get a slice of the remote, collected array to the local worker (non-blocking).
Definition DistDataCollector.h:143
~DistDataCollector()
Destructor.
Definition DistDataCollector.cpp:37
void accumulate(int chunkId, const double *data)
Atomically accumulate (add) local data into a chunk on rootRank. Uses MPI_Accumulate with MPI_SUM und...
Definition DistDataCollector.cpp:94
DistDataCollector(MPI_Comm comm, int numChunks, int numSize, int rootRank=0)
Constructor.
Definition DistDataCollector.cpp:8
MPI_Win & getWin()
Definition DistDataCollector.h:58