Processor Affinity

Each JEDI compute node consists of four sockets, each equipped with an NVIDIA GH200 Grace-Hopper superchip (consisting of one Grace CPU (with 120 GB host-side memory) and one H100 GPU (with 96 GB device-side memory)). Each CPU has 72 cores, so a JEDI node consists of 288 CPU cores distributed across four NUMA domains. In addition, each GPU-side memory is exposed as another NUMA domain (as it can be used also from the CPU). Hence, the node features eight relevant NUMA domains.

In total, numactl exposes 36 NUMA domains, but the remaining 28 domains are not used (they are associated to the MIG feature of the GPUs, which we are not using).

Here is an example output of numactl:

$ numactl -H
available: 36 nodes (0-35)
node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
node 0 size: 121694 MB
node 0 free: 117290 MB
node 1 cpus: 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
node 1 size: 122663 MB
node 1 free: 119955 MB
node 2 cpus: 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
node 2 size: 122663 MB
node 2 free: 111686 MB
node 3 cpus: 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
node 3 size: 122551 MB
node 3 free: 120063 MB
node 4 cpus:
node 4 size: 97280 MB
node 4 free: 97275 MB
node 5 cpus:
...
node 12 cpus:
node 12 size: 97280 MB
node 12 free: 97275 MB
...

On the GPU side, an example output of nvidia-smi is:

$ nvidia-smi topo -m
    GPU0    GPU1    GPU2    GPU3    NIC0    NIC1    NIC2    NIC3    CPU Affinity    NUMA Affinity   GPU NUMA ID
GPU0     X  NV6 NV6 NV6 NODE    SYS SYS SYS 0-71    0       4
GPU1    NV6  X  NV6 NV6 SYS NODE    SYS SYS 72-143  1       12
GPU2    NV6 NV6  X  NV6 SYS SYS NODE    SYS 144-215 2       20
GPU3    NV6 NV6 NV6  X  SYS SYS SYS NODE    216-287 3       28
NIC0    NODE    SYS SYS SYS  X  SYS SYS SYS
NIC1    SYS NODE    SYS SYS SYS  X  SYS SYS
NIC2    SYS SYS NODE    SYS SYS SYS  X  SYS
NIC3    SYS SYS SYS NODE    SYS SYS SYS  X

Binding a process or thread to a specific core - known as pinning - can improve the performance of your code by limiting the likelihood of remote memory accesses. Once your code runs and produces correct results on a system, the next step is performance improvement. The placement of processes and/or threads can play a significant role in performance for a process that uses multiple cores or accelerator hardware.

In general, the Linux scheduler will periodically (re-)distribute all running processes across all available threads to ensure similar usage of the threads. This leads to processes being moved from one thread, core, or socket to another within the compute node. Note that the allocated memory of a process does not necessarily move at the same time (or at all), which can make access to memory much slower. To avoid a potential performance loss due to process migration, processes are usually pinned (or bound) to a logical core through the resource management system. In the case of JEDI, this is Slurm. A pinned process (consisting of one or more threads) is bound to a specific set of cores and will only run on the cores in this set. The set can be a single core or multiple cores that implicitly includes 1st and 2nd level caches associated with those cores and is defined with an affinity mask. Since the majority of applications benefit from strict pinning that prevents migration – unless explicitly prevented – all tasks in a job step are pinned to a set of cores by default. Further information about the default behaviour can be found below.

Note

SchedMD has adapted the behavior of the pinning with Slurm version 22.05 (Currently installed version: 23.02). Whilst our customised default setting improves the performance of average applications over no process binding at all, specialised settings for your application can yield even better performance. Pay attention to maximizing data locality while minimizing latency and resource contention, and have a clear understanding of the characteristics of your own code and the machine that the code is running on.

Slurm options

Warning

The remainder of this document does not apply to JEDI at this point in time, since it does not use psslurm.

