I need help to make the following pointer setup for particle dynamics simulation.
1. Particles have X, Y, Z coordinates such as X(1:NP), Y(1:NP), and Z(1:NP), where NP is the number of particles, let's say 1000.
2. I would like to have an array pointer R3(1:NP,1:3), which points
R3(1:NP,1) => X R3(1:NP,2) => Y R3(1:NP,3) => Z
The dimension of R3 can be either NP by 3 or 3 by NP. The above method gave me an error, saying
"error #8524: The syntax of this data pointer assignment is incorrect: either 'bound spec' or 'bound remapping' is expected in this context. "
Any possible way to collect multiple vectors to an array pointer?
Thank you so much.
Transpose the pointer and pointee:
program R3program implicit none real, allocatable, target :: R3(:,:) real, pointer :: X(:), Y(:), Z(:) integer, parameter :: CACHELINESIZE_R3 = 64 / sizeof(R3(1,1)) integer :: nParticles, paddToCacheLine ! Body of R3 print *, 'Enter number of particles' read(*,'(I)') nParticles paddToCacheLine = CACHELINESIZE_R3 - MOD( nParticles, CACHELINESIZE_R3) allocate(R3(nParticles+paddToCacheLine,3)) X => R3(:,1) Y => R3(:,2) Z => R3(:,3) X = 1.0 Y = 2.0 Z = 3.0 print *, R3(1,:) end program R3program
Keep in mind that in Fortran, the contiguous data is held by the left most index.
R3(1:2,1) ! two adjacent cells of R3
R3(1,1:2) ! two stride SIZE(R3, DIM=1) separated cells of R3
You must allocate to R3, then make your pointers. This will produce the most efficient code.
Note, the sample code above padded each row to cache line size boundary thus assuring cache line aligned (X, Y and Z). While alignment like this is not necessary, alignment will produce more efficient code.