!
! Copyright (C) 2021, MaX CoE
! Distributed under the MIT License 
! (license terms are at http://opensource.org/licenses/MIT).
!
!--
!
! Auxiliary functions
!
! AF: OpenACC support is incomplete !!
!
!==================================================================
!==================================================================
! *DO NOT EDIT*: automatically generated from devxlib_auxfunc_addscal.jf90
!==================================================================
!==================================================================
!
#include<devxlib_macros.h>
#include<devxlib_defs.h>
!
!=======================================
!
submodule (devxlib_auxfunc) devxlib_auxfunc_addscal

   implicit none

   contains
{%- for t in types %}
{%- for p in precision %}
{%- for d in range(1,dimensions+1) %}
      module subroutine {{p.name}}_devxlib_mem_addscal_{{t[0]|lower}}{{d}}d(array_out, array_in, scal, &
                                                  {% for dd in range(d) -%}
                                                  {{ "range%s"|format(dd+1) }}, {{ "lbound%s"|format(dd+1) }}{% if not loop.last %}, &
                                                  {% endif %}{% endfor %} )
         use iso_fortran_env
         implicit none
         !
         {{t}}({{p.val}}), intent(inout) DEV_ATTR :: array_out({% for dd in range(d) %}:{% if not loop.last %}, {%- endif %}{% endfor %})
         {{t}}({{p.val}}), intent(in)    DEV_ATTR :: array_in({% for dd in range(d) %}:{% if not loop.last %}, {%- endif %}{% endfor %})
         {{t}}({{p.val}}), optional, intent(in) :: scal
         integer, optional, intent(in) :: {% for dd in range(d) %} {{ "range%s(2)"|format(dd+1) }}{% if not loop.last %}, {%- endif %}{% endfor %}
         integer, optional, intent(in) :: {% for dd in range(d) %} {{ "lbound%s"|format(dd+1) }}{% if not loop.last %}, {%- endif %}{% endfor %}
         !
{%-  if 'complex' in t|lower %}
         {{t}}({{p.val}}) :: scal_ = (1.0, 0.0)
{%- else %}
         {{t}}({{p.val}}) :: scal_ = 1.0
{%- endif -%}
{%- for dd in range(d) %}
         integer :: i{{dd+1}}, d{{dd+1}}s, d{{dd+1}}e
         integer :: lbound{{dd+1}}_, range{{dd+1}}_(2)
{%- endfor %}
         !
{%- for dd in range(d) %}
         lbound{{dd+1}}_=1
         if (present(lbound{{dd+1}})) lbound{{dd+1}}_=lbound{{dd+1}} 
         range{{dd+1}}_=(/1,size(array_out, {{dd+1}})/)
         if (present(range{{dd+1}})) range{{dd+1}}_=range{{dd+1}} 
         !
         ! the lower bound of the assumed shape array passed to the subroutine is 1
         ! lbound and range instead refer to the indexing in the parent caller.
         d{{dd+1}}s = range{{dd+1}}_(1) -lbound{{dd+1}}_ +1
         d{{dd+1}}e = range{{dd+1}}_(2) -lbound{{dd+1}}_ +1
         !
{%- endfor %}
         if (present(scal)) scal_=scal
         !
         !DEV_CUF kernel do({{d}})
{%- for dd in range(d,0,-1) %}
         do i{{dd}} = d{{dd}}s, d{{dd}}e
{%- endfor %}
            array_out( {%- for dd in range(d) %}i{{dd+1}}{% if not loop.last %}, {%- endif %} {%- endfor %} ) = &
                array_out( {%- for dd in range(d) %}i{{dd+1}}{% if not loop.last %}, {%- endif %} {%- endfor %} ) + &
                scal_*array_in( {%- for dd in range(d) %}i{{dd+1}}{% if not loop.last %}, {%- endif %} {%- endfor %} )
{%- for dd in range(d) %}
         enddo
{%- endfor %}
         !
      end subroutine {{p.name}}_devxlib_mem_addscal_{{t[0]|lower}}{{d}}d
      !
{%- endfor %}
{%- endfor %}
{%- endfor %}

end submodule devxlib_auxfunc_addscal
