I am writing a Fortran program. The program implements some numerical methods. Program speed is very important. I decided to get rid of dynamic arrays (whether it speeds up the program?), and faced with the following problem. I have 3d arrays (NXxNYxNZ = MAX elements) where I know MAX, but I dont know NX/NY/NZ ratio. It can be like this : 1x1xNZ or like this 2xNYx1 , etc. The solution I see - use pointers. Simplified 2D case:
program ptrtest
parameter ( MAX = 50 ) ! I know this number on compile time.
integer :: NX,NY ! I will find this numbers on run time.
real, target :: a(MAX) ! Static Array
real, pointer :: b(:,:)
a = 5
read(*,*) NX, NY ! I get NX, NY (NX*NY == MAX)
b (1:NX, 1:NY) => a ! I can use b(:,:) <- this is my goal.
end program ptrtest
This example works, but i am afraid that such update slow down the real program where I am using even 5d arrays. Is it possible?
You say that program speed is important, so old-style Fortran will give you the best:
MAX
NX
,NY
,andNZ
. This will avoid the pointer indirection that will give you a loss of performance (unlike C, the Fortran standard assumes that subroutine array arguments don't overlap, see this post).For example:
If performance really matters here are some other useful tricks:
!DIR$ ATTRIBUTES ALIGN : 32
directive!DIR$ VECTOR ALIGNED
directiveFor example:
Edit
This old-style Fortran trick avoids pointer aliasing.
References on pointer aliasing: