OpenMP and Allocate

Section 11 of the OpenMP 5.2 Examples Book..

It is not supported yet in gfortran. From the Intel compilers you’ll have to use the latest one ifx 2023.2.1.

Here is an example that seems to work in Compiler Explorer,

! omp_alloc_test.f90 --
!     Demonstration of OpenMP 5.2 allocators
use omp_lib

integer, parameter :: dp = kind(1.0d0)
real(dp), allocatable :: x(:), y(:)
real(dp) :: a

integer(omp_memspace_handle_kind )  :: xy_memspace = omp_default_mem_space
type(omp_alloctrait)                :: xy_traits(1) = &
                                            [omp_alloctrait(omp_atk_alignment,64)]
integer(omp_allocator_handle_kind)  :: xy_alloc

integer :: i, n

xy_alloc = omp_init_allocator( xy_memspace, 1, xy_traits)

n = 1000

!$omp allocators allocate(xy_alloc: x,y)
allocate(x(n),y(n))

! loc is a non-standard extension, but in practice all compilers have it
if(modulo(loc(x),64) /= 0 .and. modulo(loc(y),64) /=0 ) then
   print*,"ERROR: x|y not 64-byte aligned"
   stop
endif

y = 0
x = 4
a = 1.0_dp/7.0_dp

! daxpy
!$omp simd simdlen(4) aligned(x,y: 64)
do i = 1, n
   y(i) = y(i) + a*x(i)
end do

deallocate(x,y)
call omp_destroy_allocator(xy_alloc)

end