1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/stream_executor/host/host_platform.h"
17
18#include <thread>
19
20#include "tensorflow/stream_executor/host/host_gpu_executor.h"
21#include "tensorflow/stream_executor/host/host_platform_id.h"
22#include "tensorflow/stream_executor/lib/error.h"
23#include "tensorflow/stream_executor/lib/initialize.h"
24#include "tensorflow/stream_executor/lib/ptr_util.h"
25#include "tensorflow/stream_executor/lib/status.h"
26#include "tensorflow/stream_executor/lib/status_macros.h"
27#include "tensorflow/stream_executor/lib/stringprintf.h"
28
29namespace gpu = ::perftools::gputools;
30
31namespace perftools {
32namespace gputools {
33namespace host {
34
35HostPlatform::HostPlatform() : name_("Host") {}
36
37HostPlatform::~HostPlatform() {}
38
39Platform::Id HostPlatform::id() const { return kHostPlatformId; }
40
41int HostPlatform::VisibleDeviceCount() const {
42  return std::thread::hardware_concurrency();
43}
44
45const string& HostPlatform::Name() const { return name_; }
46
47port::StatusOr<StreamExecutor*> HostPlatform::ExecutorForDevice(int ordinal) {
48  StreamExecutorConfig config;
49  config.ordinal = ordinal;
50  config.plugin_config = PluginConfig();
51  config.device_options = DeviceOptions::Default();
52  return GetExecutor(config);
53}
54
55port::StatusOr<StreamExecutor*> HostPlatform::ExecutorForDeviceWithPluginConfig(
56    int device_ordinal, const PluginConfig& plugin_config) {
57  StreamExecutorConfig config;
58  config.ordinal = device_ordinal;
59  config.plugin_config = plugin_config;
60  config.device_options = DeviceOptions::Default();
61  return GetExecutor(config);
62}
63
64port::StatusOr<StreamExecutor*> HostPlatform::GetExecutor(
65    const StreamExecutorConfig& config) {
66  return executor_cache_.GetOrCreate(
67      config, [&]() { return GetUncachedExecutor(config); });
68}
69
70port::StatusOr<std::unique_ptr<StreamExecutor>>
71HostPlatform::GetUncachedExecutor(const StreamExecutorConfig& config) {
72  auto executor = port::MakeUnique<StreamExecutor>(
73      this, port::MakeUnique<HostExecutor>(config.plugin_config));
74  auto init_status = executor->Init(config.ordinal, config.device_options);
75  if (!init_status.ok()) {
76    return port::Status{
77        port::error::INTERNAL,
78        port::Printf(
79            "failed initializing StreamExecutor for device ordinal %d: %s",
80            config.ordinal, init_status.ToString().c_str())};
81  }
82
83  return std::move(executor);
84}
85
86void HostPlatform::RegisterTraceListener(
87    std::unique_ptr<TraceListener> listener) {
88  LOG(FATAL) << "not yet implemented: register host trace listener";
89}
90
91void HostPlatform::UnregisterTraceListener(TraceListener* listener) {
92  LOG(FATAL) << "not yet implemented: unregister host trace listener";
93}
94
95static void InitializeHostPlatform() {
96  std::unique_ptr<gpu::Platform> platform(new gpu::host::HostPlatform);
97  SE_CHECK_OK(gpu::MultiPlatformManager::RegisterPlatform(std::move(platform)));
98}
99
100}  // namespace host
101}  // namespace gputools
102}  // namespace perftools
103
104REGISTER_MODULE_INITIALIZER(
105    host_platform, perftools::gputools::host::InitializeHostPlatform());
106
107DECLARE_MODULE_INITIALIZER(multi_platform_manager);
108// Note that module initialization sequencing is not supported in the
109// open-source project, so this will be a no-op there.
110REGISTER_MODULE_INITIALIZER_SEQUENCE(host_platform, multi_platform_manager);
111