title: Why is my CUDA kernel returning old values?
body: <p>Kind of almost at the point of ripping my hair out over this issue.</p>
  
  <p>I have a CUDA kernel that does some math on data stored in a 3D array. While testing this, I used to assign some values (non-zero) to the array and observe results. I <strong>commented out</strong> those lines since, but the result is still the same. It is as if it is completely  ignoring the fact that I'm doing a memset to 0.</p>
  
  <p>The code works correctly when I step through it in Debug... But not in Release! <strong>My guess is I have a memory leak from this matrix.</strong></p>
  
  <p>I allocate this array as:</p>
  
  <pre><code>cudaExtent m_extent = make_cudaExtent(sizeof(float)*matdim.x, matdim.y, matdim.z); // width, height, depth
  cudaPitchedPtr m_device;
  cudaMalloc3D(&amp;m_device, m_extent);
  cudaMemset3D(m_device, 0, m_extent);
  </code></pre>
  
  <p>I call the kernel in a loop like this:</p>
  
  <pre><code>for (int iter = 0; iter &lt; gpu_iterations; iter++)
      {
          PF_iteration_kernel&lt;&lt;&lt;grids,threads&gt;&gt;&gt;(m_device, m_extent, matdim);
          cudaDeviceSynchronize(); 
      }
  </code></pre>
  
  <p>After which I release the <code>m_device</code> pitched pointer:</p>
  
  <pre><code>cudaFree(m_device.ptr);
  </code></pre>
  
  <p><code>matdim</code> is just matrix dimensions held by a dim3.</p>
  
  <p>Within the kernel I do the following (well, I commented everything functional out...):</p>
  
  <pre><code>__global__ void PF_iteration_kernel(cudaPitchedPtr mPtr, cudaExtent mExt, dim3 matrix_dimensions)
  {
  int x = threadIdx.x + blockIdx.x * blockDim.x;
  int y = threadIdx.y + blockIdx.y * blockDim.y;
  
  // Find location within the pitched memory
  char *m = (char*)mPtr.ptr;
  
  int sof = sizeof(float);
  size_t pitch = mPtr.pitch;
  size_t slice_pitch = pitch*mExt.height;
  char* m_addroff = m + y * pitch + x * sof;
  printf("m(%d,%d) is %f \n", x, y, *m_addroff); // display the slice
  
  *m_addroff = 0; // WILL THIS RESET IT?!
  
  __syncthreads();
  }
  </code></pre>
  
  <p>That should be just showing 0s, but it displays my old values (25, 26, 27, 28, etc).</p>
  
  <p>I have cleaned and re-cleaned and re-built everything several times. I have relaunched the IDE.</p>
  
  <p>My IDE is Visual Studio 2010 With NSight 4.6 (CUDA 7.0).
  I am on Windows 7 x64</p>