This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpiWrapper.h
367 lines (290 loc) · 9.78 KB
/
mpiWrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
Copyright (c) 2017 Institute Jožef Stefan, Jamova cesta 39, SI-1000, Ljubljana, Slovenija
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Please cite the following works (bibtex source below):
- DepolliAvbeljTrobec2008 for the simulator and simulation-based optimization
- DepolliTrobecFilipic2013 for the AMS-DEMO optimizer
- TrobecDepolliAvbelj2009 for the simulator
@article{DepolliAvbeljTrobec2008,
author = {Depolli, Matjaž and Avbelj, Viktor and Trobec, Roman},
title = {Computer-Simulated Alternative Modes of {U}-Wave Genesis},
journal = {Journal of Cardiovascular Electrophysiology},
volume = {19},
number = {1},
publisher = {Blackwell Publishing Inc},
issn = {1540-8167},
url = {http://dx.doi.org/10.1111/j.1540-8167.2007.00978.x},
doi = {10.1111/j.1540-8167.2007.00978.x},
pages = {84--89},
keywords = {U wave, ECG, action potential, repolarization, myocardium, computer simulation},
year = {2008}
}
@article{DepolliTrobecFilipic2013,
author = {Depolli, Matjaž and Trobec, Roman and Filipič, Bogdan},
title = {Asynchronous master-slave parallelization of differential evolution for multiobjective optimization},
journal = {Evolutionary Computation},
volume = {21},
number = {2},
pages = {261-291},
doi = {10.1162/EVCO_a_00076},
issn = {1063-6560},
url = {http://www.mitpressjournals.org/doi/abs/10.1162/EVCO_a_00076},
year = {2013}
}
@inproceedings{TrobecDepolliAvbelj2009,
title = {Simulation of {ECG} repolarization phase with improved model of cell action potentials},
author = {Trobec, Roman and Depolli, Matja{\v{z}} and Avbelj, Viktor},
booktitle = {International Joint Conference on Biomedical Engineering Systems and Technologies},
pages = {325--332},
year = {2009},
organization = {Springer}
}
*/
#ifndef MPIWRAPPER_H_INCLUDED
#define MPIWRAPPER_H_INCLUDED
/// mpi header does not like being included after iostream, so special care should be taken using
/// this header. Either include mpi.h earlier, or include this header before iostream
#include <mpi.h>
#include <exception>
#include <vector>
#include "Array.h"
#include <cassert>
class BinaryStream {
public:
std::vector<char> stream;
mutable size_t outPos;
public:
BinaryStream() : outPos(0) {}
void* voidStream() {
return &(stream[0]);
}
void* voidStream() const {
return const_cast<void*>((void*)&(stream[0]));
}
size_t size() const {
return stream.size();
}
void resize(size_t s) {
stream.resize(s);
}
void clear() {
stream.clear();
outPos = 0;
}
};
template<class Pod>
BinaryStream& operator<< (BinaryStream& bs, const Pod& pod) {
size_t position = bs.stream.size();
bs.stream.resize(bs.stream.size() + sizeof(Pod));
*((Pod*)&bs.stream[position]) = pod;
return bs;
}
template<class Pod>
BinaryStream& operator<< (BinaryStream& bs, const std::vector<Pod>& vec) {
bs << vec.size();
size_t position = bs.stream.size();
bs.stream.resize(bs.stream.size() + sizeof(Pod) * vec.size());
std::copy(vec.begin(), vec.end(), (Pod*)&bs.stream[position]);
return bs;
}
template<class Pod>
BinaryStream& operator>> (BinaryStream& bs, Pod& pod) {
assert(bs.outPos+sizeof(Pod) <= bs.stream.size());
pod = *reinterpret_cast<const Pod*>(&bs.stream[bs.outPos]);
bs.outPos += sizeof(Pod);
return bs;
}
template<class Pod>
BinaryStream& operator>> (BinaryStream& bs, std::vector<Pod>& vec) {
size_t s;
bs >> s;
vec.resize(s);
assert(bs.outPos+sizeof(Pod)*vec.size() <= bs.stream.size());
std::copy((Pod*)&bs.stream[bs.outPos], ((Pod*)&bs.stream[bs.outPos])
+ vec.size(), vec.begin());
bs.outPos += sizeof(Pod)*vec.size();
return bs;
}
namespace Mpi {
class Communicator {
MPI_Comm comm;
public:
Communicator(MPI_Comm c = MPI_COMM_WORLD) : comm(c) {}
const Communicator& operator= (MPI_Comm c) {
comm = c;
return *this;
}
int getSize() const {
int size;
MPI_Comm_size(comm, &size);
return size;
}
int getRank() const {
int rank;
MPI_Comm_rank(comm, &rank);
return rank;
}
operator MPI_Comm() const {
return comm;
}
// for debugging only
int toInt() const {
return (intptr_t)comm;
}
};
struct Status {
MPI_Status status;
bool messageWaiting;
int source() const {return status.MPI_SOURCE;}
int tag() const {return status.MPI_TAG;}
int error() const {return status.MPI_ERROR;}
int count() const {
int c;
MPI_Get_count(const_cast<MPI_Status*>(&status), MPI_CHAR, &c);
return c;
}
Status() : messageWaiting(false) {
}
bool probeSpecific(int sourcep, int tagp, const Communicator& comm = MPI_COMM_WORLD) {
int flag;
MPI_Iprobe(sourcep, tagp, comm, &flag, &status);
messageWaiting = (flag != 0);
return messageWaiting;
}
bool probe(const Communicator& comm = MPI_COMM_WORLD) {
return probeSpecific(MPI_ANY_SOURCE, MPI_ANY_TAG, comm);
}
/// blocking probe (wait for message)
void waitSpecific(int sourcep, int tagp, const Communicator& comm = MPI_COMM_WORLD) {
MPI_Probe(sourcep, tagp, comm, &status);
}
void wait(const Communicator& comm = MPI_COMM_WORLD) {
waitSpecific(MPI_ANY_SOURCE, MPI_ANY_TAG, comm);
}
};
struct Request {
MPI_Request request;
void wait() {
Status status;
MPI_Wait(&request, &status.status);
}
};
template<class T>
struct Streamify {
T* data;
Streamify(const T& t) : data(const_cast<T*>(&t)) {}
void* ptr() {return (void*)(data);}
size_t size() {return sizeof(T);}
void getSize(int source, int tag, const Communicator& comm) {}
void setSize(size_t ssize) {assert(ssize == sizeof(T));}
};
template<class T>
struct Streamify<std::vector<T> > {
std::vector<T>& vec;
Streamify(const std::vector<T>& t) : vec(const_cast<std::vector<T>&>(t)) {}
void* ptr() {return (void*)(&vec[0]);}
size_t size() const {return sizeof(T)*vec.size();}
void getSize(int source, int tag, const Communicator& comm) {
Status status;
status.probeSpecific(source, tag, comm);
vec.resize(status.count() / sizeof(T));
}
void setSize(size_t ssize) {vec.resize(ssize / sizeof(T));}
};
template<>
struct Streamify<BinaryStream> {
BinaryStream& data;
Streamify(const BinaryStream& t) : data(const_cast<BinaryStream&>(t)) {}
void* ptr() {return data.voidStream();}
size_t size() const {return data.size();}
void getSize(int source, int tag, const Communicator& comm) {
Status status;
status.probeSpecific(source, tag, comm);
data.resize(status.count());
}
void setSize(size_t ssize) {data.resize(ssize);}
};
template<class T, size_t N>
struct Streamify<Array<T, N> > {
Array<T, N>& data;
Streamify(Array<T, N>& t) : data(t) {}
void* ptr() {return (void*)(&data[0]);}
size_t size() const {return N;}
void getSize(int source, int tag, const Communicator& comm) {
Status status;
status.probeSpecific(source, tag, comm);
assert(status.count() / sizeof(T) == N);
}
void setSize(size_t ssize) {assert(ssize == N);}
};
template<class T>
void send(const T& t, int dest, int tag, const Communicator& comm = MPI_COMM_WORLD) {
Streamify<T> s(t);
MPI_Send(s.ptr(), s.size(), MPI_CHAR, dest, tag, comm);
}
template<class T>
void send(const T& t, int dest, int tag, Request& req, const Communicator& comm = MPI_COMM_WORLD) {
Streamify<T> s(t);
MPI_Ibsend(s.ptr(), s.size(), MPI_CHAR, dest, tag, comm, &req.request);
}
template<class T>
void receive(T& t, int source, int tag, const Communicator& comm = MPI_COMM_WORLD) {
MPI_Status status;
Streamify<T> s(t);
s.getSize(source, tag, comm);
MPI_Recv(s.ptr(), s.size(), MPI_CHAR, source, tag, comm, &status);
}
template<class T>
void receive(T& t, Status& status, const Communicator& comm = MPI_COMM_WORLD) {
Streamify<T> s(t);
s.setSize(status.count());
MPI_Recv(s.ptr(), s.size(), MPI_CHAR, status.source(), status.tag(), comm, &status.status);
}
class Environment {
Communicator comm;
public:
Environment(int &argc, char**& argv) {
if (MPI_Init(&argc, &argv) != MPI_SUCCESS) throw std::exception();
}
~Environment() {
MPI_Finalize();
}
const Communicator& getCommunicator() const {
return comm;
}
operator const Communicator& () const {
return comm;
}
};
// class that wraps process buffer allocation and deallocation
class Buffer {
char* buf;
public:
Buffer(size_t size) {
size += MPI_BSEND_OVERHEAD;
buf = new char[size];
MPI_Buffer_attach((void*)buf, size);
}
~Buffer() {
void* dummyAddr;
int dummySize;
MPI_Buffer_detach(&dummyAddr, &dummySize);
delete[] buf;
}
};
};
#endif // MPIWRAPPER_H_INCLUDED