title: CUDA - separating cpu code from cuda code
body: <p>Was looking to use system functions (such as rand() ) within the CUDA kernel. However, ideally this would just run on the CPU. Can I separate files (.cu and .c++), while still making use of gpu matrix addition? For example, something along these lines:</p>
  
  <p>in main.cpp:</p>
  
  <pre><code>int main(){
  std::vector&lt;int&gt; myVec;
  srand(time(NULL));
  
  for (int i = 0; i &lt; 1024; i++){
      myvec.push_back( rand()%26);
  }
  
  selfSquare(myVec, 1024);
  </code></pre>
  
  <p>}</p>
  
  <p>and in cudaFuncs.cu:</p>
  
  <pre><code>__global__ void selfSquare_cu(int *arr, n){
      int i = threadIdx.x;
      if (i &lt; n){
          arr[i] = arr[i] * arr[i];
      }
  
  }
  
  void selfSquare(std::vector&lt;int&gt; arr, int n){
      int *cuArr;
      cudaMallocManaged(&amp;cuArr, n * sizeof(int));
      for (int i = 0; i &lt; n; i++){
          cuArr[i] = arr[i];
      }
  
      selfSquare_cu&lt;&lt;1, n&gt;&gt;(cuArr, n);
  }
  </code></pre>
  
  <p>What are best practices surrounding situations like these? Would it be a better idea to use curand and write everything in the kernel? It looks to me like in the above example, there is an extra step in taking the vector and copying it to the shared cuda memory.</p>