cuda获取gpu当前显存

头文件:cuda_runtime_api.h

static void gpu_helper(std::string info, bool print_info=false)
{
    
    

	size_t free_byte;
	size_t total_byte;

	cudaError_t cuda_status = cudaMemGetInfo(&free_byte, &total_byte);

	if (cudaSuccess != cuda_status) {
    
    
		printf("Error: cudaMemGetInfo fails, %s \n", cudaGetErrorString(cuda_status));
		exit(1);
	}

	double free_db = (double)free_byte;
	double total_db = (double)total_byte;
	double used_db_1 = (total_db - free_db) / 1024.0 / 1024.0;

	if (print_info)
		std::cout << info << "   used GPU memory " << used_db_1 << "  MB\n";
}

猜你喜欢

转载自blog.csdn.net/qq_29007291/article/details/115404769
今日推荐