2016-08-23 1 views
0

J'essaye d'exécuter un noyau très simple qui appelle la fonction cublassgemm. Mon code est:Compilation de code CUDA lorsqu'une fonction cublas est appelée dans le noyau

__global__ void cor (float * dev_mat,float * dev_cor,int n,cublasHandle_t handle) 
{ 
     const float alpha = 1.0; 
     const float beta = 0; 
     cublasStatus_t stat; 
     stat = cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, n, n, n, &alpha, dev_mat, n, dev_mat,n,&beta, dev_cor, n); 
     if(stat != CUBLAS_STATUS_SUCCESS) 
       { 
         cout<<"error in cublas sgemm \n"; 
       } 
} 
int main() 
{ 
int m =1000,n = 1000; 
float * h_mat = new float[m*n]; 
float * h_cor = new float[m*n]; 
float * dev_mat,*dev_cor; 
cudaMalloc(&dev_mat,m*n*sizeof(float)); 
cudaMalloc(&dev_cor,m*n*sizeof(float)); 
for (int i = 0; i< m; i++) 
     for(int j = 0; j <n;j++) 
       { 
         h_mat[i*n+j]=rand()%10; 
       } 
cudaError_t cudaStat; 
cublasStatus_t stat; 
cublasHandle_t handle; 
stat = cublasSetMatrix(m, n, sizeof(float), h_mat, m, dev_mat, m); 
if(stat !=CUBLAS_STATUS_SUCCESS) 
     { 
       cout<<"error in cublassetmatrix \n"; 
       return stat; 
     } 
stat = cublasCreate (&handle); 
if(stat != CUBLAS_STATUS_SUCCESS) 
     { 
       cout<<"error in cublas create handle \n"; 
       return stat; 
     } 

cor<<<1,1>>>(dev_mat,dev_cor,n,handle); 
cudaFree(dev_mat); 
delete []h_mat; 
delete []h_cor; 
return 0; 
} 

J'ai essayé de compiler le code en utilisant la commande suivante:

nvcc -lcublas cublassegmm_inside_kernel.cu -o cublassegmm_inside_kernel

Mais je suis l'erreur suivante:

calling a host function("std::operator << > ") from a global function("cor") is not allowed

Je lis This link mais je n » Je ne comprends pas comment je devrais compiler! Quelqu'un peut-il m'expliquer ou suggérer une source? Merci beaucoup

Répondre

1

Il y a 2 questions (au moins):

  1. Vous ne pouvez pas utiliser cout dans un noyau CUDA. Modifier à une déclaration printf équivalent et vous devriez être OK:

    printf("error in cublas sgemm \n"); 
    
  2. Votre commande est incorrect compilation. Le lien que vous avez donné montre les composants nécessaires. Vous devez utiliser quelque chose comme ceci:

    nvcc -arch=sm_35 -rdc=true -o cublassgemm_inside_kernel cublassgemm_inside_kernel.cu -lcublas -lcublas_device -lcudadevrt 
    

Et bien sûr, cela ne fonctionnera que sur un cc3.5 ou GPU supérieur.