forked from pabuhr/concurrent-locking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHarness.c
701 lines (603 loc) · 24 KB
/
Harness.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
// The test harness creates N pthread worker-threads, and then blocks for a fixed period of time, T, after which a
// global stop-flag is set to indicate an experiment is over. The N threads repeatedly attempt entry into a
// self-checking critical-section until the stop flag is set. During the T seconds, each thread counts the number of
// times it enters the critical section. The higher the aggregate count, the better an algorithm, as it is able to
// process more requests for the critical section per unit time. When the stop flag is set, a worker thread stops
// entering the critical section, and atomically adds it subtotal entry-counter to a global total entry-counter. When
// the driver unblocks after T seconds, it busy waits until all threads have noticed the stop flag and added their
// subtotal to the global counter, which is then stored. Five identical experiments are performed, each lasting T
// seconds. The median value of the five results is printed.
#ifndef __cplusplus
#define _GNU_SOURCE // See feature_test_macros(7)
#endif // __cplusplus
#include <stdio.h>
#include <stdlib.h> // abort, exit, atoi, rand, qsort
#include <math.h> // sqrt
#include <assert.h>
#include <pthread.h>
#include <errno.h> // errno
#include <stdint.h> // uintptr_t, UINTPTR_MAX
#include <sys/time.h>
#include <poll.h> // poll
#include <malloc.h> // memalign
#include <unistd.h> // getpid
#include <limits.h> // ULONG_MAX
#ifdef CFMT // output comma format
#include <locale.h>
#endif // CFMT
#ifdef __ARM_ARCH
#define WO( stmt ) stmt
#else
#define WO( stmt )
#endif
#if __GNUC__ >= 7 // valid GNU compiler diagnostic ?
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough" // Mute g++
#endif // __GNUC__ >= 7
#define CACHE_ALIGN 128 // Intel recommendation
#define CALIGN __attribute__(( aligned (CACHE_ALIGN) ))
#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#ifdef FAST
// unlikely
#define FASTPATH(x) __builtin_expect(!!(x), 0)
#define SLOWPATH(x) __builtin_expect(!!(x), 1)
#else
// likely
#define FASTPATH(x) __builtin_expect(!!(x), 1)
#define SLOWPATH(x) __builtin_expect(!!(x), 0)
#endif // FASTPATH
// Architectural ST-LD barrier -- memory fences
// In theory these should be obviated by the C++11 std::atomic_thread_fence() primitives. Another option is the gcc
// _sync* primitives, but those are deprecated in favor of the new C++11 operators. Unfortunately the current
// implementations of both __sync and C++11 atomic operators is sub-optimal. On x86 these facilities use MFENCE, which
// is a poor choice.
#ifdef ATOMIC
#define Fence()
#else
#if defined(__x86_64)
//#define Fence() __asm__ __volatile__ ( "mfence" )
#define Fence() __asm__ __volatile__ ( "lock; addq $0,(%%rsp);" ::: "cc" )
#elif defined(__i386)
#define Fence() __asm__ __volatile__ ( "lock; addl $0,(%%esp);" ::: "cc" )
#elif defined(__ARM_ARCH)
#define Fence() __asm__ __volatile__ ( "DMB ISH" ::: )
#else
#error unsupported architecture
#endif
#endif // ATOMIC
// pause to prevent excess processor bus usage
#if defined( __i386 ) || defined( __x86_64 )
#define Pause() __asm__ __volatile__ ( "pause" : : : )
// #define Delay() for ( int i = random() & (512 - 1); i < 1000; i += 1 ) Pause()
// #define Delay() { for ( uint32_t r = (ThreadLocalRandom() & ( (1<<16) - 1)) + ((1<<16) - 1); r >= 8; r -= 8 ) { Pause(); Pause(); Pause(); Pause(); Pause(); Pause(); Pause(); Pause(); } }
// #define Delay() { for ( uint32_t r = 1 << (((ThreadLocalRandom() & (1<<4)) - 1) + 3); r >= 8; r -= 8 ) { Pause(); Pause(); Pause(); Pause(); Pause(); Pause(); Pause(); Pause(); } }
#elif defined(__ARM_ARCH)
#define Pause() __asm__ __volatile__ ( "YIELD" : : : )
#else
#error unsupported architecture
#endif
//------------------------------------------------------------------------------
typedef uintptr_t TYPE; // addressable word-size
typedef volatile TYPE VTYPE; // volatile addressable word-size
typedef uint32_t RTYPE; // unsigned 32-bit integer
#if __WORDSIZE == 64
typedef uint32_t HALFSIZE;
typedef volatile uint32_t VHALFSIZE;
typedef uint64_t WHOLESIZE;
typedef volatile uint64_t VWHOLESIZE;
#else
typedef uint16_t HALFSIZE;
typedef volatile uint16_t VHALFSIZE;
typedef uint32_t WHOLESIZE;
typedef volatile uint32_t VWHOLESIZE;
#endif // __WORDSIZE == 64
#ifdef ATOMIC
#define VTYPE _Atomic TYPE
#define VWHOLESIZE _Atomic WHOLESIZE
#endif // ATOMIC
//------------------------------------------------------------------------------
static RTYPE Mix64( uint64_t v ) {
static const int64_t Mix64K = 0xdaba0b6eb09322e3LL;
v = (v ^ (v >> 32)) * Mix64K;
v = (v ^ (v >> 32)) * Mix64K;
return v ^ (v >> 32);
} // Mix64
// Marsaglia shift-XOR PRNG with thread-local state
// Period is 4G-1
// 0 is absorbing and must be avoided
// Low-order bits are not particularly random
// Self-seeding based on address of thread-local state
// Not reproducible run-to-run because of user-mode address space randomization
// Pipelined to allow OoO overlap with reduced dependencies
// Critically, return the current value, and compute and store the next value
// Optionally, sequester R on its own cache line to avoid false sharing
// but on linux __thread "initial-exec" TLS variables are already segregated.
static __attribute__(( unused )) RTYPE ThreadLocalRandom() {
static __thread RTYPE R = 0;
if ( R == 0 ) R = Mix64( (uint64_t)(uintptr_t)&R ) | 1;
RTYPE v = R;
RTYPE n = v;
n ^= n << 6;
n ^= n >> 21;
n ^= n << 7;
R = n;
return v;
} // ThreadLocalRandom
//------------------------------------------------------------------------------
static __attribute__(( unused )) inline TYPE cycleUp( TYPE v, TYPE n ) { return ( ((v) >= (n - 1)) ? 0 : (v + 1) ); }
static __attribute__(( unused )) inline TYPE cycleDown( TYPE v, TYPE n ) { return ( ((v) <= 0) ? (n - 1) : (v - 1) ); }
#if defined( __GNUC__ ) // GNU gcc compiler ?
// O(1) polymorphic integer log2, using clz, which returns the number of leading 0-bits, starting at the most
// significant bit (single instruction on x86)
#define Log2( n ) ( sizeof(n) * __CHAR_BIT__ - 1 - ( \
( sizeof(n) == 4 ) ? __builtin_clz( n ) : \
( sizeof(n) == 8 ) ? __builtin_clzl( n ) : \
( sizeof(n) == 16 ) ? __builtin_clzll( n ) : \
-1 ) )
#else
static __attribute__(( unused )) int Log2( int n ) { // fallback integer log2( n )
return n > 1 ? 1 + Log2( n / 2 ) : n == 1 ? 0 : -1;
}
#endif // __GNUC__
static __attribute__(( unused )) inline int Clog2( int n ) { // integer ceil( log2( n ) )
if ( n <= 0 ) return -1;
int ln = Log2( n );
return ln + ( (n - (1 << ln)) != 0 ); // check for any 1 bits to the right of the most significant bit
}
//------------------------------------------------------------------------------
#ifdef CNT
struct CALIGN cnts {
uint64_t cnts[CNT + 1];
};
static struct cnts ** counters CALIGN;
#endif // CNT
//------------------------------------------------------------------------------
// Do not use VTYPE because -DATOMIC changes it.
static _Atomic(TYPE) stop CALIGN = 0;
static _Atomic(TYPE) Arrived CALIGN = 0;
static uintptr_t N CALIGN, Threads CALIGN, Time CALIGN;
static intptr_t Degree CALIGN = -1;
enum { RUNS = 5 };
volatile int Run CALIGN = 0;
//------------------------------------------------------------------------------
// memory allocator to align or not align storage
#define Allocator( size ) memalign( CACHE_ALIGN, (size) )
//------------------------------------------------------------------------------
enum { CSTimes = 50 };
#ifdef NONCS
static inline void NonCriticalSection( const TYPE id ) {
for ( volatile int i = 0; i < 550; i += 1 ); // Spinlock: 200
// Fence(); // optional
// for ( int i = 1; i <= CSTimes; i += 1 ) { // delay
// if ( id == Threads ) { // mutual exclusion violation ?
// printf( "Interference Id:%zu\n", id );
// } // if
// } // for
} // NonCriticalSection
#endif // NONCS
static TYPE HPAD1 CALIGN __attribute__(( unused )); // protect further false sharing
static volatile RTYPE randomChecksum CALIGN = 0;
static volatile RTYPE sumOfThreadChecksums CALIGN = 0;
// Do not use VTYPE because -DATOMIC changes it.
static volatile TYPE CurrTid CALIGN = ULONG_MAX; // shared, current thread id in critical section
static TYPE HPAD2 CALIGN __attribute__(( unused )); // protect further false sharing
//static int Identity(int v) { __asm__ __volatile__ (";" : "+r" (v)); return v; }
static inline RTYPE CriticalSection( const TYPE tid ) {
#ifdef CNT
if ( UNLIKELY( CurrTid == tid ) ) counters[Run][tid].cnts[0] += 1; // consecutive entries in the critical section
#endif // CNT
CurrTid = tid;
// If the critical section is violated, the additions are corrupted because of the load/store race unless there is
// perfect interleaving. Note, the load, delay, store to increase the chance of detecting a violation.
RTYPE randomNumber = ThreadLocalRandom(); // belt and
volatile RTYPE copy = randomChecksum;
for ( volatile int delay = 0; delay < CSTimes; delay += 1 ) {} // delay
randomChecksum = copy + randomNumber;
// The assignment to CurrTid above can delay in a store buffer, that is, committed but not pushed into coherent
// space. Hence, the load below fetchs the value from the store buffer via look aside instead of the coherent
// version. If this scenario occurs for multiple threads in the CS, these threads do not detect the violation
// because their copy of CurrTid in the store buffer is unchanged.
if ( CurrTid != tid ) { // suspenders
printf( "Interference Id:%zu\n", tid );
abort();
} // if
return randomNumber;
} // CriticalSection
//------------------------------------------------------------------------------
#ifdef FAST
enum { MaxStartPoints = 64 };
static unsigned int NoStartPoints CALIGN;
static uint64_t * Startpoints CALIGN;
// To ensure the single thread exercises all aspects of an algorithm, it is assigned different start-points on each
// access to the critical section by randomly changing its thread id. The randomness is accomplished using
// approximately 64 pseudo-random thread-ids, where 64 is divided by N to get R repetitions, e.g., for N = 5, R = 64 / 5
// = 12. Each of the 12 repetition is filled with 5 random value in the range, 0..N-1, without replacement, e.g., 0 3 4
// 1 2. There are no consecutive thread-ids within a repetition but there may be between repetition. The thread cycles
// through this array of ids during an experiment.
void startpoints() {
Startpoints[0] = N;
for ( unsigned int i = 0; i < NoStartPoints; i += N ) {
for ( unsigned int j = i; j < i + N; j += 1 ) {
unsigned int v;
L: v = rand() % N;
unsigned int k;
for ( k = i; k < j; k += 1 ) {
if ( Startpoints[k] == v ) goto L;
} // for
// Unknown performance drop caused by following assert, use -DNDEBUG for experiments
assert( k < NoStartPoints );
Startpoints[k] = v;
} // for
} // for
#if 0
printf( "N %jd NoStartPoints %d ", N, NoStartPoints );
for ( unsigned int i = 0; i < NoStartPoints; i += 1 ) {
printf( "%d ", Startpoints[i] );
} // for
printf( "\n" );
#endif // 0
} // startpoints
static inline unsigned int startpoint( unsigned int pos ) {
return Startpoints[pos];
// return rand() % N;
} // startpoint
#endif // FAST
//------------------------------------------------------------------------------
// Vary concurrency level to help detect exclusion failure and progress-liveness bugs in lock algorithms and
// implementations. In many cases lock bugs do not ever manifest in steady-state, so varying the concurrency level
// randomly every 10 msecs is usually able to perturb the system to "shake out" more lock bugs.
//
// All threads are explicitly and intentionally quiesced while changing concurrency levels to increase the frequency at
// which the lock shifts between contended and uncontended states. Specifically, concurrency shifts from M to 0 to N
// instead of from M to N.
//
// We expect "Threads" to remain stable - Should be a stationary field. When the barrier is operating BVO VStress != 0,
// Threads serves as a bound on concurrency. The actual concurrency level at any given time will be in [1,Threads].
// Arrive() respects the Halt flag.
#ifdef STRESSINTERVAL
static int StressInterval CALIGN = STRESSINTERVAL; // 500 is good
static volatile int BarHalt CALIGN = 0;
static int __attribute__((noinline)) PollBarrier() {
if ( BarHalt == 0 ) return 0;
// singleton barrier instance state
static volatile int Ticket = 0;
static volatile int Grant = 0;
static volatile int Gate = 0;
static volatile int nrun = 0;
static const int Verbose = 1;
static int ConcurrencyLevel = 0;
// We have distinct non-overlapping arrival and draining/departure phases
// Lead threads waits inside CS for quorum
// Follower threads wait at entry to CS on ticket lock
// XXX ASSERT (Threads > 0);
int t = __sync_fetch_and_add (&Ticket, 1);
while ( Grant != t ) Pause();
if ( Gate == 0 ) {
// Wait for full quorum
while ( (Ticket - t) != Threads ) Pause();
// Compute new/next concurrency level - cohort
// Consider biasing PRNG to favor 1 to more frequently alternate contended
// and uncontended modes.
// Release a subset of the captured threads
// We expect the formation of the subsets to be effectively random,
// but if that's not the case we can use per-thread flags and explicitly
// select a random subset for the next epoch.
if ( (rand() % 10) == 0 ) {
Gate = 1;
} else {
Gate = (rand() % Threads) + 1;
}
ConcurrencyLevel = Gate;
if ( Verbose ) printf ("L%d", Gate);
// XXX ASSERT (Gate > 0);
// XXX ASSERT (BarHalt != 0);
BarHalt = 0;
nrun = 0;
} // if
// Consider : shift Verbose printing to after incrementing Grant
if ( Verbose ) {
int k = __sync_fetch_and_add( &nrun, 1 );
if ( k == (ConcurrencyLevel-1) ) printf( "; " );
if ( k >= ConcurrencyLevel ) printf( "?" );
} // if
Gate -= 1;
// Need ST-ST barrier here
// Release ticket lock
Grant += 1;
// Consider a randomized delay here ...
return 0;
} // PollBarrier
#endif // STRESSINTERVAL
//------------------------------------------------------------------------------
void affinity( pthread_t pthreadid, unsigned int tid ) {
// There are many ways to assign threads to processors: cores, chips, etc.
// On the AMD, we find starting at core 32 and sequential assignment is sufficient.
// Below are alternative approaches.
#if defined( __linux ) && defined( PIN )
#if 1
#if defined( algol )
enum { OFFSETSOCK = 1 /* 0 origin */, SOCKETS = 2, CORES = 48, HYPER = 1 };
#elif defined( nasus )
enum { OFFSETSOCK = 1 /* 0 origin */, SOCKETS = 2, CORES = 64, HYPER = 1 };
#elif defined( jax )
enum { OFFSETSOCK = 1 /* 0 origin */, SOCKETS = 4, CORES = 24, HYPER = 2 /* wrap on socket */ };
#elif defined( pyke )
enum { OFFSETSOCK = 1 /* 0 origin */, SOCKETS = 2, CORES = 24, HYPER = 2 /* wrap on socket */ };
#elif defined( cfapi1 )
enum { OFFSETSOCK = 0 /* 0 origin */, SOCKETS = 1, CORES = 4, HYPER = 1 };
#else // default
enum { OFFSETSOCK = 2 /* 0 origin */, SOCKETS = 4, CORES = 16, HYPER = 1 };
#endif // HOSTS
int cpu = tid + ((tid < CORES) ? OFFSETSOCK * CORES : HYPER < 2 ? OFFSETSOCK * CORES : CORES * SOCKETS);
#endif // 0
#if 0
// 4x8x2 : 4 sockets, 8 cores per socket, 2 hyperthreads per core
int cpu = (tid & 0x30) | ((tid & 1) << 3) | ((tid & 0xE) >> 1) + 32;
#endif // 0
//printf( "%d\n", cpu );
cpu_set_t mask;
CPU_ZERO( &mask );
CPU_SET( cpu, &mask );
int rc = pthread_setaffinity_np( pthreadid, sizeof(cpu_set_t), &mask );
if ( rc != 0 ) {
errno = rc;
perror( "***ERROR*** setaffinity failure" );
abort();
} // if
#endif // linux && PIN
} // affinity
//------------------------------------------------------------------------------
static uint64_t ** entries CALIGN; // holds CS entry results for each threads for all runs
#define xstr(s) str(s)
#define str(s) #s
#ifdef __cplusplus
#include xstr(Algorithm.cc) // include software algorithm for testing
#else
#include xstr(Algorithm.c) // include software algorithm for testing
#endif // __cplusplus
//------------------------------------------------------------------------------
static __attribute__(( unused )) void shuffle( unsigned int set[], const int size ) {
unsigned int p1, p2, temp;
for ( int i = 0; i < 200; i +=1 ) { // shuffle array S times
p1 = rand() % size;
p2 = rand() % size;
temp = set[p1];
set[p1] = set[p2];
set[p2] = temp;
} // for
} // shuffle
//------------------------------------------------------------------------------
#define median(a) ((RUNS & 1) == 0 ? (a[RUNS/2-1] + a[RUNS/2]) / 2 : a[RUNS/2] )
static int compare( const void * p1, const void * p2 ) {
size_t i = *((size_t *)p1);
size_t j = *((size_t *)p2);
return i > j ? 1 : i < j ? -1 : 0;
} // compare
//------------------------------------------------------------------------------
int main( int argc, char * argv[] ) {
N = 8; // defaults
Time = 10; // seconds
switch ( argc ) {
case 4:
Degree = atoi( argv[3] );
if ( Degree < 2 ) goto USAGE;
case 3:
Time = atoi( argv[2] );
N = atoi( argv[1] );
if ( Time < 1 || N < 1 ) goto USAGE;
break;
USAGE:
default:
printf( "Usage: %s [ threads (> 0) %ju ] [ experiment duration (> 0, seconds) %ju ] [ Zhang D-ary (> 1) %jd ]\n",
argv[0], N, Time, Degree );
exit( EXIT_FAILURE );
} // switch
#ifdef CFMT
#define QUOTE "'13"
setlocale( LC_NUMERIC, "en_US.UTF-8" );
if ( N == 1 ) { // title
printf( "%s"
#ifdef __cplusplus
".cc"
#else
".c"
#endif // __cplusplus
#ifdef ATOMIC
" ATOMIC"
#endif // ATOMIC
#ifdef FAST
" FAST"
#endif // FAST
, xstr(Algorithm) );
if ( Degree != -1 ) printf( " %jd-ary", Degree ); // Zhang only
printf( "\n N T CS Entries AVG STD RSTD CAVG SMALLS\n" );
} // if
#else
#define QUOTE ""
#endif // CFMT
printf( "%3ju %3jd ", N, Time );
#ifdef FAST
assert( N <= MaxStartPoints );
Threads = 1; // fast test, Threads=1, N=1..32
NoStartPoints = MaxStartPoints / N * N; // floor( MaxStartPoints / N )
Startpoints = Allocator( sizeof(typeof(Startpoints[0])) * NoStartPoints );
startpoints( N );
#else
Threads = N; // allow testing of T < N
#endif // FAST
entries = (typeof(entries[0]) *)malloc( sizeof(typeof(entries[0])) * RUNS );
#ifdef CNT
counters = (typeof(counters[0]) *)malloc( sizeof(typeof(counters[0])) * RUNS );
#endif // CNT
for ( int r = 0; r < RUNS; r += 1 ) {
entries[r] = (typeof(entries[0][0]) *)Allocator( sizeof(typeof(entries[0][0])) * Threads );
#ifdef CNT
#ifdef FAST
counters[r] = (typeof(counters[0][0]) *)Allocator( sizeof(typeof(counters[0][0])) * N );
#else
counters[r] = (typeof(counters[0][0]) *)Allocator( sizeof(typeof(counters[0][0])) * Threads );
#endif // FAST
#endif // CNT
} // for
#ifdef CNT
//#ifdef FAST
// For FAST experiments, there is only thread but it changes its thread id to visit all the start points. Therefore,
// all the counters for each id must be initialized and summed at the end.
for ( int r = 0; r < RUNS; r += 1 ) {
for ( uintptr_t id = 0; id < N; id += 1 ) {
for ( unsigned int i = 0; i < CNT + 1; i += 1 ) { // reset for each run
counters[r][id].cnts[i] = 0;
} // for
} // for
} // for
//#endif // FAST
#endif // CNT
unsigned int set[Threads];
for ( typeof(Threads) i = 0; i < Threads; i += 1 ) set[ i ] = i;
// srand( getpid() );
// shuffle( set, Threads ); // randomize thread ids
#ifdef DEBUG
printf( "\nthread set: " );
for ( uintptr_t i = 0; i < Threads; i += 1 ) printf( "%u ", set[ i ] );
#endif // DEBUG
ctor(); // global algorithm constructor
pthread_t workers[Threads];
for ( typeof(Threads) tid = 0; tid < Threads; tid += 1 ) { // start workers
int rc = pthread_create( &workers[tid], NULL, Worker, (void *)(size_t)set[tid] );
if ( rc != 0 ) {
errno = rc;
perror( "***ERROR*** pthread create" );
abort();
} // if
affinity( workers[tid], tid );
} // for
for ( ; Run < RUNS; ) { // global variable
// threads start first experiment immediately
sleep( Time ); // delay for experiment duration
stop = 1; // stop threads
while ( Arrived != Threads ) Pause(); // all threads stopped ?
if ( randomChecksum != sumOfThreadChecksums ) {
printf( "Interference run %d randomChecksum %u sumOfThreadChecksums %u\n", Run, randomChecksum, sumOfThreadChecksums );
abort();
} // if
randomChecksum = sumOfThreadChecksums = 0;
CurrTid = ULONG_MAX; // reset for next run
Run += 1;
stop = 0; // start threads
while ( Arrived != 0 ) Pause(); // all threads started ?
} // for
for ( typeof(Threads) tid = 0; tid < Threads; tid += 1 ) { // terminate workers
int rc = pthread_join( workers[tid], NULL );
if ( rc != 0 ) {
errno = rc;
perror( "***ERROR*** pthread join" );
abort();
} // if
} // for
dtor(); // global algorithm destructor
uint64_t totals[RUNS], sort[RUNS], smalls[RUNS];
#ifdef DEBUG
printf( "\nruns: " );
#endif // DEBUG
for ( int r = 0; r < RUNS; r += 1 ) {
smalls[r] = totals[r] = 0;
for ( typeof(Threads) tid = 0; tid < Threads; tid += 1 ) {
if ( entries[r][tid] <= 5 ) smalls[r] += 1; // only 5 entries in CS => small
totals[r] += entries[r][tid];
#ifdef DEBUG
printf( "%" QUOTE "ju ", entries[r][tid] );
#endif // DEBUG
} // for
sort[r] = totals[r];
} // for
qsort( sort, RUNS, sizeof(typeof(sort[0])), compare );
uint64_t med = median( sort );
double sum = 0.0;
for ( int r = 0; r < RUNS; r += 1 ) {
sum += totals[r];
} // for
double avg = sum / RUNS; // average
sum = 0.0;
for ( int r = 0; r < RUNS; r += 1 ) { // sum squared differences from average
double diff = totals[r] - avg;
sum += diff * diff;
} // for
double std = sqrt( sum / Threads ), percent = 15.0;
avg = avg == 0 ? 0.0 : std / avg * 100;
if ( avg > percent ) printf( "\nWarning relative standard deviation %.1f%% greater than %.0f%% over %d runs.\n", avg, percent, RUNS );
unsigned int posn; // run with median result
for ( posn = 0; posn < RUNS && totals[posn] != med; posn += 1 ); // assumes RUNS is odd
#ifdef DEBUG
printf( "\ntotals: " );
for ( int i = 0; i < RUNS; i += 1 ) { // print values
printf( "%" QUOTE "ju ", totals[i] );
} // for
printf( "\nsorted: " );
for ( int i = 0; i < RUNS; i += 1 ) { // print values
printf( "%" QUOTE "ju ", sort[i] );
} // for
printf( "\nmedian posn:%d\n", posn );
#endif // DEBUG
avg = (double)totals[posn] / Threads; // average
sum = 0.0;
for ( typeof(Threads) tid = 0; tid < Threads; tid += 1 ) { // sum squared differences from average
double diff = entries[posn][tid] - avg;
sum += diff * diff;
} // for
std = sqrt( sum / Threads );
printf( "%" QUOTE "ju", med ); // median round
printf( " %" QUOTE ".1f %" QUOTE ".1f %5.1f%%", avg, std, avg == 0 ? 0.0 : std / avg * 100 );
#if 0
#ifdef CNT
printf( "\n\n" );
uint64_t cntsum2;
for ( unsigned int r = 0; r < RUNS; r += 1 ) {
cntsum2 = 0;
#ifdef FAST
for ( uintptr_t tid = 0; tid < N; tid += 1 ) {
#else
for ( uintptr_t tid = 0; tid < Threads; tid += 1 ) {
cntsum2 += counters[r][tid].cnts[0];
#endif // FAST
printf( "%" QUOTE "ju ", counters[r][tid].cnts[0] );
} // for
printf( "= %lu\n", cntsum2 );
} // for
printf( "\n" );
#endif // CNT
#endif // 0
#ifdef CNT
// posn is the run containing the median result. Other runs are ignored.
uint64_t cntsum;
for ( unsigned int i = 0; i < CNT + 1; i += 1 ) {
cntsum = 0;
#ifdef FAST
for ( uintptr_t tid = 0; tid < N; tid += 1 ) {
#else
for ( uintptr_t tid = 0; tid < Threads; tid += 1 ) {
#endif // FAST
cntsum += counters[posn][tid].cnts[i];
} // for
printf( " %5.1f%%", (double)cntsum / (double)totals[posn] * 100.0 );
} // for
#endif // CNT
printf( " %" QUOTE "ju", smalls[posn] );
for ( int r = 0; r < RUNS; r += 1 ) {
free( entries[r] );
#ifdef CNT
free( counters[r] );
#endif // CNT
} // for
#ifdef CNT
free( counters );
#endif // CNT
free( entries );
#ifdef FAST
free( Startpoints );
#endif // FAST
printf( "\n" );
} // main
// Local Variables: //
// tab-width: 4 //
// compile-command: "gcc -Wall -std=gnu11 -O3 Harness.c -lpthread -lm" //
// End: //