-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
386 lines (328 loc) · 15.2 KB
/
Copy pathmain.cpp
File metadata and controls
386 lines (328 loc) · 15.2 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <fstream>
#include <iostream>
#include <vector>
#include "DataParser.h"
#include "Population.h"
#include "ResourceMPI.h"
#include "TimetableMPI.h"
#include "mpi.h"
#include "sprng_cpp.h"
using namespace std;
#define CHILD_NUM 1
#define CHILD_OPT 2
#define CHILD_DATA 3
#define BIRTH 1000
#define MAX_FIT 100
#define GEN_TYPE 0
typedef vector<TimetableMPI> TimetablesMPI;
void help(const string& name);
int
main(int argc, char* argv[])
{
int generationSwapped = false;
int numProcs, myID, master;
int streamID, numStreams, seed;
int numTuples, numGenes, descSize, numPeriods;
int initialPopulation = 1000;
int numChild, numIndividual;
int finished;
int populationPerProcess = BIRTH;
int n = 0;
int rc = 0;
MPI_Comm world;
MPI_Status status;
DataResources resources;
DataTuples tuples;
DataTuplesMPI tuplesMPI;
Population oldGeneration, newGeneration;
string inputFile;
Timetable* result = nullptr;
Sprng* stream;
// There should be at least 3 arguments
if (argc != 3) {
help(argv[0]);
return -1;
}
///////////////////////////////////////////////////////////////////////////
// MPI Initialization
///////////////////////////////////////////////////////////////////////////
MPI_Init(&argc, &argv);
world = MPI_COMM_WORLD;
MPI_Comm_size(world, &numProcs);
MPI_Comm_rank(world, &myID);
master = 0;
///////////////////////////////////////////////////////////////////////////
// Data Parsing
///////////////////////////////////////////////////////////////////////////
if (myID == master) {
inputFile = argv[1];
DataParser parser(inputFile);
if (parser.getResources(resources, tuples)) {
cout << "Error getting resources from file " << inputFile << "!"
<< endl;
rc = -1;
}
tuplesMPI.pack(tuples);
numTuples = tuplesMPI.size();
sscanf(argv[2], "%d", &numPeriods);
}
MPI_Bcast(&rc, 1, MPI_INT, master, world);
if (rc) {
MPI_Finalize();
return rc;
}
MPI_Bcast(&numPeriods, 1, MPI_INT, master, world);
///////////////////////////////////////////////////////////////////////////
// sprng Initialization
///////////////////////////////////////////////////////////////////////////
streamID = myID;
numStreams = numProcs;
seed = make_sprng_seed();
stream = SelectType(GEN_TYPE);
stream->init_sprng(streamID, numStreams, seed, SPRNG_DEFAULT);
///////////////////////////////////////////////////////////////////////////
// Broadcast ID representation of objects
///////////////////////////////////////////////////////////////////////////
// Send number of tuples
MPI_Bcast(&numTuples, 1, MPI_INT, master, world);
if (myID != master)
tuplesMPI.resize(numTuples);
// Send tuples
MPI_Bcast(tuplesMPI.data(), tuplesMPI.getTotalIntElements(), MPI_INT,
master, world);
///////////////////////////////////////////////////////////////////////////
// Population configuration
///////////////////////////////////////////////////////////////////////////
Population::setDataTuples(&tuplesMPI);
Population::configure(MAX_FIT, numPeriods, stream);
///////////////////////////////////////////////////////////////////////////
// Start with random population
///////////////////////////////////////////////////////////////////////////
// Initialize population and send it to all workers
if (myID == master) {
oldGeneration.initRandom(initialPopulation);
}
///////////////////////////////////////////////////////////////////////////
// Broadcast population to all processes
///////////////////////////////////////////////////////////////////////////
// Send initial population
for (int i = 0; i < initialPopulation; ++i) {
TimetableMPI individual;
if (myID == master) {
individual.pack(oldGeneration.getPopulation()[i]);
descSize = individual.getDescriptor().size();
numGenes = individual.getGenes().size();
}
// Send size of descriptor and genes
MPI_Bcast(&descSize, 1, MPI_INT, master, world);
MPI_Bcast(&numGenes, 1, MPI_INT, master, world);
if (myID != master)
individual.setSize(numGenes, descSize);
// Send packed individual
MPI_Bcast(individual.getDescriptor().data(), descSize, MPI_INT, master,
world);
MPI_Bcast(individual.getGenes().data(), numGenes, MPI_INT, master,
world);
MPI_Bcast(individual.getFitness(), 1, MPI_INT, master, world);
if (myID != master) {
Timetable* unpacked = new Timetable(Population::periodsNumber);
individual.unpack(unpacked);
oldGeneration.addIndividual(unpacked);
}
}
// Update natural selection factor
if (myID == master)
Population::naturalSelection = oldGeneration.getPopulationFitness();
MPI_Bcast(&Population::naturalSelection, 1, MPI_INT, master, world);
///////////////////////////////////////////////////////////////////////////
// Main loop - server side
///////////////////////////////////////////////////////////////////////////
// Master is responsible for gathering and updating generations
if (myID == master) {
do {
newGeneration.clear();
///////////////////////////////////////////////////////////////////
// Gather children from each process
///////////////////////////////////////////////////////////////////
for (int i = 0; i < numProcs - 1; ++i) {
MPI_Recv(&numChild, 1, MPI_INT, MPI_ANY_SOURCE, CHILD_NUM,
world, &status);
// Get all children from the node
for (int i = 0; i < numChild; ++i) {
TimetableMPI individual;
MPI_Recv(&descSize, 1, MPI_INT, status.MPI_SOURCE,
CHILD_OPT, world, &status);
MPI_Recv(&numGenes, 1, MPI_INT, status.MPI_SOURCE,
CHILD_OPT, world, &status);
individual.setSize(numGenes, descSize);
// Send packed individual
MPI_Recv(individual.getDescriptor().data(), descSize,
MPI_INT, status.MPI_SOURCE, CHILD_OPT, world,
&status);
MPI_Recv(individual.getGenes().data(), numGenes, MPI_INT,
status.MPI_SOURCE, CHILD_OPT, world, &status);
MPI_Recv(individual.getFitness(), 1, MPI_INT,
status.MPI_SOURCE, CHILD_OPT, world, &status);
Timetable* unpacked =
new Timetable(Population::periodsNumber);
individual.unpack(unpacked);
newGeneration.addIndividual(unpacked);
if (unpacked->getFitness() == Population::maxFitness) {
cout << "Proc " << myID << ": "
<< "Perfect! " << unpacked->getFitness() << endl;
result = unpacked;
}
}
}
///////////////////////////////////////////////////////////////////
// Completion check
///////////////////////////////////////////////////////////////////
// If result found, inform all processes and exit
finished = result != nullptr;
MPI_Bcast(&finished, 1, MPI_INT, master, world);
if (finished)
break;
cout << "Generacja " << n
<< " fitness: " << newGeneration.getPopulationFitness()
<< endl;
///////////////////////////////////////////////////////////////////
// Generation investigation
///////////////////////////////////////////////////////////////////
if (newGeneration.hasExtincted()) {
cout << "Populacja wymarla - zmniejszam wsp. selekcji" << endl;
Population::naturalSelection--;
} else if (newGeneration.getPopulationFitness() >=
oldGeneration.getPopulationFitness()) {
cout << "Podmieniam generacje i zwiekszam wsp. selekcji"
<< endl;
Population::naturalSelection++;
oldGeneration = newGeneration;
++n;
generationSwapped = true;
} else {
cout
<< "Nowa populacja gorzej przystosowana, zostaje przy starej"
<< endl;
}
cout << endl;
///////////////////////////////////////////////////////////////////
// Broadcast generation
///////////////////////////////////////////////////////////////////
// If new generation is fitter or the same, broadcast it
MPI_Bcast(&generationSwapped, 1, MPI_INT, master, world);
if (generationSwapped) {
numIndividual = newGeneration.getPopulation().size();
MPI_Bcast(&numIndividual, 1, MPI_INT, master, world);
for (int i = 0; i < numIndividual; ++i) {
TimetableMPI individual;
individual.pack(oldGeneration.getPopulation()[i]);
descSize = individual.getDescriptor().size();
numGenes = individual.getGenes().size();
// Send size of descriptor and genes
MPI_Bcast(&descSize, 1, MPI_INT, master, world);
MPI_Bcast(&numGenes, 1, MPI_INT, master, world);
// Send packed individual
MPI_Bcast(individual.getDescriptor().data(), descSize,
MPI_INT, master, world);
MPI_Bcast(individual.getGenes().data(), numGenes, MPI_INT,
master, world);
MPI_Bcast(individual.getFitness(), 1, MPI_INT, master,
world);
}
}
generationSwapped = false;
// Broadcast natural selection factor
MPI_Bcast(&Population::naturalSelection, 1, MPI_INT, master, world);
} while (1);
} else {
///////////////////////////////////////////////////////////////////////
// Main loop - worker side
///////////////////////////////////////////////////////////////////////
do {
newGeneration.clear();
///////////////////////////////////////////////////////////////////
// Generate children
///////////////////////////////////////////////////////////////////
// Mate proper children in process
for (int i = 0; i < populationPerProcess; ++i) {
Timetable *parent1, *parent2;
oldGeneration.getParents(parent1, parent2);
Timetable* child = new Timetable(Population::periodsNumber);
bool alive = Population::mate(parent1, parent2, *child);
if (alive) {
newGeneration.addIndividual(child);
}
}
///////////////////////////////////////////////////////////////////
// Send all alive children
///////////////////////////////////////////////////////////////////
numChild = newGeneration.getPopulation().size();
MPI_Send(&numChild, 1, MPI_INT, master, CHILD_NUM, world);
// Send all children from the node
for (int i = 0; i < numChild; ++i) {
TimetableMPI individual;
individual.pack(newGeneration.getPopulation()[i]);
descSize = individual.getDescriptor().size();
numGenes = individual.getGenes().size();
MPI_Send(&descSize, 1, MPI_INT, master, CHILD_OPT, world);
MPI_Send(&numGenes, 1, MPI_INT, master, CHILD_OPT, world);
// Send packed individual
MPI_Send(individual.getDescriptor().data(), descSize, MPI_INT,
master, CHILD_OPT, world);
MPI_Send(individual.getGenes().data(), numGenes, MPI_INT,
master, CHILD_OPT, world);
MPI_Send(individual.getFitness(), 1, MPI_INT, master, CHILD_OPT,
world);
}
///////////////////////////////////////////////////////////////////
// Completion check
///////////////////////////////////////////////////////////////////
MPI_Bcast(&finished, 1, MPI_INT, master, world);
if (finished)
break;
///////////////////////////////////////////////////////////////////
// Generation update
///////////////////////////////////////////////////////////////////
MPI_Bcast(&generationSwapped, 1, MPI_INT, master, world);
if (generationSwapped) {
oldGeneration.clear();
MPI_Bcast(&numIndividual, 1, MPI_INT, master, world);
for (int i = 0; i < numIndividual; ++i) {
TimetableMPI individual;
// Receive size of descriptor and genes
MPI_Bcast(&descSize, 1, MPI_INT, master, world);
MPI_Bcast(&numGenes, 1, MPI_INT, master, world);
individual.setSize(numGenes, descSize);
// Send packed individual
MPI_Bcast(individual.getDescriptor().data(), descSize,
MPI_INT, master, world);
MPI_Bcast(individual.getGenes().data(), numGenes, MPI_INT,
master, world);
MPI_Bcast(individual.getFitness(), 1, MPI_INT, master,
world);
Timetable* unpacked =
new Timetable(Population::periodsNumber);
individual.unpack(unpacked);
oldGeneration.addIndividual(unpacked);
}
}
// Broadcast natural selection factor
MPI_Bcast(&Population::naturalSelection, 1, MPI_INT, master, world);
} while (1);
}
///////////////////////////////////////////////////////////////////////////
// Result
///////////////////////////////////////////////////////////////////////////
if (myID == master) {
cout << *result << endl;
result->printPretty(cout, tuples);
}
stream->free_sprng();
MPI_Finalize();
}
void
help(const string& name)
{
cout << "Usage of program: " << endl;
cout << "\t" << name << " data_file periods_number" << endl;
}