Slurm allows users to modify the process affinity by means of the --cpu-bind, --distribution and --hint options to srun. While the available options to srun are standard across all Slurm installations, the implementation of process affinity is done in plugins and thus may differ between installations. On JEDI a custom pinning implementation is provided by Partec (psslurm). In contrast to other options, the processor affinity options need to be directly passed to srun and must not be given to sbatch or salloc. In particular, the option cannot be specified in the header of a batch script.

Warning

srun will no longer read in SLURM_CPUS_PER_TASK and will not inherit option --cpus-per-task from sbatch! This means you will explicitly have to specify --cpus-per-task to your srun calls, or set the new SRUN_CPUS_PER_TASK env var. If you want to keep using --cpus-per-task with sbatch then you will have to add: export SRUN_CPUS_PER_TASK=${SLURM_CPUS_PER_TASK}.

Warning

Setting the option --cpus-per-task implies the option --exact, which means that each step with --cpus-per-task will now only receive the minimum number of cores requested for that job step. The pinning will change (which has an implication on the performance) and can mean threads of different tasks can share the same core (using SMT). Attention: As a result, explicitly setting --cpus-per-task=1 may result in a different affinity mask than using the implicit default, which is also 1.

Note

As we expect that most of our users will neither want to use nor benefit from SMT, we have disabled SMT by default by setting --threads-per-core=1. To use SMT, the --threads-per-core=2 option must be set for sbatch or salloc. Just setting it as a srun option is not enough. Attention: In our tests we have seen that enabling SMT can lead to suboptimal, non-intuitive affinity masks.

Warning

We recommend not to use --cpu-bind=sockets if you use more tasks than sockets, otherwise tasks will share the same hardware threads. If --cpus-per-task is to be used together with --cpu-bind=sockets, then you usually want to override the implicit --exact by specifying --overcommit so that a task is allocated the full socket.

Warning

Setting --hint can lead to unexpected pinning as it is mutually exclusive with with the following options: --ntasks-per-core, --threads-per-core, -B and --cpu-bind (other then --cpu-bind=verbose). We recommend not using the --hint option.

Note

For hybrid and pure OpenMP applications, it is important to specify the correct number of --cpus-per-task to ensure a proper affinity mask and set the OMP_NUM_THREADS environment variable accordingly. However, the individual threads of each MPI rank can still be moved between the logical threads matching the affinity mask of this rank. OMP_PROC_BIND=true can be used to prevent thread movement. For more advanced, OpenMP-internal affinity specifications, consult the documentation for OMP_PLACES or vendor-specific alternatives (KMP_AFFINITY/GOMP_CPU_AFFINITY).

Terminology

thread

One CPU thread.

task

Part of a job consisting of a number of requested CPU threads (specified by -c, --cpus-per-task).

core

One physical CPU core can run multiple CPU threads. The CPU threads sitting on the same physical core share caches.

socket

Consists of a number of CPU threads, corresponding to the NUMA domains detailed above.

--cpu-bind

--cpu-bind=[{quiet,verbose},none|rank|map_cpu:<list>|mask_cpu:<list>|rank_ldom|map_ldom:<list>|mask_ldom:<list>|sockets|cores|threads|ldoms|boards]

Implicit types

none

Do not bind tasks to CPUs

threads

Each task is pinned to as many threads as it requests. Which threads each process
gets is controlled by the --distribution option. (Default)

rank

Each task is pinned to as many threads as it requests, just filling cores
consecutively. Spread the threads and tasks to as many cores as possible.
This type is not influenced by the second and third part of the --distribution
option. (old default until 12th May 2020)

rank_ldom

Each task is pinned to as many threads as it requests, just filling the nodes
rank by rank cycling sockets and cores. This type is not influenced by the second
and third level of the –distribution option. The threads of a task are always
packed to as few cores as possible. This is the same as
--cpu-bind=threads --distribution=*:cyclic:block

sockets

In a first step the requested CPU threads of a task are assigned in exactly the
same way as with --cpu-bind=threads. But the final affinity mask for the task
is the whole socket where any thread is located that it is assigned to. This means
if a task is assigned to any thread that is part of a socket, it will be bound to
the whole socket. (The ‘whole’ here means to each thread of the socket that is
allocated to the job)

cores

