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 16 #ifndef TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_WORKER_SESSION_H_ 17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_WORKER_SESSION_H_ 18 19 #include <string> 20 21 #include "tensorflow/core/common_runtime/device_mgr.h" 22 #include "tensorflow/core/distributed_runtime/cluster_function_library_runtime.h" 23 #include "tensorflow/core/distributed_runtime/graph_mgr.h" 24 #include "tensorflow/core/distributed_runtime/worker_cache.h" 25 26 namespace tensorflow { 27 28 class ClusterFunctionLibraryRuntime; 29 class GraphMgr; 30 class WorkerCacheInterface; 31 32 // WorkerSession encapsulates all of the state relating to a given session. 33 struct WorkerSession { 34 // The name of the session. 35 const string session_name; 36 37 // The name of the worker. E.g., /job:mnist/replica:0/task:1. 38 const string worker_name; 39 40 // Object from which WorkerInterface instances can be obtained. 41 const std::unique_ptr<WorkerCacheInterface> worker_cache; 42 43 // Collection of local devices. These devices are typically 44 // RenamedDevices in all except the SessionMgr.legacy_session_ and 45 // sessions created with `isolate_session_state == false`. In the 46 // those cases, this method returns a pointer to a borrowed 47 // DeviceMgr (typically the `worker_env.device_mgr`). device_mgrWorkerSession48 DeviceMgr* device_mgr() { 49 return device_mgr_ ? device_mgr_.get() : borrowed_device_mgr_; 50 } 51 52 // graph_mgr keeps track of the registered graphs of this session. 53 // 54 // Note: graph_mgr must be deleted before rendezvous_mgr! 55 // Note: graph_mgr must be deleted before device_mgr! 56 const std::unique_ptr<GraphMgr> graph_mgr; 57 58 std::unique_ptr<ClusterFunctionLibraryRuntime> cluster_flr; 59 60 WorkerSession(const string& session_name, const string& worker_name, 61 std::unique_ptr<WorkerCacheInterface> worker_cache, 62 std::unique_ptr<DeviceMgr> device_mgr, 63 std::unique_ptr<GraphMgr> graph_mgr); 64 65 static std::shared_ptr<WorkerSession> CreateWithBorrowedDeviceMgr( 66 const string& session_name, const string& worker_name, 67 std::unique_ptr<WorkerCacheInterface> worker_cache, 68 DeviceMgr* borrowed_device_mgr, std::unique_ptr<GraphMgr> graph_mgr); 69 70 ~WorkerSession(); 71 72 private: 73 WorkerSession(const string& session_name, const string& worker_name, 74 std::unique_ptr<WorkerCacheInterface> worker_cache, 75 DeviceMgr* borrowed_device_mgr, 76 std::unique_ptr<GraphMgr> graph_mgr); 77 78 const std::unique_ptr<DeviceMgr> device_mgr_; 79 DeviceMgr* const borrowed_device_mgr_; // Not owned. 80 }; 81 82 } // namespace tensorflow 83 84 #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_WORKER_SESSION_H_ 85