将Thrust设备迭代器转换为原始指针 [英] Converting Thrust device iterators to raw pointers

查看:87
本文介绍了将Thrust设备迭代器转换为原始指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑以下简单代码,在这些代码中,我将转换为 thrust :: host_vector< int> :: iterator h_temp_iterator = h_temp.begin(); thrust :: device_vector< int> :: iterator d_temp_iterator = d_temp.begin(); 指向原始指针.

I'm considering the following simple code in which I'm converting thrust::host_vector<int>::iterator h_temp_iterator = h_temp.begin(); and thrust::device_vector<int>::iterator d_temp_iterator = d_temp.begin(); to raw pointers.

为此,我将&(h_temp_iterator [0])&(d_temp_iterator [0])分别传递给函数和内核.前者(CPU情况)可以编译,后者(GPU情况)则不能.这两种情况原则上应该是对称的,因此我不理解错误消息的原因,即:

To this end, I'm passing &(h_temp_iterator[0]) and &(d_temp_iterator[0]) to a function and a kernel, respectively. The former (CPU case) compiles, the latter (GPU case) not. The two cases should be in principle symmetric, so I do not understand the reason for the error message which is:

Error   1   error : no suitable conversion function from "thrust::device_ptr<int>" to "int *" exists    

配置为:

  1. Windows 7 Visual Studio 2010 CUDA 7.5 ,针对 3.5 体系结构进行编译.
  2. Windows 10 Visual Studio 2013 CUDA 8.0 ,针对 5.2 体系结构进行编译.
  1. Windows 7, Visual Studio 2010, CUDA 7.5, compiling for the 3.5 architecture.
  2. Windows 10, Visual Studio 2013, CUDA 8.0, compiling for the 5.2 architecture.

代码

#include <thrust\host_vector.h>
#include <thrust\device_vector.h>

__global__ void testKernel(int *a, const int N)
{
    int i = threadIdx.x;

    if (i >= N) return;

    a[i] = 2;
}

void testFunction(int *a, const int N)
{
    for (int i = 0; i < N; i++) a[i] = 2;
}

int main()
{
    const int N = 10;

    thrust::host_vector<int> h_temp(N);
    thrust::device_vector<int> d_temp(N);

    thrust::host_vector<int>::iterator h_temp_iterator = h_temp.begin();
    thrust::device_vector<int>::iterator d_temp_iterator = d_temp.begin();

    testFunction(&(h_temp_iterator[0]), N);
    testKernel<<<1, N>>>(&(d_temp_iterator[0]), N);

    for (int i = 0; i < N; i++) printf("%i %i\n", i, h_temp[i]);

    return 0;
}

推荐答案

按照爪子的评论,解决办法是通过

Following talonmies' comments, the solution is to pass

thrust::raw_pointer_cast(&d_temp_iterator[0])

不是

&d_temp_iterator[0]

下面是完整的代码

#include <thrust\host_vector.h>
#include <thrust\device_vector.h>

__global__ void testKernel(int *a, const int N)
{
    int i = threadIdx.x;

    if (i >= N) return;

    a[i] = 2;

    printf("GPU %i %i\n", i, a[i]);
}

void testFunction(int *a, const int N)
{
    for (int i = 0; i < N; i++) {
        a[i] = 2;
        printf("CPU %i %i\n", i, a[i]);
    }
}

int main()
{
    const int N = 10;

    thrust::host_vector<int> h_temp(N);
    thrust::device_vector<int> d_temp(N);

    thrust::host_vector<int>::iterator h_temp_iterator = h_temp.begin();
    thrust::device_vector<int>::iterator d_temp_iterator = d_temp.begin();

    int *temp = thrust::raw_pointer_cast(&d_temp_iterator[0]);

    testFunction(&(h_temp_iterator[0]), N);
    testKernel<<<1, N>>>(temp, N);

    return 0;
}

这篇关于将Thrust设备迭代器转换为原始指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