Thursday, September 23, 2010

Affinity Matters

Took me awhile to find this, but to pin the current thread to some core on Linux 2.6+ kernels:


cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET( SOME_CORE_INT, &mask);
int error = sched_setaffinity(0, sizeof mask, &mask);


Using this significantly improved the scaling of my multicore tree visitors. I still haven't found a good way to do it on OS X systems, which only seems to support affinity across units with distinct L2 caches.

I believe it's more general than pinning in that it could be used for affinity by setting multiple potential cores. Tuning for hyperthreads is iffy in that I believe the hardware context -> logical cpu mapping is unreliable (e.g., no guarantee the HTs are contiguously named).

Edit: also, all you need to do is link in -lpthread and include "pthread.h" ; no more funny business.

No comments:

Post a Comment