aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/client/device_lib.i
blob: 944e855cee2ab9da7a4a801d1b993bec4d8ebc55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

%include "tensorflow/python/platform/base.i"

%typemap(in) const tensorflow::ConfigProto& (tensorflow::ConfigProto temp) {
  char* c_string;
  Py_ssize_t py_size;
  if (PyBytes_AsStringAndSize($input, &c_string, &py_size) == -1) {
    // Python has raised an error (likely TypeError or UnicodeEncodeError).
    SWIG_fail;
  }

  if (!temp.ParseFromString(string(c_string, py_size))) {
    PyErr_SetString(
        PyExc_TypeError,
        "The ConfigProto could not be parsed as a valid protocol buffer");
    SWIG_fail;
  }
  $1 = &temp;
}

%{
#include "tensorflow/core/common_runtime/device.h"
#include "tensorflow/core/common_runtime/device_factory.h"
#include "tensorflow/core/framework/device_attributes.pb.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/protobuf/config.pb.h"
#include "tensorflow/core/public/session_options.h"

namespace tensorflow {
namespace swig {

static std::vector<string> ListDevicesWithSessionConfig(
    const tensorflow::ConfigProto& config, TF_Status* out_status) {
  std::vector<string> output;
  SessionOptions options;
  options.config = config;
  std::vector<Device*> devices;
  Status status = DeviceFactory::AddDevices(
      options, "" /* name_prefix */, &devices);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }

  std::vector<std::unique_ptr<Device>> device_holder(devices.begin(),
                                                     devices.end());

  for (const Device* device : devices) {
    const DeviceAttributes& attr = device->attributes();
    string attr_serialized;
    if (!attr.SerializeToString(&attr_serialized)) {
      Set_TF_Status_from_Status(
          out_status,
          errors::Internal("Could not serialize device string"));
      output.clear();
      return output;
    }
    output.push_back(attr_serialized);
  }

  return output;
}

std::vector<string> ListDevices(TF_Status* out_status) {
  tensorflow::ConfigProto session_config;
  return ListDevicesWithSessionConfig(session_config, out_status);
}

}  // namespace swig
}  // namespace tensorflow

%}

%ignoreall

%unignore tensorflow;
%unignore tensorflow::swig;
%unignore tensorflow::swig::ListDevicesWithSessionConfig;
%unignore tensorflow::swig::ListDevices;

// Wrap this function
namespace tensorflow {
namespace swig {
std::vector<string> ListDevices(TF_Status* out_status);
static std::vector<string> ListDevicesWithSessionConfig(
    const tensorflow::ConfigProto& config, TF_Status* out_status);
}  // namespace swig
}  // namespace tensorflow

%insert("python") %{
def list_devices(session_config=None):
  from tensorflow.python.framework import errors

  with errors.raise_exception_on_not_ok_status() as status:
    if session_config:
      return ListDevicesWithSessionConfig(session_config.SerializeToString(),
                                          status)
    else:
      return ListDevices(status)
%}

%unignoreall

%newobject tensorflow::SessionOptions;