Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 Discussions

Passing an array with different dimensions

eos_pengwern
Beginner
675 Views
I seem to find increasingly often that I need to (or atleast, I would find it very convenientto) pass arrays with a certain shape (e.g. a three-dimensionalm x n x q matrix) to a procedure that expects to receive data in a different shape (e.g. atwo-dimensional matrixwithdimensions m, n*q, or even a single vector with length m*n*q). Typically the reason for wanting to do thisis is so that the data can be processed more efficiently by applying an operation which is common to all elements over a single long vector rather than a lot of short vectors.

In C, this would be a piece of cake: you pass a pointer to the first element and whatever description of the data you want. In Fortran, the bounds-checking gets in the way; you simply can't pass a three-dimensional array to a function expecting a two-dimensional array, even if the arrays are exactly the same size and have the data laid out in exactly the same order.

On the face of it this is an application for the much-derided EQUIVALENCE statement, but even that isn't legal when applied to procedure arguments. I really haven't found a good solution, and am usually forced to just copy the data into a new temporary array of the correct shape before making the procedure call, and then copying it back afterwards, which seems ridiculously wasteful.

Is there a better, or even a "proper", way to do this?

Stephen.
0 Kudos
2 Replies
Steven_L_Intel1
Employee
675 Views
You do this by passing the first element of the array, say, Z(1,1,1). The Fortran language allows you to pass this to a dummy argument of a different shape as long as Z is not an assumed-shape array.
0 Kudos
eos_pengwern
Beginner
675 Views
Wow, that's neat. Thanks Steve.
0 Kudos
Reply