OpenMP `target nowait` has large overhead at start of new stream in nvfortran 26.3/26.9

I’ve spent some time over the past week comparing the performance of OpenMP’s nowait clause to OpenACC’s async in nvfortran 26.3. It initially seemed that as though the nowait clause was slower, but upon further investigation with an LLM, it seems like what’s actually happening is:

1.) every no wait region in a program is launching on a new stream up until all q queues are occupied
2. ) Each of these new queues is allocating a 4xNVCOMPILER_ACC_BUFFERSIZE buffer for the queue that is significantly larger than the corresponding buffer size in async regions, causing the first q nowait regions to run significantly slower than their async equivalents
3. ) All subsequent nowait regions run at more or less the same speed as the equivalent async code.

It seems like these memory allocations are not strictly required, and can cause significantly launch latency in the first q kernels launch in an OpenMP program. Is there a reason for these allocations, or would it be possible to remove them in a future compiler release to speed OpenMP code?

The following reproducer shows the speed difference between the first q=32 nowait regions and the subsequent regions:

!> staging.F90
!! One small nowait target region per repetition, timed per repetition.
!! With default auto streams, each of the first 32 regions lands on a new stream, and the
!! first async upload of the array descriptor on that stream pins a 64 MiB staging buffer.
!! Usage: staging [mode] [nrep]
!! mode 0: allocatable array referenced directly (its descriptor is uploaded per region)
!! mode 1: the same kernel on an explicit-shape dummy (no descriptor)
program staging
  use omp_lib, only : omp_get_wtime
  implicit none
  integer, parameter :: n = 8192
  real(8), allocatable :: a(:), t(:)
  integer :: mode, nrep, r, i
  real(8) :: t0

  call get_args(mode, nrep)
  allocate(a(n), t(nrep)) ; a = 1.d0
  !$omp target enter data map(to: a)
  do r = 1, nrep
    t0 = omp_get_wtime()
    if (mode == 0) then
      !$omp target teams loop nowait
      do i = 1, n
        a(i) = 0.5d0*a(i) + 1.d0
      enddo
      !$omp taskwait
    else
      call expl(a, n)
    endif
    t(r) = omp_get_wtime() - t0
  enddo
  !$omp target exit data map(from: a)

  print '(a,i2,a,f8.3,a,f8.3,a,f8.3,a,f8.3)', 'mode=', mode, '  rep 1 ms', 1.d3*t(1), &
        '  reps 2-32 mean ms', 1.d3*sum(t(2:32))/31, '  reps 33+ mean ms', 1.d3*sum(t(33:))/(nrep-32), &
        '  first 32 total ms', 1.d3*sum(t(1:32))
  print '(a,es22.15)', 'sum(a) ', sum(a)
contains
  subroutine expl(b, n)
    integer, intent(in) :: n
    real(8), intent(inout) :: b(n)
    integer :: i
    !$omp target teams loop nowait
    do i = 1, n
      b(i) = 0.5d0*b(i) + 1.d0
    enddo
    !$omp taskwait
  end subroutine expl

  subroutine get_args(mode, nrep)
    integer, intent(out) :: mode, nrep
    character(len=16) :: s
    mode = 0 ; nrep = 64
    if (command_argument_count() >= 1) then ; call get_command_argument(1, s) ; read(s,*) mode ; endif
    if (command_argument_count() >= 2) then ; call get_command_argument(2, s) ; read(s,*) nrep ; endif
    nrep = max(nrep, 33)
  end subroutine get_args
end program staging

Compiled with

nvfortran -Minfo=all -O2 -mp=gpu -o staging.exe staging.F90

The issue occurs in both nvfortran 26.3 and 26.9

I had an LLM look into this issue in much more detail than I am capable of, and it generated the following report outlining the conditions where this issue occurs and comparing it to the equivalent OpenACC code (it references 26.3 throughout, but I believe all the listed issues apply to 26.9 as well ):


LLM report

With nvfortran 26.3 on an A100, each of the first 32 !$omp target ... nowait regions in a
program blocks the host for about 22 ms before the kernel launches. After 32 regions it drops to
about 20 µs. In a short benchmark this makes nowait look ~20x slower than synchronous target
regions. We tracked it to the staging buffer used for the kernel’s array-descriptor upload.

Reproducer

staging.F90 (attached, ~60 lines) runs one small target teams loop nowait + taskwait per
repetition on an allocatable that is already present from target enter data, and times each
repetition.

nvfortran -O2 -mp=gpu -gpu=cc80 -o staging staging.F90
./staging 0    # allocatable referenced directly in the region
./staging 1    # same kernel on an explicit-shape dummy

A100-PCIE-40GB, driver 580.178.04, nvfortran 26.3-0, ms per repetition:

run rep 1 reps 2-32 mean reps 33+ mean first 32 total
mode 0, allocatable 23.4 21.97 0.021 704
mode 1, explicit-shape dummy 1.19 0.49 0.015 16
mode 0, NVCOMPILER_ACC_BUFFERSIZE=65536 1.82 0.57 0.018 19
mode 0, NVCOMPILER_OMP_AUTO_STREAMS=false 23.7 0.017 0.016 24

What gdb shows

Breaking on cuMemHostAlloc in mode 0 gives exactly 32 calls of 64 MiB, one per auto stream,
each with this stack:

cuMemHostAlloc bytes=67108864 flags=0x0
#1 __pgi_uacc_cuda_get_buffer (size=128, qq=0, download=0)      cuda_buffer.c:89
#2 __pgi_uacc_cuda_dataup1 (elementsize=128, name="descriptor", async=0)
#5 __pgi_uacc_create_or_update_descriptor (hostdescsize=128, hostdescptr=0x7fffffffa1d0, async=0)
   ... next calls: qq=1 async=1, qq=2 async=2, ...

