首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将二维thrust::device_vector<thrust::device_vector<int>>转换为原始指针

如何将二维thrust::device_vector<thrust::device_vector<int>>转换为原始指针
EN

Stack Overflow用户
提问于 2016-06-27 22:24:42
回答 1查看 990关注 0票数 0

当我在main函数中使用thrust::device_vector时,我可以将它正确地传递给内核函数,代码如下:

代码语言:javascript
复制
 thrust::device_vector<int> device_a(2);
 thrust::host_vector<int> host_a(2);
 MyTest << <1, 2 >> >(thrust::raw_pointer_cast(&device_a[0]),device_a.size());
 host_a = device_a;
 for (int i = 0; i < host_a.size();i++)
 cout << host_a[i] << endl;

但是我想在我的代码中使用二维device_vector,我如何使用它?如下面的代码所示

代码语言:javascript
复制
__global__ void MyTest(thrust::device_vector<int>* a,int total){  
    int idx = threadIdx.x;  
    if (idx < total){
        int temp = idx;  
        a[idx][0] = temp;  
        a[idx][1] = temp; 
        __syncthreads();
      }

}  
 void main(){
    thrust::device_vector<thrust::device_vector<int>> device_a(2,thrust::device_vector<int>(2));

    thrust::host_vector<thrust::host_vector<int>> host_a(2,thrust::host_vector<int>(2));

    MyTest << <1, 2 >> >(thrust::raw_pointer_cast(device_a.data()),device_a.size());
    host_a = device_a;
    for (int i = 0; i < host_a.size(); i++){
    cout << host_a[i][0] << endl;
    cout << host_a[i][1] << endl;
}
}
EN

回答 1

Stack Overflow用户

发布于 2016-06-27 22:48:39

通常,推力容器是仅宿主类型,不能在__device____global__函数中使用。

使用二维数组的常见方法是将其放在一维线性内存空间中,如下面的代码所示。

代码语言:javascript
复制
__global__ void MyTest(int* a, int nrows, int ncols) {
  int j = threadIdx.x;
  int i = threadIdx.y;
  if (i < nrows && j < ncols) {
    int temp = i + j;
    a[i * ncols + j] = temp;
  }

}

int main(int argc, char** argv) {
  int nrows = 2;
  int ncols = 2;
  thrust::device_vector<int> device_a(nrows * ncols);
  MyTest<<<1, dim3(2, 2)>>>(thrust::raw_pointer_cast(device_a.data()), rows, ncols);
  return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38056472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档