Are implied DO loops inefficient?

2019-07-29 17:00发布

I have an array initialization based on an implied do loop, given an odd size N.

J=(N+1)/2
XLOC(1:N) = (/ (I-J, I=1,N) /)

In the context of F90+ is it recommended to use the (/ .. /) syntax, or is more efficient to use a FORALL statement.

Example: for N=19 then XLOC=(-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9)

How else would you initialize this array?


Edit 1

How would you initialize this array with more readable code?

2条回答
Root(大扎)
2楼-- · 2019-07-29 17:54

For such a simple construct both are likely to lead to the same code because compilers are good at optimizing. The FORALL statement is not so much a looping statement but an initialization statement that has many restrictions that can inhibit optimizations. If a simple loop will work, I'd use it.

Also see this previous answer: Do Fortran 95 constructs such as WHERE, FORALL and SPREAD generally result in faster parallel code?

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-29 17:55

There is no reason they should be less efficient that actual do loops. If you find a case, where they are, report it as an missed optimization bug to your compiler vendor!

查看更多
登录 后发表回答