cuMemGetInfo problem


I’ve written a short code displaying the total and the free memory available:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include "cuda.h"

int main(void){

unsigned int total_memory;

unsigned int free_memory;

CUresult r;

r = cuInit(0);

printf("Init result     : %d\n",r);

r = cuMemGetInfo(&free_memory,&total_memory);

printf("Mem info result : %d\n",r);

printf("Total memory    : %d\n",total_memory);

printf("Free memory     : %d\n",free_memory);


The result is the following.

Init result     : 0

Mem info result : 201

Total memory    : 4333716

Free memory     : 1245120

As you can see, the cuMemGetInfo failed. Can you tell me where is the bug?



When you use the driver API (to which cuMemGetInfo belongs) you have to explicitly establish a cuda context before calling cuMemGetInfo. (-> cuCtxCreate() ).
AFAIK, the matrixMulDrv SDK example has a “initCUDA” routine which you might be able to use/adopt for your code. If cuMemGetInfo is the only driverAPI function you want to use and the rest is runtimeAPI, you can use cudaMalloc() to create a cuda context. Just allocate a single float/int/whatever and you’re all set. Somewhat dirty, but it works.


Ok, I understand. I’ve done something like that and it works.

Is there a simple example anywhere showing how to write a simple CUDA code using the driver API? I mean, just the most important steps allowing to compile and to call a kernel with memory allocation, etc. For instance, the matrix multiplication file using the driver API uses CUT_DEVICE_INIT_DRV, cutFindFilePath(“matrixMul_kernel.cubin”, executablePath), cuModuleLoad(&cuModule, module_path), etc. The cutFindFilePath function is not defined in the programming guide. I understand that it’s a function definied in cutil but if it’s very important, it should belong to the CUDA API (driver or runtime). If I want to write a clean CUDA code using only runtime or driver API, I think it’s very difficult with only the programming guide… Maybe impossible!