16 #if defined(linux) || defined(__linux) || defined(__linux__)
17 #define PLASMA_OS_LINUX 1
21 #elif defined( _WIN32 ) || defined( _WIN64 )
22 #define PLASMA_OS_WINDOWS 1
24 #elif (defined __APPLE__) || (defined macintosh) || (defined __MACOSX__)
25 #define PLASMA_OS_MACOS 1
26 #include <sys/param.h>
27 #include <sys/sysctl.h>
28 #include <mach/mach_init.h>
29 #include <mach/thread_policy.h>
30 kern_return_t thread_policy_set(thread_act_t thread, thread_policy_flavor_t flavor,
31 thread_policy_t policy_info, mach_msg_type_number_t count);
33 #define PLASMA_OS_AIX 1
35 #error "Cannot find the runing system or system not supported. Please define try to PLASMA_OS_[LINUX|MACOS|AIX|WINDOWS]"
38 #if defined(PLASMA_HWLOC) && (defined PLASMA_AFFINITY_DISABLE)
51 static volatile int sys_corenbr = 1;
52 static volatile int topo_initialized = 0;
63 if ( !topo_initialized ) {
64 #if (defined PLASMA_OS_LINUX) || (defined PLASMA_OS_AIX)
66 sys_corenbr = sysconf(_SC_NPROCESSORS_ONLN);
68 #elif (defined PLASMA_OS_MACOS)
72 size_t len =
sizeof(cpu);
79 sysctl(mib, 2, &cpu, &len, NULL, 0);
82 sysctl( mib, 2, &cpu, &len, NULL, 0 );
88 #elif (defined PLASMA_OS_WINDOWS)
90 GetSystemInfo(&sysinfo);
91 sys_corenbr = sysinfo.dwNumberOfProcessors;
111 #ifndef PLASMA_AFFINITY_DISABLE
112 #if (defined PLASMA_OS_LINUX)
116 CPU_SET( rank, &
set );
118 #if (defined HAVE_OLD_SCHED_SETAFFINITY)
119 if( sched_setaffinity( 0, &
set ) < 0 )
121 if( sched_setaffinity( 0,
sizeof(
set), &
set) < 0 )
129 #elif (defined PLASMA_OS_MACOS)
131 thread_affinity_policy_data_t ap;
135 ret = thread_policy_set( mach_thread_self(),
136 THREAD_AFFINITY_POLICY,
138 THREAD_AFFINITY_POLICY_COUNT
145 #elif (defined PLASMA_OS_WINDOWS)
147 DWORD mask = 1 << rank;
149 if( SetThreadAffinityMask(GetCurrentThread(), mask) == 0)
154 #elif (defined PLASMA_OS_AIX)
156 tid_t self_ktid = thread_self ();
157 bindprocessor(BINDTHREAD, self_ktid, rank);
176 #ifndef PLASMA_AFFINITY_DISABLE
177 #if (defined PLASMA_OS_LINUX)
183 for(i=0; i<sys_corenbr; i++)
186 #if (defined HAVE_OLD_SCHED_SETAFFINITY)
187 if( sched_setaffinity( 0, &
set ) < 0 )
189 if( sched_setaffinity( 0,
sizeof(
set), &
set) < 0 )
192 plasma_warning(
"plasma_unsetaffinity",
"Could not unbind thread");
198 #elif (defined PLASMA_OS_MACOS)
217 #elif (defined PLASMA_OS_WINDOWS)
221 for(i=0; i<sys_corenbr; i++)
224 if( SetThreadAffinityMask(GetCurrentThread(), mask) == 0) {
225 plasma_warning(
"plasma_unsetaffinity",
"Could not unbind thread");
230 #elif (defined PLASMA_OS_AIX)
249 #if (defined PLASMA_OS_LINUX) || (defined PLASMA_OS_MACOS) || (defined PLASMA_OS_AIX)
250 return sched_yield();
251 #elif PLASMA_OS_WINDOWS
258 #ifdef PLASMA_OS_WINDOWS
259 #define PLASMA_GETENV(var, str) { \
262 str = (char*)malloc(len * sizeof(char)); \
263 ret = GetEnvironmentVariable(var, str, len); \
270 #define PLASMA_CLEANENV(str) if (str != NULL) free(str);
274 #define PLASMA_GETENV(var, str) envstr = getenv(var);
275 #define PLASMA_CLEANENV(str)
287 long int thrdnbr = -1;
292 if ( envstr == NULL ) {
293 thrdnbr = sys_corenbr;
296 thrdnbr = strtol(envstr, &endptr, 10);
297 if ((errno == ERANGE) || ((thrdnbr==0) && (endptr==envstr))) {
310 long int thrdnbr = -1;
315 if ( envstr != NULL ) {
317 thrdnbr = strtol(envstr, &endptr, 10);
318 if ((errno == ERANGE) || ((thrdnbr==0) && (endptr==envstr))) {
324 thrdnbr = plasma_getnuma_size();
340 if ( envstr == NULL) {
342 coresbind[i] = i % sys_corenbr;
353 val = strtol(envstr, &endptr, 10);
354 if (endptr != envstr) {
355 coresbind[i] = (int)val;
361 plasma_error(
"plasma_get_affthreads",
"PLASMA_AFF_THREADS should have at least one entry => everything will be bind on core 0");
371 coresbind[i] = coresbind[0];
375 coresbind[i] = coresbind[i % nbr];
387 #ifndef PLASMA_HAS_COMPLEX_H
406 return fabsf(x) * sqrtf(1.0
f + y / x);
423 return fabs(x) * sqrt(1.0
f + y / x);
428 return ((
double *)&z)[1];
432 return ((
double *)&z)[0];