Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrays: "forall" vs equating them

Status
Not open for further replies.

EvLer

Programmer
Aug 27, 2008
5
US
Hi all,
so I am trying to understand Fortran and translate it into C. I came across this and don't know if there is a difference here that i need to be concerned with:
within a module, there are 2 sub-routines, both have the same arguments (arrays)

subroutine A(Ut, Uold)
real(wp):: Ut(0:x,0:y,0:n)
real(wp):: Uold(0:x,0:y,0:n)
(some condition.... blah-blah-blah)
Uold = Ut
....

subroutine B(Ut, Uold)
real(wp):: Ut(0:x,0:y,0:n)
real(wp):: Uold(0:x,0:y,0:n)
(some condition.... blah-blah-blah)
forall(i=0:x, j=0:y, k=0:n)
Uold(i,j,k) = Ut(i,j,k)
end forall

....

i thought that "forall" in this case does the same thing as the equating these arrays at least in fortran.
WHAT IS THE DIFFERENCE HERE?

Thank you in advance.


 
The first is the same as a memcpy.

The second, if you use STL or C# or VB, is the same as foreach. It is just a short form for nested do loops.

forall is quite a complex beast - you can use it for all sorts: not just assignent.

Check up on the matrix routines in Fortran: there are a few which aren't really obvious from the syntax.
 
Hi, thanks for replying to my threads.
However, i do not see a difference in my case... the arrays are of the same size and they are floats....
 
Terse reply: method 1 is faster than method 2

Verbose reply

Method 1: With the assignment/memcpy, the compiler can make use of any specialist commands to do block copies. For example, in Intel, there is the repnz command which is quite fast. The compiler can also use DMA transfers which are even faster. It doesn't make any difference to the type of variable that you are transferring.

Method 2: This happens one item at a time. There is no optimization: it is just easier to write forall rather than 3 nested do loops. The difference is that forall is a general purpose loop thing. It knows about arrays and it knows which element you're dealing with but it doesn't know anything about the element. It is just a convenient way of writing a bunch of nested do loops.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top