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 #define EIGEN_USE_THREADS
17
18 #include "tensorflow/core/framework/device_base.h"
19
20 #include <algorithm>
21 #include <vector>
22
23 #include "absl/container/flat_hash_set.h"
24 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
25 #include "tensorflow/core/util/work_sharder.h"
26
27 namespace tensorflow {
28
~DeviceBase()29 DeviceBase::~DeviceBase() {
30 for (auto& temp : eigen_cpu_devices_) {
31 delete temp;
32 }
33 eigen_cpu_devices_.clear();
34 }
35
attributes() const36 const DeviceAttributes& DeviceBase::attributes() const {
37 LOG(FATAL) << "Device does not implement attributes()";
38 }
39
name() const40 const string& DeviceBase::name() const {
41 LOG(FATAL) << "Device does not implement name()";
42 }
43
set_eigen_cpu_device(Eigen::ThreadPoolDevice * d)44 void DeviceBase::set_eigen_cpu_device(Eigen::ThreadPoolDevice* d) {
45 // Eigen::ThreadPoolDevice is a very cheap struct (two pointers and
46 // an int). Therefore, we can afford a pre-allocated array of
47 // Eigen::ThreadPoolDevice. Here, we ensure that
48 // Eigen::ThreadPoolDevices in eigen_cpu_devices_ has increasingly
49 // larger numThreads.
50 for (int i = 1; i <= d->numThreads(); ++i) {
51 eigen_cpu_devices_.push_back(new Eigen::ThreadPoolDevice(
52 d->getPool(), i /* numThreads() */, d->allocator()));
53 }
54 }
55
eigen_cpu_device()56 const Eigen::ThreadPoolDevice* DeviceBase::eigen_cpu_device() {
57 // Based on GetPerThreadMaxParallelism(), we return a different
58 // pre-allocated Eigen::ThreadPoolDevice. All these ThreadPoolDevice
59 // use the same underlying threadpool. But they use different
60 // nominal numThreads() hoping that the user of the returned
61 // Eigen::ThreadPoolDevice may not aggressively occupy all the
62 // threads in the underlying threadpool.
63 const int parallelism = std::max<int>(
64 1,
65 std::min<int>(GetPerThreadMaxParallelism(), eigen_cpu_devices_.size()));
66 return eigen_cpu_devices_[parallelism - 1];
67 }
68
69 namespace {
70
GetSymbolicDeviceList()71 absl::flat_hash_set<std::string>* GetSymbolicDeviceList() {
72 static absl::flat_hash_set<std::string>* symbolic_device_list =
73 new absl::flat_hash_set<std::string>();
74 return symbolic_device_list;
75 }
76
77 } // namespace
78
AddSymbolicExecutionDevice(const absl::string_view device_name)79 void AddSymbolicExecutionDevice(const absl::string_view device_name) {
80 GetSymbolicDeviceList()->insert(std::string(device_name));
81 }
82
IsSymbolicExecutionDevice(const absl::string_view device_name)83 bool IsSymbolicExecutionDevice(const absl::string_view device_name) {
84 absl::flat_hash_set<std::string>* symbolic_devices = GetSymbolicDeviceList();
85 if (symbolic_devices->contains(device_name)) {
86 return true;
87 } else {
88 return false;
89 }
90 }
91
92 } // namespace tensorflow
93