Calling FORTRAN FUNCTIONS from a C-Program.
- From: glump;at;kff1.uchicago.edu (Anil G. Mudholkar)
- Subject: Calling FORTRAN FUNCTIONS from a C-Program.
- Date: Wed, 12 Oct 94 11:15:20 CDT
Hinsen Konrad <hinsenk;at;ERE.UMontreal.CA> wrote in reply to
) > My molecular dynamics code, in C, involves lot of calls to the
) > C-Functions pow(), exp() and sqrt(). I was told that corresponding
) > FORTRAN calls are cheaper. Is this true ? If so can any of you
) > knowledgeable souls mail me
) Yes and no. In C these are normal functions. Although they are in
) the standard library, there is nothing in principle that prevents
) you from replacing them with your own versions that might even do
) something completely different. Therefore the compiler knows nothing
) about them except their declaration, so it can't perform many
) optimizations easily.
Yes, but the compiler knows this only if you have inserted
#include <math.h> in your C code, otherwise it does not know even the
declarations. Further on, Mr. Konrad points out many important
differences between C and Fortran making clear that there is no
obvious advantage of one over the other, (except, perhaps, in terms of
ease of use, taste, portability, etc. I don't wish to get into C
vs. Fortran. I use them both all the time for many different
reasons.) and particularly that it is not a good idea to call the
Fortran intrinsics in place of the already rather cheap C function
(also, for reference or clarification, they are usually in
libm.a, not libc.a, though C libs sometimes have copies in libc.a.)
) Basically there are two ways to mix Fortran and C code:
) 1) Translate the Fortran code to C with f2c, a free Fortran-to-C
) compiler produced by AT&T. Available for example from
) ftp.att.com in /netlib/f2c.
) 2) Use the CFORTRAN package available by ftp from zebra.desy.de.
) Even if you want to use a language other than C, these are useful
) tools, since almost all newer language systems provide a C interface
) of some kind.
Regardless of what you want to do with your Fortran routines,
you can always call them from C, and it is very simple. Satisfy in
the following conditions in the calling C functions:
1. All Fortran functions and subroutines have an underscore appended
to their names. i.e. Fortran subroutine foo() is C function foo_().
2. All Fortran integers are passed by reference, while C integers are
usually passed by value, so a call to a Fortran integer function bar()
in C would look like int *bar_(). Likewise a call to Fortran
from C looks like junk(&a).
3. Fortran strings have no '\0' at the end, so your C function
receiving the Fortran string will have to know what to do with it;
either append a '\0' or just write n chars.
4. General Fortran routines (math, etc) are in libF77.a (always link
this), Fortran IO routines are in usually in libI77.a, and other
Fortran routines are in libU77.a. (these are sometimes located in
funny places, but usually /usr/lib, also some compilers like SunOS C
stick everything in libF77.a, but add a libV77.a for a few other
I would recommend against using f2c unless you have fortran
code but no compiler. It will not help you link the routines at all,
but rather give you a code that compiles by under C, but behaves like
fortran. That is, it will produce from your Fortran code C code
having functions with appended underscores that accept only pointers
to integers and return strings without terminating '\0's. The main
reason for the program f2c is to give you something like
"gf77 = f2c + gcc -lF77". I cannot speak to CFORTRAN having never
I suspect that you will have a much better overall performance
if you concentrate first on your algorithm and then on the its
implementation in C. If you are very worried about the performance,
you can consider parallel or data-parallel implementations or use of
special libraries or even specialized languages for expensive
routines, and only as a last measure should you try efficiency hacks
(like manual inlining of code, etc). Good luck.