program main
  implicit none
  real, allocatable :: a(:)[:]
  integer :: i, me, ne
  me = this_image()
  ne = num_images()
  allocate (a(ne)[*])
  a = 0
  do i = 1, me
    a(i) = i
  end do
  sync all
  print *, "Image", me, ":", myfunc(a)
contains
  real function myfunc(x)
    real, intent(in) :: x(:)[*]
    integer :: i, me, ne
    me = this_image()
    ne = num_images()
    myfunc = 0
    do i = 1, ubound(x, 1)
      if (me == 1) then
        myfunc = myfunc + x(i)[ne]
      else
        myfunc = myfunc + x(i)[me - 1]
      end if
    end do
  end function
end program
