Fortran forall restrictions
Asked Answered
H

2

13

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?

Hillari answered 22/12, 2011 at 10:33 Comment(1)
Doesn't FORALL require PURE procedures & functions?Subcortex
K
19

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.

Kaela answered 22/12, 2011 at 15:57 Comment(0)
F
8

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 a do concurrent construct of Fortran 2008. Even read and write statements are allowed there. See Intel Fortran - DO CONCURRENT and New features of Fortran 2008.

Fulbert answered 22/12, 2011 at 11:1 Comment(2)
Not many compilers support Fortran 2008 yet, though. I'm using gfortran, the latest stable version (4.6?)Hillari
Gfortran 4.6 has partial support of it fortranwiki.org/fortran/show/Fortran+2008+status and gcc.gnu.org/onlinedocs/gfortran/Fortran-2008-status.html, that claims support for do concurrent in newest gfortran.Paintbrush

© 2022 - 2024 — McMap. All rights reserved.