Tuesday 22 November 2011

Pinning threads to cores

Enforcing cache locality and allowing a deterministic view of where threads are running.

#define _GNU_SOURCE
#include <pthread.h>
int pthread_setaffinity_np(pthread_t th,
                           size_t size,
                           const cpu_set_t *cpuset);
int pthread_getaffinity_np(pthread_t th,
                           size_t size,
                           cpu_set_t *cpuset);
int pthread_attr_setaffinity_np(
                           pthread_attr_t *at,
                           size_t size,
                           const cpu_set_t *cpuset);
int pthread_attr_getaffinity_np(
                           pthread_attr_t *at,
                           size_t size,
                           cpu_set_t *cpuset);


Only specific worker threads should be pinned to cores.
There should be a maximum of n-1 worker threads, where n is the number of cores.
Supplementary threads should be left unpinned. They will most likely run on the nth core.

http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_setaffinity_np.3.html

No comments:

Post a Comment