Hi
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
__global__
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
cudaDeviceSynchronize();
// 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
cudaFree(x);
cudaFree(y);
return 0;
}
(the code comes from here : https://devblogs.nvidia.com/parallelforall/even-easier-introduction-cuda/ )
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.