Fortran 中没有 Allocate() 的可变大小数组 [英] Variable size arrays in Fortran without Allocate()

查看:97
本文介绍了Fortran 中没有 Allocate() 的可变大小数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Fortran 中在堆栈上创建可变大小的数组?Allocate() 对我不起作用,因为它将数组放在堆上.这可能会导致并行化问题(请参阅我的另一个问题:OpenMP:堆数组性能不佳(堆栈数组工作很好)).当然,一些智能内存管理可以解决这个问题,但 Fortran 中的内存管理听起来很傻.

Is there a way to create variable size arrays in Fortran on the stack? Allocate() does not work for me, because it places the array on the heap. This may lead to problems with parallelization (see my other question: OpenMP: poor performance of heap arrays (stack arrays work fine) ). Of course, some smart memory management would give a way around that problem, but memory management in Fortran sounds silly.

本质上,我在 C 中寻找与以下内容等效的 Fortran:

Essentially, I am looking for a Fortran equivalent of the following in C:

scanf("%d", N);
int myarray[N];

重申:我不想要

Integer, PARAMETER :: N=100
Integer, Dimension(N) :: myarray

因为这在编译时决定了数组的大小.我也不想

because this determines the array size at compile time. Neither do I want

Integer, Dimension(:), Allocatable :: myarray
read(*,*) N
Allocate(myarray(1:N))

因为它将数组放在堆上.

because it places the array on the heap.

非常感谢帮助.我对 Allocatable 数组非常满意,直到我最近遇到上述问题中的问题.如果这个问题的答案是否定的,我将非常感谢源链接.

Help very much appreciated. I was very happy with Allocatable arrays until my recent encounter with the problem in the question cited above. If there is a negative answer to this question, I would very much appreciate a link to the source.

查看对 M.S.B. 的回答的评论.只有在 Fortran 2008 中才有了一种优雅的方式来做到这一点,它是在 block 结构中完成的.

see comments to M.S.B.'s answer. An elegant way of doing this only became possible in Fortran 2008, and it is done in a block construct.

推荐答案

Fortran 可以自动创建数组,只需在子例程入口处声明,只要维度在运行时已知......这不需要要声明参数属性的维度,它们可以是参数,例如,

Fortran can automatically create arrays just with declarations on entry to subroutines, as long as the the dimensions are known at run time ... this doesn't require the dimensions to be declared parameter attribute, they can be arguments, e.g.,

subroutine MySub ( N )

integer, intent (in) :: N
real, dimension (N) :: array

有效.那么为什么不在主程序或某个子程序中决定您的大小N",然后调用另一个子程序继续.可能使用这种方法,数组将在堆栈中.正如@eriktous 所写,Fortran 语言标准没有指定这种行为.一些编译器将本地数组切换到超过特定大小的堆.一些编译器提供了控制这种行为的选项.将大型数组放在堆上可能会被递归或 OpenMP 覆盖.

is valid. So why not decide your size "N" in the main program, or some subroutine, then call another subroutine to continue. Likely with this approach the array will be on the stack. As @eriktous wrote, the Fortran language standard doesn't specify this behavior. Some compilers switch local arrays to the heap past a certain size. Some compilers provide options to control this behavior. Placing large arrays on the heap would probably be overridden with recursive or OpenMP.

您还可以将可分配数组作为实际参数传递给子例程,而无需将子例程的伪参数声明为可分配的.这可能无助于您的担忧,因为原始数组仍可能在堆上.

You can also pass an allocatable array as an actual argument to a subroutine without the dummy argument of the subroutine being declared as allocatable. Which may not help with your concern because the original array will still likely be on the heap.

这篇关于Fortran 中没有 Allocate() 的可变大小数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