Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

set affinity mask

Dear all,

I am afraid that my question is too general, so if it, sorry about it in advance. I am using Intel C++ 11.1 under Linux. Intel recommends to set affinity mask in order to increase the efficiency and the sample code below is shown in the manual:

// Set affinity mask
int main(void) {
int NCPUs = sysconf(_SC_NPROCESSORS_CONF);
printf("Using thread affinity on %i NCPUs\\n", NCPUs);
#pragma omp parallel default(shared)
cpu_set_t new_mask;
cpu_set_t was_mask;
int tid = omp_get_thread_num();
// 2 packages x 2 cores/pkg x 1 threads/core (4 total cores)
CPU_SET(tid==0 ? 0 : 2, &new_mask);

if (sched_getaffinity(0, sizeof(was_mask), &was_mask) == -1) {
printf("Error: sched_getaffinity(%d, sizeof(was_mask), &was_mask)\\n", tid);
if (sched_setaffinity(0, sizeof(new_mask), &new_mask) == -1) {
printf("Error: sched_setaffinity(%d, sizeof(new_mask), &new_mask)\\n", tid);
printf("tid=%d new_mask=%08X was_mask=%08X\\n", tid,
*(unsigned int*)(&new_mask), *(unsigned int*)(&was_mask));
// Call Intel MKL FFT function
return 0;

The above code works fine, but according to my tests, it only bind the processes to the processors when only two cores are used. On more than two cores the affinity is not set. Does anybody has any idea how can I modify this code to work on any number of processors?

Thanks in advance,

0 Kudos
2 Replies
Black Belt
The question, and advice referred to, seem well out of the beaten path. Normally, one would use the higher level affinity facilities of your OpenMP (KMP_AFFINITY environment variable, and the like, for Intel OpenMP). If the purpose is simply to get efficient operation with MKL, it's hard to understand why you would avoid that route.
If you want to handle affinity entirely yourself, you would study the MKL docs to see how to avoid MKL taking measures which conflict with yours, then ask any questions on the MKL forum.
0 Kudos

Hi Dan,

Regarding the meaning of affinity mask (cpu_set_t), please see It says:

The first available CPU on the system corresponds to a cpu value of 0, the

next CPU corresponds to a cpu value of 1, and so on. The constant CPU_SETSIZE

(currently 1024) specifies a value one greater than the maximum CPU number

that can be stored in cpu_set_t.

In the above example, it binds all threads to two cores. Process 0 was binded to core 0 (mask=1 or 2^0) and other processes were binded to core 2 (mask=4 or 2^2). You can change affinity mask of course. E.g. bind each process to each core (assuming the number of processes = the number of cores) like this.

CPU_SET(tid, &new_mask);

Thank you.

Feilong H.

Intel Developer Support

0 Kudos