mercredi 31 juillet 2019

Maximum number of threads in C++

Trivia

Usually, when I want to write multi-threaded program in C++, I inquiry the hardware regarding the number of supported concurrent thread as shown in what follows:

unsigned int numThreads = std::thread::hardware_concurrency();

This returns the total number of supported concurrency. Hence, if we have 2 CPUs each of which can support 12 threads, numThreads will be equal to 24.

Problem

Recently I used numactl to enforce a program to run on ONE CPU ONLY.

numactl -N 1 ./a.out

The problem is that std::thread::hardware_concurrency() returns 24 even when I run it with numactl -N 1. However, under such settings the output of nproc is 12.

numactl -N 1 nproc --> output = 12

Question

Perhaps std::thread::hardware_concurrency() is not designed to support such a scenario. That's not my concern. My question is, what is the best practice to get the supported number of threads when I want to run my program with numactl.

Further information

In case you haven't dealt with numactl, it can be used to run a process using a NUMA policy. For example, you can use it to enforce your program to be ran on one CPU only. The usage for such a case is shown above.

Thank you.

Aucun commentaire:

Enregistrer un commentaire