ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Affinity.h File Reference

The placement of the threads on the processors, beyond the book: a worker of a parallel algorithm pinned to a processor of its own keeps its caches, its pages first touched near its memory on a machine of several sockets (the policy of the first touch of the NUMA systems), and its timings repeatable, while the scheduler of the system may move an unpinned thread at every switch. More...

#include <thread>
#include <vector>

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

const std::vector< int > & ADS::affinityCpus (void)
 the processors on which the process may run, in increasing order, as the first call found them: those of its binding, or those that the topology allows where the system does not tell the binding (macOS); empty where hwloc does not give them
unsigned ADS::availableCpus (void)
 the number of processors on which the process may run, that of affinityCpus() where it is not empty and std::thread::hardware_concurrency() otherwise (0 if not known)
bool ADS::pinThread (unsigned i)
 binds the calling thread to the processor \( i \bmod c \) of affinityCpus(), \( c \) their number; true if the thread is bound, false where the placement does not exist or the system refuses it
int ADS::currentCpu (void)
 the processor on which the calling thread runs now, \( -1 \) where it is not known

Detailed description

The placement of the threads on the processors, beyond the book: a worker of a parallel algorithm pinned to a processor of its own keeps its caches, its pages first touched near its memory on a machine of several sockets (the policy of the first touch of the NUMA systems), and its timings repeatable, while the scheduler of the system may move an unpinned thread at every switch.

affinityCpus() gives the processors on which the process may run, as the call of the first use found them (those given to taskset or to numactl, in increasing order), and pinThread( i ) binds the calling thread to the processor \( i \bmod c \) of them, \( c \) their number, so that the workers \( 0 , 1 , \ldots \) of a pool, each pinning itself at its start, fill the processors given to the process in their order. The executors do not pin their threads [see ThreadPool.h]: whoever wants it calls pinThread( i ) from the body of the worker \( i \), never from the thread that hands out the work, which would then stay pinned after the parallel part (an executor that runs one of the calls on the calling thread tells which one it is), and the function knows nothing of the executors (see Executors and schedules).

The placement is that of hwloc, with ADS_WITH_HWLOC (the topology of the machine loaded once, at the first use, the processors read by hwloc_get_cpubind(), the thread bound by hwloc_set_cpubind() and its processor by hwloc_get_last_cpu_location()), which keeps the calls of each system; without hwloc, or where it cannot load the topology, affinityCpus() is empty, availableCpus() is std::thread::hardware_concurrency(), pinThread() does nothing and returns false, and currentCpu() is \( -1 \).

The contracts [see Contract.h]: affinityCpus() is called first before any thread of the process is pinned, or it finds the mask of a pinned thread. There is no other operation to define.

See also
U. Drepper, What every programmer should know about memory, Red Hat, Inc., 2007, section 6.4 (the placement of the threads and of the memory)
F. Broquedis, J. Clet-Ortega, S. Moreaud, N. Furmento, B. Goglin, G. Mercier, S. Thibault, R. Namyst, hwloc: a generic framework for managing hardware affinities in HPC applications, 18th Euromicro Conference on Parallel, Distributed and Network-based Processing (PDP), 180-186, 2010, doi:10.1109/PDP.2010.67
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa