title: copying data from device to host not working
body: <p>i'm using vs2010 on windows 7 x64 and the CUDA toolkit v4.0 for my university project. I'd like to acheive a simple gpu-vs-cpu test, most of it is done, but none of my cuda tests return any results. I've checked the memory with the debugger and the device memory contained everything I needed, only the memory copying failed.</p>
  
  <pre><code>host_vector&lt;int&gt; addWithCuda(host_vector&lt;int&gt; h_a, host_vector&lt;int&gt; h_b)
  {
  int size = h_a.size();
  host_vector&lt;int&gt; h_c(size);
  
  // Choose which GPU to run on, change this on a multi-GPU system.
  cudaError_t cudaStatus = cudaSetDevice(0);
  if (cudaStatus != cudaSuccess) {
      fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
      return h_c;
  }
  else{
      // Allocate GPU buffers for three vectors (two input, one output).
      // Copy input vectors from host memory to GPU buffers.
      device_vector&lt;int&gt; d_c=h_c;
      device_vector&lt;int&gt; d_a=h_a;
      device_vector&lt;int&gt; d_b=h_b;
  
      int*d_a_ptr = raw_pointer_cast(&amp;d_a[0]);
      int*d_b_ptr = raw_pointer_cast(&amp;d_b[0]);
      int*d_c_ptr = raw_pointer_cast(&amp;d_c[0]);
      int*h_c_ptr = raw_pointer_cast(&amp;h_c[0]);
  
      // Launch a kernel on the GPU with one thread for each element.
      addKernel&lt;&lt;&lt;1, size&gt;&gt;&gt;(d_c_ptr, d_a_ptr, d_b_ptr);
  
      // cudaDeviceSynchronize waits for the kernel to finish, and returns
      // any errors encountered during the launch.
      cudaStatus = cudaDeviceSynchronize();
      if (cudaStatus != cudaSuccess) {
          fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
          return h_c;
      }
      thrust::device_vector&lt;int&gt;::iterator d_it;
      thrust::host_vector&lt;int&gt;::iterator h_it;
      // Copy output vector from GPU buffer to host memory.
      h_c=d_c;
      printf("||Debug h_c[0]=%d\td_c[0]=%d\n",h_c[0],d_c[0]);
  }
  cudaStatus = cudaDeviceReset();
  if (cudaStatus != cudaSuccess) {
      fprintf(stderr, "cudaDeviceReset failed!");
  }
  return h_c;
  }
  </code></pre>
  
  <p>Note the code line "h_c=d_c;". In thrust this was supposed to copy data from d_c(a device vector) to h_c(a host vector). This line doesn't fail, but doesn't execute correctly either. The h_c remains all 0 all the way.</p>
  
  <p>I've tried several other methods like </p>
  
  <pre><code>thrust::copy(d_c.begin(),d_c.end(),h_c.begin()); 
  </code></pre>
  
  <p>or </p>
  
  <pre><code>cudaMemcpy(h_c_ptr,d_c_ptr,size*sizeof(int),cudaMemcpyDeviceToHost);
  </code></pre>
  
  <p>or even </p>
  
  <pre><code>for(int i=0;i &lt; size;++i)h_c[i]=d_c[i];
  </code></pre>
  
  <p>Nothing worked. I'm lost here.</p>
  
  <p>Anyone had anything similar? All help apreciated.</p>