tensorflow/c/c_api_test.cc
// Returns the GPU device name if there is one (with arbitrary tie breaking if
// there are more than one), or "" otherwise.
Returns the GPU device name if there is one (with arbitrary tie breaking if
there are more than one), or "" otherwise.
Returns the GPU device name if there is one ( with arbitrary tie breaking if there are more than one ) or " " otherwise .
string GPUDeviceName(TF_Session* session) {
  std::unique_ptr<TF_Status, decltype(&TF_DeleteStatus)> status(
      TF_NewStatus(), TF_DeleteStatus);
  TF_Status* s = status.get();
  std::unique_ptr<TF_DeviceList, decltype(&TF_DeleteDeviceList)> list(
      TF_SessionListDevices(session, s), TF_DeleteDeviceList);
  TF_DeviceList* device_list = list.get();
  CHECK_EQ(TF_OK, TF_GetCode(s)) << TF_Message(s);
  const int num_devices = TF_DeviceListCount(device_list);
  LOG(INFO) << "There are " << num_devices << " devices.";
  for (int i = 0; i < num_devices; ++i) {
    const char* device_name = TF_DeviceListName(device_list, i, s);
    CHECK_EQ(TF_OK, TF_GetCode(s)) << TF_Message(s);
    const char* device_type = TF_DeviceListType(device_list, i, s);
    CHECK_EQ(TF_OK, TF_GetCode(s)) << TF_Message(s);
    LOG(INFO) << "Device " << i << " has name " << device_name << ", type "
              << device_type;
    if (string(device_type) == DEVICE_GPU) {
      return device_name;
    }
  }
  return "";
}
string GPUDeviceName ( TF_Session * session ) { std :: unique_ptr < TF_Status , decltype ( & TF_DeleteStatus ) > status ( TF_NewStatus ( ) , TF_DeleteStatus ) ; TF_Status * s = status . get ( ) ; std :: unique_ptr < TF_DeviceList , decltype ( & TF_DeleteDeviceList ) > list ( TF_SessionListDevices ( session , s ) , TF_DeleteDeviceList ) ; TF_DeviceList * device_list = list . get ( ) ; CHECK_EQ ( TF_OK , TF_GetCode ( s ) ) << TF_Message ( s ) ; const int num_devices = TF_DeviceListCount ( device_list ) ; LOG ( INFO ) << " " << num_devices << " " ; for ( int i = 0 ; i < num_devices ; ++ i ) { const char * device_name = TF_DeviceListName ( device_list , i , s ) ; CHECK_EQ ( TF_OK , TF_GetCode ( s ) ) << TF_Message ( s ) ; const char * device_type = TF_DeviceListType ( device_list , i , s ) ; CHECK_EQ ( TF_OK , TF_GetCode ( s ) ) << TF_Message ( s ) ; LOG ( INFO ) << " " << i << " " << device_name << " " << device_type ; if ( string ( device_type ) == DEVICE_GPU ) { return device_name ; } } return " " ; }
Returns the GPU device name if there is one (with arbitrary tie breaking if
there are more than one), or "" otherwise.
Returns the GPU device name if there is one ( with arbitrary tie breaking if there are more than one ) or " " otherwise .
// No GPU device found.