CUDA编程:获取GPU总显存和可用显存

代码很简单:
就是使用函数: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();
  }

输出如下:8G显存
CUDA编程:获取GPU总显存和可用显存_第1张图片

你可能感兴趣的:(Python)