1

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
Thales
  • 1,181
  • 9
  • 10
  • This is a well-known problem with loads of literature available https://www.mathworks.com/videos/array-of-structures-vs-structures-of-arrays-97327.html – Vladimir F Героям слава Apr 15 '19 at 21:49
  • Possible duplicate https://stackoverflow.com/questions/11616941/structure-of-arrays-and-array-of-structures-performance-difference https://stackoverflow.com/questions/1125626/array-of-structures-or-structure-of-arrays https://stackoverflow.com/questions/17924705/structure-of-arrays-vs-array-of-structures-in-cuda – Vladimir F Героям слава Apr 15 '19 at 21:54
  • 2
    @VladimirF would you have duplicates in Fortran? It seems fair to expect a Fortran-specific answer for such a performance problem, isn't it? – Pierre de Buyl Apr 16 '19 at 06:25
  • @PierredeBuyl That's why I didn't close it single-handedly, but to be fair, I think it is pretty much the same in all compiled languages. There is nothing I would add to those links and I would have expected some prior research from the OP - but I did not downvote myself. – Vladimir F Героям слава Apr 16 '19 at 08:18
  • Fair enough :-) Maybe it can stay as a duplicate, enhancing the discoverability for people looking specifically into Fortran? – Pierre de Buyl Apr 16 '19 at 14:50

0 Answers0