Home | History | Annotate | Download | only in distributed_runtime
      1 /* Copyright 2016 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/worker_session.h"
     16 
     17 namespace tensorflow {
     18 
     19 namespace {
     20 
     21 // A private cache that wraps worker_cache and allows reuse of
     22 // WorkerInterface objects.
     23 class WorkerFreeListCache : public WorkerCacheInterface {
     24  public:
     25   explicit WorkerFreeListCache(std::unique_ptr<WorkerCacheInterface> w)
     26       : wrapped_(std::move(w)) {}
     27 
     28   ~WorkerFreeListCache() final {
     29     for (auto& p : workers_) {
     30       wrapped_->ReleaseWorker(p.first, p.second.worker);
     31     }
     32   }
     33 
     34   void ListWorkers(std::vector<string>* workers) const override {
     35     wrapped_->ListWorkers(workers);
     36   }
     37 
     38   WorkerInterface* CreateWorker(const string& target) override {
     39     mutex_lock l(mu_);
     40     auto p = workers_.find(target);
     41     if (p != workers_.end()) {
     42       return p->second.worker;
     43     }
     44     WorkerState state;
     45     state.worker = wrapped_->CreateWorker(target);
     46     if (state.worker != nullptr) {
     47       workers_.insert(std::make_pair(target, state));
     48     }
     49     return state.worker;
     50   }
     51 
     52   void ReleaseWorker(const string& target, WorkerInterface* worker) override {
     53     // TODO(jeff,sanjay): Should decrement ref-count when we implement eviction.
     54   }
     55 
     56   bool GetDeviceLocalityNonBlocking(const string& device,
     57                                     DeviceLocality* locality) override {
     58     return wrapped_->GetDeviceLocalityNonBlocking(device, locality);
     59   }
     60 
     61   void GetDeviceLocalityAsync(const string& device, DeviceLocality* locality,
     62                               StatusCallback done) override {
     63     wrapped_->GetDeviceLocalityAsync(device, locality, done);
     64   }
     65 
     66   void SetLogging(bool active) override { wrapped_->SetLogging(active); }
     67 
     68   void ClearLogs() override { wrapped_->ClearLogs(); }
     69 
     70   bool RetrieveLogs(int64 step_id, StepStats* ss) override {
     71     return wrapped_->RetrieveLogs(step_id, ss);
     72   }
     73 
     74  private:
     75   std::unique_ptr<WorkerCacheInterface> wrapped_;
     76 
     77   // Information kept per created WorkerInterface.
     78   struct WorkerState {
     79     WorkerInterface* worker;
     80     // TODO(jeff,sanjay): Add reference count if we support eviction.
     81   };
     82 
     83   // TODO(jeff,sanjay): Eviction when the map becomes too big.
     84   mutex mu_;
     85   std::unordered_map<string, WorkerState> workers_ GUARDED_BY(mu_);
     86 };
     87 
     88 }  // namespace
     89 
     90 WorkerSession::WorkerSession(const string& session_name,
     91                              const string& worker_name,
     92                              std::unique_ptr<WorkerCacheInterface> worker_cache,
     93                              std::unique_ptr<DeviceMgr> device_mgr,
     94                              std::unique_ptr<GraphMgr> graph_mgr)
     95     : session_name(session_name),
     96       worker_name(worker_name),
     97       worker_cache(new WorkerFreeListCache(std::move(worker_cache))),
     98       device_mgr(std::move(device_mgr)),
     99       graph_mgr(std::move(graph_mgr)),
    100       cluster_flr(new ClusterFunctionLibraryRuntime(this)) {}
    101 
    102 }  // namespace tensorflow
    103