-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime.c
84 lines (65 loc) · 1.83 KB
/
prime.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
#include <stdio.h>
#include </usr/include/mpich/mpi.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
double is_prime(double prime) {
//printf("testing %f\n", prime);
if (prime == 1) return 0;
if (prime == 3 || prime == 5) return 1;
int primesqrt = sqrt(prime);
/*
if (primesqrt==floor(primesqrt)) {
printf("sqrt\n");
return 0;
}
*/
for (double i = 3; i < primesqrt; i+=2) if ((floor(prime/i)*i) == prime) return 0;
return 1;
}
int main(int argc, char** argv) {
int rank, total, count=0;
FILE *file;
char filename[1024];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &total);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//MPI_Comm slaves;
//MPI_Comm_split(MPI_COMM_WORLD, (rank == 0), rank, &slaves);
if (argc != 2) {
if (rank == 0) printf("Usage: %s <computation quota>\n", argv[0]);
MPI_Finalize();
return 0;
}
rank++;
sprintf(filename, "./numberlists/numberlist%d.txt", rank);
file = fopen(filename, "w");
double quota = atof(argv[1]);
if (fmod(quota, total) != 0) {
if (rank == 1) {
quota = floor(quota/total)+fmod(quota, total);
}
else {
quota = floor(quota/total);
}
}
else {
quota = quota/total;
}
printf("P: %d of %d checking in, quota: %f\n", rank, total, quota);
//printf("%f\n", quota);
double start = ((rank*2)+1);
for (double prime = start; count < quota; prime+=total) {
if (is_prime(prime)) {
//printf("%d, gucc'd\n", rank);
count++;
fprintf(file, "%.0f\n", prime);
}
}
printf("node %d done...\n", rank);
MPI_Finalize();
//MPI_Barrier(slaves);
//if (rank==1) printf("done\n");
return 0;
}