title: Unable to call thrust on CUDA memory
body: <p>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.</p>
  
  <p>Initial code</p>
  
  <pre><code>cudaMemcpy((void *)(data + stride), (void *)d_output, sizeof(unsigned int) * rows * cols, cudaMemcpyDeviceToHost);
  thrust::device_vector&lt;unsigned int&gt; vec((data + stride), (data + stride + (rows * cols)));
  sum = thrust::reduce(vec.begin(), vec.end());
  </code></pre>
  
  <p>The above code works perfectly fine. But if I change it to</p>
  
  <pre><code>thrust::device_ptr&lt;unsigned int&gt; outputPtrBegin(d_output);
  thrust::device_ptr&lt;unsigned int&gt; outputPtrEnd((d_output + stride + (rows * cols)));
  sum = thrust::reduce(outputPtrBegin, outputPtrEnd);
  </code></pre>
  
  <p>It throws me the following error.</p>
  
  <pre><code> terminate called after throwing an instance of 'thrust::system::system_error'
   what():  an illegal memory access was encountered
   Aborted (core dumped)
  </code></pre>
  
  <p>What could be the problem?
  Thanks a lot for your time.</p>
  
  <p>*Edited input from Robert Crovella
  The mistake was using stride.
  I have a following question (related to the above declaration)</p>
  
  <p>Depending on the value of toggle, I need to call thrust</p>
  
  <pre><code>if(toggle) {
      thrust::device_ptr&lt;unsigned int&gt; outputPtrBegin(d_output);
      thrust::device_ptr&lt;unsigned int&gt; outputPtrEnd((d_output + (rows * cols)));
  }
  else {
      thrust::device_ptr&lt;unsigned int&gt; outputPtrBegin(d_X);
      thrust::device_ptr&lt;unsigned int&gt; outputPtrEnd((d_X + (rows * cols)));
  }
  </code></pre>
  
  <p>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?</p>