data does not being copied from device to host (cuda)

Viewed 19

It seems there is an issue in my Cuda, that no data is being copy from device to host. consider the following simple code:


#include <stdio.h>

__global__ void kernel(int *a, int N)
{
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    a[i] = i;
}

int main()
{

    int N = 4096;
    int threads = 128;
    int blocks = (N + threads - 1) / threads;
    int *d_a;
    int *h_a = (int *)malloc(N * sizeof(int));

    cudaMallocManaged(&d_a, N * sizeof(int));
    kernel<<<blocks, threads>>>(d_a, N);
    cudaDeviceSynchronize();

    // copy the data back to the host
    cudaMemcpy(h_a, d_a, N * sizeof(int), cudaMemcpyDeviceToHost);

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

    cudaFree(d_a);
    free(h_a);
    
    return 0;
}

which only print array of zeros. I am using

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243
0 Answers
Related