!
! Copyright (C) 2022, MaX CoE
! Distributed under the MIT License 
! (license terms are at http://opensource.org/licenses/MIT).
!
!--
!
! Utility functions to check host memory mapping state
! using CUDA-Fortran, OpenACC or OpenMP Offload
!
!==================================================================
!==================================================================
! *DO NOT EDIT*: automatically generated from device_mapping_mapped.jf90
!==================================================================
!==================================================================
!
#include<devxlib_macros.h>
#include<devxlib_defs.h>
!
!=======================================
!
submodule (devxlib_mapping) devxlib_mapping_mapped

   implicit none
     !
     ! _devxlib_mapping_mapped_  functions will return allocated(array)
     !                           when no GPU support is present
     !
   contains
{%- for t in types %}
{%- for p in kinds[t] %}
{%- for d in range(1,dimensions+1) %}
      module function {{p.name}}_devxlib_mapping_mapped_{{t[0]|lower}}{{d}}d(array) result(res)
         implicit none
         !
         logical :: res
#if defined __DXL_OPENMP_GPU
         {{t}}({{p.val}}), allocatable, target :: array({% for dd in range(d) %}:{% if not loop.last %}, {%- endif %}{% endfor %})
#else
         {{t}}({{p.val}}), allocatable DEV_ATTR :: array({% for dd in range(d) %}:{% if not loop.last %}, {%- endif %}{% endfor %})
#endif
         !
         res = .false.
         !
#if defined __DXL_CUDAF
         res=allocated(array)
#elif defined __DXL_OPENACC
         res=allocated(array)
         if (res) res=acc_is_present(array)
#elif defined __DXL_OPENMP_GPU
         res=allocated(array)
         if (res) res=(omp_target_is_present(c_loc(array), omp_get_default_device())/=0)
#else
         res=allocated(array)
#endif
         return
      end function {{p.name}}_devxlib_mapping_mapped_{{t[0]|lower}}{{d}}d
      !
{%- endfor %}
{%- endfor %}
{%- endfor %}

endsubmodule devxlib_mapping_mapped
