title: How to cudaMemcpy a __device__ initialized var
body: <p>I have some working code... where I allocate a device variable pointer as follows:</p>
  
  <pre><code>float *d_var;
  cudaMalloc(&amp;d_var, sizeof(float) );
  </code></pre>
  
  <p>Later on in my code, I want to copy the contents of this var to a local var (ref):</p>
  
  <pre><code>checkCudaErrors(cudaMemcpy(&amp;h_var, &amp;d_var, sizeof(float), cudaMemcpyDeviceToHost));
  </code></pre>
  
  <p>Which works great! But using <code>cudaMalloc</code> is <strong>slow</strong>!</p>
  
  <hr>
  
  <p>So I want to instead allocate the variable without using <code>cudaMalloc</code> using a <code>__device__</code> definition:</p>
  
  <pre><code>__device__ float d_var = 1000000000.0f;
  </code></pre>
  
  <p>This works great and I know the d_var in this case is initialized properly and I can do all my work with it like before. I've been printf'ing its contents, so I know it has the right contents. But when I try to copy the contents now to my host var using the same code as before:</p>
  
  <pre><code>checkCudaErrors(cudaMemcpy(&amp;h_var, &amp;d_var, sizeof(float), cudaMemcpyDeviceToHost));
  </code></pre>
  
  <p>I get a really vague error:</p>
  
  <pre><code>invalid argument cudaMemcpy(&amp;h_var, &amp;d_var, sizeof(float), cudaMemcpyDeviceToHost)
  </code></pre>
  
  <p>I've tried referring to the variable as <code>&amp;d_var</code>, <code>d_var</code>, <code>*d_var</code> to no avail.
  Any help MUCH appreciated.</p>
  
  <p>Thanks!</p>