Community
cancel
Showing results for
Did you mean:
Highlighted
Beginner
12 Views

## How to make 2D array pointer pointing to vectors

Dear All:

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.

Albert

Tags (1)
3 Replies
Highlighted
Black Belt
12 Views

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))

! Body of R3
print *, 'Enter number of particles'
paddToCacheLine = CACHELINESIZE_R3 - MOD( nParticles, CACHELINESIZE_R3)
X => R3(:,1)
Y => R3(:,2)
Z => R3(:,3)
X = 1.0
Y = 2.0
Z = 3.0
print *, R3(1,:)
end program R3program
```

Jim Dempsey

Highlighted
Black Belt
12 Views

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.

Jim Dempsey

Highlighted
Beginner
12 Views

Dear Jim:

Thank you so much!

It is a great help.

Albert