title: cudaMemcpyDeviceToHost error in basic example
body: <p>I have recently started learning CUDA and I've integrated my CUDA into MS Visual Studio 2010 with Nsight. I have also acquired the book "CUDA by Example" and I'm going through all the examples and compiling them. I have come across an error however, which I do not understand. 
  The program comes from chapter 4 and it's the julia_gpu example. Original code:</p>
  
  <pre><code>#include "../common/book.h"
  #include "../common/cpu_bitmap.h"
  
  #define DIM 1000
  
  struct cuComplex {
      float   r;
      float   i;
      cuComplex( float a, float b ) : r(a), i(b)  {}
      __device__ float magnitude2( void ) {
          return r * r + i * i;
      }
      __device__ cuComplex operator*(const cuComplex&amp; a) {
          return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);
      }
      __device__ cuComplex operator+(const cuComplex&amp; a) {
          return cuComplex(r+a.r, i+a.i);
      }
  };
  
  __device__ int julia( int x, int y ) {
      const float scale = 1.5;
      float jx = scale * (float)(DIM/2 - x)/(DIM/2);
      float jy = scale * (float)(DIM/2 - y)/(DIM/2);
  
      cuComplex c(-0.8, 0.156);
      cuComplex a(jx, jy);
  
      int i = 0;
      for (i=0; i&lt;200; i++) {
          a = a * a + c;
          if (a.magnitude2() &gt; 1000)
              return 0;
      }
  
      return 1;
  }
  
  __global__ void kernel( unsigned char *ptr ) {
      // map from blockIdx to pixel position
      int x = blockIdx.x;
      int y = blockIdx.y;
      int offset = x + y * gridDim.x;
  
      // now calculate the value at that position
      int juliaValue = julia( x, y );
      ptr[offset*4 + 0] = 255 * juliaValue;
      ptr[offset*4 + 1] = 0;
      ptr[offset*4 + 2] = 0;
      ptr[offset*4 + 3] = 255;
  }
  
  // globals needed by the update routine
  struct DataBlock {
      unsigned char   *dev_bitmap;
  };
  
  int main( void ) {
      DataBlock   data;
      CPUBitmap bitmap( DIM, DIM, &amp;data );
      unsigned char    *dev_bitmap;
  
      HANDLE_ERROR( cudaMalloc( (void**)&amp;dev_bitmap, bitmap.image_size() ) );
      data.dev_bitmap = dev_bitmap;
  
      dim3    grid(DIM,DIM);
      kernel&lt;&lt;&lt;grid,1&gt;&gt;&gt;( dev_bitmap );
  
      HANDLE_ERROR( cudaMemcpy( bitmap.get_ptr(), dev_bitmap,
                                bitmap.image_size(),
                                cudaMemcpyDeviceToHost ) );
  
      HANDLE_ERROR( cudaFree( dev_bitmap ) );
  
      bitmap.display_and_exit();
  }
  </code></pre>
  
  <p>My Visual Studio however forces me to embelish the cuComplex constructor to <strong>device</strong>, otherwise it won't compile (it tells me I cannot use it later in the julia function), which I guess is fair enough. So I have:</p>
  
  <pre><code>__device__ cuComplex( float a, float b ) : r(a), i(b)  {}
  </code></pre>
  
  <p>But when I do run the example (having added the necessary includes for it to run through VS, which is cuda_runtime.h and device_launch_parameters.h, as well as copying the glut32.dll into the same folder as the exe) it quickly fails, killing my device driver and saying it's due to an unknown error in line 94, which is the cudaMemcpy call in main. To be exact, it's the actual line containing the call "cudaDeviceToHost". To be frank however, I have tried creating some breakpoints line after line and the driver dies at the kernel call. </p>
  
  <p>Could someone please tell me what might be wrong? I am a noob with CUDA and have no real idea why a trivial example would kill itself like that. What could I be doing wrong? Because frankly, I don't really even know what to investigate.
  I have the CUDA 4.1 toolkit, NSight 2.1 and a GeForce GT445M with computational ability rated at 2.1 and the 295 version of the drivers.</p>