Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28729 Discussions

Passing Parts of Derived Data Type to Subroutine

ScottBoyce
Beginner
318 Views

I am building a subroutine that I can either use a derived data type (my preference) or individual arrays.

The arrays will be passed to a various subroutines, including the MKL CG Solver.  My question is will this form temporary arrays or will the compiler efficiently use the arrays in the routines (particularly in MKL)

 

Bellow are is an example (forgive any minor bugs as I am just typing this from the window to illustrate what I mean)

TYPE MAT
    DOUBLE PRECISION, DIMENSION(:),   ALLOCATABLE:: X
    DOUBLE PRECISION, DIMENSION(:,:), ALLOCATABLE:: A 
END TYPE

TYPE(MAT), DIMENSION(:), ALLOCATABLE:: M

ALLOCATE(M(5))

DO I=1, 5
  ALLOCATE( M(I)%X(10) )
  ALLOCATE( M(I)%A(10,10) )
END DO

!X and A are then populated with numbers

DO I=1, 5
  CALL MYSUB( M(I)%A, M(I)%X )
END DO

The alternative would be to use standard arrays as follows:

DOUBLE PRECISION, DIMENSION(:,:),   ALLOCATABLE:: X
DOUBLE PRECISION, DIMENSION(:,:,:), ALLOCATABLE:: A 

ALLOCATE( X(10,5) )
ALLOCATE( A(10,10, 5) )

!X and A are then populated with numbers

DO I=1, 5
  CALL MYSUB( A(:,:,I), X(:,I) )
END DO

The reason why I prefer the derived data type is that there will be a lot more variables within this data type. My main concern is that will the compiler have an issue with passing portions of a derived data type to a subroutine or function, even though what is being passed is a contiguous block. (I know it will compile, but my issue is in terms of speed and temporary arrays).

 

On a side note, is there any speed benefit to using a parameterized derived data type over an allocatable one? For this example I could make M be parameterized as follows:

TYPE MAT(N)
    INTEGER, LEN::N
    DOUBLE PRECISION, DIMENSION(N):: X
    DOUBLE PRECISION, DIMENSION(N,N):: A 
END TYPE

I think its a neat feature of fortran, but fail to see any value over the flexibility of allocatable arrays. (this example would only work if N was constant across the dimension of "M(I)"

 

Thanks as always for everyone's comments and support.

0 Kudos
3 Replies
mecej4
Honored Contributor III
318 Views

I think that your description of the first version as involving "derived type" arguments is incorrect. The arguments in the subroutine call are allocatable arrays, namely, M(I)%A and M(I)%X. That these arrays are components of a derived type is probably inconsequential.

To consider questions of creation of temporary arrays, etc., we would need to be told what the interface of MySub() is.

0 Kudos
ScottBoyce
Beginner
318 Views
INTERFACE 
  SUBROUTINE MYSUB(A,X)
    DOUBLE PRECISION,DIMENSION(:,:)::A
    DOUBLE PRECISION,DIMENSION(:)  ::X
  END SUBROUTINE
END INTERFACE

 

0 Kudos
mecej4
Honored Contributor III
318 Views

Since the subroutine takes assumed-shape array arguments, an explicit interface has to be provided to the caller. Have you seen to this?

Your original question regarding efficiency can be tackled now. If you are passing one- and two-dimensional arrays to MKL subroutines, you typically do not have a choice as to whether the arguments to those subroutines are assumed size or assumed shape.

Most BLAS and Lapack routines take assumed size array arguments with lower bounds equal to 1, and the effective array upper bounds are passed as additional arguments. If the array arguments are going to be contiguous sections, whether you pass them with the A(:,:,K) or the A(1,1,K) notation should make little difference.

0 Kudos
Reply