title: Crash with CUDA/OGL interop
body: <p>I am trying to setup a little CUDA/GL interop example. I have looked around in the internet, so I found some tutorials with some helpful stuff.</p>
  
  <p>All I want is, is to produce a texture in CUDA and draw it with OpenGL.</p>
  
  <p>The source I have now is crashing my Macbook Pro every time I run it, so I thought that if somebody could take an eye on it, that would be really helpful.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>    #include &lt;stdlib.h&gt;
      #include &lt;stdio.h&gt;
      #include &lt;string.h&gt;
      #include &lt;math.h&gt;
  
      #ifdef _WIN32
      #  define WINDOWS_LEAN_AND_MEAN
      #  define NOMINMAX
      #  include &lt;windows.h&gt;
      #endif
  
      // OpenGL Graphics includes
      #include &lt;GL/glew.h&gt;
      #if defined (__APPLE__) || defined(MACOSX)
      #include &lt;GLUT/glut.h&gt;
      #else
      #include &lt;GL/freeglut.h&gt;
      #endif
  
      // includes, cuda
      #include &lt;cuda_runtime.h&gt;
      #include &lt;cuda_gl_interop.h&gt;
  
      // Utilities and timing functions
      #include &lt;helper_functions.h&gt;    // includes cuda.h and cuda_runtime_api.h
      #include &lt;timer.h&gt;               // timing functions
  
      // CUDA helper functions
      #include &lt;helper_cuda.h&gt;         // helper functions for CUDA error check
      #include &lt;helper_cuda_gl.h&gt;      // helper functions for CUDA/GL interop
  
      #include &lt;vector_types.h&gt;
       const unsigned int window_width  = 512;
       const unsigned int window_height = 512;
      GLuint viewGLTexture;
      cudaGraphicsResource_t viewCudaResource;
  
      void initGLandCUDA() {
           int argc = 0;
           char** argv = NULL;
           glutInit(&amp;argc, argv);
           glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
           glutInitWindowSize(window_width, window_height);
           glutCreateWindow("CUDA GL Interop");
  
           glewInit();
  
           glEnable(GL_TEXTURE_2D);
           glGenTextures(1, &amp;viewGLTexture);
           glBindTexture(GL_TEXTURE_2D, viewGLTexture);
           {
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
           }
           glBindTexture(GL_TEXTURE_2D, 0);
  
           cudaGLSetGLDevice(gpuGetMaxGflopsDeviceId());
           cudaGraphicsGLRegisterImage(&amp;viewCudaResource, viewGLTexture, GL_TEXTURE_2D,     cudaGraphicsRegisterFlagsWriteDiscard);
      }    
  
  
      __global__ void renderingKernel(cudaSurfaceObject_t image) {
           unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
           unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;
  
           uchar4 color = make_uchar4(0.f, 0.f, 0.f, .3f);
           //if I write in 0, 0 and not x,y, the computer is not crashing, but there is no black pixel at 0,0
           surf2Dwrite(color, image, x, y, cudaBoundaryModeClamp);
      }
  
  
      void callCUDAKernel(cudaSurfaceObject_t image) {
           dim3 block(8, 1, 1);
           dim3 grid(8, 1, 1);
           renderingKernel&lt;&lt;&lt; grid, block&gt;&gt;&gt;(image);
      }
  
       void renderFrame() {
          cudaGraphicsMapResources(1, &amp;viewCudaResource);
          {
              cudaArray_t viewCudaArray;
              checkCudaErrors(cudaGraphicsSubResourceGetMappedArray(&amp;viewCudaArray, viewCudaResource, 0, 0));
              cudaResourceDesc viewCudaArrayResourceDesc;
              {
                  viewCudaArrayResourceDesc.resType = cudaResourceTypeArray;
                  viewCudaArrayResourceDesc.res.array.array = viewCudaArray;
              }
              cudaSurfaceObject_t viewCudaSurfaceObject;
              checkCudaErrors(cudaCreateSurfaceObject(&amp;viewCudaSurfaceObject, &amp;viewCudaArrayResourceDesc));
  
              callCUDAKernel(viewCudaSurfaceObject);
  
              checkCudaErrors(cudaDestroySurfaceObject(viewCudaSurfaceObject));
          }
          checkCudaErrors(cudaGraphicsUnmapResources(1, &amp;viewCudaResource));
          checkCudaErrors(cudaStreamSynchronize(0));
  
          glBindTexture(GL_TEXTURE_2D, viewGLTexture);
          {
              glBegin(GL_QUADS);
              {
                   glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
                   glTexCoord2f(1.0f, 0.0f); glVertex2f(+1.0f, -1.0f);
                   glTexCoord2f(1.0f, 1.0f); glVertex2f(+1.0f, +1.0f);
                   glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, +1.0f);
              }
              glEnd();
           }
           glBindTexture(GL_TEXTURE_2D, 0);
           glFinish();
      }
  
      int main(int argc, char **argv)
      {
           initGLandCUDA();
  
          glutDisplayFunc(renderFrame);
          //glutKeyboardFunc(keyboard);
          //glutMouseFunc(mouse);
          glutMainLoop();
      }
  </code></pre>
  
  <p>It seems like some kind of out-of-range error, but I am currently out of ideas (btw, this is cc 3.0, running to nVidia 650M).</p>
  
  <p>Edit :
  By crashing I mean : crashing. Computer freezes. I can't move my mouse and I have to reboot.</p>
  
  <p>Yes, I have looked in all examples, they are not exactly what I want. Changing them to be want I want results in this problem. If there was any other help in the manual or anywhere else that would help me and I have found I would not bother asking for help. You need to link with cuda_runtime and glut libs</p>