为什么在C编程中需要fortran包装?



我最近在cBLAS中阅读了一些源代码,有些东西让我不清楚。在许多函数中,.c文件调用Fortran Wrapper而不是直接在C文件中编写代码,如以下文件:

/*
* 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;
}   

我完全糊涂了,为什么要这样做?是因为Fortran的计算效率更高吗?

"我想问的是,为什么需要一个中间包装器,为什么不用C写呢?">

整个CBLAS是对BLAS的包装。BLAS是使用参考Fortran实现和Fortran API来定义的。. BLAS可以用C或汇编语言实现,但API设置为Fortran。

因此CBLAS实际上并不包含全部功能。该功能存在于您安装的任何BLAS实现中。最常见的参考实现是用Fortran编写的,但它不是最快的。

但是,您可能可以直接从Ccblas_sdsdot调用sdsdot函数(以实际实现的任何语言)。CBLAS的作者选择实现Fortran中间子程序sdsdotsub。我现在不知道为什么有必要这样做。差别非常小,实际上只是将一个函数改为子程序。

正如@jxh正确注释的那样,调用函数比调用子例程(类似于void函数)存在更大的ABI不兼容风险。

最新更新