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?
>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.