problem with array initialization in GPU cuda code

1

I am new to gpu coding. I try to run a simple code on youtube, but it can not run. When I try to run it, it stopped at line when I defining the value of array a a[i] = i; with message:

Unhandled exception at 0x00007FF63DF51284 in first_sample.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

#include <stdio.h>
#define SIZE    1024

__global__ void VectorAdd(int *a, int *b, int *c, int n)
{
    int i = threadIdx.x;

    if (i < n)
        c[i] = a[i] + b[i];
}

int main()
{
    int *a, *b, *c;

    cudaMallocManaged(&a, 1024 * sizeof(int));
    cudaMallocManaged(&b, SIZE * sizeof(int));
    cudaMallocManaged(&c, SIZE * sizeof(int));

    //for (int i = 0; i < SIZE; ++i)
    for (int i = 0; i < SIZE; ++i)
    {
        a[i] = i;
        b[i] = i;
        c[i] = 0;
    }

    VectorAdd <<< 1, SIZE>>> (a, b, c, SIZE);
    cudaDeviceSynchronize();

    for (int i = 0; i < 10; ++i)
        printf("c[%d] = %d\n", i, c[i]);

    cudaFree(a);
    cudaFree(b);
    cudaFree(c);

    return 0;
}
cuda
gpu
asked on Stack Overflow Dec 26, 2018 by Lun Li • edited Dec 26, 2018 by pradeep

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0