我是Fortran的新手(在C中更有经验),我正在尝试使用allocate
命令为值矩阵分配内存。代码是:
module SMS
contains
subroutine readSMS(elem)
real, dimension(:,:), allocatable :: elem
allocate( elem(3792, 3) ) ! this will be a variable later on, for now I've
! hard-coded the number of elements in my test case
end subroutine
end module
运行时,代码始终在allocate
行崩溃,错误为:Program exception - access violation
。但是,如果elem
是局部变量而不是传递给子例程的变量,则此程序可以正常工作。在C中,我会使用类似的东西:
double **elem;
readSMS(&elem); //pass in a pointer to elem so that memory can be allocated
在Fortran中是否有类似的方法可用于在子例程中分配内存?
这是对函数的调用:
program main
use SMS
implicit none
include 'mpif.h'
! Variables
integer ierr,num_procs,my_id
integer num_elem,num_regions
real, dimension(:,:), allocatable :: elem
real, dimension(:,:), allocatable :: regions
! Body of main
call MPI_INIT(ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD,my_id,ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD,num_procs,ierr)
call readSMS(elem)
deallocate(elem)
call MPI_FINALIZE(ierr)
end program main
目前,这是整个程序(你可以看到,我刚刚开始)。
答案 0 :(得分:0)
@francescalus我认为子程序readSMS在模块中,所以显式接口不是问题。 @wolfPack88查看@Jonathan提供的链接,你就能找到解决方案。
更新
module SMS
contains
subroutine readSMS(elem)
real, dimension(:,:), allocatable :: elem
allocate( elem(10, 3) )
elem=10
end subroutine
end module
program main
use SMS
implicit none
real, dimension(:,:), allocatable :: elem
call readSMS(elem)
print *, elem
deallocate(elem)
end program main
此代码在ifort中无任何问题...