话不多说,直接上代码
#include <tensorflow/c/c_api.h>
#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <map>
// #include <unsupported/Eigen/CXX11/Tensor>
using namespace std;
using namespace tensorflow;
int main()
{
std:: cout << "Hello from TensorFlow C++ library version:" << TF_Version()<<endl;
SessionOptions options;
options.config.set_allow_soft_placement(true);
std::unique_ptr<Session> session(NewSession(options));
std::vector<DeviceAttributes> resp;
TF_CHECK_OK(session->ListDevices(&resp));
bool has_gpu = false;
for (const auto& dev : resp) {
if (dev.device_type() == "GPU") {
has_gpu = true;
}
}
if(has_gpu)
std::cout << "Suport GPU" << std::endl;
}