-
Notifications
You must be signed in to change notification settings - Fork 1
/
mPrimes.c
193 lines (159 loc) · 4.47 KB
/
mPrimes.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "mPrimes.h"
#include "clineest.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/timeb.h>
#include <string.h>
struct timeb tstart, tend;//timers
void savetofile(unsigned char ** arr, Number pLimit, int rank, Number start, Number endn){
FILE *fp;
char buffer[10];
snprintf(buffer, sizeof(buffer), "P%i.txt", rank);
fp=fopen(buffer, "a+");
if(fp == NULL)
return;
Number i;
for(i=1;i<pLimit;++i)
if((*arr)[i])
fprintf(fp,"%llu \n",i*2+1+start);
fclose(fp);
}
__inline void SieveFile(Number start, Number endnum, int rank) {
// initialize
Number blockSize = (endnum - start +1)/2;
Number i,j,index,blockstart;
unsigned char* isPrime;
isPrime = malloc((blockSize+1)* sizeof(unsigned char));
memset(isPrime, 1, sizeof(unsigned char)*(blockSize+1));
// find all non-primes
for (i = 3; i*i<endnum; i+=2){
if (i >= 9 && i % 3 == 0)
continue;
if (i >= 25 && i % 5 == 0)
continue;
if (i >= 49 && i % 7 == 0)
continue;
if (i >= 121 && i % 11 == 0)
continue;
if (i >= 169 && i % 13 == 0)
continue;
if(i >= 17*17 && i%17==0)
continue;
blockstart = ((start+i-1)/i)*i;
if (blockstart < i*i) //esnure min is reached
blockstart = i*i;
// start value must be odd
if ((blockstart & 1) == 0)
blockstart += i;
for (j = blockstart; j <= endnum; j += 2*i){
index = j - start;
isPrime[index/2] = 0;
}
}
savetofile(&isPrime,blockSize+1,rank, start,endnum);
free(isPrime);
}
void primesMPIFile(Number cachesize, Number start, Number pEnd, int rank){
Number from;
Number to;
for ( from = start; from < pEnd; from += cachesize){
to = from + cachesize;
if (to > pEnd)
to = pEnd;
SieveFile(from, to, rank);
}
}
Number primesMPI(Number cachesize, Number start, Number pEnd){
int found = 0;
Number from;
Number to;
for ( from = start; from < pEnd; from += cachesize){
to = from + cachesize;
if (to > pEnd)
to = pEnd;
found += SieveCache(from, to);
}
return found;
}
Number findPrimesSeq(char opt, Number Limit, char verboseoutput){
struct timeb tstart, tend, cStart, cEnd;
int found = 0;
Number ssize = 256*1024;//i have a 256 cacheline
if(opt){//max opt for differnt machines
unsigned int cacheLine = get_cache_line(1024*1024);
if(cacheLine>1024)
ssize = cacheLine;
if(verboseoutput)
printf("Found cacheline of:%i\n",cacheLine);
}
if(verboseoutput)
ftime(&tstart);
Number from;
for ( from = 2; from <= Limit; from += ssize){
Number to = from + ssize;
if (to > Limit)
to = Limit;
found += SieveCache(from, to);
}
if(verboseoutput){
ftime(&tend);
float diff =((float) (1000.0 * (tend.time - tstart.time) + (tend.millitm - tstart.millitm)))/1000.0;
printf("Primes found:%i\n",found+1);
printf("Computation Time:%f\n",diff);
}
return found +1;
}
__inline Number SieveCache(Number start, Number endnum) {
// initialize
Number blockSize = (endnum - start +1)/2;
Number i,j,index,blockstart;
unsigned char* isPrime;
isPrime = malloc((blockSize+1)* sizeof(unsigned char));
memset(isPrime, 1, sizeof(unsigned char)*(blockSize+1));
// find all non-primes
for (i = 3; i*i<endnum; i+=2){
if (i >= 9 && i % 3 == 0)
continue;
if (i >= 25 && i % 5 == 0)
continue;
if (i >= 49 && i % 7 == 0)
continue;
if (i >= 121 && i % 11 == 0)
continue;
if (i >= 169 && i % 13 == 0)
continue;
if(i >= 17*17 && i%17==0)
continue;
blockstart = ((start+i-1)/i)*i;
if (blockstart < i*i) //esnure min is reached
blockstart = i*i;
// start value must be odd
if ((blockstart & 1) == 0)
blockstart += i;
for (j = blockstart; j <= endnum; j += 2*i){
index = j - start;
isPrime[index/2] = 0;
}
}
// sieve is complete, count primes
int found = 0;
for (i = 0; i < blockSize; i++)
if(isPrime[i])
found +=isPrime[i];
free(isPrime);
return found;
}
//debug
/*static void DebugPrint(int rank) {
char a;
MPI_Bcast(&a, 1, MPI_BYTE, 0, MPI_COMM_WORLD);
printf("%d: DEBUG reached\n", rank);
}
static void DebugWait(int rank) {
char a;
scanf(&a);
MPI_Bcast(&a, 1, MPI_BYTE, 0, MPI_COMM_WORLD);
printf("%d: DEBUG reached\n", rank);
}
*/