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 "tensorflow/stream_executor/lib/error.h"
21 #include "tensorflow/stream_executor/lib/strcat.h"
22 #include "tensorflow/stream_executor/platform/logging.h"
23 #include "tensorflow/stream_executor/stream_executor_pimpl.h"
24
25 namespace perftools {
26 namespace gputools {
27
PlatformKindString(PlatformKind kind)28 string PlatformKindString(PlatformKind kind) {
29 switch (kind) {
30 case PlatformKind::kCuda:
31 return "CUDA";
32 case PlatformKind::kOpenCL:
33 return "OpenCL";
34 case PlatformKind::kHost:
35 return "Host";
36 case PlatformKind::kMock:
37 return "Mock";
38 default:
39 return port::StrCat("InvalidPlatformKind(", static_cast<int>(kind), ")");
40 }
41 }
42
PlatformKindFromString(string kind)43 PlatformKind PlatformKindFromString(string kind) {
44 for (int i = 0; i < static_cast<int>(PlatformKind::kSize); ++i) {
45 if (kind == PlatformKindString(static_cast<PlatformKind>(i))) {
46 return static_cast<PlatformKind>(i);
47 }
48 }
49
50 return PlatformKind::kInvalid;
51 }
52
PlatformIsRunnable(PlatformKind kind)53 bool PlatformIsRunnable(PlatformKind kind) {
54 switch (kind) {
55 case PlatformKind::kCuda:
56 case PlatformKind::kOpenCL:
57 case PlatformKind::kHost:
58 return true;
59 default:
60 return false;
61 }
62 }
63
PlatformIsRunnableOnDevice(PlatformKind kind)64 bool PlatformIsRunnableOnDevice(PlatformKind kind) {
65 switch (kind) {
66 case PlatformKind::kCuda:
67 case PlatformKind::kOpenCL:
68 return true;
69 default:
70 return false;
71 }
72 }
73
CheckPlatformKindIsValid(PlatformKind kind)74 void CheckPlatformKindIsValid(PlatformKind kind) {
75 CHECK(static_cast<int>(PlatformKind::kCuda) <= static_cast<int>(kind) &&
76 static_cast<int>(kind) <= static_cast<int>(PlatformKind::kMock))
77 << "invalid GPU executor kind: " << PlatformKindString(kind);
78 }
79
StreamExecutorConfig()80 StreamExecutorConfig::StreamExecutorConfig()
81 : ordinal(-1), device_options(DeviceOptions::Default()) {}
82
StreamExecutorConfig(int ordinal_in)83 StreamExecutorConfig::StreamExecutorConfig(int ordinal_in)
84 : ordinal(ordinal_in), device_options(DeviceOptions::Default()) {}
85
~Platform()86 Platform::~Platform() {}
87
ForceExecutorShutdown()88 port::Status Platform::ForceExecutorShutdown() {
89 return port::Status(port::error::UNIMPLEMENTED,
90 "executor shutdown is not supported on this platform");
91 }
92
GetPeerAccessMap()93 std::unique_ptr<Platform::PeerAccessMap> Platform::GetPeerAccessMap() {
94 auto *map = new PeerAccessMap;
95
96 int device_count = VisibleDeviceCount();
97 for (int i = 0; i < device_count; ++i) {
98 for (int j = 0; j < device_count; ++j) {
99 StreamExecutor *from = ExecutorForDevice(i).ValueOrDie();
100 StreamExecutor *to = ExecutorForDevice(j).ValueOrDie();
101 (*map)[{i, j}] = from->CanEnablePeerAccessTo(to);
102 }
103 }
104
105 return std::unique_ptr<Platform::PeerAccessMap>{map};
106 }
107
EnablePeerAccess()108 port::Status Platform::EnablePeerAccess() {
109 auto peer_access_map = GetPeerAccessMap();
110 for (const auto &access : *peer_access_map) {
111 auto devices = access.first;
112 if (access.second) {
113 StreamExecutor *from = ExecutorForDevice(devices.first).ValueOrDie();
114 StreamExecutor *to = ExecutorForDevice(devices.second).ValueOrDie();
115 auto status = from->EnablePeerAccessTo(to);
116 if (!status.ok()) {
117 return status;
118 }
119 } else {
120 LOG(INFO) << "cannot enable peer access from device ordinal "
121 << devices.first << " to device ordinal " << devices.second;
122 }
123 }
124 return port::Status::OK();
125 }
126
127 } // namespace gputools
128 } // namespace perftools
129