module memory

  use iso_fortran_env, only : real64

  implicit none

  real(real64), public, allocatable :: arr(:,:,:)
  real(real64), parameter :: zero = 0.0_real64
  !$OMP threadprivate(arr)

  interface
    module subroutine myalloc()
    end subroutine

    module subroutine mydealloc()
    end subroutine
  end interface
end module


submodule (memory) basic
contains
  module subroutine myalloc()
    integer :: i(10000)
    !$OMP parallel
    allocate(arr(1:3,1:8,1:2), source=zero)
    !$OMP end parallel
  end subroutine

  module subroutine mydealloc()
    !$OMP parallel
    if (allocated(arr)) deallocate(arr)
    !$OMP end parallel
  end subroutine
end submodule

program test_openmp
  !$ use omp_lib, only:  omp_set_num_threads,  omp_get_thread_num
  use iso_fortran_env, only : real64
  use memory, only : myalloc, mydealloc
  implicit none

  integer :: N, i, summ, nthreads

  N = 5
  nthreads = 25

  call omp_set_num_threads(nthreads)

  do i = 1,N
    call myalloc()
    call mydealloc()
  enddo

  write(*,*) "All done :/"

end program
