forked from OdnetninI/Splash-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread.m4
267 lines (238 loc) · 8.35 KB
/
pthread.m4
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
m4_dnl Found on the web. Written by Bastiaan Stougie, October 2003
m4_dnl Modified by Alberto Ros and Christos Sakalis, 2015
m4_divert(-1)
m4_define(NEWPROC,) m4_dnl
m4_dnl Empty ROI markers
m4_define(SPLASH3_ROI_BEGIN, `')
m4_define(SPLASH3_ROI_END, `')
m4_define(SWARM_WORKER_ROI_BEGIN, `zsim_worker_roi_begin()')
m4_define(SWARM_WORKER_ROI_END, `zsim_worker_roi_end()')
m4_dnl Region markers
m4_define(_NOTE_START_LOCK, `')
m4_define(_NOTE_END_LOCK, `')
m4_define(_NOTE_START_UNLOCK, `')
m4_define(_NOTE_END_UNLOCK, `')
m4_define(_NOTE_START_BARRIER, `')
m4_define(_NOTE_END_BARRIER, `')
m4_define(_NOTE_START_ATOMIC, `')
m4_define(_NOTE_END_ATOMIC, `')
m4_define(_NOTE_START_CMPXCHG, `')
m4_define(_NOTE_END_CMPXCHG, `')
m4_define(_NOTE_START_SEM_WAIT, `')
m4_define(_NOTE_END_SEM_WAIT, `')
m4_define(_NOTE_START_SEM_POST, `')
m4_define(_NOTE_END_SEM_POST, `')
m4_define(_NOTE_START_WAIT, `')
m4_define(_NOTE_END_WAIT, `')
m4_define(_NOTE_START_SIGNAL, `')
m4_define(_NOTE_END_SIGNAL, `')
m4_define(_NOTE_START_BDCAST, `')
m4_define(_NOTE_END_BDCAST, `')
m4_define(FETCH_ADD, `({_NOTE_START_ATOMIC(); uint64_t ___x =atomic_fetch_add(&($1), $2); _NOTE_END_ATOMIC(); ___x;});')
m4_define(FETCH_SUB, `({_NOTE_START_ATOMIC(); uint64_t ___x =atomic_fetch_sub(&($1), $2); _NOTE_END_ATOMIC(); ___x;});')
m4_define(STORE,`atomic_store(&($1), $2)')
m4_define(LOAD,`atomic_load(&($1))')
m4_define(CAS, `({_NOTE_START_CMPXCHG(); _Bool ___b = atomic_compare_exchange_weak(&($1), &($2), $3); _NOTE_END_CMPXCHG(); ___b;})')
m4_define(_CAS, `({_Bool ___b = atomic_compare_exchange_weak(($1), &($2), $3); ___b;})') m4_dnl Only intended for internal use
m4_define(FETCH_ADD_DOUBLE, `({
_NOTE_START_CMPXCHG();
double ___oldValue;
double ___newValue;
do {
___oldValue = *($1);
___newValue = ___oldValue + $2;
} while (!_CAS($1, ___oldValue, ___newValue));
_NOTE_END_CMPXCHG();
___oldValue;});')
m4_define(ABARDEF, `unsigned __count__; volatile int __sense__=1; __thread int __local_sense__=1;')
m4_define(ABAREXTERN,`
extern unsigned __count__;
extern volatile int __sense__;
extern __thread int __local_sense__;
')
m4_define(BARRIER,
m4_ifdef(`ATOMIC_BARRIERS',
`{
_NOTE_START_BARRIER();
__local_sense__ = !__local_sense__;
if (atomic_fetch_sub(&(__count__), 1) == 1) {
__count__ = $2;
__sense__ = __local_sense__;
} else {
do {} while (__sense__ != __local_sense__);
}
_NOTE_END_BARRIER();
}'
,
`{
_NOTE_START_BARRIER();
pthread_mutex_lock(&(($1).bar_mutex));
($1).bar_teller++;
if (($1).bar_teller == ($2)) {
($1).bar_teller = 0;
pthread_cond_broadcast(&(($1).bar_cond));
} else {
pthread_cond_wait(&(($1).bar_cond), &(($1).bar_mutex));
}
pthread_mutex_unlock(&(($1).bar_mutex));
_NOTE_END_BARRIER();}
'))
m4_define(BARDEC,
m4_ifdef(`ATOMIC_BARRIERS',
`',
`struct { pthread_mutex_t bar_mutex; pthread_cond_t bar_cond; unsigned bar_teller; } $1;')
)
m4_define(BARINIT,
m4_ifdef(`ATOMIC_BARRIERS',
`__count__=$2;'
,
`{
pthread_mutex_init(&(($1).bar_mutex), NULL);
pthread_cond_init(&(($1).bar_cond), NULL);
($1).bar_teller=0;
}'))
m4_define(BAREXTERN,
m4_ifdef(`ATOMIC_BARRIERS',
`
extern unsigned __count__;
extern volatile int __sense__;
extern __thread int __local_sense__;
'
, `'))
m4_define(LOCKDEC, m4_ifdef(`SIMPLE_TTAS',
`Lock $1;',
`pthread_mutex_t $1;'))
m4_define(LOCKINIT, m4_ifdef(`SIMPLE_TTAS',
`{sw_lock_init_p(&($1),NULL); printf("Using TTAS for lock $1\n");}',
`{pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
($1).__data.__spins = 10000;
pthread_mutex_init(&($1), &attr);
($1).__data.__spins = 10000;
printf("Using pmutex (adaptive) for lock $1\n"); }'))
m4_dnl `{pthread_mutex_init(&($1),NULL); printf("Using pmutex (normal) for lock $1\n");}'))
m4_define(LOCK, m4_ifdef(`SIMPLE_TTAS',
`{_NOTE_START_LOCK(); sw_lock_aquire(&($1)); _NOTE_END_LOCK();}',
`{_NOTE_START_LOCK(); pthread_mutex_lock(&($1)); _NOTE_END_LOCK();}'))
m4_define(UNLOCK, m4_ifdef(`SIMPLE_TTAS',
`{_NOTE_START_UNLOCK(); sw_lock_release(&($1)); _NOTE_END_UNLOCK();}',
`{_NOTE_START_UNLOCK(); pthread_mutex_unlock(&($1)); _NOTE_END_UNLOCK();}'))
m4_define(ALOCKDEC, m4_ifdef(`SIMPLE_TTAS',
`Lock ($1)[$2];',
`pthread_mutex_t ($1)[$2];'))
m4_define(ALOCKINIT, m4_ifdef(`SIMPLE_TTAS',
`{ printf("Using TTAS for lock $1[]\n");
int i;
for(i = 0; i < ($2); i++)
sw_lock_init_p(&(($1)[i]), NULL); }',
`{ int i; for(i = 0; i < ($2); i++) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
(($1)[i]).__data.__spins = 10000;
pthread_mutex_init(&(($1)[i]), &attr);
(($1)[i]).__data.__spins = 10000;
}
printf("Using pmutex (adaptive) for lock $1[]\n"); }'))
`{ int i; for(i = 0; i < ($2); i++)
pthread_mutex_init(&(($1)[i]), NULL);
printf("Using pmutex (normal) for lock $1[]\n"); }'))
m4_define(ALOCK, m4_ifdef(`SIMPLE_TTAS',
`{_NOTE_START_LOCK(); sw_lock_aquire(&(($1)[($2)])); _NOTE_END_LOCK();}',
`{_NOTE_START_LOCK(); pthread_mutex_lock(&(($1)[($2)])); _NOTE_END_LOCK();}'))
m4_define(AGETL, m4_ifdef(`SIMPLE_TTAS',
`(($1)[$2])',
`(($1)[$2])'))
m4_define(AULOCK, m4_ifdef(`SIMPLE_TTAS',
`{_NOTE_START_UNLOCK(); sw_lock_release(&(($1)[($2)])); _NOTE_END_UNLOCK();}',
`{_NOTE_START_UNLOCK(); pthread_mutex_unlock(&(($1)[($2)])); _NOTE_END_UNLOCK();}'))
m4_define(PAUSEDEC, `sem_t $1;')
m4_define(PAUSEINIT, `{sem_init(&($1),0,0);}')
m4_define(CLEARPAUSE, `{;}')
m4_define(SETPAUSE, `{_NOTE_START_SEM_POST(); sem_post(&($1)); _NOTE_END_SEM_POST();}')
m4_define(WAITPAUSE, `{_NOTE_START_SEM_WAIT(); sem_wait(&($1)); _NOTE_END_SEM_WAIT();}')
m4_define(CONDVARDEC, `pthread_cond_t $1;')
m4_define(CONDVARINIT, `pthread_cond_init(&($1), NULL);')
m4_define(CONDVARWAIT,`{ _NOTE_START_WAIT(); pthread_cond_wait(&($1), &($2)); _NOTE_END_WAIT(); }')
m4_define(CONDVARSIGNAL,`{ _NOTE_START_SIGNAL(); pthread_cond_signal(&($1)); _NOTE_END_SIGNAL(); }')
m4_define(CONDVARBCAST,`{ _NOTE_START_BDCAST(); pthread_cond_broadcast(&($1)); _NOTE_END_BDCAST(); }')
m4_define(RELEASE_FENCE, `{ atomic_thread_fence(memory_order_release);}')
m4_define(ACQUIRE_FENCE, `{ atomic_thread_fence(memory_order_acquire);}')
m4_define(FULL_FENCE, `{ atomic_thread_fence(memory_order_seq_cst);}')
m4_define(BIND,
m4_ifdef(`BIND_CORES', `{
cpu_set_t cpuset;
const pthread_t pid = $2;
cpu_set_t _____cpuset;
CPU_ZERO(&_____cpuset);
CPU_SET($1, &_____cpuset);
const int set_result = pthread_setaffinity_np(pid, sizeof(cpu_set_t), &_____cpuset);
assert(set_result == 0);
}',
m4_ifdef(`BIND_THREADS', `{
cpu_set_t cpuset;
const pthread_t pid = $2;
cpu_set_t _____cpuset;
CPU_ZERO(&_____cpuset);
CPU_SET($1/2+($1%2)*sysconf(_SC_NPROCESSORS_CONF)/2, &_____cpuset);
const int set_result = pthread_setaffinity_np(pid, sizeof(cpu_set_t), &_____cpuset);
assert(set_result == 0);
}')
)
)
m4_define(CREATE,
`{
long i, Error;
assert(__threads__<__MAX_THREADS__);
pthread_mutex_lock(&__intern__);
for (i = 0; i < ($2) - 1; i++) {
BIND(i, __tid__[__threads__-1])
Error = pthread_create(&__tid__[__threads__++], NULL, (void * (*)(void *))($1), NULL);
if (Error != 0) {
printf("Error in pthread_create().\n");
exit(-1);
}
}
pthread_mutex_unlock(&__intern__);
BIND(i, __tid__[__threads__-1])
$1();
}')
m4_define(WAIT_FOR_END, `{int aantal=$1; while (aantal--) pthread_join(__tid__[aantal], NULL);}')
m4_define(MAIN_INITENV, `{__tid__[__threads__++]=pthread_self();}')
m4_define(MAIN_END, `{exit(0);}')
m4_define(INCLUDES,`
#include <stdlib.h>
#include <semaphore.h>
#include <assert.h>
#if __STDC_VERSION__ >= 201112L
#include <stdatomic.h>
#endif
#include <stdint.h>
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
#include <stdio.h>
#include "/mnt/ceph/users/igi/splash4/swarm-runtime/include/swarm/worker_hooks.h"
#include "/mnt/ceph/users/igi/splash4/swarm-runtime/include/swarm/impl/simple_lock.h"
#define PAGE_SIZE 4096
#define __MAX_THREADS__ 256
')
m4_define(MAIN_ENV,`
INCLUDES
pthread_t __tid__[__MAX_THREADS__];
unsigned __threads__=0;
pthread_mutex_t __intern__;
ABARDEF
')
m4_define(EXTERN_ENV, `
INCLUDES
extern pthread_t __tid__[__MAX_THREADS__];
extern unsigned __threads__;
extern pthread_mutex_t __intern__;
BAREXTERN
')
m4_define(G_MALLOC, `({ void* mem = malloc($1); assert(mem); mem; });')
m4_define(NU_MALLOC, `({ void* mem = malloc($1); assert(mem); mem; });')
m4_define(CLOCK, `{long time(); ($1) = time(0);}')
m4_divert(0)