[comp.lang.fortran] Read dimension

xiaofei@acsu.buffalo.edu (Xiaofei Wang) (02/24/91)

I have this question for a long time:

main program 

parameter(m=10,n=5)
real a(m,n)
call subroutine(a,b)
end

I don't want to put m=10 n=5 to the program, I would like to
read into the program. so that I don't have to change program
every time. However a(m,n) has to be defined before read. 
-- 
xiaofei@acsu.buffalo.edu / rutgers!ub!xiaofei / v118raqa@ubvms.bitnet

session@uncw.UUCP (Zack C. Sessions) (02/25/91)

xiaofei@acsu.buffalo.edu (Xiaofei Wang) writes:

>I have this question for a long time:

>main program 

>parameter(m=10,n=5)
>real a(m,n)
>call subroutine(a,b)
>end

>I don't want to put m=10 n=5 to the program, I would like to
>read into the program. so that I don't have to change program
>every time. However a(m,n) has to be defined before read. 
>-- 
>xiaofei@acsu.buffalo.edu / rutgers!ub!xiaofei / v118raqa@ubvms.bitnet

I am most familiar with VAX FORTRAN, but I think this applies to
standard FORTRAN also, and that is, dynamic dimensioning simply is
not supported by the language. You can dynamically define an array,
but only in a subroutine, and the array is a passed argument.

Best you can do with FORTRAN is define the arrays as large as they
ever can get, then input the "current working value", then include
some code which performs your own array-bounds checking. This isn't
too much of a pain, especially on a virtual memory machine.

Zack Sessions
...!ecsvax!uncw!session

u714092@mustang.larc.nasa.gov (prichard devon ) (02/25/91)

In article <1012@uncw.UUCP> session@uncw.UUCP (Zack C. Sessions) writes:


   xiaofei@acsu.buffalo.edu (Xiaofei Wang) writes:

   >main program 
   >parameter(m=10,n=5)
   >real a(m,n)
   >call subroutine(a,b)
   >end

   >I don't want to put m=10 n=5 to the program, I would like to
   >read into the program. so that I don't have to change program
   >every time. However a(m,n) has to be defined before read. 
   >-- 

   I am most familiar with VAX FORTRAN, but I think this applies to
one of the computer gurus suggested to me to write the brunt of the
code in Fortran, but the main routine in C.  the C function dynamically
allocates the array in the Fortran subroutine argument list, at which
point its straight Fortran processing.

I haven't tried this yet, but it oughta work.  its definitely the simplest
method for dynamic array allocation I've seen.  of course, there may be
funny memory conflicts due to linking Fortran and C code...


--
 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 | Devon Prichard                "The atrocious crime of being a young  |
 | u714092@eagle.larc.nasa.gov,   man... I shall neither attempt to     |
 | prichard@ias.larc.nasa.gov     palliate nor deny."                   |
 |                                William Pitt, House of Commons, 1741  |
 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

MEB@SLACVM.SLAC.STANFORD.EDU (meb@slacvm.slac.stanford.edu) (02/26/91)

In article <61477@eerie.acsu.Buffalo.EDU>, xiaofei@acsu.buffalo.edu (Xiaofei
Wang) says:

(that he wants to use variably dimensioned arrays without declaring
 specific dimensions until run time).

I have little to add to preceding responses, and wouldn't have sent this
this if the originator had not had "buffalo" in his email address.

One of my programming "pleasures" about 20 years ago, when I was working
in the Research Computing Office at Ball State Universiyt, involved the
understanding and maintenance of a rather large program which we called
"Buffalo," but was named something like the Multivariate Analysis of
Variance and Covariance Program Package, distributed by the University
of Buffalo.  It dealt in a quite general fashion with the simultaneous
problems of:

 - no dynamic storage allocation
 - no virtual memory
 - variously sized (memory/partition capacity) target machines
 - various numbers of variously shaped matrices, depending on
     number of cases, number of variables, type of analysis, etc.

The programmer who installed Buffalo was required to provide a single
value before compiling the MAIN (driver) program; that value was the
DIMENSION of a REAL vector (nominally as large as the installation
could accommodate).  The same value also was coded as the first (only?)
value in a COMMON block.

