My first CUDA project crash when executed (but it compiles) why ?


I am a big beginner in CUDA programming and I have some questions.

When I create a new CUDA project in visual studio 2013 by doing :

File > New > Project > Nvidia > Cuda 8.0

I can compile and execute the code inside without any problems.

But when I copy-paste this code :

#include <iostream>
#include <math.h>
// Kernel function to add the elements of two arrays
void add(int n, float *x, float *y)
  for (int i = 0; i < n; i++)
    y[i] = x[i] + y[i];

int main(void)
  int N = 1<<20;
  float *x, *y;

  // Allocate Unified Memory – accessible from CPU or GPU
  cudaMallocManaged(&x, N*sizeof(float));
  cudaMallocManaged(&y, N*sizeof(float));

  // initialize x and y arrays on the host
  for (int i = 0; i < N; i++) {
    x[i] = 1.0f;
    y[i] = 2.0f;

  // Run kernel on 1M elements on the GPU
  add<<<1, 1>>>(N, x, y);

  // Wait for GPU to finish before accessing on host

  // Check for errors (all values should be 3.0f)
  float maxError = 0.0f;
  for (int i = 0; i < N; i++)
    maxError = fmax(maxError, fabs(y[i]-3.0f));
  std::cout << "Max error: " << maxError << std::endl;

  // Free memory
  return 0;

(the code comes from here : )

The program compiles but crash on execution.

But I know this code perfectly works as before I just replaced one of the CUDA examples given when we install it by this code, and it compiled and ran without a problem.

But when I want to do things “properly” by creating a new project, this project crash on execution.

Could someone help me to understand why it crash and how to fix it ?

I am really a big beginner as well in CUDA as in the usage of visual studio (I know C++ but never really used either CUDA or visual studio to code). So the mistake is probably very stupid.

[edit] : the simple fact to create variable on the GPU makes the program crash here. I removed all the lines excepted the ones to create x and y on the GPU and it crashes.

Thank you a lot.

Unfortunately that teaching example was simplified as much as possible to make the concepts clear. This involved removing normal error checking. If you look at the bottom of that posting, you will find people who are reporting trouble.

Add proper cuda error checking to that code (any any other CUDA code you are having trouble with.) Not sure what that is? Google “proper cuda error checking” and take the first hit, and read it and apply it to your code…

My guess is that your platform or your compile command does not support managed memory. If so, the cudaMallocManaged operations would be returning an error (which that code ignores).

Thanks a lot.

In fact my program could run and compile without problem using the default examples of CUDA (and copy/pasting the code I gave above in thoose example).

But it didn’t worked when I created new projects.

I have followed your instructions and indeed the cudaMallocManaged made the program crash (I used your proper cuda error checking).

The mistake was that I have to run the debug in x64 and not in win32, and now everything works well.

Thank you !