Is a Fortran "pointer" available to allocate a var

According to CUDA Fortran Programming Guide, a Fortran “allocatable” is available to allocate a variable in GPUs as follows.


integer, allocatable, device :: ndev
allocate(ndev)


What about a Fortran “pointer” ?


integer, pointer, device :: ndev
allocate(ndev)


Hi KOUCHI_Hiroyuki,

In this case, the pointer would be treated the same as an allocatable array and the pointer’s device data created on the device when allocated.

Though, a pointer to a scalar is very inefficient (host or device) due to the extra storage needed to hold the pointer’s descriptor. Is there a reason why you wouldn’t just use the scalar directly?

-Mat

Dear Mat-san,

Thank you for the reply. Sorry, I made a mistake
Actually, I don’t know if the following test code work normally using CUDA Fortran on a GPU.
When a program calls the following subroutine “init”, the following array “d_a” is allocated in the subroutine “init”.
I suppose the program retains the memory area of array “d_a” in the GPU until the program calls the following subroutine “finalize” and the array “d_a” is deallocated.
When I checked PGI documents and googled, I found the attribute “Allocatable” is just only used. So I suppose CUDA Fortran is not available to use the attribute"Pointer".
If the attribute “Pointer” is not available, when the program returns from the subroutine “init”, I suppose the program releases the memory area of array “d_a” in the GPU.
According to the Fortran standard, a program returns from a subroutine, Fortran does not guarantee that the program retains a allocated memory area with the attribute “Allocatable”.
In the case of attribute “Pointer”, even if a program returns from a subroutine, Fortran guarantee that the program retains a allocated memory area until the program calls the statement “deallocate”.

So I wonder if it is possible for me to write the following source code using CUDA Fortran and the program works normally on a GPU.


---------------- kernels.cuf --------------------------

module gpu_kernels
use cudafor
implicit none
integer,dimension(:),pointer,device :: d_a
contains

subroutine init(n)
integer(kind=4),intent(in) :: n

allocate(d_a(n))

end subroutine init

subroutine finalize

deallocate(d_a)

end subroutine finalize

attributes(global) subroutine kernel1

end subroutine kernel1

attributes(global) subroutine kernel2

end subroutine kernel2

end module gpu_kernels


------------------ main.cuf -----------------------------

program test
use gpu_kernels
implicit none
integer,parameter :: n = 1000000

call init(n)




call finalize

end program test


Hi KOUCHI_Hiroyuki,

Are you actually encountering an error or are you speculating?

In this case “pointer” and “allocatable” are basically interchangeable. Actually, I’m not sure why you aren’t using “allocatable”, but also don’t see a reason why “pointer” wouldn’t work. If it’s not working, please post a reproducing example.

According to the Fortran standard, a program returns from a subroutine, Fortran does not guarantee that the program retains a allocated memory area with the attribute “Allocatable”.

I think this only applies if you’re passing in the array to a subroutine not within a module. They way you have it here, i.e. using an module initialization routine, is common.

-Mat

Thank you for the reply.
I have not encountered an error. Also a program work normally even if I use the attribute “pointer” instead of the attribute “allocatable”.
When I read PGI documents, I did not see if the attribute “allocatable” is available.
If I encounter an error about memory allocation, I will post again.

Thank you for your advice.