- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
??> 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
#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.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page