【发布时间】:2019-04-24 16:47:42
【问题描述】:
继续我的 CUDA 初学者冒险,我被介绍给 Thrust,这似乎是一个方便的库,可以为我省去显式内存(取消)分配的麻烦。
我已经尝试将它与一些 cuBLAS 例程结合起来,例如gemv,通过使用thrust::raw_pointer_cast(array.data()) 生成指向底层存储的原始指针,然后将其提供给例程,它工作得很好。
当前任务是求矩阵的逆,为此我使用getrfBatched 和getriBatched。来自文档:
cublasStatus_t cublasDgetrfBatched(cublasHandle_t handle,
int n,
double *Aarray[],
int lda,
int *PivotArray,
int *infoArray,
int batchSize);
在哪里
Aarray - device - array of pointers to <type> array
当然,我认为我可以使用另一层推力向量来表达这个指针数组并再次将其原始指针提供给 cuBLAS,所以这就是我所做的:
void test()
{
thrust::device_vector<double> in(4);
in[0] = 1;
in[1] = 3;
in[2] = 2;
in[3] = 4;
cublasStatus_t stat;
cublasHandle_t handle;
stat = cublasCreate(&handle);
thrust::device_vector<double> out(4, 0);
thrust::device_vector<int> pivot(2, 0);
int info = 0;
thrust::device_vector<double*> in_array(1);
in_array[0] = thrust::raw_pointer_cast(in.data());
thrust::device_vector<double*> out_array(1);
out_array[0] = thrust::raw_pointer_cast(out.data());
stat = cublasDgetrfBatched(handle, 2,
(double**)thrust::raw_pointer_cast(in_array.data()), 2,
thrust::raw_pointer_cast(pivot.data()), &info, 1);
stat = cublasDgetriBatched(handle, 2,
(const double**)thrust::raw_pointer_cast(in_array.data()), 2,
thrust::raw_pointer_cast(pivot.data()),
(double**)thrust::raw_pointer_cast(out_array.data()), 2, &info, 1);
}
执行时,stat 表示 CUBLAS_STATUS_SUCCESS (0) 和 info 表示 0(执行成功),但如果我尝试使用标准括号表示法访问 in、pivot 或 out 的元素,我打了一个thrust::system::system_error。在我看来,相应的内存不知何故损坏了。
我在这里遗漏了什么明显的东西?
【问题讨论】: