NUMA-aware SAXPY

This example demonstrates how to define and run a SAXPY kernel (single-precision Y[i] = a * X[i] + Y[i]) such that it runs efficiently on a system with multiple memory domains (NUMA) with the multithreaded CPU backend. (You likely will need to fine-tune the value of N on your system of interest if you care about the particular measurement.)

# EXCLUDE FROM TESTING# Run with `POCL_AFFINITY=1` to pin the threads that run the kernels, e.g.#   POCL_AFFINITY=1 julia -t 128 examples/numa_aware.jlusing BenchmarkToolsusing KernelAbstractions@kernel function saxpy_kernel(a, @Const(X), Y)    I = @index(Global)    @inbounds Y[I] = a * X[I] + Y[I]end@kernel function fill_kernel(A, x)    I = @index(Global)    @inbounds A[I] = xend"""  measure_membw(; kwargs...) -> membw, flopsEstimate the memory bandwidth (GB/s) by performing a time measurement of aSAXPY kernel. Returns the memory bandwidth (GB/s) and the compute (GFLOP/s)."""function measure_membw(        backend = CPU(); verbose = true, N = 1024 * 500_000, dtype = Float32,        init = :parallel,    )    bytes = 3 * sizeof(dtype) * N # num bytes transferred in SAXPY    flops = 2 * N # num flops in SAXY    workgroup_size = 1024    a = dtype(3.1415)    X = KernelAbstractions.allocate(backend, dtype, N)    Y = KernelAbstractions.allocate(backend, dtype, N)    if init == :serial        # The calling thread touches all the memory first        fill!(X, dtype(1))        fill!(Y, dtype(2))    else        # The threads that run the kernels touch the memory first        fill_kernel(backend, workgroup_size)(X, dtype(1), ndrange = size(X))        fill_kernel(backend, workgroup_size)(Y, dtype(2), ndrange = size(Y))        KernelAbstractions.synchronize(backend)    end    t = @belapsed begin        kernel = saxpy_kernel($backend, $workgroup_size, $(size(Y)))        kernel($a, $X, $Y, ndrange = $(size(Y)))        KernelAbstractions.synchronize($backend)    end evals = 2 samples = 10    mem_rate = bytes * 1.0e-9 / t # GB/s    flop_rate = flops * 1.0e-9 / t # GFLOP/s    if verbose        println("\tMemory Bandwidth (GB/s): ", round(mem_rate; digits = 2))        println("\tCompute (GFLOP/s): ", round(flop_rate; digits = 2))    end    return mem_rate, flop_rateendmeasure_membw(CPU());# On a system with multiple NUMA domains, this places all the memory in the domain of the calling thread# measure_membw(CPU(); init=:serial);

Important remarks:

The CPU backend runs kernels with PoCL 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. 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, 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.
  3. Initialize your data in parallel(!), with a kernel. Under the "NUMA first-touch policy" 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:

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:

threadsinitializationPOCL_AFFINITY=1POCL_AFFINITY unset
64init = :parallel, or zeros and ones242130–155
64init = :serial142137
32init = :parallel, or zeros and ones157121–135
32init = :serial124121–139

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.

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.