After reading in the necessary problem-specification values, Buffalo
would compute the necessary dimensions, cumulative offsets, etc., and
perform the analyses with one- two-dimensional arrays that were all
allocated within that single vector by calls of the sort:

  (in caller)   CALL SUB(M,N,VEC(IOFF1),VEC(IOFF2),...)

  (in called)   SUBROUTINE SUB(M,N,MAT1,MAT2,...)
                DIMENSION MAT1(M,N),MAT2(N,N)

It wasn't fun to debug, but it didn't need very much.  That program
taught me much of what I know about FORTRAN storage allocation.

klassen@sol.UVic.CA (Melvin Klassen) (02/26/91)

xiaofei@acsu.buffalo.edu (Xiaofei Wang) writes:
>
>parameter(m=10,n=5)
>real a(m,n)
>call subroutine(a,b)
>end
>
>I don't want to put m=10 n=5 to the program, I would like to
>read into the program. so that I don't have to change program
>every time. However a(m,n) has to be defined before read. 
>-- 
Years ago, for the IBM OS/VS1 and VM/CMS environments,
our group wrote a collection of Fortran-callable assembly-language
routines to allocate/release storage, and to indirectly call a 
Fortran subroutine.  For example:
  EXTERNAL MYSUB
  READ(*,*) M,N 
  IBytes=M*N*4
  CALL IADDR (M, IM, N, IN)
  CALL GETSP (IER, ISTOR, IBytes)
  CALL EXSUBR (MYSUB, IM, IN, ISTOR)
  CALL FREESP (ISTOR, IBytes)
  END
  SUBROUTINE MYSUB (M, N, STOR)
  REAL STOR(M,N)
  END

userAKDU@mts.ucs.UAlberta.CA (Al Dunbar) (02/26/91)

In article <U714092.91Feb25074058@mustang.larc.nasa.gov>, u714092@mustang.larc.nasa.gov (prichard devon      ) writes:
>In article <1012@uncw.UUCP> session@uncw.UUCP (Zack C. Sessions) writes:
> 
> 
>   xiaofei@acsu.buffalo.edu (Xiaofei Wang) writes:
> 
>   >main program 
>   >parameter(m=10,n=5)
>   >real a(m,n)
>   >call subroutine(a,b)
>   >end
> 
>   >I don't want to put m=10 n=5 to the program, I would like to
>   >read into the program. so that I don't have to change program
>   >every time. However a(m,n) has to be defined before read. 
>   >-- 
> 
>   I am most familiar with VAX FORTRAN, but I think this applies to
>one of the computer gurus suggested to me to write the brunt of the
>code in Fortran, but the main routine in C.  the C function dynamically
>allocates the array in the Fortran subroutine argument list, at which
>point its straight Fortran processing.
> 
>I haven't tried this yet, but it oughta work.  its definitely the simplest
>method for dynamic array allocation I've seen.  of course, there may be
>funny memory conflicts due to linking Fortran and C code...
> 
Only thing wrong with this that I can see is the flame you
are going to get from Bill Silvert on the "expensive
functionality" of this approach.
 
 -------------------+-------------------------------------------
 Al Dunbar          | 
 Edmonton, Alberta  |  Disclaimer: "I disclaim disclaimers"
 CANADA             |                              
 -------------------+-------------------------------------------

session@uncw.UUCP (Zack C. Sessions) (02/27/91)

u714092@mustang.larc.nasa.gov (prichard devon      ) writes:

>In article <1012@uncw.UUCP> session@uncw.UUCP (Zack C. Sessions) writes:


>   xiaofei@acsu.buffalo.edu (Xiaofei Wang) writes:

>   >main program 
>   >parameter(m=10,n=5)
>   >real a(m,n)
>   >call subroutine(a,b)
>   >end

>   >I don't want to put m=10 n=5 to the program, I would like to
>   >read into the program. so that I don't have to change program
>   >every time. However a(m,n) has to be defined before read. 
>   >-- 

