Do you need to allocate memory to operate on an array in CUDA? : <p>I've seen CUDA programs where you allocate memory on the device, operate on it, and then copy it back to the host, like this:</p>

<pre><code>float* h_a = (float*) malloc(numBytes);
float* d_a = 0;
cudaMalloc((void**) &amp;a, numBytes);
cuda_function&lt;&lt;&lt; N/blockSize, blockSize&gt;&gt;&gt;(d_a);
cudaMemcpy(d_a, h_a, numBytes, cudaMemcpyDeviceToHost);
</code></pre>

<p>But then I've also seen code where the CUDA program just operates on memory whose reference was passed to it, like this:</p>

<pre><code>__global__ void cuda_function(int* a)
{
  ...&lt;operate on a&gt;...
}

int main()
{
  cuda_function&lt;&lt;&lt;N/256, 256&gt;&gt;&gt;(a)
}
</code></pre>

<p>What's the different between these two approaches?</p>

<p>Thanks!</p>
