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 OpenACCparallel loop async(k)gets the bounds as by-value kernel arguments
and uploads nothing. -
Why it pins memory: with
nowaitthe 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 xNVCOMPILER_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 MiBcuMemHostAlloc!$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
- 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. - 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? - Is pinning 4 x
NVCOMPILER_ACC_BUFFERSIZElazily 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 chunksNVCOMPILER_OMP_AUTO_STREAMS=false(one stream, so only one buffer, but no concurrency)- 32 warm-up
nowaitregions at startup