int pthread_create ( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg ) { int (*real_pthread_create) (pthread_t*, const pthread_attr_t*, void *(*routine)(void *), void*) = dlsym (RTLD_NEXT, "pthread_create"); int (*real_pthread_setaffinity_np) (pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset) = dlsym (RTLD_NEXT, "pthread_setaffinity_np"); int res; cpu_set_t cpuset; int nbProcessors = getNbProcessorsAvailable (); assert (real_pthread_create != NULL); assert (real_pthread_setaffinity_np != NULL); res = real_pthread_create (thread, attr, start_routine, arg); /* Pinning stuff */ CPU_ZERO(&cpuset); CPU_SET(core, &cpuset); if (real_pthread_setaffinity_np (*thread, sizeof (cpu_set_t), &cpuset) != 0) { fprintf (stderr, "Error: Cannot pin thread %p on core %d\n", thread, core); perror (""); } core++; core %= nbProcessors; return res; }
void myth_bind_worker(int rank) { #if defined(HAVE_PTHREAD_AFFINITY_NP) int cpu = myth_get_worker_cpu(rank); if (cpu != -1) { cpu_set_t cs; CPU_ZERO(&cs); CPU_SET(cpu, &cs); real_pthread_setaffinity_np(real_pthread_self(), sizeof(cpu_set_t), &cs); } #else (void)rank; #endif }