# OpenMP and Allocate

**URL:** <https://fortran-lang.discourse.group/t/openmp-and-allocate/6795>\
**Category:** Uncategorized\
**Created:** [November 14, 2023, 5:33am UTC](https://fortran-lang.discourse.group/t/openmp-and-allocate/6795 "2023-11-14T05:33:49Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [November 14, 2023, 7:19am UTC](https://fortran-lang.discourse.group/t/openmp-and-allocate/6795/5 "2023-11-14T07:19:57Z")

</div>

> [@JohnCampbell](#):
>
> Do you know where there is a Fortran code example of this being achieved, ie selecting that an allocatable array in a !$OMP region is positioned to the start of a memory page ?

Section 11 of the[OpenMP 5.2 Examples Book.](https://www.openmp.org/wp-content/uploads/openmp-examples-5.2.1.pdf).

> [@JohnCampbell](#):
>
> Also, from what version of Gfortran is this syntax supported ?

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](https://godbolt.org/z/sYsan5oqz),

```auto
! 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

```

---

_[View the full topic](https://fortran-lang.discourse.group/t/openmp-and-allocate/6795)._
