2013-05-06 3 views
1

CUBLAS는 데이터의 각 유형에 대한 separate function을 가지고,하지만 난 템플릿 함수 내에서 CUBLAS 라이브러리를 사용하는 방법은 무엇입니까?

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

가 어떻게 올바른 함수 호출을 트리거 할 : 템플릿 내에서 CUBLAS를 호출 예컨대 줄까?

+0

C switch case statement? [this answer] (http://stackoverflow.com/questions/6179295/if-statement-inside-a-cuda-kernel/6179580#6179580)를보십시오. –

+0

@RobertCrovella - 알았지 만 유형을 비교하려면 어떻게해야합니까? 그래서 예. 'switch (T) {case float : ...}'? 그게 유효합니까? – mchen

+0

그게 작동하지 않습니다. 그것은 아마 나쁜 생각입니다. 데이터 유형의 크기를 전환 할 수 있지만 double과 cuComplex는 같은 크기입니다. –

답변

2

동일한 함수 이름을 가진 여러 유형의 cublas 래퍼 함수를 ​​작성했습니다.

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); 
} 

그런 다음 동일한 함수 이름을 가진 모든 유형에 대해 geam()을 호출 할 수 있습니다. C++ 컴파일러는 매개 변수 유형별로 올바른 함수를 선택합니다. 당신이 경우에 당신은 래퍼 함수에 대한 긴 목록을 작성해야하지만, 그것은

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

이것은 공단 시간 과부하와 전혀 런타임 비용처럼해야한다.

관련 문제