Eldar Azulay
Eldar Azulay

Reputation: 85

I get access violation writing location error when trying to allocate memory using CUDA

I just started learning how to use CUDA api and I started with this introduction guide: https://devblogs.nvidia.com/parallelforall/even-easier-introduction-cuda/

I am following the tutorial but after I am trying to allocate memory with cudaMallocManaged() I want to initialize the array, but when trying to initialize the array, an exception is thrown, which says:

Exception thrown at 0x003983D7 in VectorAdd.exe: 0xC0000005: Access violation writing location 0x00000000.

The program can identify my GPU, I know that because I made a function that writes all of my CUDA devices and my GPU is there.

this is my code, just as shown on the tutorial:

int main(void)
{
PrintCudaDevices();

int N = 1 << 20; //1M elements

float *x, *y;

cudaMallocManaged(&x, N * sizeof(float));
cudaMallocManaged(&y, N * sizeof(float));

for (int i = 0; i < N; i++)
{
    x[i] = 1.0f;
    y[i] = 2.0f;
}
}

I googled that problem but I didn't find any working solution.

Upvotes: 0

Views: 802

Answers (1)

Eldar Azulay
Eldar Azulay

Reputation: 85

The problem was that I was compiling it with x86, but when I compiled it on x64 it worked great. It didn't work with x86 because unified memory requires x64 bit OS. In addition, don't forget to call cudaDeviceSynchronize() after any kernel if you want to use its results once it's done.

Upvotes: 1

Related Questions