Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

c++ - Can't retrieve cuda memory from device

I have isolated a problem, namely that I can't seem to be able to retrieve memory from my CUDA device. I have created a simple script to demonstrate my problem:

#include <iostream>

#define CUDA_WARN(XXX) 
    do { if (XXX != cudaSuccess) std::cerr << "CUDA Error: " << 
    cudaGetErrorString(XXX) << ", at line " << __LINE__ 
    << std::endl; cudaDeviceSynchronize(); } while (0)

struct Car {
    int id;
    float max_vel;
    float max_acc;
    float max_steer;
    bool alive;
};

int main() {
    Car* dCar;
    Car* hCar = new Car;
    Car* hCar2;

    hCar->id = 1;

    CUDA_WARN(cudaMalloc((void**) &dCar, sizeof(Car)));
    CUDA_WARN(cudaMemcpy( dCar,  hCar, sizeof(Car), cudaMemcpyHostToDevice));
    CUDA_WARN(cudaMemcpy( hCar2, dCar, sizeof(Car), cudaMemcpyDeviceToHost));
    std::cout << "hCar2 id:" << hCar2->id << std::endl;
}

I would fully expect hCar2 now to have been set to point to a value as it came from the device, but for some reason this does not work.

question from:https://stackoverflow.com/questions/65907048/cant-retrieve-cuda-memory-from-device

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...