Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would cblas_dgemm and cblas_sgemm have different pointer types in a function pointer array?

I have a function pointer array which I use to call the appropriate cblas_xgemm (e.g., cblas_dgemm or cblas_sgemm, etc., from ATLAS/CBLAS).

This works great when I tell it to use cblas_dgemm by function pointer; dgemm gets called with the appropriate arguments and returns a correct result.

However, when I call cblas_sgemm by function pointer, I get the following output:

ldc must be >= MAX(N,1): ldc=0 N=2Parameter 14 to routine cblas_sgemm was incorrect

I have written a short test program which demonstrates the problem. Calls to cblas_sgemm without the function pointer work fine.

Note especially the following gcc warning (see also the gist linked above, which has the full gcc output):

test_cblas_sgemm.c:20:3: warning: initialization from incompatible pointer type [enabled by default]

If I comment out the cblas_sgemm line in the function pointer array definition, I do not get such a warning, even for the cblas_dgemm line. But that makes no sense because both of these functions should have the same return type!

Here are the appropriate lines from cblas.h:

void cblas_sgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA,
                 const enum CBLAS_TRANSPOSE TransB, const int M, const int N,
                 const int K, const float alpha, const float *A,
                 const int lda, const float *B, const int ldb,
                 const float beta, float *C, const int ldc);
void cblas_dgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA,
                 const enum CBLAS_TRANSPOSE TransB, const int M, const int N,
                 const int K, const double alpha, const double *A,
                 const int lda, const double *B, const int ldb,
                 const double beta, double *C, const int ldc);

So what gives? Is it somehow getting one of the xgemm functions from one header and the other from another? Or am I dealing with some weird function pointer issue?

like image 211
Translunar Avatar asked Jan 23 '12 21:01

Translunar


1 Answers

Your question seems to boil down to the fact that you have functions of differing signatures that you are trying to call from a function pointer array. I've never tried this myself, but from a little bit of research, it doesn't look like this is possible. That makes sense, since a statically typed language would not be able to evaluate type safety of this maneuver.

I've seen suggestions of using boost::variant, and other workarounds for this, but since you are linking to a library that is not our yown, I'm not sure there is much value in pursing those options.

If you want to prove or disprove, you could remove CBLAS from your test program, and simply implement a class with multiple methods of signature, which differ only in datatype.

On a side note, column major is a major pain.

like image 138
Evan Avatar answered Sep 24 '22 01:09

Evan