Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
7234 Discussions

What is exactly the calling convention for the blas and lapack functions?

Hanyou_Chu
Beginner
839 Views
I must say that I have been confused about the MKL calling conventions when some of the arguments are strings for the blas and lapack functions. First of all if it is callable in Fortran, it must at least conform to the default Fortran convention which passes string lengths as hidden arguments at the end of argument list. For instance, mkl_blas.h defines

void DGEMM(const char *transa, const char *transb, const MKL_INT *m, const MKL_INT *n, const MKL_INT *k,
const double *alpha, const double *a, const MKL_INT *lda, const double *b, const MKL_INT *ldb,
const double *beta, double *c, const MKL_INT *ldc);

shouldn't it be (in C++):
void DGEMM(const char *transa, const char *transb, const MKL_INT *m, const MKL_INT *n, const MKL_INT *k,
const double *alpha, const double *a, const MKL_INT *lda, const double *b, const MKL_INT *ldb,
const double *beta, double *c, const MKL_INT *ldc, int lengths=1, int lengths=1);
or
void DGEMM(const char *transa, const char *transb, const MKL_INT *m, const MKL_INT *n, const MKL_INT *k,
const double *alpha, const double *a, const MKL_INT *lda, const double *b, const MKL_INT *ldb,
const double *beta, double *c, const MKL_INT *ldc, int const& lengtha=1, int const& lengthb=1);
?

I guess in the c calling convention, where arguments are passed from right to left on the stack and the stack is cleaned by the caller, and fortunately lapack routines do not check the string length, it really does not matter. That's probably why it works anyway you call it.



0 Kudos
4 Replies
TimP
Honored Contributor III
840 Views
In C++, extern "C" is required, to make a C compatible call without name mangling. I've seen people try to pass by reference (&) even in such cases, but that would not match the appended character length arguments. Basically, I believe you're correct about appended const int string length arguments. As there are include files with suggested C prototypes (which you quoted), it's definitely the responsibility of the library to make it work when you conform with those include files.
0 Kudos
Hanyou_Chu
Beginner
840 Views
Quoting - tim18
In C++, extern "C" is required, to make a C compatible call without name mangling. I've seen people try to pass by reference (&) even in such cases, but that would not match the appended character length arguments. Basically, I believe you're correct about appended const int string length arguments. As there are include files with suggested C prototypes (which you quoted), it's definitely the responsibility of the library to make it work when you conform with those include files.
Yes, I am aware of extern "C" for C++. I was specifically referring to the default parameter values. I just want some one from Intel MKL team to clarify the exact calling conventions because the definitions in their header files don't seem to be consistent with their Fortran compiler conventions. Besides, I would protype the functions with referrences instead of pointers whenever possible for C++.
0 Kudos
Michael_C_Intel4
Employee
840 Views
Quoting - Hanyou Chu
Yes, I am aware of extern "C" for C++. I was specifically referring to the default parameter values. I just want some one from Intel MKL team to clarify the exact calling conventions because the definitions in their header files don't seem to be consistent with their Fortran compiler conventions. Besides, I would protype the functions with referrences instead of pointers whenever possible for C++.

You're right, C definitions don't contain length parameters as it's supposed by Fortran calling convention. Actually,these parameters are never used, and these are not necessary to be passed. This just simplifies calling MKL routine from C. Is it a problem for you that C prototypes don't have lentghs?

We use pointers in prototypesbecauseheader filesare for C customers generally. Do you want MKL todistribute "C++ style" headers (not actually C++, but using references instead of pointers)?

Michael.
0 Kudos
Hanyou_Chu
Beginner
840 Views

You're right, C definitions don't contain length parameters as it's supposed by Fortran calling convention. Actually,these parameters are never used, and these are not necessary to be passed. This just simplifies calling MKL routine from C. Is it a problem for you that C prototypes don't have lentghs?

We use pointers in prototypesbecauseheader filesare for C customers generally. Do you want MKL todistribute "C++ style" headers (not actually C++, but using references instead of pointers)?

Michael.
Thanks for your clarafications. I never had any issues with omission of the length arguments. Just want to make sure that things work flawlessly.

I do prefer C++ style headers since I use C++ and references are much more convenient to use, especially for constant references. I usually write my own header files.
0 Kudos
Reply