• 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/common_runtime/renamed_device.h"
19 
20 #include "absl/memory/memory.h"
21 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
22 #include "tensorflow/core/lib/core/threadpool.h"
23 
24 namespace tensorflow {
25 
26 /* static */
NewRenamedDevice(const string & new_base,Device * underlying,bool owns_underlying,bool isolate_session_state,thread::ThreadPoolInterface * underlying_threadpool)27 std::unique_ptr<Device> RenamedDevice::NewRenamedDevice(
28     const string& new_base, Device* underlying, bool owns_underlying,
29     bool isolate_session_state,
30     thread::ThreadPoolInterface* underlying_threadpool) {
31   DeviceNameUtils::ParsedName parsed_name;
32   CHECK(DeviceNameUtils::ParseFullName(new_base, &parsed_name));
33   DeviceNameUtils::ParsedName underlying_parsed_name =
34       underlying->parsed_name();
35   CHECK(underlying_parsed_name.has_type);
36   CHECK(underlying_parsed_name.has_id);
37   parsed_name.type = underlying_parsed_name.type;
38   parsed_name.id = underlying_parsed_name.id;
39   string name = DeviceNameUtils::FullName(parsed_name.job, parsed_name.replica,
40                                           parsed_name.task, parsed_name.type,
41                                           parsed_name.id);
42   DeviceAttributes attributes(underlying->attributes());
43   attributes.set_name(name);
44   // Call absl::WrapUnique to access private constructor.
45   return absl::WrapUnique(
46       new RenamedDevice(underlying, attributes, owns_underlying,
47                         isolate_session_state, underlying_threadpool));
48 }
49 
RenamedDevice(Device * underlying,const DeviceAttributes & attributes,bool owns_underlying_device,bool isolate_session_state,thread::ThreadPoolInterface * underlying_threadpool)50 RenamedDevice::RenamedDevice(Device* underlying,
51                              const DeviceAttributes& attributes,
52                              bool owns_underlying_device,
53                              bool isolate_session_state,
54                              thread::ThreadPoolInterface* underlying_threadpool)
55     : Device(underlying->env(), attributes),
56       underlying_device_(underlying),
57       owns_underlying_device_(owns_underlying_device),
58       isolate_session_state_(isolate_session_state) {
59   if (underlying_threadpool != nullptr) {
60     underlying_threadpool_.reset(new thread::ThreadPool(underlying_threadpool));
61     eigen_worker_threads_.workers = underlying_threadpool_.get();
62     eigen_worker_threads_.num_threads = underlying_threadpool->NumThreads();
63     set_tensorflow_cpu_worker_threads(&eigen_worker_threads_);
64     set_tensorflow_device_thread_pool(underlying_threadpool_.get());
65 
66     Eigen::ThreadPoolDevice eigen_threadpool_device(
67         underlying_threadpool, underlying_threadpool->NumThreads());
68     set_eigen_cpu_device(&eigen_threadpool_device);
69   }
70 }
71 
~RenamedDevice()72 RenamedDevice::~RenamedDevice() {
73   if (owns_underlying_device_) {
74     delete underlying_device_;
75   }
76 }
77 
78 }  // namespace tensorflow
79