Appendix E: Calling LAPACK from C

Calling a Fortran-based matrix library from Fortran is less trouble than calling it from some other language. However,if you refuse to be a Fortran programmer, then there are often C and Java implementations of Fortran libraries available, such as JAMA, JLAPACK, LAPACK++, and TNT, for use from these languages.

Some of our research projects have required us to have Fortran and C programs calling each other, and, after some experimentation, we have had success doing it [L&F 93]. Care is needed in accounting for the somewhat different ways compilers store subroutine names, for the quite different ways they store arrays with more than one subscript, and for the different data types available in the two languages.

The first thing you must do is ensure that the data types of the variables in the two languages are matched. The matching data types are given in Table E.1. Note that if the data are stored in arrays, the C calling program must convert to the storage scheme used in the Fortran subroutine before you can call the Fortran subroutine (and then convert back to the C scheme after the subroutine does its job if you have overwritten the original array and intend to use it again).

When a function is called in the C language, usually the actual value of the argument is passed to the function. In contrast, Fortran passes the address in memory where the value of the argument is to be found (a reference pass). If you do not ensure that both languages have the same passing protocol, your program will process the numerical value of the address of a variable as if it were the actual value of the variable (we are willing to place a bet on the correctness of the result). Here are some procedures for calling Fortran from C:

1.  Use pointers for all the arguments in your C program. Generally this is done with the address operator &.

2.  Do not have your program make calls such as sub(1, N) where the actual value of the constant 1 is fed to the subroutine. Instead, assign the value 1 to a variable and feed that variable (actually a pointer to it) to the subroutine. For example:

image

This is important because the value 1 in the Fortran subroutine call is actually the address where the value 1 is stored. If the subroutine modifies that variable, it will modify the value of 1 every place in your program!

3.  Depending on the particular operating system you are using,you may have to append an underscore _ to the called Fortran subprogram names; for example, sub(one, N) sub_(one, N). Generally, the Fortran compiler appends an underscore automatically to the names of its subprograms, while the C compiler does not (but you’ll need to experiment).

TABLE E.1
Matching Data Types in C and Fortran

image

4.  Use lowercase letters for the names of external functions. The exception is when the Fortran subprogram being called was compiled with a -U option, or the equivalent, for retaining uppercase letters.

E.1 Calling LAPACK Fortran from C

image

Notice here that the call to the Fortran subroutine dgesv is made as

image

That is, lowercase letters are used and an underscore is added to the subroutine name. In addition, we convert the matrix A,

image

This changes C’s row-major order to Fortran’s column-major order.

E.2 Compiling C Programs with Fortran Calls

Multilanguage programs are actually created when the compiler links the object files together. The tricky part is that while Fortran automatically includes its math library, if your final linking is done with the C compiler, you may have to explicitly include the Fortran library as well as others. Here we give some examples that have worked at one time or another on one machine or another; you probably will need to read the user’s guide and experiment to get this to work with your system:

image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset