代码很简单:
就是使用函数:cudaMemGetInfo( &avail, &total );
#include
#include
#include
#include
#include
void checkGpuMem() {
size_t avail;
size_t total;
cudaMemGetInfo( &avail, &total );
size_t used = total - avail;
std::cout << "================="<< std::endl;
std::cout << "Device memory used: " << used << std::endl;
std::cout << "Total memory used: " << total << std::endl;
std::cout << "================="<< std::endl;
}
int main() {
int deviceCount = 0;
cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
// This function call returns 0 if there are no CUDA capable devices.
if (deviceCount == 0) {
printf("There are no available device(s) that support CUDA\n");
} else {
printf("Detected %d CUDA Capable device(s)\n", deviceCount);
}
checkGpuMem();
getchar();
}