Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the proper way to allocate GPU memory to a member variable of a class?

Basically I hope to allocate GPU memory to class Image‘s member variable frame with cudaMallocManaged:

class Image {
public:
    Color* frame;
};

But with the following code I got CUDA error = 1 when allocating for pointer frame:

#include <iostream>

// limited version of checkCudaErrors from helper_cuda.h in CUDA examples
#define checkCudaErrors(val) check_cuda((val), #val, __FILE__, __LINE__)

void check_cuda(cudaError_t result, char const* const func, const char* const file,
                int const line) {
    if (result) {
        std::cerr << "CUDA error = " << static_cast<unsigned int>(result) << " at " << file << ":"
                << line << " '" << func << "' \n";
        // Make sure we call CUDA Device Reset before exiting
        cudaDeviceReset();
        exit(-1);
    }
}

class Color {
public:
    double r, g, b;

    __host__ __device__ Color() : r(0.0), g(0.0), b(0.0) {
    }
};


class Image {
public:
    Color* frame;
};

int main() {
    int width = 1960;
    int height = 1080;

    Image *image;
    checkCudaErrors(cudaMallocManaged((void **)&image, sizeof(Image)));
    checkCudaErrors(cudaMallocManaged((void **)&(image->frame), sizeof(Color) * width, height));

    return 0;
}

What’s the proper way to allocate GPU memory to this member variable?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

cudaMallocManaged is declared like this:

__host__ ​cudaError_t cudaMallocManaged(void** devPtr, size_t size,
                                       unsigned int flags = cudaMemAttachGlobal)

So maybe you can use this:

checkCudaErrors(cudaMallocManaged((void **)&(image->frame),
                                  sizeof(Color) * width * height));

Note: The third argument, flags, is here left to use the default value cudaMemAttachGlobal and the size is calculated by multiplying sizeof(Color), width and height.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading