-
Notifications
You must be signed in to change notification settings - Fork 21
/
utils.h
465 lines (416 loc) · 11.6 KB
/
utils.h
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
#ifndef _UTILS_H_INCLUDED_
#define _UTILS_H_INCLUDED_
//some utility functions
//#define _GNU_SOURCE
//#define USE_MUTEX_LOCKS
//#define ADD_PADDING
//#define OPTERON
//#define OPTERON_OPTIMIZE
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sched.h>
#include <inttypes.h>
#include <sys/time.h>
#include <unistd.h>
#include <malloc.h>
#ifdef __sparc__
# include <sys/types.h>
# include <sys/processor.h>
# include <sys/procset.h>
#elif defined(__tile__)
# include <arch/atomic.h>
# include <arch/cycle.h>
# include <tmc/cpus.h>
# include <tmc/task.h>
# include <tmc/spin.h>
# include <sched.h>
#else
# include <emmintrin.h>
# include <xmmintrin.h>
# ifdef NUMA
# include <numa.h>
# endif
#endif
#include <pthread.h>
#include "sspfd.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ALIGNED(N) __attribute__ ((aligned (N)))
#ifdef __sparc__
# define PAUSE asm volatile("rd %%ccr, %%g0\n\t" \
::: "memory")
#elif defined(__tile__)
# define PAUSE cycle_relax()
#else
# define PAUSE _mm_pause()
#endif
static inline void
pause_rep(uint32_t num_reps)
{
uint32_t i;
for (i = 0; i < num_reps; i++)
{
PAUSE;
/* PAUSE; */
/* asm volatile ("NOP"); */
}
}
static inline void
nop_rep(uint32_t num_reps)
{
uint32_t i;
for (i = 0; i < num_reps; i++)
{
asm volatile ("NOP");
}
}
//machine dependent parameters
#ifdef __sparc__
# define NUMBER_OF_SOCKETS 8
# define CORES_PER_SOCKET 8
# define CACHE_LINE_SIZE 64
# define NOP_DURATION 9
//mapping from threads to cores on the niagara
# define ALTERNATE_SOCKETS
# ifdef ALTERNATE_SOCKETS
static uint8_t __attribute__ ((unused)) the_cores[] = {
0, 8, 16, 24, 32, 40, 48, 56,
1, 9, 17, 25, 33, 41, 49, 57,
2, 10, 18, 26, 34, 42, 50, 58,
3, 11, 19, 27, 35, 43, 51, 59,
4, 12, 20, 28, 36, 44, 52, 60,
5, 13, 21, 29, 37, 45, 53, 61,
6, 14, 22, 30, 38, 46, 54, 62,
7, 15, 23, 31, 39, 47, 55, 63
};
static uint8_t __attribute__ ((unused)) the_sockets[] = {
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7
};
//static uint8_t __attribute__ ((unused)) the_sockets[] = {
// 0, 1, 2, 3, 4, 5, 6, 7,
// 0, 1, 2, 3, 4, 5, 6, 7,
// 0, 1, 2, 3, 4, 5, 6, 7,
// 0, 1, 2, 3, 4, 5, 6, 7,
// 0, 1, 2, 3, 4, 4, 5, 7,
// 0, 1, 2, 3, 4, 5, 6, 7,
// 0, 1, 2, 3, 4, 5, 6, 7,
// 0, 1, 2, 3, 4, 5, 6, 7
//};
# else
static uint8_t __attribute__ ((unused)) the_cores[] = {
0, 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
};
static uint8_t __attribute__ ((unused)) the_sockets[] = {
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7
};
# endif
#elif defined __tile__
# define NUMBER_OF_SOCKETS 1
# define CORES_PER_SOCKET 36
# define CACHE_LINE_SIZE 64
# define NOP_DURATION 4
static uint8_t __attribute__ ((unused)) the_cores[] = {
0, 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
};
#elif defined(OPTERON)
# define NUMBER_OF_SOCKETS 8
# define CORES_PER_SOCKET 6
# define CACHE_LINE_SIZE 64
# define NOP_DURATION 2
static uint8_t __attribute__ ((unused)) the_cores[] = {
0, 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
};
#elif defined(COREi7) | defined(DEFAULT)
# define NUMBER_OF_SOCKETS 1
# define CORES_PER_SOCKET 8
# define CACHE_LINE_SIZE 64
# define NOP_DURATION 2
static uint8_t __attribute__ ((unused)) the_cores[] = {
0, 1, 2, 3, 4, 5, 6, 7
};
#elif defined(XEON2)
# define NUMBER_OF_SOCKETS 2
# define CORES_PER_SOCKET 20
# define CACHE_LINE_SIZE 64
# define NOP_DURATION 2
# define USE_HYPERTRHEADS 1
# if USE_HYPERTRHEADS == 0
static uint8_t __attribute__ ((unused)) the_cores[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
};
# else
static uint8_t __attribute__ ((unused)) the_cores[] = {
0, 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,
};
# endif
#elif defined(XEON)
# define NUMBER_OF_SOCKETS 8
# define CORES_PER_SOCKET 10
# define CACHE_LINE_SIZE 64
# define NOP_DURATION 1
static uint8_t __attribute__ ((unused)) the_cores[] = {
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,
0, 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
};
static uint8_t __attribute__ ((unused)) the_sockets[] =
{
4, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};
#endif
/* PLATFORM specific -------------------------------------------------------------------- */
#if defined(__x86_64__)
# define PREFETCHW(x) asm volatile("prefetchw %0" :: "m" (*(unsigned long *)x))
# define PREFETCH(x) asm volatile("prefetch %0" :: "m" (*(unsigned long *)x))
#elif defined(__sparc__)
# define PREFETCHW(x)
#elif defined(XEON)
# define PREFETCHW(x)
#else
# define PREFETCHW(x)
#endif
//debugging functions
#ifdef DEBUG
# define DPRINT(args...) fprintf(stderr,args);
# define DDPRINT(fmt, args...) printf("%s:%s:%d: "fmt, __FILE__, __FUNCTION__, __LINE__, args)
#else
# define DPRINT(...)
# define DDPRINT(fmt, ...)
#endif
static inline int get_cluster(int thread_id) {
#ifdef __solaris__
if (thread_id>64){
perror("Thread id too high");
return 0;
}
return thread_id/CORES_PER_SOCKET;
// return the_sockets[thread_id];
#elif XEON
if (thread_id>=80){
perror("Thread id too high");
return 0;
}
return the_sockets[thread_id];
#elif defined(__tile__)
return 0;
#else
return thread_id/CORES_PER_SOCKET;
#endif
}
typedef uint64_t ticks;
static inline double wtime(void)
{
struct timeval t;
gettimeofday(&t,NULL);
return (double)t.tv_sec + ((double)t.tv_usec)/1000000.0;
}
static inline void set_cpu(int cpu) {
#if !defined(NO_SET_CPU)
# ifdef __sparc__
processor_bind(P_LWPID,P_MYID, cpu, NULL);
# elif defined(__tile__)
if (cpu>=tmc_cpus_grid_total()) {
perror("Thread id too high");
}
// cput_set_t cpus;
if (tmc_cpus_set_my_cpu(cpu)<0) {
tmc_task_die("tmc_cpus_set_my_cpu() failed.");
}
# else
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu, &mask);
/* numa_set_preferred(get_cluster(cpu)); */
pthread_t thread = pthread_self();
if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
fprintf(stderr, "Error setting thread affinity\n");
}
# endif
#endif
}
/* #if defined(__i386__) */
/* static inline ticks getticks(void) { */
/* ticks ret; */
/* __asm__ __volatile__("rdtsc" : "=A" (ret)); */
/* return ret; */
/* } */
/* #elif defined(__x86_64__) */
/* static inline ticks getticks(void) */
/* { */
/* unsigned hi, lo; */
/* __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); */
/* return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); */
/* } */
/* #elif defined(__sparc__) */
/* static inline ticks getticks(){ */
/* ticks ret; */
/* __asm__ __volatile__ ("rd %%tick, %0" : "=r" (ret) : "0" (ret)); */
/* return ret; */
/* } */
/* #elif defined(__tile__) */
/* static inline ticks getticks(){ */
/* return get_cycle_count(); */
/* } */
/* #endif */
static inline void cdelay(ticks cycles){
ticks __ts_end = getticks() + (ticks) cycles;
while (getticks() < __ts_end);
}
static inline void cpause(ticks cycles){
#if defined(XEON)
cycles >>= 3;
ticks i;
for (i=0;i<cycles;i++) {
_mm_pause();
}
#else
ticks i;
for (i=0;i<cycles;i++) {
__asm__ __volatile__("nop");
}
#endif
}
static inline void udelay(unsigned int micros)
{
double __ts_end = wtime() + ((double) micros / 1000000);
while (wtime() < __ts_end);
}
//getticks needs to have a correction because the call itself takes a
//significant number of cycles and skewes the measurement
static inline ticks getticks_correction_calc() {
#define GETTICKS_CALC_REPS 5000000
ticks t_dur = 0;
uint32_t i;
for (i = 0; i < GETTICKS_CALC_REPS; i++) {
ticks t_start = getticks();
ticks t_end = getticks();
t_dur += t_end - t_start;
}
// printf("corr in float %f\n", (t_dur / (double) GETTICKS_CALC_REPS));
ticks getticks_correction = (ticks)(t_dur / (double) GETTICKS_CALC_REPS);
return getticks_correction;
}
static inline ticks get_noop_duration() {
#define NOOP_CALC_REPS 1000000
ticks noop_dur = 0;
uint32_t i;
ticks corr = getticks_correction_calc();
ticks start;
ticks end;
start = getticks();
for (i=0;i<NOOP_CALC_REPS;i++) {
__asm__ __volatile__("nop");
}
end = getticks();
noop_dur = (ticks)((end-start-corr)/(double)NOOP_CALC_REPS);
return noop_dur;
}
/// Round up to next higher power of 2 (return x if it's already a power
/// of 2) for 32-bit numbers
static inline uint64_t pow2roundup (uint64_t x){
if (x==0) return 1;
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
return x+1;
}
#define my_random xorshf96
/*
* Returns a pseudo-random value in [1;range).
* Depending on the symbolic constant RAND_MAX>=32767 defined in stdlib.h,
* the granularity of rand() could be lower-bounded by the 32767^th which might
* be too high for given values of range and initial.
*/
static inline long rand_range(long r) {
int m = RAND_MAX;
long d, v = 0;
do {
d = (m > r ? r : m);
v += 1 + (long) (d * ((double) rand() / ((double) (m) + 1.0)));
r -= m;
} while (r > 0);
return v;
}
//fast but weak random number generator for the sparc machine
static inline uint32_t fast_rand() {
return ((getticks()&4294967295)>>4);
}
static inline unsigned long* seed_rand() {
unsigned long* seeds;
seeds = (unsigned long*) memalign(64, 64);
seeds[0] = getticks() % 0x992123E456789LL;
seeds[1] = getticks() % 0x22136D2436069LL;
seeds[2] = getticks() % 0x2119F521288629LL;
return seeds;
}
//Marsaglia's xorshf generator
static inline unsigned long xorshf96(unsigned long* x, unsigned long* y, unsigned long* z) { //period 2^96-1
unsigned long t;
(*x) ^= (*x) << 16;
(*x) ^= (*x) >> 5;
(*x) ^= (*x) << 1;
t = *x;
(*x) = *y;
(*y) = *z;
(*z) = t ^ (*x) ^ (*y);
return *z;
}
#ifdef __cplusplus
}
#endif
#endif