1/* Copyright 2015 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/multi_platform_manager.h"
17
18#include "tensorflow/stream_executor/lib/error.h"
19#include "tensorflow/stream_executor/lib/initialize.h"
20#include "tensorflow/stream_executor/lib/str_util.h"
21#include "tensorflow/stream_executor/lib/stringprintf.h"
22
23namespace perftools {
24namespace gputools {
25
26/* static */ port::Status MultiPlatformManager::RegisterPlatform(
27    std::unique_ptr<Platform> platform) {
28  CHECK(platform != nullptr);
29  string key = port::Lowercase(platform->Name());
30  mutex_lock lock(GetPlatformsMutex());
31  if (GetPlatformMap()->find(key) != GetPlatformMap()->end()) {
32    return port::Status(port::error::INTERNAL,
33                        "platform is already registered with name: \"" +
34                            platform->Name() + "\"");
35  }
36  GetPlatformByIdMap()->insert(std::make_pair(platform->id(), platform.get()));
37  // Release ownership/uniqueness to prevent destruction on program exit.
38  // This avoids Platforms "cleaning up" on program exit, because otherwise,
39  // there are _very_ tricky races between StreamExecutor and underlying
40  // platforms (CUDA, OpenCL) during exit. Since these are fixed-size and 1x per
41  // program, these are deemed acceptable.
42  (*GetPlatformMap())[key] = platform.release();
43  return port::Status::OK();
44}
45
46/* static */ port::StatusOr<Platform*> MultiPlatformManager::PlatformWithName(
47    const string& target) {
48  tf_shared_lock lock(GetPlatformsMutex());
49  auto it = GetPlatformMap()->find(port::Lowercase(target));
50
51  if (it == GetPlatformMap()->end()) {
52    return port::Status(
53        port::error::NOT_FOUND,
54        "could not find registered platform with name: \"" + target + "\"");
55  }
56
57  return it->second;
58}
59
60/* static */ port::StatusOr<Platform*> MultiPlatformManager::PlatformWithId(
61    const Platform::Id& id) {
62  tf_shared_lock lock(GetPlatformsMutex());
63  auto it = GetPlatformByIdMap()->find(id);
64  if (it == GetPlatformByIdMap()->end()) {
65    return port::Status(
66        port::error::NOT_FOUND,
67        port::Printf("could not find registered platform with id: 0x%p", id));
68  }
69
70  return it->second;
71}
72
73/* static */ void MultiPlatformManager::ClearPlatformRegistry() {
74  mutex_lock lock(GetPlatformsMutex());
75  GetPlatformMap()->clear();
76  GetPlatformByIdMap()->clear();
77}
78
79}  // namespace gputools
80}  // namespace perftools
81
82REGISTER_MODULE_INITIALIZER(
83    multi_platform_manager,
84    {
85        // Nothing -- this is just a module initializer
86        // definition to reference for sequencing
87        // purposes from Platform subclasses that register
88        // themselves with the MultiPlatformManager.
89    });
90