我正在尝试在驻留在 GPU 内存中的“设备”数据结构中使用可分配数组。代码(粘贴在下面)编译,但给出了段错误。我在做一些明显错误的事情吗?
模块文件称为“gpu_modules.F90”,如下所示:
!=============
! This module contains definitions for data structures and the data
! stored on the device
!=============
module GPU_variables
use cudafor
type :: data_str_def
!=============
! single number quantities
!=============
integer :: i, j
real(kind=8) :: a
!=============
! Arrays
!=============
real(kind=8), allocatable :: b(:)
real(kind=8), allocatable :: c(:,:)
real(kind=8), allocatable :: d(:,:,:)
real(kind=8), allocatable :: e(:,:,:,:)
end type data_str_def
!=============
! Actual data is here
!=============
type(data_str_def), device, allocatable :: data_str(:)
contains
!=============
! subroutine to allocate memory
!=============
subroutine allocate_mem(n1)
implicit none
integer, intent(in) :: n1
call deallocate_mem()
write(*,*) 'works here'
allocate(data_str(n1))
write(*,*) 'what about allocating memory?'
allocate(data_str(n1) % b(10))
write(*,*) 'success!'
return
end subroutine allocate_mem
!=============
! subroutine to deallocate memory
!=============
subroutine deallocate_mem()
implicit none
if(allocated(data_str)) deallocate(data_str)
return
end subroutine deallocate_mem
end module GPU_variables
主程序是'gpu_test.F90',如下所示:
!=============
! main program
!=============
program gpu_test
use gpu_variables
implicit none
!=============
! local variables
!=============
integer :: i, j, n
!=============
! allocate data
!=============
n = 2 ! number of data structures
call allocate_mem(n)
!=============
! dallocate device data structures and exit
!=============
call deallocate_mem()
end program
编译命令(来自当前文件夹)是:
pgfortran -Mcuda=cc5x *.F90
终端输出:
$ ./a.out
works here
what about allocating memory?
Segmentation fault (core dumped)
任何帮助/见解和解决方案将不胜感激。不,使用指针不是一个可行的选择。
编辑:另一个可能相关的细节:我正在使用 pgfortran 版本 16.10