当 Fortran 生成大型内部临时数组时,如何避免堆栈溢出? [英] How can I avoid a stack overflow when Fortran produces a large, internal, temporary array?

查看:47
本文介绍了当 Fortran 生成大型内部临时数组时,如何避免堆栈溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 Fortran 代码调用 RESHAPE 对矩阵进行重新排序,这样我现在要循环遍历的维度成为第一个可变维度(Fortran 中的列优先顺序).

I have some Fortran code that calls RESHAPE to reorder a matrix such that the dimension that I am now about to loop over becomes the first varying dimension (Column-major order in Fortran).

这与 C/Fortran 互操作性无关.

This has nothing to do with C/Fortran interoperability.

现在矩阵相当大,当我调用 RESHAPE 函数时,我得到一个段错误,我非常确信这是堆栈溢出.我知道这一点,因为我可以在 ifort 中使用 -heap-arrays 编译我的代码,问题就消失了.

Now the matrix is rather large and when I call the RESHAPE function I get a seg fault which I am very confident is a stack overflow. I know this because I can compile my code in ifort with -heap-arrays and the problem disappears.

我不想修改堆栈大小.此代码需要可移植到任何计算机上,而用户不必担心堆栈大小.

I do not want to modify the stack-size. This code needs to be portable for any computer without the user having to concern himself with stack-size.

有没有办法让我调用 RESHAPE 函数来使用堆而不是堆栈来使用其内部内存.

Is there someway I can get this call of the RESHAPE function to use the heap and not the stack for its internal memory use.

在最坏的情况下,我将不得不为此实例滚动我自己的"RESHAPE 函数,但我希望有更好的方法.

Worst case I will have to 'roll my own' RESHAPE function for this instance but I wish there was a better way.

推荐答案

Fortran 标准根本没有谈到栈和堆,这是一个实现细节.在内存的哪个部分放置东西以及是否有任何限制是实现定义的.

The Fortran standard does not speak about stack and heap at all, that is an implementation detail. In which part of memory something is placed and whether there are any limits is implementation defined.

因此,不可能从 Fortran 代码本身控制堆栈或堆的行为.如果您想指定这个并且编译器选项用于那个,则必须通过其他方式指示编译器.Intel Fortran 默认使用堆栈并具有 -heap-arrays n 选项(n 是限制,以 kB 为单位),gfortran 略有不同,具有相反的 -fstack-arrays选项(包含在 -Ofast 中,但可以禁用).

Therefore it is impossible to control the stack or heap behaviour from the Fortran code itself. The compiler must be instructed by other means if you want to specify this and the compiler options are used for that. Intel Fortran uses stack by default and has the -heap-arrays n option (n is the limit in kB), gfortran is slightly different and has the opposite -fstack-arrays option (included in -Ofast, but can be disabled).

这对各种临时数组和自动数组都有效.

This is valid for all kinds of temporaries and automatic arrays.

这篇关于当 Fortran 生成大型内部临时数组时,如何避免堆栈溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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