Mode 1, explicit shape, makes no cuMemHostAlloc calls at all.

  • What is uploaded: every region re-uploads the 128-byte descriptor of a (176 bytes for a
    2-D array). The OpenMP kernel reads the array bounds from the device copy of the descriptor.
    The equivalent OpenACC parallel loop async(k) gets the bounds as by-value kernel arguments
    and uploads nothing.

  • Why it pins memory: with nowait the upload is asynchronous and its source is a host
    stack temporary. The first asynchronous pageable upload on a (host thread, queue) pins a
    staging buffer of 4 x NVCOMPILER_ACC_BUFFERSIZE (default 16 MiB), so 64 MiB. Pinning 64 MiB
    takes ~22 ms on this node.

  • Where the 32 comes from: the OpenMP auto streams are OpenACC async queues 0-31, so it
    happens 32 times. With launches from several host threads it happens per thread.

  • Not OpenMP-specific: an asynchronous update from pageable memory pays the same cost in
    both models (async_update.F90, attached, no kernels):

    • !$acc update device/self(a(:,k)) async(k) over 4 queues: 4 x 64 MiB cuMemHostAlloc
    • !$omp target update to/from(a(:,k)) nowait: 8 x 64 MiB. The from-updates probably land
      on 4 further streams. [unverified]

    The OpenACC kernels just never trigger it.

Questions / suggestions

  1. Could OpenMP kernels take array bounds by value, as the OpenACC ones do? Then a region on
    present data would need no host-to-device copy at launch.
  2. Failing that, could small descriptor-sized copies use a small pre-pinned pool, separate from
    the bulk staging buffer? Or could the upload be skipped when the descriptor has not changed
    since the last region?
  3. Is pinning 4 x NVCOMPILER_ACC_BUFFERSIZE lazily on the launching thread intended?
    Allocating it at stream creation, or growing it on demand, would avoid a 22 ms stall
    inside a timed region.

Workarounds we use

  • explicit-shape dummy arguments (no descriptor, so no pinned allocation)
  • NVCOMPILER_ACC_BUFFERSIZE=65536, which shrinks the buffer, though large pageable async
    transfers are then staged in smaller chunks
  • NVCOMPILER_OMP_AUTO_STREAMS=false (one stream, so only one buffer, but no concurrency)
  • 32 warm-up nowait regions at startup

Hi Utheri,

For #1, and #2, I’m not really sure. I think there’s technical reasons why it’s this way, but I’ll ask someone from our OpenMP.

For #3, yes this is intended. Both our OpenMP and OpenACC implementations share the same data management system.

By default, the runtime creates a double buffering system to perform data transfers. Data transfers need to be performed from physical, “pinned”, memory so the OS doesn’t swap out virtual memory mid-transfer. The compiler copies the data from virtual memory to the pinned buffer and starts the transfer. While this transfers, the second buffer is loaded. This process hides much of the virtual to pinned memory copy cost and helps the overall performance of data transfers.

Note that you can disable the use of the pinned memory buffers via the environment variable NVCOMPILER_ACC_CUDA_NOCOPY=1. However, the data transfer time could be penalized depending on how much data and how often the data is transferred. Lowering the buffer size via NVCOMPILER_ACC_BUFFERSIZE can help with the allocation cost, but smaller buffers mean more transfers are needed. So be sure to look at the overall performance, including data transfers, before deciding if the fixed cost of buffer allocation is more expensive than the cost of increased data transfer time.

The stream and buffer creation (each stream needs its own buffers) can be expensive, but only occurs once per stream construction. The cost is primarily due to the OS call for pinned memory allocation. The streams are persistent so the cost is amortized over the whole program.

OpenACC’s async has two functions. async itself lets host execution continue past the region. With a queue id, it also maps directly to a CUDA stream for concurrent execution on the device. Using async(1) only creates one stream. While async(k) creates a stream for each distinct k, so the buffer creation penalty can occur. I typically recommend using a fixed number of streams, something like async(mod(k,4)+1), so streams are reused. Rarely does using a large number of streams help, as most kernels utilize much of the GPU so the extra streams would need to wait for device resources to free up.

OpenMP doesn’t have a direct correlation between nowait and streams, so the compiler has to infer the mapping. Given that, it assumes you want concurrency and spreads nowait regions across up to 32 streams rather than reusing them. If you don’t need concurrent kernels, set NVCOMPILER_OMP_AUTO_STREAMS=false so a single stream is used, similar to OpenACC async(1). If you do want concurrency and need explicit control over how many streams are used and how work maps to them, OpenACC async(n) gives you that directly.

My advice is to look at the overall performance rather than the fixed cost of the buffer allocation, as for most codes this cost gets amortized. If using OpenACC, limit the number of streams to just a few, with the actual number determined by looking at the device utilization of the kernel.

-Mat

Here’s engineering’s response for #1 and #2

#1 Bounds are already passed by value. In the PTX the OpenMP kernel takes bounds as scalar params (constant and runtime bounds, 2-D with non-default lbound), identical to the OpenACC kernel. Neither reads a descriptor.

#2: This is a compiler issue. The descriptor is passed to the runtime only when the scope has “contains”. Without it, or in a module procedure, there is no upload and no pin. The runtime creates the device descriptor at region entry and deletes it at exit, so every region re-uploads. Filed as TPR#39186.