我有一个C++程序,使用Tensorflow 2来运行卷积神经网络的推断。程序在具有专用GPU的服务器上运行,预期行为是在GPU上运行的推断。如果GPU出现故障,Tensorflow将开始使用CPU而不是GPU。Tensorflow C++API有没有办法检查Tensorflow是否正在使用CPU?在GPU出现故障的情况下,C++API是否有任何方法可以避免Tensorflow切换到CPU?
最后我用以下方式解决了它:
bool DedicatedGPUAvailable(tensorflow::Session* session){
if (session != nullptr) {
std::vector<tensorflow::DeviceAttributes> response;
session->ListDevices(&response);
// If a single device is found, we assume that it's the CPU.
// You can check that name if you want to make sure that this is the case
if (response.size() == 1) {
std::cout << "No GPU found! Total available devices = 1" << std::endl;
std::cout << "Available devices: " << std::endl;
for (unsigned int i = 0; i < response.size(); ++i) {
std::cout << i << " " << response[i].name() << std::endl;
}
return false;
}
}
return true;
}