-
Notifications
You must be signed in to change notification settings - Fork 0
/
primegen.c
271 lines (234 loc) · 6.89 KB
/
primegen.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
#include <inttypes.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <stdbool.h>
#include <unistd.h>
#include <x86intrin.h> // for __rdtsc()
#include <primesieve.h>
// @CREDITS: hard-coded primes for random number generation come from xxhash
// https://github.com/Cyan4973/xxHash/
// @CREDITS: uses primesieve for prime generation
// https://github.com/kimwalisch/primesieve/
#define DBG_FFL {printf("DBG: File:[%s] Func:[%s] Line:[%d]\n",\
__FILE__, __FUNCTION__, __LINE__);fflush(stdout);}
//------------------------------------------------------------------------------
typedef struct
thrInfo_st {
pthread_t thrId;
int thrNum;
} thrInfo_st;
typedef struct primegen_cfg_st {
char* dirOut;
uint64_t primeMin;
uint64_t primeMax;
int thrCnt;
int thrPrimeLoops;
int thrPrimesPerLoop;
} primegen_cfg_st;
primegen_cfg_st cfg = {
.dirOut = "out.primes.64",
.primeMin = UINT32_MAX,
.primeMax = UINT64_MAX,
.thrCnt = 2,
.thrPrimeLoops = 4,
.thrPrimesPerLoop = 4096,
};
//------------------------------------------------------------------------------
// i believe this is a merseinne twister i found at some point
static uint64_t rand_x = 0;
static uint64_t rand_y = 0;
void
rand_init() {
rand_x = __rdtsc() * 0x9E3779B185EBCA87ULL;
rand_y = __rdtsc() * 0xC2B2AE3D27D4EB4FULL;
}
uint64_t
rand_get(void) {
rand_x = rand_y;
rand_x ^= (rand_x << 23);
rand_x ^= (rand_x >> 17);
rand_x ^= rand_y ^ (rand_y >> 26);
rand_y = rand_x;
return rand_x + rand_y;
}
//------------------------------------------------------------------------------
// get a new starting prime range, based on the thread number.
// this helps to ensure more of a range of primes.
// it's likely unnecessary, as rang_get() should handle that just fine.
// but might as well try.
uint64_t
primesNewMin(int thrId)
{
const uint64_t rangeCnt = (cfg.primeMax - cfg.primeMin) / cfg.thrCnt;
const uint64_t rangeMin = cfg.primeMin + (thrId * rangeCnt);
const uint64_t rangeMax = rangeMin + rangeCnt;
uint64_t p = 0;
do {
p = rand_get();
// print to check how many times we're having to run this...
// averages slightly more than nThreads/2 times, which makes sense.
// for larger numbers of threads, it may make sense to pre-calculate these.
// printf(".");fflush(stdout);
} while (p <= rangeMin || p >= rangeMax);
return p;
}
//------------------------------------------------------------------------------
static void *
thrRun(void *arg)
{
thrInfo_st *thrInfo = arg;
printf("thrNum [%d] starting...\n", thrInfo->thrNum);
fflush(stdout);
for (int loop = 0; loop < cfg.thrPrimeLoops; loop++)
{
const uint64_t primeMin = primesNewMin(thrInfo->thrNum);
// not sure of the performance cost of reallocing on every batch...
// previously used a different method with a single alloc per thread...
// but this was recommended in the documentation, so trying that...
uint64_t* primesFound
= (uint64_t*)primesieve_generate_n_primes(cfg.thrPrimesPerLoop,
primeMin,
UINT64_PRIMES);
char fName[256] = {0};
sprintf(fName, "./%s/%"PRIu64".primes.u64.bin", cfg.dirOut, primesFound[0]);
FILE *fp = NULL;
if (NULL == (fp = fopen(fName, "wb"))) {
printf("can't open file for output: %s\n", fName);
exit(1);
}
fwrite(primesFound, sizeof(*primesFound), cfg.thrPrimesPerLoop, fp);
fflush(fp);
primesieve_free(primesFound);
printf("thr: %3d completed loop: %4d of %4d\n",
thrInfo->thrNum, loop+1, cfg.thrPrimeLoops);
fflush(stdout);
}
return (int*)1;
}
//------------------------------------------------------------------------------
void
printCfg()
{
printf("using configuration:\n");
printf("\tdirOut : %s\n", cfg.dirOut);
printf("\tprimeMin : %"PRIu64"\n", cfg.primeMin);
printf("\tprimeMax : %"PRIu64"\n", cfg.primeMax);
printf("\tthrCnt : %d\n", cfg.thrCnt);
printf("\tthrPrimeLoops : %d\n", cfg.thrPrimeLoops);
printf("\tthrPrimesPerLoop : %d\n", cfg.thrPrimesPerLoop);
printf("\n");
fflush(stdout);
}
//------------------------------------------------------------------------------
void
printHelpAndExit()
{
printf(
"\n"
"primegen: find and output prime numbers to a binary file\n"
"options:\n"
"\n\t" "-h: help"
"\n\t" "-o: output directory. default: %s"
"\n\t" "-b: prime range begin. default: %"PRIu64""
"\n\t" "-e: prime range end. default: %"PRIu64""
"\n\t" "-t: thread count. default: %d"
"\n\t" "-l: thread prime loops. default: %d"
"\n\t" "-p: thread primes per loop. default: %d"
"\n\n",
cfg.dirOut,
cfg.primeMin,
cfg.primeMax,
cfg.thrCnt,
cfg.thrPrimeLoops,
cfg.thrPrimesPerLoop
);
exit(1);
}
//------------------------------------------------------------------------------
void
cliOptsToCfg(int argc, char *argv[])
{
int opt;
bool hasErr = false;
while ((opt = getopt(argc, argv, ":h:o:b:e:t:l:p:")) != -1)
{
switch(opt)
{
case 'h':
printHelpAndExit();
break;
case 'o':
cfg.dirOut = optarg;
break;
case 'b':
cfg.primeMin = strtoull(optarg, NULL, 10);
break;
case 'e':
cfg.primeMax = strtoull(optarg, NULL, 10);
break;
case 't':
cfg.thrCnt = strtol(optarg, NULL, 10);
break;
case 'l':
cfg.thrPrimeLoops = strtol(optarg, NULL, 10);
break;
case 'p':
cfg.thrPrimesPerLoop = strtol(optarg, NULL, 10);
break;
default:
hasErr = true;
break;
}
}
// other unparsed options
for(; optind < argc; optind++){
hasErr = true;
}
if (hasErr) {
printf("invalid options given.\n");
printHelpAndExit();
}
}
//==============================================================================
int
main(int argc, char *argv[])
{
cliOptsToCfg(argc, argv);
printCfg();
int ret;
void *thrRet;
thrInfo_st *thrInfo;
pthread_attr_t thrAttr;
rand_init();
if (0 != (ret = pthread_attr_init(&thrAttr))) {
perror("pthr_attr_init");
exit(1);
}
if (NULL == (thrInfo = calloc(cfg.thrCnt, sizeof(*thrInfo)))) {
perror("calloc");
exit(1);
}
for (int thrId = 0; thrId < cfg.thrCnt; ++thrId) {
thrInfo[thrId].thrNum = thrId;
if (0 != (ret = pthread_create(&thrInfo[thrId].thrId, &thrAttr,
&thrRun, &thrInfo[thrId]))) {
perror("pthr_create");
exit(1);
}
}
if (0 != (ret = pthread_attr_destroy(&thrAttr))) {
perror("pthr_attr_destroy");
exit(1);
}
for (int thrId = 0; thrId < cfg.thrCnt; ++thrId) {
if (0 != (ret = pthread_join(thrInfo[thrId].thrId, &thrRet))) {
perror("pthr_join");
exit(1);
}
printf("thread %d completed\n", thrInfo[thrId].thrNum);
}
return 0;
}