aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/stream_executor/machine_manager.cc
blob: 6d7bc50379d3fe1b0e15f934e25525b633b45ec4 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "tensorflow/stream_executor/machine_manager.h"

#include "tensorflow/stream_executor/platform/port.h"

#include "tensorflow/stream_executor/dso_loader.h"
#include "tensorflow/stream_executor/lib/error.h"
#include "tensorflow/stream_executor/platform/logging.h"
#include "tensorflow/stream_executor/platform/mutex.h"
#include "tensorflow/stream_executor/platform/port.h"

namespace perftools {
namespace gputools {

mutex MachineManager::mu_{LINKER_INITIALIZED};

MachineManager *MachineManager::singleton_ = nullptr;

PlatformKind MachineManager::DetectPreferredPlatform() {
// TODO(leary) for KNC card experiments, figure out a legitimate way to
// determine this. For now, we use a compile-time hint so we can compile tests
// for both.
#if defined TENSORFLOW_STREAM_EXECUTOR_MACHINE_MANAGER_PREFER_OPENCL
  return PlatformKind::kOpenCL;
#elif defined TENSORFLOW_STREAM_EXECUTOR_MACHINE_MANAGER_PREFER_HOST
  return PlatformKind::kHost;
#else
  return PlatformKind::kCuda;
#endif
}

/* static */ port::StatusOr<std::unique_ptr<MachineManager>>
MachineManager::Create(PlatformKind kind, DeviceOptions options,
                       const PluginConfig &config) {
  std::unique_ptr<MachineManager> machine_manager{
      new MachineManager{kind, options, config}};
  auto init_status = machine_manager->Init();
  if (!init_status.ok()) {
    return init_status;
  }

  return std::move(machine_manager);
}

MachineManager::MachineManager(PlatformKind platform,
                               DeviceOptions device_options,
                               const PluginConfig &config)
    : platform_(platform),
      device_options_(device_options),
      plugin_config_(config),
      min_numa_node_(0),
      limit_numa_node_(0) {}

port::Status MachineManager::Init() {
  // Initialize the first StreamExecutor, then use that platform interface to
  // grab the device count.
  executors_.resize(1);
  executors_[0].reset(new StreamExecutor{platform_, plugin_config_});
  auto status = executors_[0]->Init(0 /* = device_ordinal */, device_options_);
  if (!status.ok()) {
    return port::Status{
        port::error::FAILED_PRECONDITION,
        port::StrCat(
            "failed to initialize StreamExecutor for device ordinal 0: ",
            status.ToString())};
  }
  int device_count = executors_[0]->PlatformDeviceCount();
  if (device_count == 0) {
    LOG(WARNING) << "no devices found for platform "
                 << PlatformKindString(platform_);
    min_numa_node_ = limit_numa_node_ = 0;
    return port::Status::OK();
  }

  streams_.resize(device_count);
  streams_[0].reset(new Stream(executors_[0].get()));
  if (!streams_[0]->Init().ok()) {
    return port::Status{
        port::error::FAILED_PRECONDITION,
        "failed to initialize default stream for device ordinal 0"};
  }

  min_numa_node_ = executors_[0]->GetDeviceDescription().numa_node();
  limit_numa_node_ = min_numa_node_ + 1;

  executors_.resize(device_count);
  for (int device_ordinal = 1; device_ordinal < device_count;
       ++device_ordinal) {
    StreamExecutor *stream_exec = new StreamExecutor{platform_, plugin_config_};
    executors_[device_ordinal].reset(stream_exec);
    auto status = stream_exec->Init(device_ordinal, device_options_);
    if (!status.ok()) {
      return port::Status(
          port::error::FAILED_PRECONDITION,
          port::StrCat(
              "failed to initialize StreamExecutor for device ordinal ",
              device_ordinal, ": ", status.ToString()));
    }

    min_numa_node_ = std::min(min_numa_node_,
                              stream_exec->GetDeviceDescription().numa_node());
    limit_numa_node_ = std::max(
        limit_numa_node_, stream_exec->GetDeviceDescription().numa_node() + 1);

    if (!stream_exec->GetDeviceDescription().ecc_enabled()) {
      LOG(WARNING) << "ECC not enabled for device ordinal: " << device_ordinal;
    }

    streams_[device_ordinal].reset(
        new Stream(executors_[device_ordinal].get()));
    if (!streams_[device_ordinal]->Init().ok()) {
      return port::Status(
          port::error::FAILED_PRECONDITION,
          port::StrCat(
              "failed to initialize default stream for device ordinal ",
              device_ordinal));
    }
  }

  return port::Status::OK();
}

int MachineManager::device_count() const { return executors_.size(); }

port::Status MachineManager::EnablePeerAccess() {
  auto peer_access_map = GetPeerAccessMap();
  for (const auto &access : *peer_access_map) {
    auto devices = access.first;
    if (access.second) {
      StreamExecutor *from = executors_[devices.first].get();
      StreamExecutor *to = executors_[devices.second].get();
      auto status = from->EnablePeerAccessTo(to);
      if (!status.ok()) {
        return status;
      }
    } else {
      LOG(INFO) << "cannot enable peer access from device ordinal "
                << devices.first << " to device ordinal " << devices.second;
    }
  }
  return port::Status::OK();
}

std::unique_ptr<std::map<std::pair<int, int>, bool>>
MachineManager::GetPeerAccessMap() {
  auto *map = new std::map<std::pair<int, int>, bool>;
  for (int i = 0; i < device_count(); ++i) {
    for (int j = 0; j < device_count(); ++j) {
      StreamExecutor *from = executors_[i].get();
      StreamExecutor *to = executors_[j].get();
      (*map)[{i, j}] = from->CanEnablePeerAccessTo(to);
    }
  }

  return std::unique_ptr<std::map<std::pair<int, int>, bool>>{map};
}

StreamExecutor *MachineManager::executor_for_device(int device_ordinal) const {
  CHECK_GE(device_ordinal, 0) << "device ordinal must be non-negative";
  CHECK(0 <= device_ordinal && device_ordinal < device_count())
      << "device " << device_ordinal << " out of range with device count "
      << device_count();
  StreamExecutor *executor = executors_[device_ordinal].get();
  CHECK(executor != nullptr);
  return executor;
}

int MachineManager::ExecutorToBus(const StreamExecutor *stream_exec) const {
  return stream_exec->GetDeviceDescription().numa_node() - min_numa_node_;
}

int MachineManager::DeviceToBus(int device_ordinal) const {
  return ExecutorToBus(executor_for_device(device_ordinal));
}

int MachineManager::ExecutorToNumaNode(
    const StreamExecutor *stream_exec) const {
  return stream_exec->GetDeviceDescription().numa_node();
}

int MachineManager::DeviceToNumaNode(int device_ordinal) const {
  return ExecutorToNumaNode(executor_for_device(device_ordinal));
}

StreamExecutor *MachineManager::first_executor_for_bus(int bus_ordinal) {
  CHECK_LT(bus_ordinal, bus_count()) << "bus ordinal out of available range";
  for (auto &executor : executors_) {
    if (ExecutorToBus(executor.get()) == bus_ordinal) {
      return executor.get();
    }
  }

  LOG(WARNING) << "could not find executor requested for bus ordinal: "
               << bus_ordinal;
  return nullptr;
}

StreamExecutor *MachineManager::first_executor_for_numa_node(int numa_node) {
  for (auto &executor : executors_) {
    if (ExecutorToNumaNode(executor.get()) == numa_node) {
      return executor.get();
    }
  }

  LOG(WARNING) << "could not find executor requested for numa_node: "
               << numa_node;
  return nullptr;
}

Stream *MachineManager::stream_for_device(int device_ordinal) {
  CHECK(0 <= device_ordinal && device_ordinal < device_count());
  Stream *stream = streams_[device_ordinal].get();
  CHECK(stream != nullptr);
  return stream;
}

/* static */ port::StatusOr<MachineManager *>
MachineManager::CreateSingletonInternal(PlatformKind platform,
                                        DeviceOptions options,
                                        const PluginConfig &config) {
  if (singleton_ != nullptr) {
    return port::Status{
        port::error::ALREADY_EXISTS,
        "cannot create machine manager singleton; one already exists"};
  }

  auto create_status = Create(platform, options, config);
  if (!create_status.ok()) {
    return create_status.status();
  }

  singleton_ = create_status.ConsumeValueOrDie().release();

  VLOG(1) << "machine manager singleton is " << singleton_ << " with platform "
          << PlatformKindString(platform) << " and device options "
          << options.ToString();

  return singleton_;
}

/* static */ MachineManager *MachineManager::CreateSingletonOrDie(
    PlatformKind platform, DeviceOptions options, const PluginConfig &config) {
  auto status = CreateSingleton(platform, options, config);
  if (!status.ok()) {
    LOG(FATAL) << "failed to create MachineManager singleton: "
               << status.status();
  }
  return status.ValueOrDie();
}

/* static */ port::StatusOr<MachineManager *> MachineManager::CreateSingleton(
    PlatformKind platform, DeviceOptions device_options,
    const PluginConfig &config) {
  mutex_lock lock{mu_};
  return CreateSingletonInternal(platform, device_options, config);
}

/* static */ MachineManager *MachineManager::singleton() {
  mutex_lock lock{mu_};
  if (singleton_ == nullptr) {
    PlatformKind platform = DetectPreferredPlatform();
    DeviceOptions options = DeviceOptions::Default();
    auto status = CreateSingletonInternal(platform, options, PluginConfig());
    if (!status.ok()) {
      LOG(FATAL)
          << "failed to create MachineManager singleton: "
             "singleton accessor attempted lazy construction but failed: "
          << status.status();
    }
    return status.ValueOrDie();
  }

  return singleton_;
}

}  // namespace gputools
}  // namespace perftools