0

I have exactly the same problem that was asked 8 and 9 years ago (!) in error when copying dynamically allocated data in device to host? and CUDA - Copy device data to host?

There was mention this will be fixed soon. Is this still not done or am I doing something else wrong here? Running Nvidia Cuda 11.1

so my code is

#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>

#define N 400
__device__ int* d_array;

__global__ void allocDeviceMemory() {
    d_array = new int[N];
    for (int i = 0; i < N; i++) d_array[i] = 123;
}

int main() {
    allocDeviceMemory <<<1, 1>>> ();
    cudaDeviceSynchronize();

    int* d_a = NULL;
    cudaMemcpyFromSymbol((void**) &d_a, d_array, sizeof(d_a), 0, cudaMemcpyDeviceToHost);
    printf("gpu adress: %p\n", d_a);

    int* h_array = (int*) malloc(N * sizeof(int));
    cudaError_t errr = cudaMemcpy(h_array, d_a, N * sizeof(int), cudaMemcpyDeviceToHost);
    printf("h_array: %d, %d\n", h_array[0], errr);
    printf("%s\n", cudaGetErrorString(errr)); //invalid argument
    return errr;
}
ray_ray_ray
  • 316
  • 1
  • 14
  • 1
    There has been no change to the behavior, the behavior is expected, and the fact that that dynamically allocated device data cannot participate in transactions involving the host API is [documented](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#interoperability-host-memory-api). – Robert Crovella Feb 06 '21 at 02:26
  • https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#interoperability-host-memory-api – talonmies Feb 06 '21 at 02:30

0 Answers0