OpenACC Fortran maxval

Hello,

I was wondering why this code does not work with the pgi compiler.

program minmax


  implicit none

  integer, parameter  :: N=300
  real, allocatable   :: a(:,:,:)
  integer             :: i,j,k
  real                :: maxa

  allocate(a(N,N,N))

  a = 1.0

  !$ACC PARALLEL
  maxa = maxval(a)
  !$ACC END PARALLEL

  print *, maxa

end program minmax

Hi MHUGUES,

This is a case where you want to use the “kernels” rather that “parallel” directive. Maxval gets inlined and includes several loops. Since “kernels” allow the compiler to discover parallelization, it’s able to properly to accelerate the loops.

% cat tst.f90
program minmax


  implicit none

  integer, parameter  :: N=300
  real, allocatable   :: a(:,:,:)
  integer             :: i,j,k
  real                :: maxa

  allocate(a(N,N,N))

  a = 1.0

  !$ACC KERNELS
  maxa = maxval(a)
  !$ACC END kernels
  print *, maxa

end program minmax
% pgf90 tst.f90 -Minfo=accel -acc; a.out
minmax:
      0, Accelerator kernel generated
     15, Generating implicit copyin(a(1:300,1:300,1:300))
     16, Loop is parallelizable
         Accelerator scalar kernel generated
         Accelerator kernel generated
         Generating Tesla code
         16, !$acc loop gang, vector(32) ! blockidx%x threadidx%x
             !$acc loop gang, vector(4) ! blockidx%z threadidx%y
             !$acc loop gang ! blockidx%y
             Generating implicit reduction(max:a$r)
    1.000000
  • Mat

Thanks Matt.

It is working with KERNELS indeed