Very briefly, I have 3 vectors from my CUDA C program and then I call an extern C function which does stream compaction using thrust. My problem starts when I finish the stream compaction algorithm and have to copy the results back to normal C arrays. So I need some help to find a way to do this. Here is the code
extern "C" void sort(float*intx,float*inty,float*intz){
thrust::device_vector<float> x(112*88*1408);
thrust::device_vector<float> y(112*88*1408);
thrust::device_vector<float> z(112*88*1408);
thrust::copy(intx,intx+112*88*1408,x.begin());
thrust::copy(inty,inty+112*88*1408,y.begin());
thrust::copy(intz,intz+112*88*1408,z.begin());
thrust::device_vector<float> x_out(112*88*1408);
thrust::device_vector<float> y_out(112*88*1408);
thrust::device_vector<float> z_out(112*88*1408);
typedef thrust::device_vector<float>::iterator Iterator;
typedef thrust::tuple<Iterator, Iterator, Iterator> IteratorTuple;
typedef thrust::zip_iterator<IteratorTuple> ZipIterator;
ZipIterator begin(thrust::make_tuple(x.begin(), y.begin(), z.begin()));
ZipIterator end (thrust::make_tuple(x.end(), y.end(), z.end()));
ZipIterator output_begin(thrust::make_tuple(x_out.begin(), y_out.begin(), z_out.begin()));
ZipIterator output_end = thrust::remove_copy_if(begin, end, output_begin, less_than_zero<float>());
// compute size of output
size_t N = output_end - output_begin;
// resize output
x_out.resize(N);
y_out.resize(N);
z_out.resize(N);
//Must have a function here to alloc new C output arrays
for(size_t i = 0; i < x_out.size(); i++)
std::cout << x_out[i] << " " << y_out[i] << " " << z_out[i] << std::endl;
}
If you want the pointer to the device memory, yes. If you want to use the data in “plain C” then copy it to a host_vector first, and then take the raw_pointer. I made a little wrapper function for this, I’ll reply it later (it’s not at my home computer).
Thank you davkec and yes I want the the array in plain C. Just one thing, I can use this stream compaction algorithm with device vector input instead of host vector? If so, i just have to pass the pointer to the device arrays?
also i think that you can do the following if you dont use the functionalities of a host vecrors.
1.Create your plain C arrays.
2.Copy them to a device vector using copy with a raw pointer cast.
3.Perform stream compaction.
4.Copy back the data using the raw pointers of the host vector from step 2.
nope,i meant that in the final copy (from device to host) you can simply use &x[0] as the begining of the array you want to end the data to .
You can even write your own function to copy data starting from &x_out[0] to a new array xout_new[N]
It doesn’t seems that complex to me. (unless i missunderstood your problem)
Sorry, I assume it is an easy problem, but I am no programmer =/ so basic things, sometimes are hard for me to understand
I ended up doing this:
thrust::host_vector<float> x_outh=xout;
intxc=thrust::raw_pointer_cast(x_outh.data()) //the intxc pointer is the same as the input one
I print array intxc(INSIDE the thrust function) and the values are correct, however if I print the array with printf in the main function afterwards, the values that are printed are the ones before the stream compaction algorithm =/
EDIT: @apostglen46 I did as you said(at least I think it was what you said), and the results are the same
could you please write a simple test case (very small input size) so we can help you further?
I thought that what you wrote would work,it was more or less what i suggested.
Here is a simple test case: I have 3 arrays(x,y,z) with 10 elements each. The first 5 elements are 0, and the other 5 elements are 5’s. According to my boolean operator, it is supposed to delete the lines which meet the criteria x[i]=y[i]=z[i]=0;
The print function inside the thrust function outputs this:
I think you are right, it should be working, I did some tests with CPU functions and yes you copy an array by assigning the array’s first element address