Jan 12
14
CUDA Get Amount of Free Global Device Memory
Leave a comment »
There are three steps:
1. Add includes/ .h files
2. create a cuda context
3. query the memory on the device
Below is an example of the code required to get the amount of free and total global memory on a cuda device:
#include <iostream> // to output to the console
#include <cuda.h> // to get memory on the device
#include <cuda_runtime.h> // to get device count
int main( int argc, char** argv)
{
//get device count
int deviceCount = 0;
cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
if(deviceCount == 0)
{
cout << "Error no cuda devices";
return 1;
}
//get the first cuda device
CUdevice cudaDevice;
CUresult result = cuDeviceGet(&cudaDevice, 0);
if(result!= CUDA_SUCCESS)
{
cout << "Error fetching cuda device";
return 1;
}
//create cuda context
CUcontext cudaContext;
result = cuCtxCreate(&cudaContext, CU_CTX_SCHED_AUTO, cudaDevice);
if(result != CUDA_SUCCESS)
{
cout << "Error creating cuda context";
return 1;
}
//get the amount of free memory on the graphics card
size_t free;
size_t total;
result = cuMemGetInfo(&free, &total);
cout << "free memory: " << free / 1024 / 1024 << "mb, total memory: " << total / 1024 / 1024 << "mb" << endl;
char c;
cin >> c;
return 0;
}
Gotchas
if you get compile error “unresolved external symbol _cuMemGetInfo”
you need to update the project properties to link the cuda.lib file. To do this:
1. right click on project->properties->configuration->Linker.
2. in the additional dependencies field add cuda.lib
See screenshot below:

References
Nvidia toolkit doc on cuMemGetInfo