In a first step the requested CPU threads of a task are assigned in exactly the
same way as with --cpu-bind=threads. But the final affinity mask for the task
is the whole core where any thread is located that it is assigned to. This means
if a task is assigned to any thread that is part of a core, it will be bound to
the whole core. (The ‘whole’ here means to each thread of the core that is
allocated to the job)

ldoms

This is the same as --cpu-bind=sockets

boards

Currently not supported on systems with more than one board per node.
JEDI has only one board: same behavivor as none

Explicit types

map_cpu:<list>

Explicit passing of maps or masks to pin the tasks to threads in a round-robin fashion.

mask_cpu:<list>

map_ldom:<list>

Explicit passing of maps or masks to pin the tasks to sockets in a round-robin fashion.

mask_ldom:<list>

Note

Explicitly specified masks or bindings are only honored when the job step has allocated every available CPU on the node. If you want to use a map_ or mask_ bind, then you should have the steps request a whole allocation (do not use --exact or --cpus-per-task or --exclusive). You also may want to use --overlap so that other steps can also allocate all of the cpus and you have control over the task to cpu binding via one of the map or mask options for --cpu-bind.

--distribution

The string passed to --distribution/-m can have up to four parts separated by colon and comma:

  • The first part controls the distribution of the task over the nodes.

  • The second part controls the distribution of tasks over sockets inside one node.

  • The third part controls the distribution of tasks over cores inside one node.

  • The fourth part is an additional information concerning the distribution of tasks over nodes.

--distribution/-m=<node_level>[:<socket_level>[:<core_level>[,Pack|NoPack]]]

First part (node_level)

*

The default is block

block

Distribute tasks to a node such that consecutive tasks share a node

cyclic

Distribute tasks to a node such that consecutive tasks are distributed over
consecutive nodes (in a round-robin fashion)

arbitrary

see https://slurm.schedmd.com/srun.html

plane=<options>

see https://slurm.schedmd.com/dist_plane.html

Second part (socket_level)

*

The default is cyclic

block

Each socket is first filled with tasks before the next socket will be used.

cyclic

Each task will be assigned to the next socket(s) in a round-robin fashion.

fcyclic

Each thread inside a task will be assigned to the next socket in a round-robin
fashion, spreading the task itself as much as possible over all sockets.
fcyclic implies cyclic.

Third part (core_level)

*

The default is inherited from the second part

block

Each core is first filled with tasks before the next core will be used.

cyclic

Each task will be assigned to the next core(s) in a round-robin fashion.
The threads of a task will fill the cores.

fcyclic

Each thread inside a task will be assigned to the next core in a round-robin
fashion, spreading the task itself as much as possible over all cores.

Fourth part

Optional control for task distribution over nodes.

Pack

Default is NoPack. See: https://slurm.schedmd.com/srun.html

NoPack

--hint

We do not recommend using this option, as our tests have shown that it can lead to unexpected pinning.

Possible values are nomultithread, compute_bound, and memory_bound (They imply other options).

--hint=nomultithread

Affinity visualization tool

We have tried to understand and implement the Slurm affinity rules. The result is our PinningWebtool, which allows you to test and visualise different Slurm affinity setups yourself. A description of the displayed scheme can be found in the section below.

Affinity examples

Example: One MPI task with two threads:

This JEDI information is still under construction

Default processor affinity

--cpu-bind=threads --distribution=block:cyclic:cyclic --threads-per-core=1

The behavior of this combination is shown in the following examples for JEDI.

Further examples

Examples for manual pinning

For advanced use cases it can be desirable to manually specify the binding masks or core sets for each task. This is possible using the options --cpu-bind=map_cpu and --cpu-bind=mask_cpu. For example,

srun -n 2 --cpu-bind=map_cpu:1,5

spawns two tasks pinned to core 1 and 5, respectively. The command

srun -n 2 --cpu-bind=mask_cpu:0x3,0xC

spawns two tasks pinned to cores 0 and 1 (0x3 = 3 = 2^0 + 2^1) and cores 2 and 3 (0xC = 12 = 2^2 + 2^3), respectively.