-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
332 lines (301 loc) · 7.16 KB
/
queue.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Derived from ejolson's C OMP implementation */
#include <math.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sysexits.h>
#include <unistd.h>
typedef uint32_t prime_t;
#define MAX_SQUARES 100000000
#define NUM_PRIMES 1300
#define NUM_FACTORS 10
#define MAX_FREE_FACTORS 4000
#define FIDO 15
static const unsigned T_S = 200;
static uint32_t num_procs;
typedef struct factors_s
{
struct factors_s* next;
struct factors_s* prev;
prime_t s, p[NUM_FACTORS];
uint32_t fmax, i;
uint8_t n[NUM_FACTORS];
} factors_t;
typedef struct
{
factors_t* next;
factors_t* prev;
pthread_mutex_t mtx;
} queue_t;
static pthread_t* workers;
static uint8_t stop;
static queue_t task_q;
static pthread_cond_t cond;
static queue_t free_q;
static factors_t free_factors[MAX_FREE_FACTORS];
static inline void q_add_last(factors_t* f, queue_t* q)
{
f->next = (void*)q;
f->prev = q->prev;
q->prev->next = f;
q->prev = f;
}
static inline int q_empty(queue_t* q)
{
return q->next == (void*)q;
}
static inline factors_t* q_remove_first(queue_t* q)
{
factors_t* f = q->next;
f->next->prev = (void*)q;
q->next = f->next;
return f;
}
static void q_init(queue_t* q)
{
q->next = q->prev = (void*)q;
pthread_mutex_init(&q->mtx, NULL);
}
static void free_q_init(void)
{
q_init(&free_q);
for (uint32_t i = 0; i < MAX_FREE_FACTORS; i++)
q_add_last(free_factors + i, &free_q);
}
static void t_threaded_work(factors_t f);
static void* worker(void* arg)
{
for (;;)
{
pthread_mutex_lock(&task_q.mtx);
while (q_empty(&task_q))
{
if (stop)
{
pthread_mutex_unlock(&task_q.mtx);
return NULL;
}
pthread_cond_wait(&cond, &task_q.mtx);
}
factors_t* f = q_remove_first(&task_q);
pthread_mutex_unlock(&task_q.mtx);
t_threaded_work(*f);
pthread_mutex_lock(&free_q.mtx);
q_add_last(f, &free_q);
pthread_mutex_unlock(&free_q.mtx);
}
}
static void work_start(void)
{
workers = (pthread_t*)malloc(sizeof(pthread_t) * num_procs);
stop = 0;
free_q_init();
q_init(&task_q);
pthread_cond_init(&cond, NULL);
for (uint32_t i = 0; i < num_procs; i++)
pthread_create(workers + i, NULL, worker, NULL);
}
static void work_end(void)
{
pthread_mutex_lock(&task_q.mtx);
stop = 1;
pthread_mutex_unlock(&task_q.mtx);
pthread_cond_broadcast(&cond);
void* retval;
for (uint32_t i = 0; i < num_procs; i++)
pthread_join(workers[i], &retval);
}
static void work_enqueue(factors_t* f)
{
pthread_mutex_lock(&task_q.mtx);
q_add_last(f, &task_q);
pthread_mutex_unlock(&task_q.mtx);
pthread_cond_signal(&cond);
}
static prime_t P[NUM_PRIMES], gMin;
static uint32_t num_primes;
static uint32_t is_prime(prime_t p)
{
uint32_t i;
static uint32_t i_limit = 1;
for (i = 1; i < i_limit; i++)
if (!(p % P[i]))
return 0;
for (i = i_limit; P[i] * P[i] <= p; i++)
if (!(p % P[i]))
return 0;
i_limit = i - 1;
return 1;
}
static void calc_primes()
{
prime_t p;
P[0] = 2;
P[1] = 3;
num_primes = 2;
for (p = 5; num_primes < NUM_PRIMES; p += 2)
if (is_prime(p))
P[num_primes++] = p;
}
static prime_t p_pow(prime_t p, uint8_t n)
{
prime_t r = 1;
for (; n; p *= p)
{
if (n & 1)
r *= p;
n >>= 1;
}
return r;
}
static prime_t sigma_0_div_2(factors_t* f)
{
prime_t r = f->n[0];
for (uint32_t i = 1; i <= f->fmax; i++)
r *= f->n[i] + 1;
return r;
}
static uint32_t t_free(prime_t k, prime_t l)
{
prime_t n = l / k;
prime_t lmin = (k + 1) * n + 2;
prime_t lmax = (k - 1) * (n + 1) - 2;
return lmin <= l && l <= lmax;
}
static uint32_t t(factors_t* f)
{
uint8_t z[NUM_FACTORS];
for (uint32_t i = 0; i < NUM_FACTORS; i++)
z[i] = 0;
uint32_t r = 0;
for (;;)
{
uint32_t i;
for (i = 0; i <= f->fmax; i++)
{
if (z[i] < f->n[i])
{
z[i]++;
break;
}
z[i] = 0;
}
if (i > f->fmax)
break;
prime_t k = 1;
for (i = 0; i <= f->fmax; i++)
{
k *= p_pow(f->p[i], z[i]);
}
prime_t l = f->s / k;
if ((k <= l) && t_free(k, l))
r++;
}
return r;
}
static void t_threaded_work(factors_t f)
{
prime_t p = P[f.i];
prime_t s = f.s;
prime_t pMax = gMin / s + 1;
if (p <= pMax)
{
uint32_t fmax = f.fmax;
f.n[fmax]++;
f.s = s * p;
if ((sigma_0_div_2(&f) >= T_S && (t(&f) == T_S)))
{
prime_t sMin = gMin;
while (f.s < sMin)
{
if (__atomic_compare_exchange_n(&gMin, &sMin, f.s, 0,
__ATOMIC_RELAXED, __ATOMIC_RELAXED))
sMin = gMin;
}
}
t_threaded_work(f);
f.s = s;
f.n[fmax]--;
if (f.i >= NUM_PRIMES - 1)
return;
f.i++;
if (f.n[fmax])
f.fmax++;
f.p[f.fmax] = P[f.i];
f.n[f.fmax] = 0;
t_threaded_work(f);
}
}
static void t_work(factors_t f)
{
prime_t s = f.s;
prime_t pMax = gMin / s + 1;
prime_t p = P[f.i];
if (p <= pMax)
{
uint32_t fmax = f.fmax;
if ((pow(log(pMax), sqrt(2)) / log(p)) < FIDO)
{
pthread_mutex_lock(&free_q.mtx);
factors_t* factor = q_remove_first(&free_q);
pthread_mutex_unlock(&free_q.mtx);
*factor = f;
work_enqueue(factor);
return;
}
f.n[fmax]++;
f.s = s * p;
if ((sigma_0_div_2(&f) >= T_S && (t(&f) == T_S)))
{
prime_t sMin = gMin;
while (f.s < sMin)
{
if (__atomic_compare_exchange_n(&gMin, &sMin, f.s, 0,
__ATOMIC_RELAXED, __ATOMIC_RELAXED))
sMin = gMin;
}
}
t_work(f);
f.s = s;
f.n[fmax]--;
if (f.i >= NUM_PRIMES - 1)
return;
f.i++;
if (f.n[fmax])
f.fmax++;
f.p[f.fmax] = P[f.i];
f.n[f.fmax] = 0;
t_work(f);
}
}
int main()
{
num_procs = sysconf(_SC_NPROCESSORS_ONLN);
struct timeval start, end;
gettimeofday(&start, NULL);
calc_primes();
gettimeofday(&end, NULL);
double elapsedTime = (end.tv_sec - start.tv_sec);
elapsedTime += (end.tv_usec - start.tv_usec) / 1000000.0;
printf("Prime time: %f seconds\n", elapsedTime);
end = start;
factors_t f;
f.s = 2;
f.p[0] = P[0];
f.fmax = 0;
f.i = 0;
f.n[0] = 1;
gMin = MAX_SQUARES;
work_start();
t_work(f);
work_end();
gettimeofday(&end, NULL);
elapsedTime = (end.tv_sec - start.tv_sec);
elapsedTime += (end.tv_usec - start.tv_usec) / 1000000.0;
printf("Threads: %u\n", num_procs);
printf("Tatami time: %f seconds\n", elapsedTime);
printf("T(%u)=%u\n", gMin, T_S);
return EX_OK;
}