I need to use derived type array in my code. So I started with the following test code and it worked:
module structure
type mytype
real,allocatable :: a(:)
end type mytype
type mytypeDevice
real,managed,allocatable :: a(:)
end type mytypeDevice
end module structure
program main
use cudafor
use structure
type(mytype),allocatable :: T(:)
type(mytypeDevice),allocatable,managed :: T_Device(:)
! For the host
allocate(T(3))
do i=1,3
allocate(T(i)%a(10))
end do
T(1)%a=1; T(2)%a=2; T(3)%a=3
print *, 'Host derived array is ok'
! For the managed derived array
allocate(T_Device(3))
do i=1,3
allocate(T_Device(i)%a(10))
end do
do i=1,3
T_Device(i)%a=T(i)%a
end do
print *, 'Managed derivd array is okay.'
end program main
However, the following code failed compiling when I tried to access the derived type array inside a kernel subroutine:
module structure
type mytype
real,allocatable :: a(:)
end type mytype
type mytypeDevice
real,managed,allocatable :: a(:)
end type mytypeDevice
end module structure
program main
use cudafor
use structure
interface
attributes(global) subroutine testkernel(T_device)
use structure
type(mytypeDevice),managed :: T_device(3)
end subroutine testkernel
end interface
type(mytype),allocatable :: T(:)
type(mytypeDevice),allocatable,managed :: T_Device(:)
! For the host
allocate(T(3))
do i=1,3
allocate(T(i)%a(10))
end do
T(1)%a=1; T(2)%a=2; T(3)%a=3
print *, 'Host derived array is ok'
! For the managed
allocate(T_Device(3))
do i=1,3
allocate(T_Device(i)%a(10))
end do
do i=1,3
T_Device(i)%a=T(i)%a
end do
print *, 'Managed derivd array is okay.'
call testkernel<<<1,256>>>(T_device)
end program main
attributes(global) subroutine testkernel(T_device)
use cudafor
use structure
implicit none
type(mytypeDevice),managed :: T_device(3)
integer i
i = (blockidx%x-1)*blockdim%x+threadidx%x
if(i.eq.1)write(*,*)'T_device(1)%a=',T_device(1)%a
if(i.eq.1)write(*,*)'T_device(2)%a=',T_device(2)%a
if(i.eq.1)write(*,*)'T_device(3)%a=',T_device(3)%a
end subroutine testkernel
The compiling error message is as follows:
PGF90-F-0155-Compiler failed to translate accelerator region (see -Minfo messages): Unexpected runtime function call (testDerived2.cuf: 1)
PGF90/x86-64 Linux 16.5-0: compilation aborted.
Please help me understand what is wrong in the code, or what is the right way to use the derived array in kernel routines. Thanks a lot!