From: " --MM-->>" on
Hello,
I have a question regandin to "internal optimization" or the meanig of
some instruction in fortran 95.

I'm speaking of :
- DO...ENDDO
- FORALL
- WHERE...END WHERE

I'm using a laptop whit single core processor on linux ubuntu 9.10 and
gfortran 4.4.1.

Up to now I was shure that forall an where give me a increading of the
speed in the computation due to internal optimization.

I tried to run the next code, please adjust the comments dependi the
method that you want investigate:
-------------------------------
program forall
implicit none
integer :: i,j
integer :: x,y
real, allocatable :: a(:,:)
real :: ii,jj

!write(*,*) "dimensione nxm?"
!read(*,*) x,y
x=10000
y=10000
allocate(a(x,y))
call random_number(a)

do j=1,y
do i=1,x
if (a(i,j)<=0.5) then
a(i,j) = 100
endif
enddo
enddo

!forall (i=1:x,j=1:y,a(i,j)<=0.5)
! a(i,j)=100
!end forall

!where (a<=0.5)
! a=100
!end where
call random_number(ii)
call random_number(jj)
x=int(1+ii*x)
y=int(1+jj*x)
write(*,*) a(x,y)

end program forall
----------------------------------------

At the end I find this time:
gauss:~/Documenti$ time -p ./for2
100.00000
real 4.41
user 2.60
sys 0.42

gauss:~/Documenti$ time -p ./forall
100.00000
real 11.12
user 7.12
sys 0.57

gauss:~/Documenti$ time -p ./where
100.00000
real 4.65
user 2.90
sys 0.36

All program was compiled with gfortran -O3 optimization

My question is: "where and forall are only a confortable instruction for
the programmer or are there some case where we ca have a performance
improvment?"

Regard

From: Tim Prince on
<<--MM-->> wrote:

> !forall (i=1:x,j=1:y,a(i,j)<=0.5)
> ! a(i,j)=100
> !end forall
>

> All program was compiled with gfortran -O3 optimization
>
> My question is: "where and forall are only a confortable instruction for
> the programmer or are there some case where we ca have a performance
> improvment?"
>
This question has been debated at some length.
My personal take is that forall was adopted to stem the threat of HPF
developing as a separate fork of a Fortran-like language, rather than
for practical advantage.
ifort doesn't attempt to optimize a single assignment forall unless
preceded by !$ ivdep directive. That doesn't work beyond a single
assignment, due, in part, to the peculiar meaning of forall which
implies multiple assignments (technically not loops).
It may be difficult to optimize a rank 2 forall, particularly for
allocatable array.
From: Gordon Sande on
On 2010-01-04 11:18:39 -0400, Tim Prince <TimothyPrince(a)sbcglobal.net> said:

> <<--MM-->> wrote:
>
>> !forall (i=1:x,j=1:y,a(i,j)<=0.5)
>> ! a(i,j)=100
>> !end forall
>>
>
>> All program was compiled with gfortran -O3 optimization
>>
>> My question is: "where and forall are only a confortable instruction
>> for the programmer or are there some case where we ca have a
>> performance improvment?"
>>
> This question has been debated at some length.
> My personal take is that forall was adopted to stem the threat of HPF
> developing as a separate fork of a Fortran-like language, rather than
> for practical advantage.

I thought it was a technical fix to the limitations of array slices in array
assignment. The diagonal of a matrix is the quickest example. It came from
HPF and has other advantages but is basically array assignment done right or
on steroids particularly when combined with where. As an array assiignment it
can match formulaes more readily at the cost of temporary arrays that are not
overtly visible and that can be hard for compilers to optimize away.

> ifort doesn't attempt to optimize a single assignment forall unless
> preceded by !$ ivdep directive. That doesn't work beyond a single
> assignment, due, in part, to the peculiar meaning of forall which
> implies multiple assignments (technically not loops).
> It may be difficult to optimize a rank 2 forall, particularly for
> allocatable array.


From: Tim Prince on
Gordon Sande wrote:
> On 2010-01-04 11:18:39 -0400, Tim Prince <TimothyPrince(a)sbcglobal.net>
> said:

>> This question has been debated at some length.
>> My personal take is that forall was adopted to stem the threat of HPF
>> developing as a separate fork of a Fortran-like language, rather than
>> for practical advantage.
>
> I thought it was a technical fix to the limitations of array slices in
> array
> assignment. The diagonal of a matrix is the quickest example.
As that's your quickest example, it shows what a can of worms this is.
!$omp parallel workshare
forall(i=1:n)x(i,i)=1
!$omp end parallel workshare

is optimized by few compilers, and doesn't bring much economy of
expression. Equally few compilers take forall as an implicit invitation
for threaded optimization.

The typical architectural requirement for threading to optimize this
operation, in view of inherent high rate of DTLB miss on current
architectures, may not have been foremost among the considerations when
the syntax was thought up originally.
From: Richard Maine on
<<--MM-->> <no.spma(a)now.it> wrote:

> Hello,
> I have a question regandin to "internal optimization" or the meanig of
> some instruction in fortran 95.
>
> I'm speaking of :
> - DO...ENDDO
> - FORALL
> - WHERE...END WHERE
>
> I'm using a laptop whit single core processor on linux ubuntu 9.10 and
> gfortran 4.4.1.
>
> Up to now I was shure that forall an where give me a increading of the
> speed in the computation due to internal optimization.

Forall was not designed with optimization in mind. It was designed (in
HPF) for parallelism, and then added to the Fortran standard as part of
incorporating the syntactic parts pf HPF. I don't have experience with
parallel machines to comment knowlegably. But for serial machines, there
is little reason to expect forall to be more efficient than simple DO
loops, and there is substantial data to suggest that it is often worse,
largely because it often involves temporary arrays. I don't know why you
would think that forall was somehow inherently more optimizable than DO
loops.

Tim and Gordon discussed that a little, but there is one point which
they did not mention and which I consider fundamental. Perhaps you know
this or consider it obvious. But you did ask, and there are some people
who definitely have been confused by the point, so I feel it important
to make.

DO is a looping construct. Forall and Where are array assignments. That
is a really fundamental difference. There are cases where one can
achieve a desired result using any of the forms, but do not let that
blind you to the fundamental difference. I have seen people take
"random" DO loops and change the syntax of the DO statement to that of a
FORALL, hoping that this might improve their performance or something.
Except in special cases, this results in something that won't even
compile.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain