/
factor.c
155 lines (128 loc) · 3.38 KB
/
factor.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h> /* for printf */
#include <gmp.h>
#include <math.h>
#include <mpi.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// declare mpz structs
mpz_t n;
mpz_t zero;
mpz_t r;
mpz_t d;
mpz_t start;
mpz_t end;
mpz_t temp;
mpz_t my_rank_mpz;
mpz_t local_end;
mpz_t current_prime;
mpz_t global_end;
mpz_init_set_ui(zero, 0);
mpz_init(d);
double t1, t2, totaltime;
int tag = 0;
int my_rank, p;
int source, dest, i;
mpz_init_set_str(n, argv[1], 10); // initialize the key (n)
mpz_init(global_end);
mpz_sqrt(global_end, n);
// MPI boilerplate
MPI_Status status;
MPI_Request request;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
t1 = MPI_Wtime();
// initialize mpz structs
mpz_init(r);
mpz_init(start);
mpz_init(local_end);
mpz_init(end);
mpz_init(temp);
mpz_init(current_prime);
mpz_init(my_rank_mpz);
mpz_init_set_ui(start, 2);
mpz_set(temp, start);
size_t temp_window_size = mpz_sizeinbase(temp, 2);
mpz_add_ui(temp, temp, temp_window_size);
for (i = 0; i < my_rank; i++){
temp_window_size = mpz_sizeinbase(temp, 2);
mpz_add_ui(temp, temp, temp_window_size);
}
mpz_set(start, temp);
size_t window_size = mpz_sizeinbase(start, 2);
mpz_add_ui(local_end, start, window_size);
mpz_init_set_ui(my_rank_mpz, my_rank);
mpz_init_set(end, global_end);
mpz_nextprime(current_prime, start);
mpz_init_set_str(n, argv[1], 10);
int check = 0;
int flag = 0;
int found = 0;
MPI_Irecv(&found, 1, MPI_INTEGER, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &request);
MPI_Barrier(MPI_COMM_WORLD);
while (mpz_cmp(start, end) <= 0){
while(mpz_cmp(current_prime, local_end) <= 0 && mpz_cmp(current_prime, end) <= 0){
mpz_mod(r, n, current_prime);
if (mpz_cmp(zero, r) == 0){
mpz_divexact(d, n, current_prime);
gmp_printf("%Zd %Zd\n", d, current_prime);
found = 1;
for (dest = 0; dest < p; dest++){
MPI_Isend(&found, 1, MPI_INTEGER, dest, tag, MPI_COMM_WORLD, &request);
}
break;
}
check++;
if (check == 500){
check = 0;
MPI_Test(&request, &flag, &status);
if (flag){
found = 1;
break;
}
}
mpz_nextprime(current_prime, current_prime);
}
if( found == 1){
break;
}
// calculate new start index
mpz_set(temp, start);
size_t temp_window_size = mpz_sizeinbase(start, 2);
mpz_add_ui(temp, temp, temp_window_size);
mpz_set(start, local_end);
for (i = my_rank + 1; i < p; i++){
temp_window_size = mpz_sizeinbase(temp, 2);
mpz_add_ui(temp, temp, temp_window_size);
}
for (i = 0; i < my_rank; i++){
temp_window_size = mpz_sizeinbase(temp, 2);
mpz_add_ui(temp, temp, temp_window_size);
}
mpz_set(start, temp);
temp_window_size = mpz_sizeinbase(start, 2);
mpz_add_ui(local_end, start, temp_window_size);
mpz_nextprime(current_prime, start);
}
t2 = MPI_Wtime();
totaltime = t2 - t1;
MPI_Barrier(MPI_COMM_WORLD);
if (my_rank == 0){
char buf[0x1000];
snprintf(buf, sizeof(buf), "time_%s", argv[1]);
FILE *f = fopen(buf, "w");
double other_totaltime = 0;
fprintf(f, "0\t%1.2e\n", totaltime);
for (source = 1; source < p; source++){
MPI_Recv(&other_totaltime, 1, MPI_DOUBLE, source, tag, MPI_COMM_WORLD, &status);
fprintf(f, "%d\t%1.2e\n", source, other_totaltime);
}
fclose(f);
}
else{
MPI_Send(&totaltime, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}