-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAreaUnderCurve.c
127 lines (87 loc) · 3.19 KB
/
AreaUnderCurve.c
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
#include <stdlib.h>
#include <mpi.h>
#include <stdio.h>
#include <math.h>
void areaUnderLine(double *area, long long *limits, double percision);
void areaUnderCurve(long long *calcCounter, double *area, long long *limits, double percision);
int main(int argc, char **argv) {
MPI_Init(&argc, &argv);
long long totalSearch = atoi(argv[1]);
long packetSize = atoi(argv[2]);
double percision = atof(argv[3]);
int myid, numprocs;
long long limits[2];
long long j = 0;
long long k = 0;
long long i = 0;
long long z;
long long place;
long long sbe = 0;
double startTime, endTime;
double area = 0;
long long calcCounter = 0;
MPI_Status status;
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
place = 0; //BEGINING AT ORIGIN
if (myid == 0) {//------------------MASTER----------------------------------
long double totalArea = 0;
long long totalCalc = 0;
startTime = MPI_Wtime();
do {
j = 1; //RESETTING SLAVE COUNT
while (totalSearch >= place && j < numprocs) {
//SETTING SEARCH BOUNDS TO SEND TO SLAVE
limits[0] = place;
if (place + packetSize >= totalSearch) { //TRIGGERED WHEN FINISHED. SENDS == BOUNDS
limits[1] = totalSearch;
place = totalSearch;
} else { //NORMAL PACKET BOUNDS WRITE
place += packetSize;
limits[1] = place; // BOUNDS START WHERE OLD BOUNDS BEGIN
}
//SENDING SEARCH BOUNDS TO SLAVE
MPI_Send(limits, 2, MPI_LONG_LONG, j, 11, MPI_COMM_WORLD);
j++;
}
//RECEIVEING AREAS
for (sbe = 1; sbe < numprocs; sbe++) {
MPI_Recv(&area, 1, MPI_DOUBLE, sbe, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&calcCounter, 1, MPI_LONG_LONG, sbe, 1, MPI_COMM_WORLD, &status);
totalArea += area;
totalCalc += calcCounter;
}
} while (totalSearch > place);
endTime = MPI_Wtime();
printf("That took %f seconds\n",endTime - startTime);
fflush(stdout);
printf("Total area: %.10LG \n",totalArea);
fflush(stdout);
printf("Found with %lld calculations\n\n",totalCalc);
fflush(stdout);
}
if (myid != 0) { //------------------SLAVES------------------------------------
for (z = 0; z < (double)(totalSearch - 2)/packetSize/(numprocs - 1); z++) {
MPI_Recv(limits, 2, MPI_LONG_LONG, 0, 11, MPI_COMM_WORLD, &status); //RECIEVES SEARCH BOUNDS
area = 0;
areaUnderCurve(&calcCounter, &area, limits, percision);
MPI_Send(&area, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD);
MPI_Send(&calcCounter, 1, MPI_LONG_LONG, 0, 1, MPI_COMM_WORLD);
}
}
MPI_Finalize(); /* EXIT MPI */
return 0;
}
void areaUnderLine(double *area, long long *limits, double percision) {
long double position;
for (position = limits[0]; position + percision <= limits[1]; position += percision) {
(*area) += position * 2 * percision;
}
}
void areaUnderCurve(long long *calcCounter, double *area, long long *limits, double percision) {
long double position;
for (position = limits[0]; position + (percision * 2) <= limits[1]; position += percision) {
(*calcCounter)++;
(*area) += (cos(position) + 10) * percision;
}
}