Quantcast
Channel: Intel® Fortran Composer XE
Viewing all articles
Browse latest Browse all 1424

Array of Parameterized derived data types with different length type parameters

$
0
0

I have been unable to find any documentation, so it may not be possible, about being able to make an array of parameterized derived data types with different length parameters. I have done some simple tests, but I get strange compiler results where multiple allocations seem to overwrite the preceding one, rather than raise an error.

 

TYPE MAT_TYPE(N)
     INTEGER,LEN::N
     REAL,DIMENSION(N    )::X
     REAL,DIMENSION(N,N  )::A
     REAL,DIMENSION(N,2*N)::B
END TYPE

TYPE ARRAY_OF_MAT
   TYPE(MAT_TYPE(:)),DIMENSION(:),ALLOCATABLE:: MAT
END TYPE

TYPE (ARRAY_OF_MAT) :: GRP

 

In the above example code, I'd like to be able to allocate MAT within GRP such that each has a different length parameter as follows:

 

!FIRST ALLOCATE MAIN DIMENSION --this sometimes causes a compiler error
ALLOCATE(TYPE(MAT_TYPE(:)::GRP%MAT(10))
!
!NEXT ALLOCATE EACH OF THE PARAMETERIZED DIMENSIONS --these work without the preceding allocation

ALLOCATE(TYPE(MAT_TYPE(50)::GRP%MAT(1))

ALLOCATE(TYPE(MAT_TYPE(80)::GRP%MAT(2))

ALLOCATE(TYPE(MAT_TYPE(40)::GRP%MAT(3))

 

That way I can have as data type of paramterized data types with the correct length factors for different array groups and I can have loops similar to the following:

 

DO I=1, 10
  !DO STUFF WITH
  GRP%MAT(I)%X
  GRP%MAT(I)%A
  GRP%MAT(I)%B
END DO

 

The way I have my code currently is as follows:

 

TYPE MAT_TYPE
     INTEGER::N
     REAL,ALLOCATABLE,DIMENSION(:  )::X
     REAL,ALLOCATABLE,DIMENSION(:,:)::A
     REAL,ALLOCATABLE,DIMENSION(:,:)::B
END TYPE

TYPE ARRAY_OF_MAT
   TYPE(MAT_TYPE(:)),DIMENSION(:),ALLOCATABLE:: MAT
END TYPE

ALLOCATE(GRP%MAT(10))

DO I=1, 10
  ! THE VALUE OF "N" CHANGES FOR EACH LOOP TO REQUIRED VALUE (ie N=50, then 80, then 40, then ...)
  GRP%MAT(I)%N=N

  ALLOCATE(GRP%MAT(I)%X(N    ))
  ALLOCATE(GRP%MAT(I)%A(N,  N))
  ALLOCATE(GRP%MAT(I)%B(N,2*N))
END DO

 

The only way I can think of doing this with parameterized data types is with a linked list, but that to me seems like it would effect speed. (These arrays are used in a numerical model solver for different grids).

What would be the best way to handle this or should I stick with allocatable (non-parameterized data types) arrays as the second example shows.

Lastly is there any speed benefit beyond cleaner code with using parameterized data types that are allocatable versus a standard allocatable array within the data type. At least for several of my tests using the intel compiler I get strange allocatation results. For example the following causes very strange behavior.

 

ALLOCATE(TYPE(MAT_TYPE(50)::GRP%MAT(1))

ALLOCATE(TYPE(MAT_TYPE(80)::GRP%MAT(2))

ALLOCATE(TYPE(MAT_TYPE(40)::GRP%MAT(3))

 

In each case, it seems like the next allocation over writes the old one, thus GRP%MAT becomes dimension of length 1 with length parameter type 50,

then magically becomes dimension of length 2 with length parameter type 80 (ie GRP%MAT(1) and GRP%MAT(2) both have length parameters of 80 now),

and then the third statement changes it all to a dimension of length 3 with length parameter type 40.

 

Thanks for all your help and comments.

 


Viewing all articles
Browse latest Browse all 1424

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>