This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: QUERY regarding FORALL construct
sudeshc@noida.hcltech.com wrote:
> Hi,
>
> I have a doubt in FORALL construct when there is a scalar assignment statement in FORALL body.
> Consider following example:
>
> Program main
> Implicit none
> Integer::a,i
>
> Forall (i=1:10)
> ??????????? a=a+1
> endforall
> print *,a
> end
>
> What would be output of above program( value of a) ?
>
> Some compilers generate 1 while some generate 10 for this. Also
> please let me know scalar assignment in forall construct is
> standard conforming or not ? Although standard doesn't say
> anything about this "gfortran" generates following error for
> this.
>
> a=a+1
> ?1
> Error: The FORALL with index 'i' cause more than one assignment to this object at (1)
Your code is not standard conforming; FORALL is for array
assignments only. Note in particular, that FORALL is not a loop
construct. A compiler could translate a FORALL to a loop, but it
isn't what it is quite conceptually. A loop is just one possible
way to implement it. A parallelizing compiler could (if it
accepts your code) produce code to increment the value of a
simultaneously on 10 processors (giving the result 1). Or it
could produce code to increment a 9 times on one processor, and
once on another, giving either 1 or 9 as a result, depending on
which processor stores it's value of a first, and so on. In
short; the output of your program is not well defined. Rejecting
the code (like gfortran does) is, in my opinion, a prefectly
correct thing to do. A compiler that doesn't even produce a
warning for your code is, in my opinion, broken.
Erik