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
/
testParallelFramework.cpp
186 lines (156 loc) · 5.98 KB
/
testParallelFramework.cpp
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
/*
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}
}
*/
#include "ParallelFramework.h"
#include "pDemo.h"
#include "TestFunctions.h"
#include <iostream>
#include <algorithm>
#include <windows.h>
struct Complex {
std::vector<int> vec;
double d;
int i;
};
BinaryStream& operator<< (BinaryStream& bs, const Complex& c) {
return bs << c.vec << c.d << c.i;
}
BinaryStream& operator>> (BinaryStream& bs, Complex& c) {
bs >> c.vec;
bs >> c.d;
bs >> c.i;
return bs;
}
struct Job {
Complex operator() (Complex& in) {
std::cerr << "job " << in.i << " is being executed on cpu " << Mpi::Communicator().getRank() << "\n";
Sleep(0); //forfeit cpu time
in.d = -in.d;
in.vec.resize(4);
in.vec[0] = Mpi::Communicator().getRank();
return in;
}
};
struct Bureaucracy {
int jobId;
Bureaucracy() : jobId(0) {}
Complex generateJob() {
Complex j;
std::cerr << "generating new job " << jobId << "\n";
if (jobId > 19)
throw "job id overflow";
j.i = jobId++;
j.vec.resize(4, 0);
j.d = 2+j.i*0.031;
return j;
}
// return the number of jobs that can be generated at the call time;
// supply the function with an ideal number of jobs to be generated
// return value <0 will be a signal that generation has stopped
// return value 0 will signal that no job can be generated at this time but will be
// in the future
int moreJobs(int preferredNum) {
if (jobId == 20)
return -1;
else
return std::min(20 - jobId, preferredNum);
}
void newResult(Complex res) {
std::cerr << "recieved output " << res.i << "," << res.d << "," << res.vec[0] << "\n";
}
};
int main(int argc, char** argv) {
try {
Mpi::Environment mpi(argc, argv);
Mpi::Buffer buffer(10000);
/*
// test mpi I communication
int from = (mpi.getCommunicator().getRank() + mpi.getCommunicator().getSize() - 1) % mpi.getCommunicator().getSize();
int to = (mpi.getCommunicator().getRank() + 1) % mpi.getCommunicator().getSize();
int tag = 1;
if (mpi.getCommunicator().getRank() == 0) {
int data;
Mpi::Status status;
for (int i = 1; i < mpi.getCommunicator().getSize(); ++i) {
while (!status.probe())
std::cout << "\r waiting " << i << "\r";
Mpi::receive(data, status.source(), 0);
std::cout << "\r recieved " << data << " from " << status.source() << "\n";
}
} else {
Mpi::Request dummy;
int data = mpi.getCommunicator().getRank();
Mpi::send(data, 0, 0, dummy);
std::cout << "\r sent " << data << "\n";
}
*/
// test parallel framework
size_t numProcesses = mpi.getCommunicator().getSize();
Job job;
Bureaucracy bureau;
PFramework<Complex, Complex> fw(mpi.getCommunicator());
if (fw.master()) {
while (fw.execute(job, bureau));
std::cerr << "master finished\n";
} else {
while (fw.execute(job, bureau));
std::cerr << "worker " << Mpi::Communicator().getRank() << " finished\n";
}
} catch (...) {
std::cout << "exception!\n";
}
return 0;
}