From b2885db18e0b893ca8154336d98de4a63b2b83e8 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 26 Oct 2026 14:18:31 +0100 Subject: [PATCH] Require PoCL's NUMA-aware scheduling, and measure the NUMA example pocl_standalone_jll 7.2.3+1 gives each PoCL thread a contiguous block of a kernel's workgroups, so that a kernel touching memory first and a later kernel with as many workgroups work on each part of an array from the same thread, and makes POCL_AFFINITY=1 spread the threads over the NUMA domains. Compat can't name a build, so require 7.2.3, which resolves to the latest one. The NUMA example's page said that it hadn't been measured on a system with several memory domains. Explain the scheduling and the pinning, and add measurements on 2x EPYC 7502: 242 GB/s with 64 pinned threads and parallel initialization, against 142 with serial initialization and 130-155 without pinning; a 32-thread team gets 157 against 124. --- Project.toml | 2 +- docs/src/examples/numa_aware.md | 28 ++++++++++++---------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Project.toml b/Project.toml index 0a0426ce9..3a081c612 100644 --- a/Project.toml +++ b/Project.toml @@ -70,7 +70,7 @@ StaticArraysCore = "1" UnsafeAtomics = "0.4" julia = "1.10" oneAPI = "2" -pocl_standalone_jll = "7.2.2" +pocl_standalone_jll = "7.2.3" [extras] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/docs/src/examples/numa_aware.md b/docs/src/examples/numa_aware.md index 5cd4a3829..ad14ba484 100644 --- a/docs/src/examples/numa_aware.md +++ b/docs/src/examples/numa_aware.md @@ -17,27 +17,23 @@ $(read(path, String)) The `CPU` backend runs kernels with [PoCL](https://portablecl.org) on PoCL's own threads, not on Julia's. That determines how to apply the usual advice for NUMA systems: -1) Pin the threads that run the kernels. Set PoCL's `POCL_AFFINITY=1` environment variable before the backend is first used, which pins each of PoCL's threads to a core. Tools that pin Julia's threads, like [ThreadPinning.jl](https://github.com/carstenbauer/ThreadPinning.jl), don't affect the threads that run the kernels. +1) Pin the threads that run the kernels. Set PoCL's `POCL_AFFINITY=1` environment variable before the backend is first used. It pins each of PoCL's threads to one of the CPUs the process may run on (respecting e.g. `taskset`), a core of its own first, spread over the NUMA domains, so that fewer threads than cores still use the memory bandwidth of every domain. Tools that pin Julia's threads, like [ThreadPinning.jl](https://github.com/carstenbauer/ThreadPinning.jl), don't affect the threads that run the kernels. Without pinning, the operating system moves the threads around, and where they touch the memory first says little about where they run later. 2) Choose the number of threads with `julia -t N` or the `JULIA_KA_CPU_THREADS` environment variable; see [`CPU`](@ref). -3) Initialize your data in parallel(!), with a kernel. Under the ["NUMA first-touch policy"](https://queue.acm.org/detail.cfm?id=2513149#:~:text=This%20is%20called%20the%20first,policy%20associated%20with%20a%20task.) a page of memory is placed in the memory domain of the thread that first writes to it. `fill!` writes from the calling thread, which places all the memory in that thread's domain. The example instead allocates with `KernelAbstractions.allocate` and writes the initial values with a kernel (`init = :parallel`), so that the threads that run the computational kernel touch the memory first. `KernelAbstractions.zeros(backend, dtype, N)` and `KernelAbstractions.ones` do the same for arrays of bits types of at least 1 MiB; smaller arrays, and arrays of other types, are filled from the calling thread. +3) Initialize your data in parallel(!), with a kernel. Under the ["NUMA first-touch policy"](https://queue.acm.org/detail.cfm?id=2513149#:~:text=This%20is%20called%20the%20first,policy%20associated%20with%20a%20task.) a page of memory is placed in the memory domain of the thread that first writes to it. `fill!` writes from the calling thread, which places all the memory in that thread's domain. The example instead allocates with `KernelAbstractions.allocate` and writes the initial values with a kernel (`init = :parallel`). PoCL gives each thread a contiguous block of a kernel's workgroups, so a later kernel with as many workgroups mostly works on each part of the array from the thread that touched it first. `KernelAbstractions.zeros(backend, dtype, N)` and `KernelAbstractions.ones` initialize with a kernel too, for arrays of bits types of at least 1 MiB; smaller arrays, and arrays of other types, are filled from the calling thread. +This needs KernelAbstractions 0.10.2 or later, which avoids the masked memory accesses that made kernels slow on some AMD CPUs, and the `pocl_standalone_jll` 7.2.3+1 or later that it installs, whose PoCL schedules workgroups and pins threads this way. **Demonstration:** -So far, the example has only been measured with KernelAbstractions 0.10 on a system with a single memory domain, where pinning and the initialization don't change where memory is placed. With 16 Julia threads on an AMD Ryzen 9 5950X (16 physical cores, 1 NUMA domain), one gets the following numbers (comments for demonstration purposes): +On a system with two AMD EPYC 7502 (2 × 32 cores, 2 NUMA domains), the example (`N = 512_000_000`, workgroups of 1024) gets the following memory bandwidth, in GB/s: -``` -Memory Bandwidth (GB/s): 41.19 # POCL_AFFINITY=1, init = :parallel -Compute (GFLOP/s): 6.87 - -Memory Bandwidth (GB/s): 41.22 # POCL_AFFINITY=1, init = :serial -Compute (GFLOP/s): 6.87 +| threads | initialization | `POCL_AFFINITY=1` | `POCL_AFFINITY` unset | +|--:|---|--:|--:| +| 64 | `init = :parallel`, or `zeros` and `ones` | 242 | 130–155 | +| 64 | `init = :serial` | 142 | 137 | +| 32 | `init = :parallel`, or `zeros` and `ones` | 157 | 121–135 | +| 32 | `init = :serial` | 124 | 121–139 | -Memory Bandwidth (GB/s): 41.24 # POCL_AFFINITY unset, init = :parallel -Compute (GFLOP/s): 6.87 - -Memory Bandwidth (GB/s): 41.16 # POCL_AFFINITY unset, init = :serial -Compute (GFLOP/s): 6.86 -``` +A plain `Threads.@threads :static` loop with its threads pinned by ThreadPinning.jl gets 240 GB/s with 64 threads. Initializing serially puts all the memory in one domain. Without pinning, the threads that touch the memory first aren't reliably the ones that work on it later, and the numbers vary from run to run with where the operating system places the threads. -As expected for a single memory domain, the four configurations don't differ: the kernel is limited by the memory bandwidth of the system. How much pinning and parallel initialization gain on a system with multiple memory domains has not been measured with the PoCL-based `CPU` backend yet. +On a system with a single memory domain, pinning and the initialization don't change where memory is placed: on an AMD Ryzen 9 9950X with 8 or 16 threads, all configurations get 53–55 GB/s.