1 /* Copyright 2018 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 #include "tensorflow/core/distributed_runtime/device_resolver_distributed.h"
16
17 #include "tensorflow/core/common_runtime/device_mgr.h"
18 #include "tensorflow/core/distributed_runtime/worker_cache.h"
19
20 namespace tensorflow {
DeviceResolverDistributed(const DeviceMgr * dev_mgr,WorkerCacheInterface * worker_cache,const string & task_name)21 DeviceResolverDistributed::DeviceResolverDistributed(
22 const DeviceMgr* dev_mgr, WorkerCacheInterface* worker_cache,
23 const string& task_name)
24 : dev_mgr_(dev_mgr), worker_cache_(worker_cache), task_name_(task_name) {}
25
GetLocalityAsync(const string & device,const string & task,DeviceLocality * locality,const StatusCallback & done)26 void DeviceResolverDistributed::GetLocalityAsync(const string& device,
27 const string& task,
28 DeviceLocality* locality,
29 const StatusCallback& done) {
30 if (task.empty() || task == task_name_) {
31 // Device is local to this task.
32 Device* dev;
33 Status s = dev_mgr_->LookupDevice(device, &dev);
34 if (s.ok()) {
35 *locality = dev->attributes().locality();
36 }
37 done(s);
38 return;
39 } else {
40 // Lookup of a remote device: first try the local cache.
41 bool found = false;
42 {
43 mutex_lock l(mu_);
44 auto it = attr_table_.find(device);
45 if (it != attr_table_.end()) {
46 *locality = it->second.locality();
47 found = true;
48 }
49 }
50 if (found) {
51 done(Status::OK());
52 return;
53 }
54 }
55 // Device is remote and no cache entry was found. Refresh the cache
56 // then retry the lookup.
57 RefreshRemoteAttributes(
58 device, task, [this, device, task, locality, done](const Status& s) {
59 if (!s.ok()) {
60 done(s);
61 } else {
62 GetLocalityAsync(device, task, locality, done);
63 }
64 });
65 }
66
GetDeviceLocalitiesAsync(const CollInstanceParams & inst_params,std::vector<DeviceLocality> * localities,const StatusCallback & done)67 void DeviceResolverDistributed::GetDeviceLocalitiesAsync(
68 const CollInstanceParams& inst_params,
69 std::vector<DeviceLocality>* localities, const StatusCallback& done) {
70 localities->clear();
71 GetDeviceLocalitiesRecursive(inst_params, localities, done);
72 }
73
GetDeviceLocalitiesRecursive(const CollInstanceParams & inst_params,std::vector<DeviceLocality> * localities,const StatusCallback & done)74 void DeviceResolverDistributed::GetDeviceLocalitiesRecursive(
75 const CollInstanceParams& inst_params,
76 std::vector<DeviceLocality>* localities, const StatusCallback& done) {
77 size_t i = localities->size();
78 if (i < inst_params.device_names.size()) {
79 localities->push_back(DeviceLocality());
80 GetLocalityAsync(inst_params.device_names[i], inst_params.task_names[i],
81 &localities->back(),
82 [this, &inst_params, localities, done](const Status& s) {
83 if (!s.ok()) {
84 done(s);
85 return;
86 } else {
87 GetDeviceLocalitiesRecursive(inst_params, localities,
88 done);
89 }
90 });
91 } else {
92 done(Status::OK());
93 }
94 }
95
RefreshRemoteAttributes(const string & device,const string & task,const StatusCallback & done)96 void DeviceResolverDistributed::RefreshRemoteAttributes(
97 const string& device, const string& task, const StatusCallback& done) {
98 GetStatusRequest* req = new GetStatusRequest;
99 GetStatusResponse* resp = new GetStatusResponse;
100 WorkerInterface* worker = worker_cache_->CreateWorker(task);
101 CHECK(worker) << "Failed to get worker for " << task;
102 worker->GetStatusAsync(
103 req, resp, [this, device, task, req, resp, worker, done](Status s) {
104 if (s.ok()) {
105 mutex_lock l(mu_);
106 for (const DeviceAttributes& da : resp->device_attributes()) {
107 attr_table_[da.name()] = da;
108 }
109 }
110 done(s);
111 delete req;
112 delete resp;
113 worker_cache_->ReleaseWorker(task, worker);
114 });
115 }
116
ClearTask(const string & task)117 void DeviceResolverDistributed::ClearTask(const string& task) {
118 mutex_lock l(mu_);
119 // First find all the keys belonging to the task.
120 std::unordered_set<string> task_keys;
121 for (const auto& it : attr_table_) {
122 const string& device_name = it.first;
123 if (DeviceNameUtils::IsSameAddressSpace(task, device_name)) {
124 task_keys.insert(device_name);
125 }
126 }
127 // Then delete them.
128 for (const string& key : task_keys) {
129 attr_table_.erase(key);
130 }
131 }
132
133 } // namespace tensorflow
134