-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdlthread.c
759 lines (546 loc) · 15.2 KB
/
dlthread.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/**
* @file dlthread.c
* @brief Functions for thread communicators.
* @author Dominique LaSalle <[email protected]>
* Copyright (c) 2014-2015, Dominique LaSalle
* @version 1
* @date 2014-12-05
*/
#ifndef DLTHREAD_C
#define DLTHREAD_C
#include "dlthread.h"
#include "time.h"
/******************************************************************************
* THREAD LOCAL STORAGE ********************************************************
******************************************************************************/
#if __STDC_VERSION__ >= 201101L
/* C11 */
#define THREAD_LOCAL thread_local
#elif defined(__GNUC__) || defined (__GNUG__)
/* GNU Compliant */
#define THREAD_LOCAL __thread
#elif defined(_MSC_VER)
/* Microsoft */
#define THREAD_LOCAL __declspec(thread)
#else
/* break things */
THIS WILL NOT WORK WITHOUT SOMETYPE OF THREAD LOCAL STORAGE
-- TRY A DIFFERENT COMPILER
#endif
/******************************************************************************
* OMP/PTHREAD SETUP ***********************************************************
******************************************************************************/
#ifndef CACHE_LINE_SIZE
#define CACHE_LINE_SIZE 64
#endif
#ifdef __DOMLIB_USE_PTHREADS
#include <pthread.h>
typedef pthread_barrier_t barrier_t;
/* perhaps change these to inline functions to eliminate possible warnings */
#define init_lock(lock) pthread_init_mutex(lock,NULL)
#define set_lock(lock) pthread_lock_mutex(lock)
#define unset_lock(lock) pthread_unlock_mutex(lock)
#define free_lock(lock) pthread_mutex_destroy(lock)
#define init_barrier(bar,nthreads) pthread_barrier_init(bar,NULL,nthreads)
#define free_barrier(bar) pthread_barrier_destroy(bar)
static inline int wait_barrier(
barrier_t * const bar,
size_t const myid)
{
return pthread_barrier_wait(bar);
}
#else
#include <omp.h>
static size_t const IDX_OFFSET = CACHE_LINE_SIZE / sizeof(int);
typedef struct barrier_t {
size_t nthreads;
int volatile * vec;
} barrier_t;
/* perhaps change these to inline functions to eliminate possible warnings */
#define init_lock(lock) omp_init_lock(lock)
#define set_lock(lock) omp_set_lock(lock)
#define unset_lock(lock) omp_unset_lock(lock)
#define free_lock(lock) omp_destroy_lock(lock)
static inline void spin_wait()
{
#ifdef __x86_64__
__asm__("pause;");
#else
// try to sleep for 100ns
struct timespec timin, timout;
timin.tv_sec = 0;
timin.tv_nsec = 100;
nanosleep(&timin, &timout);
#endif
}
static inline void init_barrier(
barrier_t * const bar,
size_t const nthreads)
{
bar->nthreads = nthreads;
bar->vec = calloc(nthreads,CACHE_LINE_SIZE);
}
static inline void free_barrier(
barrier_t * const bar)
{
size_t i;
int volatile * mybar;
mybar = bar->vec;
/* check to make sure bar is not in use */
for (i=0;i<bar->nthreads;++i) {
while (mybar[i*IDX_OFFSET] != 0) {
spin_wait();
}
}
/* keep the compiler from whining about freeing volatile memory */
dl_free((void*)bar->vec);
}
static inline void wait_barrier(
barrier_t * const bar,
size_t const myid)
{
int volatile * mybar;
size_t lc, rc;
DL_ASSERT(myid<bar->nthreads,"Invalid thread num %zu/%zu in barrier\n", \
myid,bar->nthreads);
if (bar->nthreads == 1) {
/* don't waste time for a single thread */
return;
} else if ((int)bar->nthreads == omp_get_num_threads()) {
/* all threads are in this barrier */
#pragma omp barrier
}
mybar = bar->vec;
lc = (myid*2)+1;
rc = (myid*2)+2;
if (lc >= bar->nthreads) {
lc = 0;
}
if (rc >= bar->nthreads) {
rc = 0;
}
/* wait for my children to reach the barrier */
while ((lc && mybar[lc*IDX_OFFSET] != 1) || \
(rc && mybar[rc*IDX_OFFSET] != 1)) {
spin_wait();
}
/* mark that I have reached the barrier */
mybar[myid*IDX_OFFSET] = 1;
/* wait for the root thread to be finished */
while (mybar[0] != 1) {
spin_wait();
}
/* barrier achieved -- now reset */
/* wait for my children to reset */
while ((lc && mybar[lc*IDX_OFFSET] == 1) || \
(rc && mybar[rc*IDX_OFFSET] == 1)) {
spin_wait();
}
/* reset myself */
mybar[myid*IDX_OFFSET] = 2;
/* wait for the root to reset */
while (mybar[0] == 1) {
spin_wait();
}
/* so that when a barrier is free'd, we can be sure all threads are done
* using the array */
mybar[myid*IDX_OFFSET] = 0;
/* make sure we protect against synchronization breaking optimiztions */
__asm__ volatile("" : : : "memory");
}
#endif
/******************************************************************************
* TYPES ***********************************************************************
******************************************************************************/
typedef struct comm_t {
int in_use;
size_t nthreads;
size_t bufsize;
size_t nlarray; /* lock array for each thread */
void * buffer;
dlthread_lock_t loc;
dlthread_lock_t ** larray;
barrier_t bar;
} comm_t;
typedef struct thread_arg_t {
size_t id;
void * ptr;
void (*funptr)(void*);
} thread_arg_t;
/******************************************************************************
* CONSTANTS *******************************************************************
******************************************************************************/
static size_t const __DEFAULT_BYTES_PER_THREAD = 128;
#define __MAX_NCOMMS 1024
/******************************************************************************
* VARIABLES *******************************************************************
******************************************************************************/
static dlthread_lock_t * ncomms_lock = NULL;
static dlthread_comm_t last_free_comm = 1; /* space for root comm */
static comm_t my_comms[__MAX_NCOMMS];
static THREAD_LOCAL size_t my_ids[__MAX_NCOMMS];
static THREAD_LOCAL size_t __local_buffer_size = 0;
static THREAD_LOCAL void * __local_buffer = NULL;
/******************************************************************************
* PRIVATE FUNCTIONS ***********************************************************
******************************************************************************/
static void __config_comm(
comm_t * const comm,
size_t const nthreads)
{
size_t maxthreads;
comm->in_use = 1;
comm->nthreads = nthreads;
comm->larray = NULL;
/* handle data types up to size 16 and round up to a power of two */
maxthreads = size_uppow2(nthreads);
comm->bufsize = size_uppow2((__DEFAULT_BYTES_PER_THREAD*maxthreads) + 4096);
comm->buffer = malloc(comm->bufsize);
init_lock(&comm->loc);
init_barrier(&comm->bar,nthreads);
}
static void __thread_start(
void * const ptr)
{
size_t myid;
thread_arg_t * arg;
arg = ptr;
myid = arg->id;
/* set my thread id for this communicator */
my_ids[DLTHREAD_COMM_ROOT] = myid;
arg->funptr(arg->ptr);
}
/******************************************************************************
* PUBLIC FUNCTIONS ************************************************************
******************************************************************************/
void dlthread_launch(
size_t const nthreads,
void (*funptr)(void*),
void * const ptr)
{
size_t myid, i;
comm_t * comm;
comm = my_comms+DLTHREAD_COMM_ROOT;
ncomms_lock = malloc(sizeof(dlthread_lock_t));
init_lock(ncomms_lock);
__config_comm(comm,nthreads);
#ifdef __DOMLIB_USE_PTHREADS
size_t i;
pthread_t * threads;
thread_arg_t * args;
threads = malloc(sizeof(pthread_t)*nthreads);
args = malloc(sizeof(thread_arg_t)*nthreads);
for (i=0;i<nthreads;++i) {
args[i].id = i;
args[i].ptr = ptr;
args[i].funptr = funptr;
pthread_create(threads+i,NULL,&__thread_start,args+i);
}
for (i=0;i<nthreads;++i) {
pthread_join(threads[i],NULL);
}
dl_free(threads);
dl_free(args);
#else
#pragma omp parallel num_threads(nthreads)
{
thread_arg_t arg;
arg.id = omp_get_thread_num();
arg.ptr = ptr;
arg.funptr = funptr;
__thread_start(&arg);
}
#endif
if (comm->larray) {
for (myid=0;myid<nthreads;++myid) {
/* destroy locks if they exist */
for (i=0;i<comm->nlarray;++i) {
free_lock(comm->larray[myid]+i);
}
dl_free(comm->larray[myid]);
}
dl_free(comm->larray);
}
dl_free(comm->buffer);
free_barrier(&(comm->bar));
free_lock(&(comm->loc));
comm->in_use = 0;
free_lock(ncomms_lock);
dl_free(ncomms_lock);
}
void dlthread_exclude(
dlthread_comm_t const comm_idx)
{
comm_t * gcomm;
if (comm_idx != DLTHREAD_COMM_SINGLE) {
gcomm = my_comms+comm_idx;
set_lock(&gcomm->loc);
}
}
void dlthread_unexclude(
dlthread_comm_t const comm_idx)
{
comm_t * gcomm;
if (comm_idx != DLTHREAD_COMM_SINGLE) {
gcomm = my_comms+comm_idx;
unset_lock(&gcomm->loc);
}
}
void dlthread_init_lock(
dlthread_lock_t * const lock)
{
init_lock(lock);
}
void dlthread_free_lock(
dlthread_lock_t * lock)
{
free_lock(lock);
}
void dlthread_set_lock(
dlthread_lock_t * const lock)
{
set_lock(lock);
}
void dlthread_unset_lock(
dlthread_lock_t * const lock)
{
unset_lock(lock);
}
void dlthread_init_locks(
size_t const lpt,
dlthread_comm_t const comm_idx)
{
size_t i, myid, nthreads;
comm_t * gcomm;
if (comm_idx != DLTHREAD_COMM_SINGLE) {
myid = dlthread_get_id(comm_idx);
nthreads = dlthread_get_nthreads(comm_idx);
gcomm = my_comms+comm_idx;
if (myid == 0) {
gcomm->larray = malloc(sizeof(dlthread_lock_t*)*nthreads);
gcomm->nlarray = lpt;
}
dlthread_barrier(comm_idx);
gcomm->larray[myid] = malloc(sizeof(dlthread_lock_t)*lpt);
for (i=0;i<lpt;++i) {
init_lock(gcomm->larray[myid]+i);
}
dlthread_barrier(comm_idx);
}
}
void dlthread_lock_index(
size_t const tid,
size_t const idx,
dlthread_comm_t const comm_idx)
{
size_t i;
comm_t * gcomm;
if (comm_idx != DLTHREAD_COMM_SINGLE) {
gcomm = my_comms+comm_idx;
i = idx % gcomm->nlarray;
set_lock(gcomm->larray[tid]+i);
}
}
void dlthread_unlock_index(
size_t const tid,
size_t const idx,
dlthread_comm_t const comm_idx)
{
size_t i;
comm_t * gcomm;
if (comm_idx != DLTHREAD_COMM_SINGLE) {
gcomm = my_comms+comm_idx;
i = idx % gcomm->nlarray;
unset_lock(gcomm->larray[tid]+i);
}
}
dlthread_comm_t dlthread_comm_split(
size_t group,
size_t ngroups,
dlthread_comm_t const comm_idx)
{
size_t i, lid, cidx, myid, nthreads;
size_t volatile * tid;
comm_t * lcomm;
dlthread_comm_t volatile * gcom;
dlthread_comm_t cid;
if (comm_idx != DLTHREAD_COMM_SINGLE) {
myid = dlthread_get_id(comm_idx);
nthreads = dlthread_get_nthreads(comm_idx);
DL_ASSERT(group < ngroups,"Invalid group %zu/%zu for thread %zu/%zu\n", \
group,ngroups,myid,nthreads);
gcom = dlthread_get_buffer((sizeof(dlthread_comm_t)*ngroups) + \
(sizeof(size_t)*nthreads),comm_idx);
tid = (size_t*)(gcom+ngroups);
if (myid == 0) {
/* number the new communicators */
set_lock(ncomms_lock);
for (i=0;i<ngroups;++i) {
/* find the next free communicator */
while (my_comms[last_free_comm].in_use == 1) {
++last_free_comm;
}
gcom[i] = last_free_comm++;
/* initialize my comm */
my_comms[gcom[i]].nthreads = 0;
my_comms[gcom[i]].in_use = 1;
}
unset_lock(ncomms_lock);
}
/* I use an alarming number of barriers here -- someday reduce this */
dlthread_barrier(comm_idx);
cid = gcom[group];
DL_ASSERT(cid < __MAX_NCOMMS,"Exceeded maximum number of communicators\n");
lcomm = my_comms+cid;
tid[myid] = group;
dlthread_barrier(comm_idx);
if (myid == 0) {
/* number the threads per communicator */
for (i=0;i<nthreads;++i) {
cidx = gcom[tid[i]];
tid[i] = my_comms[cidx].nthreads++;
}
}
dlthread_barrier(comm_idx);
lid = tid[myid];
if (lid == 0) {
/* root for each comm */
__config_comm(lcomm,lcomm->nthreads);
}
my_ids[cid] = lid;
dlthread_barrier(comm_idx);
dprintf("[%zu:%zu] new communicator %zu with %zu threads\n",myid,lid, \
(size_t)cid,lcomm->nthreads);
} else {
cid = DLTHREAD_COMM_SINGLE;
}
return cid;
}
void dlthread_comm_finalize(
dlthread_comm_t const comm_idx)
{
size_t i, myid;
comm_t * comm;
if (comm_idx != DLTHREAD_COMM_SINGLE) {
myid = dlthread_get_id(comm_idx);
dlthread_barrier(comm_idx);
comm = my_comms+comm_idx;
if (comm->larray) {
/* destroy locks if they exist */
for (i=0;i<comm->nlarray;++i) {
free_lock(comm->larray[myid]+i);
}
dl_free(comm->larray[myid]);
dlthread_barrier(comm_idx);
if (myid == 0) {
dl_free(comm->larray);
}
}
if (myid == 0) {
dl_free(comm->buffer);
free_barrier(&(comm->bar));
free_lock(&(comm->loc));
comm->in_use = 0;
set_lock(ncomms_lock);
if (comm_idx < last_free_comm) {
last_free_comm = comm_idx;
}
unset_lock(ncomms_lock);
}
} else {
/* clear this threads local buffer if it exists */
if (__local_buffer) {
dl_free(__local_buffer);
__local_buffer = NULL;
__local_buffer_size = 0;
}
}
}
size_t dlthread_get_id(
dlthread_comm_t const comm)
{
if (comm != DLTHREAD_COMM_SINGLE) {
return my_ids[comm];
} else {
return 0;
}
}
size_t dlthread_get_nthreads(
dlthread_comm_t const comm)
{
if (comm != DLTHREAD_COMM_SINGLE) {
return my_comms[comm].nthreads;
} else {
return 1;
}
}
void * dlthread_get_shmem(
size_t nbytes,
dlthread_comm_t const comm_idx)
{
size_t myid;
void * ptr;
comm_t * comm;
if (comm_idx != DLTHREAD_COMM_SINGLE) {
myid = dlthread_get_id(comm_idx);
comm = my_comms+comm_idx;
DL_ASSERT(comm->buffer != NULL,"Null buffer on communicator %zu\n", \
(size_t)comm_idx);
if (myid == 0) {
ptr = malloc(nbytes);
((void**)comm->buffer)[0] = ptr;
}
dlthread_barrier(comm_idx);
ptr = ((void**)comm->buffer)[0];
dlthread_barrier(comm_idx);
} else {
ptr = malloc(nbytes);
}
return ptr;
}
void dlthread_free_shmem(
void * ptr,
dlthread_comm_t const comm_idx)
{
size_t const myid = dlthread_get_id(comm_idx);
dlthread_barrier(comm_idx);
if (myid == 0) {
dl_free(ptr);
}
}
void dlthread_barrier(
dlthread_comm_t const comm_idx)
{
if (comm_idx != DLTHREAD_COMM_SINGLE) {
wait_barrier(&(my_comms[comm_idx].bar),dlthread_get_id(comm_idx));
}
}
void * dlthread_get_buffer(
size_t const n,
dlthread_comm_t const comm_idx)
{
void * buffer;
comm_t * comm;
size_t const myid = dlthread_get_id(comm_idx);
if (comm_idx != DLTHREAD_COMM_SINGLE) {
comm = my_comms+comm_idx;
if (comm->bufsize < n) {
dlthread_barrier(comm_idx);
if (myid == 0) {
dl_free(comm->buffer);
comm->buffer = malloc(n);
comm->bufsize = n;
}
dlthread_barrier(comm_idx);
}
buffer = comm->buffer;
} else {
if (__local_buffer_size < n) {
__local_buffer = realloc(__local_buffer,n);
__local_buffer_size = n;
}
buffer = __local_buffer;
}
return buffer;
}
#endif