[comp.lang.fortran] How much illegal are these?

gsg0384@uxa.cso.uiuc.edu (08/17/89)

Hi,
How much illegal are the following two statements?

      program main

      real b, c, a(10,10)
      data a/100*1./  ! or assign them by two do loops

          b = 0*a(0,2)
          c = 0*a(1,0)

      print*, b,c

      stop
      end


1. Is the first line equivalent to  b = 0*a(10,1) on every machine?  If it is,
   can't we say it's legal?  Anyway, is the outcome machine-dependent?

2. Does the second line always give c = 0. on every machine?

This kind of cases always occur when one wants to vectorize do loops.

Hugh      song@ardent1.ceg.uiuc.edu or gsg0384@uxa.cso.uiuc.edu

brainerd@unmvax.unm.edu (Walt Brainerd) (08/19/89)

In article <116400001@uxa.cso.uiuc.edu>, gsg0384@uxa.cso.uiuc.edu writes:
> 
> 
> Hi,
> How much illegal are the following two statements?
> 
>       program main
> 
>       real b, c, a(10,10)
>       data a/100*1./  ! or assign them by two do loops
> 
>           b = 0*a(0,2)
>           c = 0*a(1,0)
> 
The standard says (p. 5-5, lines 9-14) that the values of the subscripts
must be withing the declared bounds, so both are illegal.  There has been
much discussion about how much trouble you can get into using nonstandard
extensions.  This is not allowed because some vendors have an "explicit
extension", but rather because nonstandard usage is not checked.  It is hoped
that in future you will at least have an option to turn on during development
that will catch errors of this kind.
-- 
Walt Brainerd  Unicomp, Inc.           brainerd@unmvax.cs.unm.edu
               2002 Quail Run Dr. NE
               Albuquerque, NM 87122
               505/275-0800

afs@ardent.com (08/20/89)

Hugh Song asked about illegality of Fortran constructs that come up
in vectorizing programs. (Judging by the name of his machine, I'd
guess that it is one of ours... \_/ ). Perhaps a few observations are in order:

1. IF his intention is to make use of the subscript 0 as a legitimate
element of his arrays, ANSI 77 provides a mechanism for declaring this:
	real a(0:10,0:10)
which makes both of his subscript expressions legal.  
	
2. IF the intention is to make use of the forced overlap of array element
subscripts with each other, the official mechanism is (assuming valid 
declaration of a as having 0 as a valid subscript):
	equivalence (a(0,0),c(10,10))
where c is suitably declared as an array elsewhere.

3. It IS legally the decision of the compiler as to where arrays are placed
in storage; they do NOT have to be stored in the order in which they
are listed in the declarations statement.  Thus, the result of his
execution of an statement addressing   a(0,i) which precedes the 
beginning of the declared region of memory CAN produce a segmentation
fault type error instead of picking up a zero from another array.  

4. Vectorization of programs does NOT force the accessing of array elements
outside of the defined boundaries of arrays.  If the goal is to force
particular elements to "appear" as elements of an array so that they
may be accessed by a "vector" operation, the array declarations should
be made (and/or equivalenced) so that what the programmer has in mind
is explicitly declared.    Vectorization / Optimization generally does
not proceed well when non-standard assumptions are made about the 
object code/memory layout.

-----
Al Shpuntoff              afs@ardent.com or afs%ardent.com@uunet.uu.net,
Ardent Computer           408-732-0400
880 West Maude Avenue
Sunnyvale, CA 94086        

                UUCP:   {hplabs, ubvax,decwrl, ucbcad, uunet}!ardent!afs
                Dialcom:    134:CMP2016
-----
-----
Al Shpuntoff, Senior Instructor/Course Developer
Ardent Computer            408-732-0400
880 West Maude Avenue

jerry@violet.berkeley.edu ( Jerry Berkman ) (08/21/89)

