-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPISimpsons.c
74 lines (56 loc) · 1.37 KB
/
MPISimpsons.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
#include<stdio.h>
#include<mpi.h>
#include<math.h>
double f(double x);
int main(int argc, char *argv[]) {
int rank, world;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &world);
int i, bpp, remainder, start, end;//, n;
double a, b, h, procsum, sum, t1, t2;
long n;
t1 = MPI_Wtime();
n = 1000000000;
a = 1;
b = 10;
h = (b - a) / n;
bpp = (n)/world;
remainder = (n - 1)%world;
if(rank > remainder || remainder == 0) {
start = remainder + rank * bpp;
end = start + bpp;
} else {
start = rank*(bpp + 1);
end = start + bpp;
if(rank < remainder) {
end++;
}
}
for(i = start; i < end; i++) {
int x = i +1;
if(x%2 == 0) {
procsum += f(a + h * x) * 2;
//printf("%d Yields: %f\n", x, f(a + h * x) * 2);
} else {
procsum += f(a + h * x) * 4;
//printf("%d Yields: %f\n", x, f(a + h * x) * 4);
}
}
MPI_Reduce(&procsum, &sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if(rank == 0){
sum += f(a);
//printf("0 Yields: %f\n", f(a));
sum += f(b);
//printf("0 Yields: %f\n", f(b));
printf("MPI Simpsons Rule.\nIntegral of cos(x) on interval: [1, 10], n = 1000000000\n");
printf("Result: %f\n", (h/3)*sum);
t2 = MPI_Wtime();
printf("Time taken: %d seconds %d milliseconds.\n", (int)(t2-t1), (t2 - t1) - (int)(t2-t1));
}
MPI_Finalize();
return 0;
}
double f(double x) {
return cos(x);
}