1

CUBLAS has a separate function for each type of data, but I want to call CUBLAS from within a template, e.g.:

template <typename T> foo(...) {
    ...
    cublas<S/D/C/Z>geam(..., const T* A, ...);
    ...
}

How do I trigger the correct function call?

talonmies
  • 70,661
  • 34
  • 192
  • 269
mchen
  • 9,808
  • 17
  • 72
  • 125
  • C switch case statement? Take a look at [this answer](http://stackoverflow.com/questions/6179295/if-statement-inside-a-cuda-kernel/6179580#6179580). – Robert Crovella May 06 '13 at 15:36
  • @RobertCrovella - OK, but how can I compare types? So e.g. `switch(T) {case float: ...}`? Is that valid? – mchen May 06 '13 at 15:39
  • no that won't work. It's probably a bad idea. You could switch on the sizeof the data type, but double and cuComplex have the same size. – Robert Crovella May 06 '13 at 16:03
  • If you are asking about host side code, and if you have a very recent C++ compiler, you should be able to use C++ RTTI (ie. `typeid()`) to achieve this. But I fail to see how this has *anything* to do with CUDA or CUBLAS, it is a generic C++ programming question. – talonmies May 06 '13 at 16:45

1 Answers1

2

I wrote cublas wrapper functions for different types with same function name.

inline cublasStatus_t cublasGgeam(cublasHandle_t handle,
        cublasOperation_t transa, cublasOperation_t transb,
        int m, int n,
        const float *alpha,
        const float *A, int lda,
        const float *beta,
        const float *B, int ldb,
        float *C, int ldc)
{
    return cublasSgeam(handle, transa, transb, m, n, alpha, A, lda, beta, B, ldb, C, ldc);
}

inline cublasStatus_t cublasGgeam(cublasHandle_t handle,
        cublasOperation_t transa, cublasOperation_t transb,
        int m, int n,
        const double *alpha,
        const double *A, int lda,
        const double *beta,
        const double *B, int ldb,
        double *C, int ldc)
{
    return cublasDgeam(handle, transa, transb, m, n, alpha, A, lda, beta, B, ldb, C, ldc);
}

After that, you can call geam() for any type with the same function name. C++ compiler will choose the right function by the type of the parameters. In you case it should be like

template <typename T> foo(...) {
    ...
    cublasGgeam(..., A, ...);
    ...
}

This is a comple-time overload and no runtime cost at all, although you have to write a long list for wrapper functions.

kangshiyin
  • 9,681
  • 1
  • 17
  • 29