Home > Enterprise >  Why is fortran wrapper needed in C programming?
Why is fortran wrapper needed in C programming?

Time:10-19

I'm recently reading some source code in cBLAS, and something make me unclear. In many functions, a .c file calls Fortran Wrapper instead of writing the codes directly in the C file, like the following file:

/*
 * cblas_sdsdot.c
 *
 * The program is a C interface to sdsdot.
 * It calls the fortran wrapper before calling sdsdot.
 *
 * Written by Keita Teranishi.  2/11/1998
 *
 */
#include "cblas.h"
#include "cblas_f77.h"
float cblas_sdsdot( const int N, const float alpha, const float *X,
                      const int incX, const float *Y, const int incY)
{
   float dot;
#ifdef F77_INT
   F77_INT F77_N=N, F77_incX=incX, F77_incY=incY;
#else 
   #define F77_N N
   #define F77_incX incX
   #define F77_incY incY
#endif
   F77_sdsdot_sub( &F77_N, &alpha, X, &F77_incX, Y, &F77_incY, &dot);
   return dot;
}   

I'm totally confused, why should this be done? Is it because Fortran is more efficiency in computing?

CodePudding user response:

"What I wanna ask is actually why there is a need for a intermediate wrapper, why not write it in C?"

The whole CBLAS is a wrapper to BLAS. BLAS is defined using a reference Fortran implementation and a Fortran API. BLAS can be implemented in C or assembly, but the API is set to be Fortran.

Therefore the CBLAS does not actually contain the whole functionality. The functionality is in whatever BLAS implementation you install. The very common reference implementation is written in Fortran, but it is not the fastest one.

However, you probably could call the sdsdot function (in whichever language it is actually implemented) directly from C cblas_sdsdot. The author of CBLAS chose to implement a Fortran intermediate subroutine sdsdotsub. I do not have an answer why that is necessary right now. The difference is very small, really just changing a function to a subroutine.

As @jxh correctly comments, there is a larger risk of ABI incompatibility in calling a function vs calling a subroutine (similar to a void function).

  • Related