• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/stream_executor/platform.h"
17 
18 #include "tensorflow/stream_executor/platform/port.h"
19 
20 #include "absl/strings/str_cat.h"
21 #include "tensorflow/stream_executor/lib/error.h"
22 #include "tensorflow/stream_executor/platform/logging.h"
23 #include "tensorflow/stream_executor/stream_executor_pimpl.h"
24 
25 namespace stream_executor {
26 
PlatformKindString(PlatformKind kind)27 string PlatformKindString(PlatformKind kind) {
28   switch (kind) {
29     case PlatformKind::kCuda:
30       return "CUDA";
31     case PlatformKind::kROCm:
32       return "ROCm";
33     case PlatformKind::kOpenCL:
34       return "OpenCL";
35     case PlatformKind::kHost:
36       return "Host";
37     case PlatformKind::kMock:
38       return "Mock";
39     default:
40       return absl::StrCat("InvalidPlatformKind(", static_cast<int>(kind), ")");
41   }
42 }
43 
PlatformKindFromString(string kind)44 PlatformKind PlatformKindFromString(string kind) {
45   for (int i = 0; i < static_cast<int>(PlatformKind::kSize); ++i) {
46     if (kind == PlatformKindString(static_cast<PlatformKind>(i))) {
47       return static_cast<PlatformKind>(i);
48     }
49   }
50 
51   return PlatformKind::kInvalid;
52 }
53 
PlatformIsRunnable(PlatformKind kind)54 bool PlatformIsRunnable(PlatformKind kind) {
55   switch (kind) {
56     case PlatformKind::kCuda:
57     case PlatformKind::kROCm:
58     case PlatformKind::kOpenCL:
59     case PlatformKind::kHost:
60       return true;
61     default:
62       return false;
63   }
64 }
65 
PlatformIsRunnableOnDevice(PlatformKind kind)66 bool PlatformIsRunnableOnDevice(PlatformKind kind) {
67   switch (kind) {
68     case PlatformKind::kCuda:
69     case PlatformKind::kROCm:
70     case PlatformKind::kOpenCL:
71       return true;
72     default:
73       return false;
74   }
75 }
76 
CheckPlatformKindIsValid(PlatformKind kind)77 void CheckPlatformKindIsValid(PlatformKind kind) {
78   CHECK(static_cast<int>(PlatformKind::kCuda) <= static_cast<int>(kind) &&
79         static_cast<int>(kind) <= static_cast<int>(PlatformKind::kMock))
80       << "invalid GPU executor kind: " << PlatformKindString(kind);
81 }
82 
StreamExecutorConfig()83 StreamExecutorConfig::StreamExecutorConfig()
84     : ordinal(-1), device_options(DeviceOptions::Default()) {}
85 
StreamExecutorConfig(int ordinal_in)86 StreamExecutorConfig::StreamExecutorConfig(int ordinal_in)
87     : ordinal(ordinal_in), device_options(DeviceOptions::Default()) {}
88 
~Platform()89 Platform::~Platform() {}
90 
Initialized() const91 bool Platform::Initialized() const { return true; }
92 
Initialize(const std::map<string,string> & platform_options)93 port::Status Platform::Initialize(
94     const std::map<string, string> &platform_options) {
95   if (!platform_options.empty()) {
96     return port::Status(port::error::UNIMPLEMENTED,
97                         "this platform does not support custom initialization");
98   }
99   return port::Status::OK();
100 }
101 
ForceExecutorShutdown()102 port::Status Platform::ForceExecutorShutdown() {
103   return port::Status(port::error::UNIMPLEMENTED,
104                       "executor shutdown is not supported on this platform");
105 }
106 
GetPeerAccessMap()107 std::unique_ptr<Platform::PeerAccessMap> Platform::GetPeerAccessMap() {
108   auto *map = new PeerAccessMap;
109 
110   int device_count = VisibleDeviceCount();
111   for (int i = 0; i < device_count; ++i) {
112     for (int j = 0; j < device_count; ++j) {
113       StreamExecutor *from = ExecutorForDevice(i).ValueOrDie();
114       StreamExecutor *to = ExecutorForDevice(j).ValueOrDie();
115       (*map)[{i, j}] = from->CanEnablePeerAccessTo(to);
116     }
117   }
118 
119   return std::unique_ptr<Platform::PeerAccessMap>{map};
120 }
121 
EnablePeerAccess()122 port::Status Platform::EnablePeerAccess() {
123   auto peer_access_map = GetPeerAccessMap();
124   for (const auto &access : *peer_access_map) {
125     auto devices = access.first;
126     if (access.second) {
127       StreamExecutor *from = ExecutorForDevice(devices.first).ValueOrDie();
128       StreamExecutor *to = ExecutorForDevice(devices.second).ValueOrDie();
129       auto status = from->EnablePeerAccessTo(to);
130       if (!status.ok()) {
131         return status;
132       }
133     } else {
134       LOG(INFO) << "cannot enable peer access from device ordinal "
135                 << devices.first << " to device ordinal " << devices.second;
136     }
137   }
138   return port::Status::OK();
139 }
140 
141 }  // namespace stream_executor
142