Vishwa07 / thrust

Automatically exported from code.google.com/p/thrust
Apache License 2.0
0 stars 0 forks source link

Add example to quick start that shows how to initialize a device_vector to existing memory #481

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I've searched the documentation, but I do not see how to initialize a 
device_vector for existing data on the device:

E.G.

thrust::device_vector<int> vals = dev_vals;

I get an error when compiling this, so it would really be nice for new users if 
you would put in the quickstart guide the best way to initialize data that is 
already allocated on the device to a thrust device vector.  

Apologies if this is obvious, but I just don't see how to do it.

Original issue reported on code.google.com by JacobGMa...@gmail.com on 2 Mar 2012 at 3:22

GoogleCodeExporter commented 9 years ago
Sorry, this isn't really a defect, I couldn't see how to change it.  

The compiler error is here:

error: no suitable constructor exists to convert from "float *" to 
"thrust::device_vector<float, thrust::device_malloc_allocator<float>>"

I spent a long time looking for how to do this without creating a host vector 
and then transferring to the device again (including documentation on the 
device_vector) but I just can't seem to find it.  

Original comment by JacobGMa...@gmail.com on 2 Mar 2012 at 3:24

GoogleCodeExporter commented 9 years ago
Hi Jacob,

For data that already resides on the GPU you don't need a device_vector at all, 
just "wrap" the raw pointer with device_ptr, as illustrated in this example 
[1].  You can use a pair of device_ptrs to initialize a device_vector too, but 
keep in mind that will cost you a copy (and more memory) while wrapping the 
pointer is basically free.

I believe device_ptr is discussed in the QuickStartGuide, though we could 
probably give it some more emphasis.  The GPU Computing Gems chapter about 
Thrust [2] discusses this topic in the context of interoperability.

Anyway, I'll keep this issue open as a reminder to improve the docs/examples.

[1] http://code.google.com/p/thrust/source/browse/examples/cuda/wrap_pointer.cu
[2] 
http://thrust.googlecode.com/files/Thrust%20-%20A%20Productivity-Oriented%20Libr
ary%20for%20CUDA.pdf

Original comment by wnbell on 2 Mar 2012 at 4:02

GoogleCodeExporter commented 9 years ago
Forwarded to https://github.com/thrust/thrust/issues/44

Original comment by jaredhoberock on 7 May 2012 at 8:49