title: Cuda: Copy 1D Array From CPU to GPU
body: <p>I am newbie to Cuda, trying to copy array from Host to Device via cudaMemcpy(...)
  However,the data passed to GPU seems to be totally different (for cost: totally wrong, for G: wrong after index of 5)</p>
  
  <p>My data is a malloc array (written in C) of size 25 for example,
  I tried to copy through the following way (MAX = 5):</p>
  
  <p>Declaration:</p>
  
  <pre><code>int *cost, int* G
  int *dev_cost, *dev_G;
  </code></pre>
  
  <p>Allocation:</p>
  
  <pre><code>cost = (int*)malloc(MAX* MAX * sizeof(int));
  G = (int*)malloc(MAX* MAX* sizeof(int));
  cudaMalloc((void**)&amp;dev_cost, MAX*MAX);
  cudaMalloc((void**)&amp;dev_G, MAX*MAX);
  </code></pre>
  
  <p>Data transfer:</p>
  
  <pre><code>cudaMemcpy(dev_cost, cost, MAX*MAX, cudaMemcpyHostToDevice);
  cudaMemcpy(dev_G, G, MAX*MAX, cudaMemcpyHostToDevice);
  </code></pre>
  
  <p>Kernel Trigger:</p>
  
  <pre><code>assignCost&lt;&lt;&lt;1,MAX*MAX&gt;&gt;&gt;(dev_G,dev_cost);
  </code></pre>
  
  <p>Kernel Function:</p>
  
  <pre><code>__global__ void assignCost(int *G, int *cost)
  {
      int tid = threadIdx.x + blockDim.x*blockIdx.x;
  
      printf("cost[%d]: %d G[%d] = %d\n", tid, cost[tid], tid, G[tid]);
      if(tid&lt;MAX*MAX)
      {
          if (G[tid] == 0)
              cost[tid] = INT_MAX;
          else
              cost[tid] = G[tid];
      }
  }
  </code></pre>
  
  <p>Is there anything wrong with my approach? If then, how should i modify?</p>