2011-10-06 3 views
13

나는 벡터에서 원시 포인터로가는 방법을 이해하지만, 뒤로가는 방법은 비트를 건너 뛰고있다.thrust :: device_vector에서 원시 포인터로 및 다시 포인터로?

// our host vector 
thrust::host_vector<dbl2> hVec; 

// pretend we put data in it here 

// get a device_vector 
thrust::device_vector<dbl2> dVec = hVec; 

// get the device ptr 
thrust::device_ptr devPtr = &d_vec[0]; 

// now how do i get back to device_vector? 
thrust::device_vector<dbl2> dVec2 = devPtr; // gives error 
thrust::device_vector<dbl2> dVec2(devPtr); // gives error 

예를 들어 설명해 줄 수 있습니까?

답변

9

당신은 초기화 단지 표준 컨테이너, 즉 통해 반복자 같은 추력 벡터를 채울 :

#include <thrust/device_vector.h> 
#include <thrust/device_ptr.h> 

int main() 
{ 
    thrust::device_vector<double> v1(10);     // create a vector of size 10 
    thrust::device_ptr<double> dp = v1.data();    // or &v1[0] 

    thrust::device_vector<double> v2(v1);     // from copy 
    thrust::device_vector<double> v3(dp, dp + 10);   // from iterator range 
    thrust::device_vector<double> v4(v1.begin(), v1.end()); // from iterator range 
} 

당신의 간단한 예에서 방금 직접 다른 컨테이너를 복사 할 수있는 포인터를 통해 우회를 갈 필요가 없습니다. 일반적으로 배열의 시작 부분에 대한 포인터가있는 경우 배열 크기를 입력하면 v3에 대한 버전을 사용할 수 있습니다.

+0

대답는? – madmaze

+3

dbl2 * ptrDVec = thrust :: raw_pointer_cast (& d_vec [0]); 여기에서 device_vector로 돌아갈 방법이 있습니까? – madmaze

+0

"돌아오다"는 것은 무엇을 의미합니까? 이미 장치 포인터가 아닙니까? 정확히 무엇이 필요합니까? –

3

dbl2 * ptrDVec = thrust :: raw_pointer_cast (& d_vec [0]); 여기에서 device_vector로 돌아갈 수있는 방법이 있습니까?

없습니다. 초기 벡터 변수를 재사용 할 수 있어야합니다.

18

http://code.google.com/p/thrust/source/browse/examples/cuda/wrap_pointer.cu

추력이 질문에 대한 좋은 예를 제공합니다.

#include <thrust/device_ptr.h> 
#include <thrust/fill.h> 
#include <cuda.h> 

int main(void) 
{ 
    size_t N = 10; 

    // obtain raw pointer to device memory 
    int * raw_ptr; 
    cudaMalloc((void **) &raw_ptr, N * sizeof(int)); 

    // wrap raw pointer with a device_ptr 
    thrust::device_ptr<int> dev_ptr = thrust::device_pointer_cast(raw_ptr); 

    // use device_ptr in Thrust algorithms 
    thrust::fill(dev_ptr, dev_ptr + N, (int) 0);  

    // access device memory transparently through device_ptr 
    dev_ptr[0] = 1; 

    // free memory 
    cudaFree(raw_ptr); 

    return 0; 
} 

그리고 추력 용기에서 원시 포인터를 받고

는 길이없이 다시 device_vector을 얻을 수있는 방법이 없습니다 .. 자신이 이미 그래서 그냥 포인터에서

dbl2* ptrDVec = thrust::raw_pointer_cast(&d_vec[0]);