How to correctly retrieve data from a __device__ float* back to the CPU?

I try to run this code,error occured:
CUDA error in function ‘saveThreeChannelGpuMemoryAsImage’ at /home/jetson/Desktop/depth/DataParameter.cu:232 (call: cudaMemcpyFromSymbol(&d_data_ptr, d_data, sizeof(float*))):
Error code: 13
Error message: invalid device symbol

__device__ float* d_inputImg;
__device__ float* d_inputImgRec;

void DataParameter::UpdateImgToGPU() {

    cv::Mat inputImgFloat, inputImgRecFloat;
    m_inputImg.convertTo(inputImgFloat, CV_32FC3, 1.0f / 255.0f);
    m_inputImgRec.convertTo(inputImgRecFloat, CV_32FC3, 1.0f / 255.0f);


    size_t inputImgSize = inputImgFloat.total() * inputImgFloat.elemSize();
    size_t inputImgRecSize = inputImgRecFloat.total() * inputImgRecFloat.elemSize();

    float* d_inputImgPtr;
    float* d_inputImgRecPtr;


    CUDA_CHECK(cudaMalloc((void**)&d_inputImgPtr, inputImgSize));
    CUDA_CHECK(cudaMalloc((void**)&d_inputImgRecPtr, inputImgRecSize));

  
    CUDA_CHECK(cudaMemcpy(d_inputImgPtr, inputImgFloat.ptr<float>(0), inputImgSize, cudaMemcpyHostToDevice));
    CUDA_CHECK(cudaMemcpy(d_inputImgRecPtr, inputImgRecFloat.ptr<float>(0), inputImgRecSize, cudaMemcpyHostToDevice));


    CUDA_CHECK(cudaMemcpyToSymbol(d_inputImg, &d_inputImgPtr, sizeof(float*)));
    CUDA_CHECK(cudaMemcpyToSymbol(d_inputImgRec, &d_inputImgRecPtr, sizeof(float*)));

	saveThreeChannelGpuMemoryAsImage(d_inputImg, m_rawImageParameter.m_srcImgWidth, m_rawImageParameter.m_srcImgHeight, "inputImg.bmp");
}

void saveThreeChannelGpuMemoryAsImage(float* d_data, int width, int height,const std::string& filename) {
	float* d_data_ptr;

    CUDA_CHECK(cudaMemcpyFromSymbol(&d_data_ptr, d_data, sizeof(float*)));

	float* h_data = new float[width * height * 3];
    CUDA_CHECK(cudaMemcpy(h_data, d_data_ptr, width * height * 3 * sizeof(float), cudaMemcpyDeviceToHost));
    cv::Mat image(height, width, CV_32FC3, h_data);
    cv::Mat scaledImage;
    image.convertTo(scaledImage, CV_8UC3, 255.0);
    cv::imwrite(filename, scaledImage);
    delete[] h_data;
}

You should not do that. d_inputImg is a global symbol and is not intended to be used as an argument to a host function.

d_data is not a device symbol. You cannot pass device symbols in host code arguments like that. That is the proximal reason for the error.

So, one possible approach:

  1. get rid of the first parameter in the function prototype:

    void saveThreeChannelGpuMemoryAsImage(int width, int height,const std::string& filename)
    
  2. In the function body, use the device symbol directly:

    CUDA_CHECK(cudaMemcpyFromSymbol(&d_data_ptr, d_inputImg, sizeof(float*)));
    
  3. at the call point, remove the first argument:

    saveThreeChannelGpuMemoryAsImage(m_rawImageParameter.m_srcImgWidth, m_rawImageParame...
    

OK,I will try it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.