In article <116400001@uxa.cso.uiuc.edu> gsg0384@uxa.cso.uiuc.edu writes:
>
>How much illegal are the following two statements?
>
>      real b, c, a(10,10)
>      data a/100*1./  ! or assign them by two do loops
>
>          b = 0*a(0,2)
>          c = 0*a(1,0)
>
>      print*, b,c
>
>1. Is the first line equivalent to  b = 0*a(10,1) on every machine?  If it is,
>   can't we say it's legal?  Anyway, is the outcome machine-dependent?

This does not necessarily work on all machines, or more correctly, with
all compilers.  For example, on our IBM:

    watfor77 test
    *ERR* SS-04 subscript expression out of range; A(0,2) does not exist
    TraceBack:  Executing in MAIN, statement 4 in file TEST FORTRAN
    Ready(01001); T=0.02/0.05 10:27:06

WATFOR77 gives a fatal error, and never executes the print statement.
I believe there have been descriptor based implementations of Fortran
(Burroughs?) which would also abort on illegal subscripts.

Most Fortran systems don't check subscripts.  However, optimizing compilers
move code around and vectorizing compilers check for recurrences
assuming that array subscripts are legal.  Consider:

	do 100 j=1,100
	    a(1,j+ind1) = sqrt(b(j))+d(j)+e(j)
	    a(2,j+ind2) =     b(j)
100	continue

the compiler may decide to do the second line first in order not to tie
up a register with b.  If these overlap due to illegal subscripts,
the result in general is unpredictable.

>
>2. Does the second line always give c = 0. on every machine?

No, not with WATFOR77.  The location of a(1,0) using the storage
layout of the standard would be equivalent to a1(-9) in an
equivalent linear array a1.  There is no guarantee this storage
location is available for this user; i.e. there could be a
segmentation violation.  Also, this is a bad test as many
compilers will see it is "0*" and not even bother compiling code
for the array reference.

>
>This kind of cases always occur when one wants to vectorize do loops.

I've vectorized a lot of DO loops and rarely encountered this problem.
This is very bad programming practice; it leads to unreadable and
unmaintainable code.

>
>Hugh      song@ardent1.ceg.uiuc.edu or gsg0384@uxa.cso.uiuc.edu

Jerry Berkman, U.C. Berkeley, jerry@violet.berkeley.edu

gsg0384@uxa.cso.uiuc.edu (08/22/89)

Ok, Ok.

The more detailed one that I wanted to ask is as follows:


      program main
      real a(200), b(10, 20), c(200)
      data b/200*1./, a/200*1./

      do 10 i = 1, 10
        b(i,1) = 0.
 10   continue

      do 20 j = 1, 10
        b(1,j) = 0.
 20   continue

      call sub(10, 10, a, b, c)

      print*, c

      stop
      end

c---------------
      subroutine sub(m, n, a, b, c)
      real a(m,n), b(m,n), c(m,n)

      do 20 i = 1, m
        do 10 j = 1, n
          c(i,j) = b(i,j)*a(i-1,j-1)
 10     continue
 20   continue

      return
      end
  

On Ardent1, there is a compiler switch -subcheck which gives warning at
run time.  But the result is what I intended, the c(.,.) = zero's for the
out-of-bounds.  Without the switch, the result is again what I intended.
  On Apollos (SR10.1), with the BSD f77 switch -C, the program terminates
with a message. 'Arithmetic exception: Subscript range'.  However,
without -C, the result was again what I inteded.

  OK.  SO they are illegal.  Then how about this modified sub?

c--------------
      subroutine sub(m, n, a, b, c)
      real a(m,n), b(m,n), c(m,n)

      do 20 i = 1, m
        do 10 j = 3, n
c                 ^ Notice!
          c(i,j) = b(i,j)*a(i-1,j-1)
 10     continue
 20   continue

      return
      end

The behavior on Apollo is the same.  But on Ardent, the compiler does not
catch anything wrong, even at the run time with -subcheck switch.
Ok, OK, They are still illegal.  Then how can I vectorize this without if's
and without introducing more do loops?

Thank you for kind replies in the past and for the new replies I get.

Hugh SONG