This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
multicore.c
507 lines (418 loc) · 12.2 KB
/
multicore.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* Copyright (c) 2019 Trail of Bits, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __APPLE__
#define _GNU_SOURCE
#endif
#ifndef __FILE_NAME__
#define __FILE_NAME__ "MULTICORE"
#endif
#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "output.h"
#include <inttypes.h>
#include <string.h>
#include "avx.h"
#include "avx512.h"
#include "sse.h"
#include "neon.h"
#include "naive.h"
#include "multicore.h"
#include "common.h"
#include "workpool.h"
typedef enum found_status_ {
sec_unknown,
sec_found,
sec_notfound,
sec_tryagain
} found_status_t;
int CPU_LIMIT = 0;
#ifdef __APPLE__
#include <mach/thread_policy.h>
#include <mach/thread_act.h>
// Support thread affinity on MacOS
// taken from: http://yyshen.github.io/2015/01/18/binding_threads_to_cores_osx.html
// sadly, MacOS doesn't respect these settings
// but we try anyway
typedef struct cpu_set {
uint32_t count;
} cpu_set_t;
static inline void
CPU_ZERO(cpu_set_t *cs) { cs->count = 0; }
static inline void
CPU_SET(int num, cpu_set_t *cs) { cs->count |= (1 << num); }
static inline int
CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); }
int pthread_setaffinity_np(pthread_t thread, size_t cpu_size,
cpu_set_t *cpu_set)
{
thread_port_t mach_thread;
unsigned core = 0;
for (core = 0; core < 8 * cpu_size; core++) {
if (CPU_ISSET(core, cpu_set)) break;
}
thread_affinity_policy_data_t policy = { core };
mach_thread = pthread_mach_thread_np(thread);
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy, 1);
return 0;
}
#endif // __APPLE__
unsigned char VALID_CPUS[MAX_CPU]= {0};
static thread_args_t thread_args[MAX_CPU];
static pthread_mutex_t cond_guard;
static pthread_cond_t thread_is_done;
#define SLEN 4096
void multicore_print_cpu_map(void) {
char s[SLEN];
int np = 0;
for(int i = 0; i < MAX_CPU; i++) {
if(np > SLEN-2) {
break;
}
if(VALID_CPUS[i] == 1) {
int v = snprintf(s+np, SLEN-np, "%d,", i);
if(v < 0) {
log_error(__FILE_NAME__, "Could not print string!\n");
exit(1);
}
np += v;
}
}
log_output(__FILE_NAME__, "Operating on CPUs: %s\n", s);
}
#undef SLEN
// parse and set a CPU mapping
bool multicore_set_cpu_map(const char *map) {
long ncpu = sysconf(_SC_NPROCESSORS_ONLN);
if (ncpu <= 0) {
perror("Could not get cpu count");
exit(-1);
}
// clear valid CPUs
memset(VALID_CPUS, 0, sizeof(VALID_CPUS[0]) * MAX_CPU);
char *s = strdup(map);
char *rest = s;
char *tok;
if (NULL == rest) {
log_error(__FILE_NAME__, "Could not duplicate string");
exit(1);
}
while ((tok = strtok_r(rest, ",", &rest))) {
unsigned int cpu = strtoul(tok, NULL, 0);
if (cpu < MAX_CPU && cpu < ncpu) {
VALID_CPUS[cpu] = 1;
} else {
log_error(__FILE_NAME__, "Tried to set an invalid CPU: %d [%s]\n", cpu, tok);
free(s);
return false;
}
}
free(s);
return true;
}
// figure out the next CPU a worker thread
// should attach to
static int multicore_next_valid_cpu() {
for(int i = 0; i < MAX_CPU; i++) {
if(VALID_CPUS[i] == 1) {
VALID_CPUS[i] = 0;
return i;
}
}
return -1;
}
bool multicore_check(void) {
// used to check for ncpu > 1; but it is equally valid
// to test multicore and threading overhead on one cpu
return true;
}
// check if one thread found secret
// or if all threads expired without finding it
static void check_if_found(long ncpu, found_status_t *status) {
int done_count = 0;
for(int i = 0; i < ncpu; i++ ) {
// one thread got it
if(thread_args[i].found) {
*status = sec_found;
return;
}
if(thread_args[i].done) {
done_count += 1; }
}
// all threads done, no secret
if(ncpu == done_count) {
*status = sec_notfound;
return;
}
// some threads are still working
*status = sec_tryagain;
}
static void* search_thread(void *arg) {
thread_args_t *targ = (thread_args_t*)arg;
uint64_t nops = 0;
uint64_t (*method_func)(uint64_t, uint64_t, uint64_t, bool*);
// make this thread cancellable
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
// make it cancellable *immediately*
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
uint64_t start, stop;
char *method = NULL;
targ->found = false;
if(avx512_check()) {
method = "AVX512";
method_func = avx512_do_range;
} else if(avx2_check()) {
method = "AVX2";
method_func = avx2_do_range;
} else if(sse_check()) {
method = "SSE4.1";
method_func = sse_do_range;
} else if(neon_check()) {
method = "NEON";
method_func = neon_do_range;
} else {
method = "NAIVE";
method_func = naive_do_range;
}
PoolStatus is_done = workpool_get_chunk(&start, &stop);
while (is_done != PoolFinished) {
//log_output(__FILE_NAME__, "TID [%" PRIx64 "] doing a workitem\n",(uint64_t)targ->tid);
nops += method_func(start, stop, targ->secret, &targ->found);
if(true == targ->found) {
break;
}
if(true == targ->done) {
// someone else told us we're done
// bail out early
targ->nops = nops;
log_output(__FILE_NAME__, "TID[%" PRIx64"]: Exiting since someone told us we're done\n",
(uint64_t)targ->tid);
return NULL;
}
do {
is_done = workpool_get_chunk(&start, &stop);
} while (is_done == PoolTryAgain);
}
targ->done = true;
targ->nops = nops;
log_output(__FILE_NAME__, "Thread [%" PRIx64 "] done [%" PRIu64 "] ops using [%s]\n", (uint64_t)targ->tid, nops, method);
// we found it! signal to main thread to stop everything
if(true == targ->found ) {
log_output(__FILE_NAME__, "TID[%" PRIx64"]: Secret [%" PRIx64 "] found between [%" PRIx64 "] - [%" PRIx64 "]\n",
(uint64_t)targ->tid,
targ->secret, start, stop);
pthread_mutex_lock(&cond_guard);
pthread_cond_signal(&thread_is_done);
pthread_mutex_unlock(&cond_guard);
} else {
found_status_t f = sec_unknown;
check_if_found(targ->ncpu, &f);
switch(f) {
case sec_found:
// found it!
// fall through
case sec_notfound:
// we were the last thread, signal anyway
pthread_mutex_lock(&cond_guard);
pthread_cond_signal(&thread_is_done);
pthread_mutex_unlock(&cond_guard);
break;
case sec_tryagain:
break;
default:
log_error(__FILE_NAME__, "Unknown value in 'are we done?' loop\n");
// other threads still running, ignore
break;
}
}
return NULL;
}
// estimate ops completed by all threads
static uint64_t estimate_nops(long ncpu) {
uint64_t est_nops = 0;
int count = 0;
// use real values for all completed threads
for(int i = 0; i < ncpu; i++ ) {
if(thread_args[i].nops > 0) {
est_nops += thread_args[i].nops;
count += 1;
}
}
if (count < ncpu) {
log_output(__FILE_NAME__,
"WARNING: Not all CPUs completed a work item [%d/%d]\n", count,
ncpu);
}
return est_nops;
}
bool multicore_launch_threads(long ncpu, uint64_t secret, uint64_t h_start, uint64_t h_end) {
if(ncpu > MAX_CPU) {
log_output(__FILE_NAME__, "too many CPUs [%ld]. Limiting to [%d]\n",
ncpu, MAX_CPU);
ncpu = MAX_CPU;
} else {
log_output(__FILE_NAME__, "Found [%ld] enabled processors\n", ncpu);
}
// initialize condition variable and mutex used to detect
// when a thread has found the needle in the haystack
pthread_mutex_init(&cond_guard, NULL);
pthread_cond_init(&thread_is_done, NULL);
if(!workpool_is_set()) {
workpool_set(h_start, h_end, ncpu);
}
// assign each thread a range to search
for(uint64_t i = 0; i < (uint64_t)ncpu; i++) {
// clean up this thread's area
memset(&thread_args[i], 0, sizeof(thread_args_t));
thread_args[i].secret = secret;
thread_args[i].done = false;
thread_args[i].ncpu = ncpu;
}
// 3) start threads
//
// lock notification condition so we know when a thread finds secret
pthread_mutex_lock(&cond_guard);
// initialize each thread
int i;
log_output(__FILE_NAME__, "Starting threads\n");
for(i = 0; i < ncpu; i++) {
int ret;
// thread attributes used to specify a cpu affinity
pthread_attr_t attr;
ret = pthread_attr_init(&attr);
// bind each thread to a specific CPU
cpu_set_t cpus;
CPU_ZERO(&cpus);
int valid_cpu = multicore_next_valid_cpu();
if(valid_cpu < 0) {
log_error(__FILE_NAME__, "Ran out of CPUs!\n");
exit(-1);
}
log_output(__FILE_NAME__, "Thread will use CPU: %d\n", valid_cpu);
CPU_SET(valid_cpu, &cpus);
// create the thread
log_output(__FILE_NAME__, ".\n");
ret = pthread_create(&(thread_args[i].tid), &attr, search_thread, &(thread_args[i]) );
if(ret < 0) {
perror("Could not create thread");
return false;
}
ret = pthread_setaffinity_np(thread_args[i].tid, sizeof(cpu_set_t), &cpus);
if(ret < 0) {
perror("Could not set thread affinity");
return false;
}
}
log_output(__FILE_NAME__, "\n");
return true;
}
void multicore_init_cpus(int count) {
long ncpu = sysconf(_SC_NPROCESSORS_ONLN);
if(ncpu <= 0) {
perror("Could not get cpu count");
exit(-1);
}
if(ncpu > MAX_CPU) {
ncpu = MAX_CPU;
}
if(count > 0 && ncpu > count) {
ncpu = count;
}
memset(VALID_CPUS, 0, sizeof(VALID_CPUS));
for(int i = 0; i < ncpu; i++) {
VALID_CPUS[i] = 1;
}
}
long multicore_get_ncpus(void) {
long sum = 0;
for(int i = 0; i < MAX_CPU; i++) {
if(VALID_CPUS[i] == 1) {
sum += 1;
}
}
return sum;
}
void multicore_wait_for_threads(long ncpu, method_results_t *results) {
results->found = false;
results->ops_done = 0;
bool found = false;
found_status_t status = sec_unknown;
bool threads_done = false;
while(false == threads_done) {
pthread_cond_wait(&thread_is_done, &cond_guard);
check_if_found(ncpu, &status);
switch(status) {
case sec_found: // found the secret
found = true;
threads_done = true;
break;
case sec_notfound: // secret not found by any threads
found = false;
threads_done = true;
break;
case sec_tryagain: // someone is still working
// redundant, here for clarity
threads_done = false;
break;
default:
log_output(__FILE_NAME__, "Unknown failure in status check loop\n");
exit(-1);
}
}
pthread_mutex_unlock(&cond_guard);
// wait for threads to complete work items
// tell them not to fetch anymore
log_output(__FILE_NAME__, "Finished. Stopping threads...\n");
for(int i = 0; i < ncpu; i++) {
thread_args[i].done = true;
}
log_output(__FILE_NAME__, "Waiting for threads to finish last work items\n");
for(int i = 0; i < ncpu; i++) {
void *v;
pthread_join(thread_args[i].tid, &v);
}
uint64_t nops = estimate_nops(ncpu);
results->ops_done = nops;
results->found = found;
}
uint64_t multicore_method(uint64_t secret, bool *found, uint64_t h_start, uint64_t h_end) {
// 1) Get CPU count
long ncpu = multicore_get_ncpus();
method_results_t res = {0};
if(false == multicore_launch_threads(ncpu, secret, h_start, h_end)) {
log_error(__FILE_NAME__, "Could not launch worker threads!");
exit(-1);
}
if(0 == ncpu) {
log_error(__FILE_NAME__, "No CPUs available; aborting method\n");
res.found = false;
res.ops_done = 0;
} else {
// while loop checking that something was found
// and which thread found it
log_output(__FILE_NAME__, "Waiting for worker threads\n");
multicore_wait_for_threads(ncpu, &res);
}
*found = res.found;
return res.ops_done;
}