How to use CUBLAS library within a template function?

﹥>﹥吖頭↗ 提交于 2019-12-20 05:44:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16402087/how-to-use-cublas-library-within-a-template-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!