Subject: Re: CCL:Calling FORTRAN FUNCTIONS from a C-Program.

From: [email-masked] (Hinsen Konrad)
Date: Tue, 11 Oct 94 23:38:34 -0400

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.

Contrarily, in Fortran the corresponding functions are built-in, i.e.
once and for all defined in the language standard. The compiler knows
exactly what they do and can therefore optimize - it could for example
replace X**4 by X*X*X*X, which is typically cheaper to do. If you
use the most general case however, i.e. with only variables as arguments,
there is little to do and therefore no advantage in comparison to C.

>From this it should be clear that there is nothing to be gained from
calling the Fortran functions from C. What makes Fortran faster
(sometimes) is just the fact that it might not have to call the function
at all. Functions calls in general are very cheap in C - typically
cheaper than in Fortran.

Before you rush to the conclusion that Fortran is better than C for
scientific programming, think again. Does the advantage in some
built-in function really make a large difference in the execution
time of *your* program? Is it more than just one percent? Is it really
worth the additional expense of *your* time that you need to
deal with Fortran's archaic structures and lacking functionality?
Isn't your time worth much more than CPU time? And even if you
value CPU time higher, don't forget the CPU time wasted by the
more involved debugging cycles typically necessary with Fortran code.

Besides, compiler technology improves. It is possible nowadays to
write compilers that are smart even about non-built-in functions.
They are still rare, but this will change. If you write programs
with an eye towards long-time development, don't tie yourself
to outdated technology for the sake of a small advantage today.
Choose a language that you are comfortable with. Whether or not
that is C is another matter - C is not without faults either.

   the procedure to call FORTRAN functions from within a C-Program. I run my 
   code on DEC-5000/200, SPARC-10 workstations and Intel-Paragon Parallel 
   Computer. General tips about calling FORTRAN from C will be particularly 
   useful as we have tons of FORTRAN code out in the cyber space !

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.

-------------------------------------------------------------------------------
Konrad Hinsen                     | E-Mail: [email-masked]
Departement de Chimie             | Tel.: +1-514-343-6111 ext. 3953
Universite de Montreal            | Fax:  +1-514-343-7586
C.P. 6128, succ. A                | Deutsch/Esperanto/English/Nederlands/
Montreal (QC) H3C 3J7             | Francais (phase experimentale)
-------------------------------------------------------------------------------

---

From: [email-masked] (Anil G. Mudholkar)
Date: Wed, 12 Oct 94 11:15:20 CDT

Hinsen Konrad <[email-masked]> 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

the following:

) 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
calls.

	(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
	subroutine junk(a)
	integer a
>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
functions.)

	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
seen it.

	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.

				Anil Mudholkar
				[email-masked]
				http://rainbow.uchicago.edu/~glump

---

From: [email-masked] (Hinsen Konrad)
Date: Wed, 12 Oct 94 14:01:49 -0400

Date: Wed, 12 Oct 94 11:15:20 CDT
   From: [email-masked] (Anil G. Mudholkar)
   Cc: Anil <[email-masked]>
   Reply-To: Anil <[email-masked]>
   Sender: Computational Chemistry List <[email-masked]>
   Errors-To: [email-masked]
   Precedence: bulk
   X-Caution: User has inserted an Explicit Reply-To

Anil G. Mudholkar wrote:

An ANSI compiler should complain if you do not include math.h.
I would recommend *never* to use an old K&R-style C compiler for
scientific programming (or in fact for anything else). ANSI
compilers are easily available nowadays; gcc is even free.
K&R C can be as unpleasant as Fortran, although for different
reasons.

   ...

This might work on one machine, but not on another. It will probably
work with most Unix machines (except possibly IBM's RS/6000), but
again there is no guarantee. It is definitely not portable. This is
exactly why CFORTRAN was created - it wraps the machine dependant
details in a portable layer, using the C preprocessor.

I would agree on that - if you have a Fortran compiler and can link
Fortran and C code, that is certainly the better way. You don't get
any portability advantage by using f2c, since the source code it
produces is not portable in general (the file f2c.h must be modified
for each system).

Underline that twice and put it next to your keyboard! A better
algorithm does more for performance than even the best compiler.