Fortran全部限制 [英] Fortran forall restrictions
问题描述
我试图使用 forall
分配动态数组,但gfortran不喜欢这样。我还发现在 forall
块中禁止 write
语句,并且我怀疑读取
语句也是如此。
$ forall
块中不允许使用其他函数/操作吗?
确切地说,这个结构是什么,除了有时在订单无关紧要时替换 do
循环?我认为这会使编码更加清晰和优雅,特别是当操作顺序不重要时显示,但似乎在 forall
内可以完成的操作非常有限。
这些限制的原因是什么,即它们保护/防止用户搞乱什么?使用 forall
是个好主意吗?如果是这样,为了什么目的?
现在在我正在处理的代码中,只有一个 forall
块,如果我翻译它所有在 do
循环中它会给出四个嵌套循环。哪种方式更好?
对于 FORALL
和 WHERE
现在构建。它们是作为Fortran 95(对Fortran 90的小扩展)的一部分引入的,主要是为了优化的目的,而代码向量化是HPC中的主要事情。 FORALL
在应用程序中如此受限的原因正是因为它是为循环优化而设计的。还要注意, FORALL
不是循环结构,而是赋值。因此,块中只允许赋值语句。理论上, DO
循环给出了关于处理器将循环的索引顺序的明确指令。一个 FORALL
结构允许编译器根据数组在存储器中的存储方式选择最优化的顺序。然而,随着时间的推移,这已经失去了意义,因为现代编译器非常擅长 DO
循环向量化,并且您不太可能注意到使用 FORALL
。
查看 FORALL
和 WHERE
< a href =https://stackoverflow.com/a/4141572/827297>这里
如果您担心代码性能,您可能更愿意想要考虑一个不同的编译器 - PGI 或 ifort 。根据我自己的经验,gfortran适合开发,但不适用于HPC。您会注意到使用pgf90或ifort编译的代码执行速度快几倍。
I tried to use forall
to allocate dynamic arrays, but gfortran didn't like that. I also found out that write
statements are forbidden in a forall
block ,and I suspect read
statements are too.
What other functions/operations are not permitted in a forall
block?
Exactly what is this construct for, besides sometimes replacing do
loops when order doesn't matter? I thought it would make coding more legible and elegant, especially showing when the order of operations are not important, but it seems quite restrictive with what operations can be done inside a forall
.
What are the reasons for these restrictions, i.e. what do they protect/prevent the user from messing up? Is it a good idea to use forall
? If so, for what purposes?
Right now in the code I'm working on there is only one forall
block, and if I translated it all out in do
loops it would give four nested loops. Which way is better?
There is not much need for FORALL
and WHERE
constructs nowadays. They were introduced as part of Fortran 95 (minor extension to Fortran 90), mostly for the purpose of optimization, when code vectorization was a major thing in HPC. The reason that FORALL
is so limited in application is exactly because it was designed for loop optimization. Also note that, FORALL
is not a looping construct, but assignment. Thus, only assignment statements are allowed inside the block. In theory, DO
loops give explicit instructions about the order of indices that the processor is going to loop over. A FORALL
construct allows the compiler to choose the most optimal order based on how the array is stored in memory. However, this has lost meaning over time, since modern compilers are very good at DO
loop vectorizations and you are not likely to notice any improvement by using FORALL
.
See a nice discussion on FORALL
and WHERE
here
If you are worried about code performance, you may rather want to consider a different compiler - PGI or ifort. From my own experience, gfortran is suitable for development, but not really for HPC. You will notice up to several times faster execution with code compiled with pgf90 or ifort.
这篇关于Fortran全部限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!