I am trying to find the sum of an array (already present in CUDA memory) using thrust library. Few replies here, said that is possible by wrapping it using thrust::device_ptr, but it is throwing an error for me.
Initial code
cudaMemcpy((void *)(data + stride), (void *)d_output, sizeof(unsigned int) * rows * cols, cudaMemcpyDeviceToHost);
thrust::device_vector<unsigned int> vec((data + stride), (data + stride + (rows * cols)));
sum = thrust::reduce(vec.begin(), vec.end());
The above code works perfectly fine. But if I change it to
thrust::device_ptr<unsigned int> outputPtrBegin(d_output);
thrust::device_ptr<unsigned int> outputPtrEnd((d_output + stride + (rows * cols)));
sum = thrust::reduce(outputPtrBegin, outputPtrEnd);
It throws me the following error.
terminate called after throwing an instance of 'thrust::system::system_error'
what(): an illegal memory access was encountered
Aborted (core dumped)
What could be the problem? Thanks a lot for your time.
*Edited input from Robert Crovella The mistake was using stride. I have a following question (related to the above declaration)
Depending on the value of toggle, I need to call thrust
if(toggle) {
thrust::device_ptr<unsigned int> outputPtrBegin(d_output);
thrust::device_ptr<unsigned int> outputPtrEnd((d_output + (rows * cols)));
}
else {
thrust::device_ptr<unsigned int> outputPtrBegin(d_X);
thrust::device_ptr<unsigned int> outputPtrEnd((d_X + (rows * cols)));
}
But the compilation says outputPtrBegin and outputPtrEnd are not declared, because they are in the if statement. How do I declare these device pointers before and then use?