http://stackoverflow.com/questions/7678995/from-thrustdevice-vector-to-raw-pointer-and-back
1: // our host vector
2: 3: thrust::host_vector<dbl2> hVec; 4: 5: 6: 7: 8: // pretend we put data in it here
9: 10: …. 11: 12: 13: 14: // get a device_vector
15: 16: thrust::device_vector<dbl2> dVec = hVec; 17: 18: 19: 20: // get the device ptr
21: 22: thrust::device_ptr devPtr = &d_vec[0]; 23: 24: 25: 26: // if you want to pass it to the kernel, need to convert to a raw pointer
27: 28: dbl2* ptrDVec = thrust::raw_pointer_cast(&d_vec[0]); 29: 30: 31: 32: // To get back from the raw pointer to device_ptr so that host code can access it
33: 34: thrust::device_ptr<int> dev_ptr = thrust::device_pointer_cast(raw_ptr);
35: 36: // Now we can, for example:
37: 38: // use device_ptr in Thrust algorithms
39: 40: thrust::fill(dev_ptr, dev_ptr + N, (int) 0);
41: 42: // access device memory transparently through device_ptr
43: 44: dev_ptr[0] = 1; 45: 46: 47: 48: // On a side note, if you have a device_vector, you can get its device_ptr
49: 50: thrust::device_vector<double> v1(10); // create a vector of size 10
51: 52: thrust::device_ptr<double> dp = v1.data(); // or &v1[0]
53: 54: 55: 56: // Now: From thrust::device_ptr<T> we can construct thrust:device_vector<T>
57: 58: thrust::device_vector<double> v2(v1); // from copy
59: 60: thrust::device_vector<double> v3(dp, dp + 10); // from iterator range
61: 62: thrust::device_vector<double> v4(v1.begin(), v1.end()); // from iterator range
No comments:
Post a Comment