• 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 #define EIGEN_USE_THREADS
17 
18 #include "tensorflow/core/framework/device_base.h"
19 
20 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
21 #include "tensorflow/core/lib/gtl/stl_util.h"
22 #include "tensorflow/core/util/work_sharder.h"
23 
24 namespace tensorflow {
25 
~DeviceBase()26 DeviceBase::~DeviceBase() { gtl::STLDeleteElements(&eigen_cpu_devices_); }
27 
attributes() const28 const DeviceAttributes& DeviceBase::attributes() const {
29   LOG(FATAL) << "Device does not implement attributes()";
30 }
31 
name() const32 const string& DeviceBase::name() const {
33   LOG(FATAL) << "Device does not implement name()";
34 }
35 
set_eigen_cpu_device(Eigen::ThreadPoolDevice * d)36 void DeviceBase::set_eigen_cpu_device(Eigen::ThreadPoolDevice* d) {
37   // Eigen::ThreadPoolDevice is a very cheap struct (two pointers and
38   // an int).  Therefore, we can afford a pre-allocated array of
39   // Eigen::ThreadPoolDevice.  Here, we ensure that
40   // Eigen::ThreadPoolDevices in eigen_cpu_devices_ has increasingly
41   // larger numThreads.
42   for (int i = 1; i <= d->numThreads(); ++i) {
43     eigen_cpu_devices_.push_back(new Eigen::ThreadPoolDevice(
44         d->getPool(), i /* numThreads() */, d->allocator()));
45   }
46 }
47 
eigen_cpu_device()48 const Eigen::ThreadPoolDevice* DeviceBase::eigen_cpu_device() {
49   // Based on GetPerThreadMaxParallelism(), we return a different
50   // pre-allocated Eigen::ThreadPoolDevice. All these ThreadPoolDevice
51   // use the same underlying threadpool. But they use different
52   // nominal numThreads() hoping that the user of the returned
53   // Eigen::ThreadPoolDevice may not aggressively occupy all the
54   // threads in the underlying threadpool.
55   const int parallelism = std::max<int>(
56       1,
57       std::min<int>(GetPerThreadMaxParallelism(), eigen_cpu_devices_.size()));
58   return eigen_cpu_devices_[parallelism - 1];
59 }
60 
61 }  // namespace tensorflow
62