This repository has been archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
86 lines (68 loc) · 1.99 KB
/
main.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
// crunchy_breakfast_demo_code, main.cpp, jake deery, 2020
#include "main.h"
double getFit(double x, double y) {
// vars
double fit;
// calculate the fit
fit = -1.0 * (y + 47.0) * sin(sqrt(abs(y + x / 2.0 + 47.0))) + -1.0 * x * sin(sqrt(abs(x - (y + 47.0))));
// end process
return fit;
}
int main(int argc, char** argv) {
// vars
array<double, 3> bestFit;
double startBound = -16384.0;
double endBound = 16384.0;
double boundRange = abs(startBound - endBound);
double fitness;
int worldSize;
int worldRank;
int i;
uTimeOut deltaTime;
// setup
locale systemLocale("en_GB.UTF-8");
cout.imbue(systemLocale);
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// Get the number of processes
MPI_Comm_size(MPI_COMM_WORLD, &worldSize);
// Get the rank of this process
MPI_Comm_rank(MPI_COMM_WORLD, &worldRank);
// Get the bunch size
int bunchSize = boundRange / worldSize;
// main system echo
if(worldRank == 0) {
cout << "[I] Op1 program by Jake Deery, 2020" << "\n";
cout << "[I] Program commencing, please wait . . . " << "\n";
cout << "\n";
}
// vars
startBound = startBound + (bunchSize * worldRank);
endBound = endBound - (bunchSize * ((worldSize - 1) - worldRank));
i = 0;
// getting time
auto startTime = uTimeGet::now();
// main for loop
for(double x = startBound; x < endBound + .1; x += .1) {
for(double y = startBound; y < endBound + .1; y += .1) {
fitness = getFit(x,y);
if(fitness < bestFit[2]) {
bestFit[0] = x;
bestFit[1] = y;
bestFit[2] = fitness;
};
// alert counter
i++;
}
}
// end and calculate time
auto endTime = uTimeGet::now();
deltaTime = endTime - startTime;
// report findings
cout << "[I] Process " << worldRank << " ran through " << i << " loops in " << deltaTime.count() / 1000 << "ms\n";
cout << "[I] Process " << worldRank << " says best fit was " << bestFit[2] << " found at [" << bestFit[0] << "," << bestFit[1] << "]\n";
cout << "\n";
// end process
MPI_Finalize();
return 0;
}