-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeStats.main.c
279 lines (237 loc) · 7.43 KB
/
PrimeStats.main.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
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <limits.h>
#include <immintrin.h>
#include <string.h>
#include <stdarg.h>
#include "PrimeStats.h"
#include "PrimeStats.Util.h"
//
// keys are mapped to a file. that file is a constant string of keys,
// not separated by a newline.
//
// based on tests, that may or may not be faster. (appears only slightly faster)
//
// however, it does significantly reduce memory usage since the data
// is memory mapped.
//
//==============================================================================
static char* file_out_data = NULL;
static char* key_files_dir = NULL;
static char* prime_files_dir = NULL;
//------------------------------------------------------------------------------
typedef struct PrimeStats_KeyFileKeys_st {
char filePath[1024];
off_t fileSize;
const void* keys;
uint64_t keyCnt;
int keyLen;
} PrimeStats_KeyFileKeys_st;
typedef struct PrimeStats_KeyFiles_st {
PrimeStats_KeyFileKeys_st keyFile[8];
int keyLenMax;
int keyFilesCnt;
} PrimeStats_KeyFiles_st;
const char* keyFileNames[] = {
"toks.len.sequential.1.64.txt",
"toks.len.sequential.2.3929.txt",
"toks.len.sequential.3.201228.txt",
"toks.len.sequential.4.2429953.txt",
"toks.len.sequential.5.12629812.txt",
"toks.len.sequential.6.18510142.txt",
"toks.len.sequential.7.15311088.txt",
"toks.len.sequential.8.18679078.txt",
};
//------------------------------------------------------------------------------
bool
keyFileInit(PrimeStats_KeyFiles_st* keyFiles, const char* dirName, const char* fileName)
{
int keyLen = 0;
uint64_t keyCnt = 0;
sscanf(fileName, "toks.len.sequential.%d.%"PRIu64".txt", &keyLen, &keyCnt);
int keyLenMax = sizeof(keyFiles->keyFile) / sizeof(keyFiles->keyFile[0]);
if (keyLen > keyLenMax) { return false; }
int keyFileIdx = keyLen - 1;
sprintf(keyFiles->keyFile[keyFileIdx].filePath, "%s%s", dirName, fileName);
keyFiles->keyFile[keyFileIdx].keyLen = keyLen;
keyFiles->keyFile[keyFileIdx].keyCnt = keyCnt;
keyFiles->keyFile[keyFileIdx].keys
= mmapFileToPtr( keyFiles->keyFile[keyFileIdx].filePath,
&keyFiles->keyFile[keyFileIdx].fileSize);
return true;
}
//------------------------------------------------------------------------------
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 file : file_out_data"
"\n\t" "-k: keys dir : key_files_dir"
"\n\t" "-p: primes file : prime_files_dir"
"\n\n"
"eg:\n"
"\n./PrimeStats.main"
"\n\t-o \"./PrimeStats.8.data\" "
"\n\t-k \"/media/src/o/libo/src/Hash/\" "
"\n\t-p \"/media/src/c/primesieve/out.primes2.128.64/8/\""
"\n\n"
);
exit(1);
}
void
cliOptsToCfg(int argc, char *argv[])
{
int opt;
bool hasErr = false;
while ((opt = getopt(argc, argv, ":h:o:k:p:")) != -1)
{
switch(opt)
{
case 'h':
printHelpAndExit();
break;
case 'o':
file_out_data = optarg;
break;
case 'k':
key_files_dir = optarg;
break;
case 'p':
prime_files_dir = optarg;
break;
default:
hasErr = true;
break;
}
}
// other unparsed options
for(; optind < argc; optind++){
hasErr = true;
}
if (file_out_data == NULL) {
hasErr = true;
}
if (key_files_dir == NULL) {
hasErr = true;
}
if (prime_files_dir == NULL) {
hasErr = true;
}
if (hasErr) {
printf("invalid options given.\n");
printHelpAndExit();
}
}
void
printCfg()
{
printf("using config:\n");
printf("\tfile_out_data : %s\n", file_out_data);
printf("\tkey_files_dir : %s\n", key_files_dir);
printf("\tprime_files_dir : %s\n", prime_files_dir);
printf("\n");
fflush(stdout);
}
//==============================================================================
int
main(int argc, char *argv[])
{
cliOptsToCfg(argc, argv);
printCfg();
DIR *dPrimes;
struct dirent *dirPrimes;
dPrimes = opendir(prime_files_dir);
if (!dPrimes) {
printf("couldn't open prime files dir\n");
exit(1);
}
PrimeStats_KeyFiles_st keyFiles = {0};
keyFiles.keyLenMax = 8;
keyFiles.keyFilesCnt = 8;
for (int i = 0; i < keyFiles.keyFilesCnt; ++i) {
keyFileInit(&keyFiles, key_files_dir, keyFileNames[i]);
}
const int maxKeysPerFile = 10000;
const uint64_t keysTotal
= (keyFiles.keyFile[0].keyCnt > maxKeysPerFile
? maxKeysPerFile : keyFiles.keyFile[0].keyCnt)
+ (keyFiles.keyFile[1].keyCnt > maxKeysPerFile
? maxKeysPerFile : keyFiles.keyFile[1].keyCnt)
+ (keyFiles.keyFile[2].keyCnt > maxKeysPerFile
? maxKeysPerFile : keyFiles.keyFile[2].keyCnt)
+ (keyFiles.keyFile[3].keyCnt > maxKeysPerFile
? maxKeysPerFile : keyFiles.keyFile[3].keyCnt)
+ (keyFiles.keyFile[4].keyCnt > maxKeysPerFile
? maxKeysPerFile : keyFiles.keyFile[4].keyCnt)
+ (keyFiles.keyFile[5].keyCnt > maxKeysPerFile
? maxKeysPerFile : keyFiles.keyFile[5].keyCnt)
+ (keyFiles.keyFile[6].keyCnt > maxKeysPerFile
? maxKeysPerFile : keyFiles.keyFile[6].keyCnt)
+ (keyFiles.keyFile[7].keyCnt > maxKeysPerFile
? maxKeysPerFile : keyFiles.keyFile[7].keyCnt);
//--------------------------------------------------------------------
int statsCnt = 0;
const int statsBatchCnt = 4096;
PrimeStats_st* statsArr = calloc(statsBatchCnt, sizeof(*statsArr));
while ((dirPrimes = readdir(dPrimes)) != NULL)
{
if (strlen(dirPrimes->d_name) < 10) { continue; }
char primesFilePath[1024] = {0};
sprintf(primesFilePath, "%s%s", prime_files_dir, dirPrimes->d_name);
printf("\nfilename: %s\n", dirPrimes->d_name); fflush(stdout);
uint64_t primesFileBytes = 0;
const void* primesArr = mmapFileToPtr(primesFilePath, &primesFileBytes);
uint64_t* primeMap = (uint64_t*)primesArr;
const int numPrimes = primesFileBytes / sizeof(*primeMap);
//------------------------------------------------------
struct timespec timeStart = timerStart();
int timerIterCnt = 0;
for (int iPrime = 0; iPrime < numPrimes; iPrime++)
{
//--------------------------------
timerIterCnt++;
const int statsArrIdx = statsCnt % statsBatchCnt;
PrimeStats_st* stats = &statsArr[statsArrIdx];
PrimeStats_Init(stats, primeMap[iPrime]);
//--------------------------------
for (int iKeyFile = 0; iKeyFile < keyFiles.keyFilesCnt; iKeyFile++) {
PrimeStats_RunKeys(stats,
keyFiles.keyFile[iKeyFile].keys,
keyFiles.keyFile[iKeyFile].keyLen,
keyFiles.keyFile[iKeyFile].keyCnt,
maxKeysPerFile);
}
PrimeStatsMeta_Calc(stats);
//--------------------------------
if (statsArrIdx == (statsBatchCnt - 1))
{
uint64_t timeDiff = timerEnd(timeStart);
printf("\ntimerIterCnt : %d", timerIterCnt);
printf("\ntime taken : ");
printU64WithCommas(timeDiff);
uint64_t nsPerPrime = timeDiff / (uint64_t)timerIterCnt;
printf("\nns Per Prime : ");
printU64WithCommas(nsPerPrime);
printf("\n");
fileAppendBytes(file_out_data, statsArr,
statsBatchCnt * sizeof(*statsArr));
printf("\n");
memset(statsArr, 0, statsBatchCnt * sizeof(*statsArr));
timerIterCnt = 0;
timeStart = timerStart();
}
statsCnt++;
}
//------------------------------------------------------
}
closedir(dPrimes);
exit(1);
}