Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在fortran子程序中分配矩阵_Fortran - Fatal编程技术网

在fortran子程序中分配矩阵

在fortran子程序中分配矩阵,fortran,Fortran,我是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

我是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
运行时,代码总是在
分配
行崩溃,错误为:
程序异常-访问冲突
。但是,如果
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

目前,这是整个程序(如您所见,我才刚刚开始)。

@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中工作,没有任何问题…

隔离的代码没有任何问题(
intent
会让我更高兴)。我们需要看到调用代码(以及其中的声明)才能说得更多。@francescalus:编辑以添加调用代码和声明。可能的重复需要一个显式接口,以使用可分配数组作为参数;最好是在模块中有子例程。特别是Jonathan Dursi的链接,请注意对显式接口的要求。主程序中似乎缺少此项。