Re: OON: f90 vrs CC

From: ferrell@cpca.com
Date: Wed Mar 19 1997 - 16:37:59 EST


   Date: Wed, 19 Mar 1997 12:04:18 -0500 (EST)
   From: "Nikolai A. Bannov" <bannov@c11177-330dan.ece.ncsu.edu>
   X-Sun-Charset: US-ASCII
   Sender: owner-oon-list@oonumerics.org
   Precedence: bulk

   In f90 it is up to a programmer to decide whether you want to copy or
   a reference. If you write A=B+C (where A and B are matrixes of the same shape)
   you will get A with elements obtained by `elemental' addition
   (possibly obtained by simultaneous addition, if you processor has this
   capability).
   If you want to overload multiplication and then write A=B*C, you have a choice:
   1) if you need to construct a result in A then the function defining
      multiplication should return a matrix
      (in f90, a function can return a matrix),
      and elements will be simply computed in A.
      E.g function mult(B,C)
             real, dimension(:,:), intent(in) :: B, C
             real, dimension(size(B,1),size(B,1)) :: mult
             integer :: N = size(B,1)
             integer :: K,J
                mult=0. ; do J=1,N ; do K=1,N
                mult(1:N,J)=mult(1:N,J)+B(1:N,K)*C(K,J) ; endo ; endo
           end function mult

How do you handle the case where the user writes:
        A = A*C?

This is a perfectly valid invocation, and well defined in F90
(behavior should be as if RHS gets fully evaluated before assignment
is done). Using the function you wrote would give incorrect results
because A would get overwritten before the RHS was fully evaluated.

I have come across this problem frequently in providing library
routines. I would like to optimize for the most common case: LHS does
not overlap any of the arguments. However, I find it tricky to do
that optimization and still provide correct results in the event user
to writes statements such as A=A*C.

-robert

-- 
========================================================================
Robert C. Ferrell, PhD			      E-mail:   ferrell@cpca.com
  Massachusetts Institute of Technology	      MIT Office: (617) 253-3961
  Cambridge Power Computing Associates, Ltd.  CPCA Office:(617) 734-8569
========================================================================



This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 03:20:05 EST