Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26747 Discussions

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

Beginner
83 Views

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

3 Replies
Black Belt
83 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

Black Belt
83 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

Beginner
83 Views

Dear Jim:

Thank you so much!

It is a great help.

Albert