-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime_mpi.c
More file actions
64 lines (51 loc) · 1.52 KB
/
Copy pathprime_mpi.c
File metadata and controls
64 lines (51 loc) · 1.52 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
int isprime(int n) {
int i, sq;
sq = (int) sqrt(n);
for (i=3; i<=sq; i+=2)
if ((n%i) == 0) return 0;
return 1;
}
int main(int argc, char *argv[])
{
int i, p=0, max;
//max=100000000; // 100 million (primes=5761455)
max=2100000000; // 2.1 billion (primes=102886526)
int ntasks, rank, size, length, start, h, psum;
double start_time, end_time, wct;
char node[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv); //MPI
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Rank
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
MPI_Get_processor_name(node, &length); //Name
start_time = MPI_Wtime(); // Timer
start = (rank*2)+1; //
h = ntasks*2;
MPI_Bcast(&max,1,MPI_INT,0,MPI_COMM_WORLD); //broadcasting max variable to all ranks
if (rank==0){
printf("Numbers to be scanned = %d\n", max);
for (i=start; i<=max; i+=h){
if (isprime(i)) p++;
}
MPI_Reduce(&p,&psum,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
printf("Rank = %d/%d , Node = %s , Primes found = %d\n", rank, ntasks, node, p);
}
if(rank>0){
for (i=start; i<=max; i+=h) {
if (isprime(i)) p++;
}
printf("Rank = %d/%d , Node = %s , Primes found = %d\n", rank, ntasks, node, p);
MPI_Reduce(&p,&psum,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
if(rank==0){
printf("Total primes = %d\n", psum);
end_time = MPI_Wtime();
wct=end_time-start_time;
printf("Wallclock time elapsed: %.2lf seconds\n", wct);
}
MPI_Finalize();
}