Fortran forall 限制 [英] Fortran forall restrictions

查看:20
本文介绍了Fortran forall 限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 forall 来分配动态数组,但 gfortran 不喜欢这样.我还发现 write 语句在 forall 块中是被禁止的,我怀疑 read 语句也是如此.

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.

forall 块中不允许有哪些其他功能/操作?

What other functions/operations are not permitted in a forall block?

除了在顺序无关紧要时有时替换 do 循环之外,这个构造究竟是做什么用的?我认为它会使编码更加清晰和优雅,尤其是在操作顺序不重要时显示,但是对于可以在 forall 中完成的操作似乎非常有限制.

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.

这些限制的原因是什么,即它们保护/防止用户搞砸的原因是什么?使用 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?

现在在我正在处理的代码中只有一个 forall 块,如果我在 do 循环中将其全部翻译出来,它将产生四个嵌套循环.哪种方式更好?

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?

推荐答案

现在已经不太需要 FORALLWHERE 构造了.它们是作为 Fortran 95(Fortran 90 的小扩展)的一部分引入的,主要是为了优化,当时代码向量化是 HPC 中的主要内容.FORALL 之所以在应用中如此受限,正是因为它是为循环优化而设计的.另请注意,FORALL 不是循环构造,而是赋值.因此,块内只允许赋值语句.理论上,DO 循环给出了关于处理器将要循环的索引顺序的明确说明.FORALL 构造允许编译器根据数组在内存中的存储方式选择最佳顺序.然而,随着时间的推移,这已经失去了意义,因为现代编译器非常擅长 DO 循环向量化,并且您不太可能注意到使用 FORALL 的任何改进.

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.

查看关于 FORALLWHERE 这里的精彩讨论

See a nice discussion on FORALL and WHERE here

如果您担心代码性能,您可能更愿意考虑使用不同的编译器 - PGIifort.根据我自己的经验,gfortran 适合开发,但不适合 HPC.您会注意到使用 pgf90 或 ifort 编译的代码的执行速度提高了几倍.

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

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