>   I am most familiar with VAX FORTRAN, but I think this applies to
>one of the computer gurus suggested to me to write the brunt of the
>code in Fortran, but the main routine in C.  the C function dynamically
>allocates the array in the Fortran subroutine argument list, at which
>point its straight Fortran processing.

>I haven't tried this yet, but it oughta work.  its definitely the simplest
>method for dynamic array allocation I've seen.  of course, there may be
>funny memory conflicts due to linking Fortran and C code...

Hmm, wonder why you didn't include anything from MY posting, even
though there was a header line in there? Anyway, as someone
mentioned, some programmers don't have the luxury of having both
a FORTRAN compiler and a C compiler. While it is obviously the
most efficient way to tackle the problem given that the only
thing you want to do in C is the dynamic memory allocation. But
IMHO, if your going to do that much of it in C, may as well
do the whole thing in C!!

Zack Sessions
...!ecsvax!uncw!session

dprpjf@inetg1.Arco.Com (Paul Fowler) (02/27/91)

In article <U714092.91Feb25074058@mustang.larc.nasa.gov>, u714092@mustang.larc.nasa.gov (prichard devon      ) writes:
> In article <1012@uncw.UUCP> session@uncw.UUCP (Zack C. Sessions) writes:
> 
> 
>    xiaofei@acsu.buffalo.edu (Xiaofei Wang) writes:
> 
          [He wants to be able to dynamically allocate arrays.]
> 
>    I am most familiar with VAX FORTRAN, but I think this applies to
> one of the computer gurus suggested to me to write the brunt of the
> code in Fortran, but the main routine in C.  the C function dynamically
> allocates the array in the Fortran subroutine argument list, at which
> point its straight Fortran processing.
> 
> I haven't tried this yet, but it oughta work.  its definitely the simplest
> method for dynamic array allocation I've seen.  of course, there may be
> funny memory conflicts due to linking Fortran and C code...
> 

To substantiate this suggestion with my 40% of a nickel's worth of experience,
I have been using just this approach (C main program using malloc() for
dynamic allocation and then calling Fortran computational subroutines) for
many years in various flavors of UNIX environments and it has worked just
fine for me.  

-- 

  Paul Fowler               ARCO Oil and Gas Co. 
  dprpjf@arco.com           Plano, TX 

seymour@milton.u.washington.edu (Richard Seymour) (02/27/91)

In article <61477@eerie.acsu.Buffalo.EDU> xiaofei@acsu.buffalo.edu (Xiaofei Wang) writes:
>main program 
>parameter(m=10,n=5)
>real a(m,n)
>call subroutine(a,b)
>end
>
... that he'd rather read in the array dimensions, rather than recompile
them.  As many others posted, there's no precise standard way, other than
dimensioning a monster array in the mainline, and then lying to the subroutine
and letting it view part of the original monster as the multi-dimensioned.
Thusly:
    main:       real x(1000000)
                read() i,j
                call subroutine(x,i,j)
                end
   sub:         subroutine subroutine(x,i,j)
                 dimension x(i,j)
                 ....
                 end
That's very portable.
A few people mentioned VAXes, but didn't give the VMS-answer: You CAN create
a virtual array on the fly.  You call the Create-Virtual-Space system 
service (SYS$CRETVA), giving it two addresses which define the size you
want.  It returns two virtual address, the first of which you pass to
your subroutine as the starting address (the (1,1) location) of your
array.
Roughly:  common/private/where    (i like to make private PSECTs)
          call make_space(where)
          end
           subroutine make_space(where)
          integer*4 want(2),result(2),access,size
          external sec$m_wrt
          integer*4 status,sys$cretva
          read()i,j
          size=i*j*4    (4 bytes per real datum)
          want(1)=%LOC(where)
          want(2)=want(1)+size    (number of bytes to create)
          access=%loc(sec$m_wrt)       (request write-access)
               status=SYS$CRETVA(want,result,access)
                call subroutine(%VAL(result(1)),i,j)
                end
                subroutine subroutine(a,i,j)
                dimension a(i,j)
                 ....
good luck
--dick
(reaching for his asbestos bvd's...)