From: glen herrmannsfeldt on
Hifi-Comp <wenbinyu.heaven(a)gmail.com> wrote:
> My original question was not clear and get sidetracked. Hence I am
> trying to clarify the question:

> Currently I have the following:

> INTEGER,PARAMETER:: n=3

> TYPE,PUBLIC:: User_Data
> REAL(DBL_AD)::scale
> REAL(DBL_AD)::vector(n)
> END TYPE User_Data

> TYPE(User_Data):: x, y(30),z(100,10)

> In the real situation, I want the user of compiled code to input n
> (remain the same throughout the calculation) and with almost no
> changes to the original code.

> Richard suggested to change the type definition as follows
> TYPE,PUBLIC:: User_Data
> REAL(DBL_AD)::scale
> REAL(DBL_AD),ALLOCATABLE::vector(:)
> END TYPE User_Data

If the largest n isn't so big, you are probably better off
just setting a constant n and not using all the elements.
The usual implementation of an allocatable structure component
will use a descriptor and address of the allocated memory.
That will likely take much more than the space for the three
array elements.

> However, I don't want to insert allocate statement for each variables
> of User_Data as there are many such types in the overall program. It
> seems to me I should be able to do something like

> ALLOCATE(USER_DATA%VECTOR(N))

> to fix the dimension of vector, and all the variables of User_Data
> will then know the dimension already. Is it posslbe to achieve this?

Even if you could do that, which you can't, you would still need
to ALLOCATE them. It would save you typing (N), but that's all.

There is a PL/I feature, which Fortran doesn't have; when you
declare a CONTROLLED (PL/I's version of ALLOCATABLE) variable
you can specify a default dimension. You can override it at
ALLOCATE, or use the default. I believe it can be a variable
known at block entry, though I am not so sure about that.

In the case of arrays of structures, I believe you need to
allocate for each array element. I might wonder if it would
be better done as a structure of arrays, though that will
likely take some changes to the program. Another PL/I feature
that Fortran doesn't have is the ability to move subscripts
around in structure arrays references. That is, instead of
z(1,2)%vector(3), you could write z%vector(1,2,3) and the
compiler will figure out where the arrays are.
It will likely take up less memory as a structure of arrays.
Speed will depend on the access patterns.

On the other hand, if your compiler has the allocate on assignment
feature from Fortran 2003, then you might be able to avoid
the ALLOCATE. (Assuming you do an array assignment.)

In many programs there is a convenient place to do the ALLOCATE.
It is hard for us to know without seeing the code, though.

-- glen

 | 
Pages: 1
Prev: reasons for failure
Next: reshape efficiency