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

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

问题描述

有没有办法用Fortran堆栈上的 以创建可变大小的数组?分配()对我不起作用,因为它把堆的阵列。这可能会导致问题并行(见我的其他问题:
<一href=\"http://stackoverflow.com/questions/6605677/openmp-poor-performance-of-heap-arrays-stack-arrays-work-fine/\">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];

再次重申:我不想

To re-iterate: I do NOT want

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))

由于它把堆的阵列。

帮助非常AP preciated。我很高兴与分配数组,直到我最近遇到与上面提到的问题的问题。如果有一个否定的回答这个问题,我将非常AP preciate到源的链接。

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.的答案。这样做的一个优雅的方式只在2008年的Fortran成为可能,它是在块中完成结构。

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中没有分配()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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