How to manipulate multiple threads’ CPU affinity properly?

  affinity, c++, multithreading

Env: Ubuntu 18.04, C++11
Now I have 2 threads (and I want them to execute simultaneously), with different CPU affinity set, but they don’t behave as my expectation, following code demonstrates my application:


// This 2 functions are defined in 2 .o files respectively
int compute1(args);
int compute2(args);

void wrapper_1(args) {
    struct timeval st;
    gettimeofray(&st, NULL);

    // Do some computation
    compute1(args);
    gettimeofday(&ed, NULL);
    
    // Print the beginning time stamp
    cout << st.tv_sec << " " << st.tv_usec << endl;
}

void wrapper_2(args){
    struct timeval st;
    gettimeofray(&st, NULL);

    // Do some computation
    compute2(args);
    gettimeofday(&ed, NULL);
    
    // Print the beginning time stamp
    cout << st.tv_sec << " " << st.tv_usec << endl;
}

int main() {
    // Prepare something.

    // Assuming the machine have 8 logic CPU core with SMT enabled.
    int core_number_1 = 4, core_number_2 = 4;

    // Set the CPU affinity.
    cpu_set_t mask1, mask2;
    CPU_ZERO(&mask1);
    CPU_ZERO(&mask2);
    for (int i = 0; i < core_number_1; i++) CPU_SET(i, &mask1);
    for (int i = core_number_1; i < core_number_1 + core_number_2; i++) CPU_SET(i, &mask2);

    // Create the STD thread for my wrapper functions.
    std::thread t1(wrapper_1);
    pthread_setaffinity_np(t1.native_handle(), sizeof(cpu_set_t), &mask1);
    std::thread t2(wrapper_2);
    pthread_setaffinity_np(t2.native_handle(), sizeof(cpu_set_t), &mask2);

    // Here, by pthread_getaffinity_np, I can get these affinity info:
    // Thread 1 Affinity = 1 1 1 1 0 0 0 0
    // Thread 2 Affinity = 0 0 0 0 1 1 1 1

    // Finish, Join.
    t1.join();
    t2.join();

    return;
}

To check whether these 2 threads are really executed simultaneously, I use 2 methods:

  • print the start time (st in wrapper), and they are almost the same, from which I assume that these threads are executed simultaneously.
  • However, when I check the CPU utils. with htop, I have following result: Core 1-4 are fully utilized (they may stand for thread 1), and Core 8 are fully utilized, but Core 5 6 7 are not used. (As the picture)
    CPU_AFFINITY_RESULT

My question is that: whether I set the affinity correctly, if not, how can I set the affinity of thread 1 and thread 2 to make these 2 threads fully utilize 8 CPU cores?

Source: Windows Questions C++

LEAVE A COMMENT