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

pass 2d arrays to/from C subroutine

jchenil
Beginner
775 Views
I have Intel C++ subroutines which I need to call from Fortran main program, and I need to pass 4 2d array between C++ & Fortran code. Where I can find sample code for such operation?
0 Kudos
4 Replies
Steven_L_Intel1
Employee
775 Views
You can pass 2D arrays the same as 1D. Just pass the array on the Fortran side. On the C side, you'll have to represent it as a single-dimensioned array and keep in mind that C array indexing syntax starts with element zero. (C does not really have arrays, it has pointers and an indexing syntax.) What looks in C like a two-dimensional array is actually an array of pointers to arrays, not the same thing.
0 Kudos
TimP
Honored Contributor III
775 Views
One way of generating sample C code is to write a Fortran 77 sample which uses the 2d arrays and use f2c to get an ugly C translation. You could write C macros which look like Fortran array element references but expand to C code. A C compiler will require typed aliasing analysis to optimize this code (done by default by gcc -O2, requires icc -ansi_alias).
0 Kudos
jimdempseyatthecove
Honored Contributor III
775 Views

??> What looks in C like a two-dimensional array is actually an array of pointers to arrays, not the same thing.

C has no in memory array descriptor as does Fortran, C has just the block of raw data. The C compiler derives the indexing parametersat compile time and applies the index scheme to the base address of the block of raw data. IOW there is no array of pointers. C passes the base pointer to the array.

Jim Dempsey

0 Kudos
TimP
Honored Contributor III
775 Views
I believe what Steve means is that C syntax array[][] would be incompatible with an array reference passed from Fortran (or with f2003 C interop). The Fortran 2D array appears as a plain linear array in C, and could be accessed by a macro such as
#define ARRAY(i,j) array[(i-1) + (j-1)* *arraydim1]
if array were passed as an f77 adjustable array (f90 assumed size), with arraydim1, the size of the leading Fortran dimension, passed by reference.
As I mentioned early, in spite of the ugliness, C can do this quite efficiently, provided that typed aliasing optimizations are engaged. If array is typed integer, it would be necessary to make a local copy of *arraydim1 to have any chance at optimization.
0 Kudos
Reply