FORTRAN/C INTEROPERABILITY

FORTRAN/C INTEROPERABILITY | INTEROPERABILITY between FORTRAN & C | C , FORTRAN INTEROPERABILITY | Accessing FORTRAN classes in C

   CALLING C ROUTINES FROM FORTRAN         ===============================     Machine        C Routine name    -------        ------------------    ALPHA/DUNIX    lowercase letters_  (default)    ALPHA/VMS      anything    CRAY           UPPERCASE LETTERS    HP             lowercase letters   (?)    IBM/AIX        lowercase letters   (all IBM's ?)    SGI/IRIX       lowercase letters_    SUN            lowercase letters_    TITAN          UPPERCASE LETTERS    VAX/VMS        anything    VAX/ULTRIX     lowercase letters_  (default)    Variable passing mechanisms  ---------------------------  Fortran usually passes its variables by reference (passes pointers).   That means that you MUST give addresses in your calling C-program.   Function results, may be passed by value, for example, the following   code calls a FORTRAN function called "gamma":     double   x, y;    ..................    x = 2.0;     y = gamma_(&x)      Array storage order  -------------------  Fortran uses a column wise storage of matrices while C stores them  row wise. This means that when you want to pass a matrix from your  C-program to the fortran routine you must transpose the matrix  in your program before entering the routine. Of course, any output  from such a routine must be transposed again.    If you omit this step, then probably your program will run (because  it has data to compute on) but it will generate wrong answers.   Transposition is expensive, you may avoid it by adapting the source  code, when the FORTRAN source says A(j+1,i+1) it could mean a[i][j] in C,.   There are times when you don't want to modify already existing FORTRAN   and C code, in such cases you may have to write a transposition wrapper.  This can be advisable for reasons of clarity (i.e. keeping the   documentation the code and the math in sync.)    Array indexes  -------------  Remember that array indexes in Fortran starts by default at 1 while   in C they start at index 0; hence a passed array fortran_array[1:100]  must be used in the C-routine/program as c_array[0:99].    Variable type matching  ----------------------  Watch out especially with float's and double's. Make sure that the  size of the variable in the calling program is identical to the size  in the Fortran routine:       float  --- REAL     (this is typical, but not always the case)     double --- REAL*8

No comments:

Post a Comment