-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquarkos.c
433 lines (378 loc) · 12.2 KB
/
quarkos.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
/**
*
* @file quarkos.c
*
* This file handles the mapping from pthreads calls to windows threads
* QUARK is a software package provided by Univ. of Tennessee,
* Univ. of California Berkeley and Univ. of Colorado Denver
*
* @version 2.8.0
* @author Piotr Luszczek
* @author Mathieu Faverge
* @date 2010-11-15
*
* Note : this file is a copy of a PLASMA file for use of QUARK alone
*
**/
#if defined(linux) || defined(__linux) || defined(__linux__)
#define QUARK_OS_LINUX 1
#define _GNU_SOURCE
#include <unistd.h>
#include <sched.h>
#elif defined(__FreeBSD__)
#define QUARK_OS_FREEBSD 1
#include <unistd.h>
#include <inttypes.h>
#include <sys/param.h>
#include <sys/cpuset.h>
#include <sched.h>
#elif defined( _WIN32 ) || defined( _WIN64 )
#define QUARK_OS_WINDOWS 1
#include <Windows.h>
#elif (defined __APPLE__) || (defined macintosh) || (defined __MACOSX__)
#define QUARK_OS_MACOS 1
#include <sys/param.h>
#include <sys/sysctl.h>
#include <mach/mach_init.h>
#include <mach/thread_policy.h>
kern_return_t thread_policy_set(thread_act_t thread, thread_policy_flavor_t flavor,
thread_policy_t policy_info, mach_msg_type_number_t count);
#elif (defined _AIX)
#define QUARK_OS_AIX 1
#else
#error "Cannot find the runing system or system not supported. Please define try to QUARK_OS_[LINUX|MACOS|AIX|WINDOWS]"
#endif
#if defined(QUARK_HWLOC) && (defined QUARK_AFFINITY_DISABLE)
#undef QUARK_HWLOC
#endif
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
//#include "common.h"
#if defined( _WIN32 ) || defined( _WIN64 )
#include "quarkwinthread.h"
#else
#include <pthread.h>
#endif
#include "quark.h"
#define QUARK_SUCCESS 0
#define QUARK_ERR -1
#define QUARK_ERR_UNEXPECTED -1
// maximum cores per context
#define CONTEXT_THREADS_MAX 256
#ifdef __cplusplus
extern "C" {
#endif
static pthread_mutex_t mutextopo = PTHREAD_MUTEX_INITIALIZER;
static volatile int sys_corenbr = 1;
static volatile int topo_initialized = 0;
/*
* Topology functions
*/
#ifdef QUARK_HWLOC
#include "quarkos-hwloc.c"
#else
void quark_topology_init(){
pthread_mutex_lock(&mutextopo);
if ( !topo_initialized ) {
#if (defined QUARK_OS_LINUX) || (defined QUARK_OS_FREEBSD) || (defined QUARK_OS_AIX)
sys_corenbr = sysconf(_SC_NPROCESSORS_ONLN);
#elif (defined QUARK_OS_MACOS)
int mib[4];
int cpu;
size_t len = sizeof(cpu);
/* set the mib for hw.ncpu */
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU;
/* get the number of CPUs from the system */
sysctl(mib, 2, &cpu, &len, NULL, 0);
if( cpu < 1 ) {
mib[1] = HW_NCPU;
sysctl( mib, 2, &cpu, &len, NULL, 0 );
}
if( cpu < 1 ) {
cpu = 1;
}
sys_corenbr = cpu;
#elif (defined QUARK_OS_WINDOWS)
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
sys_corenbr = sysinfo.dwNumberOfProcessors;
#endif
}
pthread_mutex_unlock(&mutextopo);
}
void quark_topology_finalize()
{
quark_unsetaffinity();
}
/**
This routine will set affinity for the calling thread that has rank 'rank'.
Ranks start with 0.
If there are multiple instances of QUARK then affinity will be wrong: all ranks 0
will be pinned to core 0.
Also, affinity is not resotred when QUARK_Finalize() is called.
*/
int quark_setaffinity(int rank) {
#ifdef QUARK_AFFINITY_DISABLE
return QUARK_SUCCESS;
#else /* QUARK_AFFINITY_DISABLE not defined */
#if defined(QUARK_OS_LINUX) || defined(QUARK_OS_FREEBSD)
{
#if (defined QUARK_OS_LINUX)
cpu_set_t set;
#elif (defined QUARK_OS_FREEBSD)
cpuset_t set;
#endif
CPU_ZERO( &set );
CPU_SET( rank, &set );
#if (defined HAVE_OLD_SCHED_SETAFFINITY)
if( sched_setaffinity( 0, &set ) < 0 )
#else /* HAVE_OLD_SCHED_SETAFFINITY */
#if (defined QUARK_OS_LINUX)
if( sched_setaffinity( 0, sizeof(set), &set) < 0 )
#elif (defined QUARK_OS_FREEBSD)
if( cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 0, sizeof(set), &set) < 0 )
#endif
#endif /* HAVE_OLD_SCHED_SETAFFINITY */
{
return QUARK_ERR_UNEXPECTED;
}
return QUARK_SUCCESS;
}
#elif (defined QUARK_OS_MACOS)
{
thread_affinity_policy_data_t ap;
int ret;
ap.affinity_tag = 1; /* non-null affinity tag */
ret = thread_policy_set( mach_thread_self(),
THREAD_AFFINITY_POLICY,
(integer_t*) &ap,
THREAD_AFFINITY_POLICY_COUNT
);
if(ret != 0)
return QUARK_ERR_UNEXPECTED;
return QUARK_SUCCESS;
}
#elif (defined QUARK_OS_WINDOWS)
{
DWORD mask = 1 << rank;
if( SetThreadAffinityMask(GetCurrentThread(), mask) == 0)
return QUARK_ERR_UNEXPECTED;
return QUARK_SUCCESS;
}
#elif (defined QUARK_OS_AIX)
{
tid_t self_ktid = thread_self ();
bindprocessor(BINDTHREAD, self_ktid, rank);
return QUARK_SUCCESS;
}
#else
return QUARK_ERR_NOT_SUPPORTED;
#endif
#endif /* QUARK_AFFINITY_DISABLE */
}
/**
This routine will set affinity for the calling thread that has rank 'rank'.
Ranks start with 0.
If there are multiple instances of QUARK then affinity will be wrong: all ranks 0
will be pinned to core 0.
Also, affinity is not resotred when QUARK_Finalize() is called.
*/
int quark_unsetaffinity() {
#ifdef QUARK_AFFINITY_DISABLE
return QUARK_SUCCESS;
#else /* QUARK_AFFINITY_DISABLE not defined */
#if defined(QUARK_OS_LINUX) || defined(QUARK_OS_FREEBSD)
{
int i;
#if (defined QUARK_OS_LINUX)
cpu_set_t set;
#elif (defined QUARK_OS_FREEBSD)
cpuset_t set;
#endif
CPU_ZERO( &set );
for(i=0; i<sys_corenbr; i++)
CPU_SET( i, &set );
#if (defined HAVE_OLD_SCHED_SETAFFINITY)
if( sched_setaffinity( 0, &set ) < 0 )
#else /* HAVE_OLD_SCHED_SETAFFINITY */
#if (defined QUARK_OS_LINUX)
if( sched_setaffinity( 0, sizeof(set), &set) < 0 )
#elif (defined QUARK_OS_FREEBSD)
if( cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, 0, sizeof(set), &set) < 0 )
#endif
#endif /* HAVE_OLD_SCHED_SETAFFINITY */
{
quark_warning("quark_unsetaffinity", "Could not unbind thread");
return QUARK_ERR_UNEXPECTED;
}
return QUARK_SUCCESS;
}
#elif (defined QUARK_OS_MACOS)
{
/* TODO: check how to unbind the main thread if necessary for OpenMP */
/* thread_affinity_policy_data_t ap; */
/* int ret; */
/* ap.affinity_tag = 1; /\* non-null affinity tag *\/ */
/* ret = thread_policy_set( mach_thread_self(), */
/* THREAD_AFFINITY_POLICY, */
/* (integer_t*) &ap, */
/* THREAD_AFFINITY_POLICY_COUNT */
/* ); */
/* if(ret != 0) { */
/* quark_warning("quark_unsetaffinity", "Could not unbind thread"); */
/* return QUARK_ERR_UNEXPECTED; */
/* } */
return QUARK_SUCCESS;
}
#elif (defined QUARK_OS_WINDOWS)
{
int i;
DWORD mask = 0;
for(i=0; i<sys_corenbr; i++)
mask |= 1 << i;
if( SetThreadAffinityMask(GetCurrentThread(), mask) == 0) {
quark_warning("quark_unsetaffinity", "Could not unbind thread");
return QUARK_ERR_UNEXPECTED;
}
return QUARK_SUCCESS;
}
#elif (defined QUARK_OS_AIX)
{
/* TODO: check how to unbind the main thread if necessary for OpenMP */
/* tid_t self_ktid = thread_self (); */
/* bindprocessor(BINDTHREAD, self_ktid, rank); */
return QUARK_SUCCESS;
}
#else
return QUARK_ERR_NOT_SUPPORTED;
#endif
#endif /* QUARK_AFFINITY_DISABLE */
}
#endif /* QUARK_HWLOC */
/** ****************************************************************************
A thread can unlock the CPU if it has nothing to do to let
another thread of less priority running for example for I/O.
*/
int quark_yield() {
#if (defined QUARK_OS_LINUX) || (defined QUARK_OS_FREEBSD) || (defined QUARK_OS_MACOS) || (defined QUARK_OS_AIX)
return sched_yield();
#elif QUARK_OS_WINDOWS
return SleepEx(0,0);
#else
return QUARK_ERR_NOT_SUPPORTED;
#endif
}
#ifdef QUARK_OS_WINDOWS
#define QUARK_GETENV(var, str) { \
int len = 512; \
int ret; \
str = (char*)malloc(len * sizeof(char)); \
ret = GetEnvironmentVariable(var, str, len); \
if (ret == 0) { \
free(str); \
str = NULL; \
} \
}
#define QUARK_CLEANENV(str) if (str != NULL) free(str);
#else /* Other OS systems */
#define QUARK_GETENV(var, str) envstr = getenv(var);
#define QUARK_CLEANENV(str)
#endif
/** ****************************************************************************
* Check for an integer in an environment variable, returning the
* integer value or a provided default value
*/
int quark_get_numthreads()
{
char *envstr = NULL;
char *endptr;
long int thrdnbr = -1;
extern int errno;
/* Env variable does not exist, we search the system number of core */
QUARK_GETENV("QUARK_NUM_THREADS", envstr);
if ( envstr == NULL ) {
thrdnbr = sys_corenbr;
} else {
/* Convert to long, checking for errors */
thrdnbr = strtol(envstr, &endptr, 10);
if ((errno == ERANGE) || ((thrdnbr==0) && (endptr==envstr))) {
QUARK_CLEANENV(envstr);
return -1;
}
}
QUARK_CLEANENV(envstr);
return (int)thrdnbr;
}
int *quark_get_affthreads(/* int *coresbind */) {
char *envstr = NULL;
int i;
int *coresbind = (int *)malloc(CONTEXT_THREADS_MAX*sizeof(int));
/* Env variable does not exist, we search the system number of core */
QUARK_GETENV("QUARK_AFF_THREADS", envstr);
if ( envstr == NULL) {
for (i = 0; i < CONTEXT_THREADS_MAX; i++)
coresbind[i] = i % sys_corenbr;
}
else {
char *endptr;
int wrap = 0;
int nbr = 0;
long int val;
/* We use the content of the QUARK_AFF_THREADS env. variable */
for (i = 0; i < CONTEXT_THREADS_MAX; i++) {
if (!wrap) {
val = strtol(envstr, &endptr, 10);
if (endptr != envstr) {
coresbind[i] = (int)val;
envstr = endptr;
}
else {
/* there must be at least one entry */
if (i < 1) {
//quark_error("quark_get_affthreads", "QUARK_AFF_THREADS should have at least one entry => everything will be bind on core 0");
fprintf(stderr, "quark_get_affthreads: QUARK_AFF_THREADS should have at least one entry => everything will be bind on core 0");
coresbind[i] = 0;
i++;
}
/* there is no more values in the string */
/* the next threads are binded with a round robin policy over this array */
wrap = 1;
nbr = i;
coresbind[i] = coresbind[0];
}
}
else {
coresbind[i] = coresbind[i % nbr];
}
}
}
QUARK_CLEANENV(envstr);
/* return QUARK_SUCCESS; */
return coresbind;
}
/** ****************************************************************************
*/
int quark_getenv_int(char* name, int defval)
{
char *envstr = NULL;
char *endptr;
long int longval = -1;
extern int errno;
QUARK_GETENV(name, envstr);
if ( envstr == NULL ) {
longval = defval;
} else {
/* Convert to long, checking for errors */
longval = strtol(envstr, &endptr, 10);
if ((errno == ERANGE) || ((longval==0) && (endptr==envstr))) {
longval = defval;
}
}
QUARK_CLEANENV(envstr);
return (int)longval;
}
#ifdef __cplusplus
}
#endif