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
andWHERE
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 thatFORALL
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. AFORALL
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 atDO
loop vectorizations and you are not likely to notice any improvement by usingFORALL
.See a nice discussion on
FORALL
andWHERE
hereIf 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.
Forall
construct proved to be really too restrictive and is mostly useful only for array operations. For exact limitations see IBM Fortran - FORALL. Less restrictive is ado concurrent
construct of Fortran 2008. Evenread
andwrite
statements are allowed there. See Intel Fortran - DO CONCURRENT and New features of Fortran 2008.