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/device_description.h"
17
18 #include <algorithm>
19
20 #include "absl/strings/str_cat.h"
21 #include "tensorflow/stream_executor/lib/human_readable.h"
22 #include "tensorflow/stream_executor/lib/mathutil.h"
23
24 namespace stream_executor {
25
26 static const uint64 kUninitializedUint64 = -1ULL;
27 /* static */ const char *DeviceDescription::kUndefinedString = "<undefined>";
28
DeviceDescription()29 DeviceDescription::DeviceDescription()
30 : device_vendor_(kUndefinedString),
31 platform_version_(kUndefinedString),
32 driver_version_(kUndefinedString),
33 runtime_version_(kUndefinedString),
34 pci_bus_id_(kUndefinedString),
35 name_(kUndefinedString),
36 thread_dim_limit_(kUninitializedUint64, kUninitializedUint64,
37 kUninitializedUint64),
38 block_dim_limit_(kUninitializedUint64, kUninitializedUint64,
39 kUninitializedUint64),
40 threads_per_core_limit_(kUninitializedUint64),
41 threads_per_block_limit_(kUninitializedUint64),
42 threads_per_warp_(kUninitializedUint64),
43 registers_per_core_limit_(kUninitializedUint64),
44 registers_per_block_limit_(kUninitializedUint64),
45 device_address_bits_(kUninitializedUint64),
46 device_memory_size_(kUninitializedUint64),
47 memory_bandwidth_(kUninitializedUint64),
48 shared_memory_per_core_(kUninitializedUint64),
49 shared_memory_per_block_(kUninitializedUint64),
50 clock_rate_ghz_(-1.0),
51 cuda_compute_capability_major_(-1),
52 cuda_compute_capability_minor_(-1),
53 rocm_amdgpu_isa_version_(-1),
54 rocm_amdgpu_gcn_arch_name_(kUndefinedString),
55 numa_node_(-1),
56 core_count_(-1),
57 ecc_enabled_(false) {}
58
ToMap() const59 std::unique_ptr<std::map<std::string, std::string>> DeviceDescription::ToMap()
60 const {
61 std::unique_ptr<std::map<std::string, std::string>> owned_result{
62 new std::map<std::string, std::string>};
63 std::map<std::string, std::string> &result = *owned_result;
64 result["Device Vendor"] = device_vendor();
65 result["Platform Version"] = platform_version();
66 result["Driver Version"] = driver_version();
67 result["Runtime Version"] = runtime_version();
68 result["PCI bus ID"] = pci_bus_id_;
69 result["Device Name"] = name_;
70
71 const ThreadDim &thread_dim = thread_dim_limit();
72 result["ThreadDim Limit"] =
73 absl::StrCat(thread_dim.x, ",", thread_dim.y, ",", thread_dim.z);
74 const BlockDim &block_dim = block_dim_limit();
75 result["BlockDim Limit"] =
76 absl::StrCat(block_dim.x, ",", block_dim.y, ",", block_dim.z);
77
78 result["Threads Per Core Limit"] = absl::StrCat(threads_per_core_limit());
79 result["Threads Per Block Limit"] = absl::StrCat(threads_per_block_limit());
80 result["Registers Per Block Limit"] =
81 absl::StrCat(registers_per_block_limit());
82
83 result["Device Address Bits"] = absl::StrCat(device_address_bits());
84 result["Device Memory Size"] =
85 port::HumanReadableNumBytes::ToString(device_memory_size());
86 result["Memory Bandwidth"] = absl::StrCat(
87 port::HumanReadableNumBytes::ToString(memory_bandwidth_), "/s");
88
89 result["Shared Memory Per Core"] =
90 port::HumanReadableNumBytes::ToString(shared_memory_per_core_);
91 result["Shared Memory Per Block"] =
92 port::HumanReadableNumBytes::ToString(shared_memory_per_block_);
93
94 result["Clock Rate GHz"] = absl::StrCat(clock_rate_ghz());
95
96 result["CUDA Compute Capability"] = absl::StrCat(
97 cuda_compute_capability_major_, ".", cuda_compute_capability_minor_);
98
99 result["AMDGPU GCN Arch Name"] = rocm_amdgpu_gcn_arch_name_;
100
101 result["NUMA Node"] = absl::StrCat(numa_node());
102 result["Core Count"] = absl::StrCat(core_count());
103 result["ECC Enabled"] = absl::StrCat(ecc_enabled());
104 return owned_result;
105 }
106
107 namespace internal {
108
DeviceDescriptionBuilder()109 DeviceDescriptionBuilder::DeviceDescriptionBuilder()
110 : device_description_(new DeviceDescription) {}
111
112 } // namespace internal
113
cuda_compute_capability(int * major,int * minor) const114 bool DeviceDescription::cuda_compute_capability(int *major, int *minor) const {
115 *major = cuda_compute_capability_major_;
116 *minor = cuda_compute_capability_minor_;
117 return cuda_compute_capability_major_ != 0;
118 }
119
rocm_amdgpu_isa_version(int * version) const120 bool DeviceDescription::rocm_amdgpu_isa_version(int *version) const {
121 bool status = false;
122 if (rocm_amdgpu_isa_version_ > 0) {
123 *version = rocm_amdgpu_isa_version_;
124 status = true;
125 }
126 return status;
127 }
128
ThreadDimOk(const DeviceDescription & device_description,const ThreadDim & thread_dim)129 bool ThreadDimOk(const DeviceDescription &device_description,
130 const ThreadDim &thread_dim) {
131 const int64 total_threads = thread_dim.x * thread_dim.y * thread_dim.z;
132 const int64 threads_per_block_limit =
133 device_description.threads_per_block_limit();
134 if (total_threads > threads_per_block_limit) {
135 VLOG(2) << "exceeded total-thread-per-block limit: " << total_threads
136 << " vs limit " << threads_per_block_limit;
137 return false;
138 }
139
140 const auto &limit = device_description.thread_dim_limit();
141 bool ok = thread_dim.x <= limit.x && thread_dim.y <= limit.y &&
142 thread_dim.z <= limit.z;
143 if (!ok) {
144 VLOG(2) << "thread dim " << thread_dim.ToString()
145 << " exceeds limit constraints of " << limit.ToString();
146 }
147 return ok;
148 }
149
DivideCeil(uint64 x,uint64 y)150 uint64 DivideCeil(uint64 x, uint64 y) {
151 return port::MathUtil::CeilOfRatio(x, y);
152 }
153
CalculateDimensionality(const DeviceDescription & device_description,int64 element_count,int64 * threads_per_block,int64 * block_count)154 void CalculateDimensionality(const DeviceDescription &device_description,
155 int64 element_count, int64 *threads_per_block,
156 int64 *block_count) {
157 *threads_per_block = device_description.threads_per_block_limit();
158 *block_count = port::MathUtil::CeilOfRatio(element_count, *threads_per_block);
159 if (*block_count == 1) {
160 CHECK_LE(element_count, *threads_per_block);
161 *threads_per_block = element_count;
162 }
163 }
164
165 } // namespace stream_executor
166