2011-10-06 13 views
13

Bir vektörden ham bir işaretçiye nasıl gidileceğini anlıyorum, ancak geriye doğru nasıl gidileceği konusunda bir atlayış yapıyorum.Thrust :: device_vector öğesinden ham işaretçiye ve arkadan?

// 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 

Birisi bana bir örneği açıklayabilir/işaret edebilir mi?

cevap

9

Sen başlatmak ve sadece standart konteynerler, yani yoluyla yineleyiciler gibi itme vektörleri doldurmak:

#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 
} 

sizin basit örnekte sadece doğrudan konteyneriniz kopyalayabilir olarak, göstericiler üzerinden dolambaçlı yoldan gitmeye gerek yok. Genel olarak, bir dizinin başlangıcına işaretçiyseniz, dizi boyutunu sağladığınızda, v3 sürümünü kullanabilirsiniz.

+0

cevap olarak? – madmaze

+3

dbl2 * ptrDVec = thrust :: raw_pointer_cast (& d_vec [0]); bundan bir device_vector'a geri dönmenin bir yolu var mı? – madmaze

+0

Ne demek "geri dön" - zaten bir aygıt işaretçisi değil mi? Tam olarak neye ihtiyacınız var? –

3

dbl2 * ptrDVec = itme :: raw_pointer_cast (& d_vec [0]); bundan bir device_vector'a geri dönmenin bir yolu var mı?

Yok. İlk vektör değişkenini yeniden kullanabilmeniz gerekir.

18

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

İtme bu soru için iyi bir örnek oluşturmaktadır.

#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; 
} 

Ve itme kapları ham işaretçi alma

uzunluğu olmadan geri device_vector almak için bir yolu yoktur, .. kendiniz zaten bu yüzden sadece bir işaretçi

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