Thursday, 5 April 2018

Fortran array of derived type vs derived type arrays

In Fortran, we are allowed to create a derived data type:




type vig_
double precision :: L
double precision :: D_i
double precision :: D_o
end type vig_

type(vig_) :: vig



My question is about performance when using derived data types.



I can create an array of the derived type:



type vig1_
double precision :: L
double precision :: D_i
double precision :: D_o
end type vig1_


type(vig1_), allocatable :: vig1(:)

! ...
allocate(vig1(n))
! ...


or a derived data type of arrays:



type vig2_

double precision, allocatable :: L(:)
double precision, allocatable :: D_i(:)
double precision, allocatable :: D_o(:)
end type vig2_

type(vig2_) :: vig2

! ...
allocate( vig2%L(n) , vig2%D_i(n) , vig2%D_o(n) )
! ...



Of course there is a difference, but what exactly is the difference between both approaches? Which is faster? If I want to call a subroutine with these values, which performs better?



! option 1
do k=1,n
call foo1( vig1(k) )
end do

! option 2

do k=1,n
call foo2( vig2%L(k) , vig2%D_i(k) , vig2%D_o(k) )
end do

subroutine foo1(vig)
type(vig1_) :: vig
end subroutine foo1

subroutine foo2(L,D_i,D_o)
double precision :: L, D_i, D_o

end subroutine foo2

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...