当所有参数都在设备内存中时如何调用cuSparse?

时间:2018-07-15 04:00:59

标签: cuda gpu

我有一个使用cusparseDgemmi将稀疏向量与密集向量相乘的代码:

cusparseDgemmi(cusparsehandle,cols,cols,cols,nnz,&al,G,
        cols,cscVal,cscColPtr,cscRowInd,
        &bet,SE,cols);

其中cusparsehandlecolsnnzalbet在主机内存中,其余在设备内存中。上面的工作正常。

现在假设我先将cols移至设备内存,然后调用cusparse:

//d_cols->cols
int *d_cols,*p_cols;
p_cols=&cols;
printf("%d\n",*p_cols);
cudaMalloc((void**)&d_cols,sizeof(int));
cudaMemcpy(d_cols,p_cols, sizeof(int), cudaMemcpyHostToDevice);

//...

//call cusparse with *d_cols instead of cols
cusparseDgemmi(cusparsehandle,*d_cols,*d_cols,*d_cols,nnz,&al,G,
        *d_cols,cscVal,cscColPtr,cscRowInd,
        &bet,SE,*d_cols);

代码将编译,但在运行时崩溃。 cusparse参考手册说"cuSPARSE API assumes that input and output data reside in GPU (device) memory, unless it is explicitly indicated otherwise by the string DevHostPtr"

如果是这种情况,那么为什么cols在主机内存中而不是在设备内存中时,我的代码可以正常运行?当cusparse函数的所有arguments参数都位于设备内存中时,如何调用cusparse。我该怎么办?

1 个答案:

答案 0 :(得分:3)

  

cusparse参考手册指出,“ cuSPARSE API假定   输入和输出数据驻留在GPU(设备)内存中,除非   否则由字符串DevHostPtr明确表示”。

documentation实际上说,alpha和beta可以作为指针传递到设备或主机存储器中,具体取决于您使用的指针模式设置。这就是全部。而您尝试做的事与此无关。

您真正要问的是“我可以在主机代码中取消引用设备指针吗?”,答案显然是否定的,并且是看到的段错误的来源。

唯一可行的方法是(在支持它的平台上)使用托管内存分配其他参数,以便它们在主机和设备上都是有效的指针,并在cuSparse调用中取消引用它们。请注意,这样做会降低性能。

相关问题