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_MASTER_ENV_H_ 17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_MASTER_ENV_H_ 18 19 #include <functional> 20 #include <vector> 21 22 #include "tensorflow/core/distributed_runtime/worker_cache.h" 23 #include "tensorflow/core/protobuf/cluster.pb.h" 24 #include "tensorflow/core/protobuf/tensorflow_server.pb.h" 25 #include "tensorflow/core/public/session_options.h" 26 27 namespace tensorflow { 28 29 class CollectiveExecutorMgrInterface; 30 class Device; 31 class DeviceSet; 32 class Env; 33 class MasterSession; 34 class OpRegistryInterface; 35 36 // Options passed to the worker_cache_factory function. 37 struct WorkerCacheFactoryOptions { 38 const ClusterDef* cluster_def = nullptr; 39 const string* job_name = nullptr; 40 int task_index; 41 const string* protocol = nullptr; 42 WorkerCacheFactoryOptionsWorkerCacheFactoryOptions43 WorkerCacheFactoryOptions() {} 44 45 // Construct from a ServerDef proto. 46 // 47 // Note: server_def must outlive WorkerCacheFactoryOptions! WorkerCacheFactoryOptionsWorkerCacheFactoryOptions48 WorkerCacheFactoryOptions(const ServerDef& server_def) { 49 if (server_def.has_cluster() && !server_def.job_name().empty()) { 50 cluster_def = &server_def.cluster(); 51 job_name = &server_def.job_name(); 52 task_index = server_def.task_index(); 53 protocol = &server_def.protocol(); 54 } 55 } 56 }; 57 58 // The master environment class, which holds a bag of pointers to 59 // per-master state. 60 // 61 // MasterEnv does not own its member pointers. 62 struct MasterEnv { 63 Env* env = nullptr; 64 65 // Object from which WorkerInterface instances can be obtained. 66 WorkerCacheInterface* worker_cache = nullptr; 67 68 // The operation definitions to use. Must be filled before use. 69 const OpRegistryInterface* ops = nullptr; 70 71 // Local devices co-located with this master. Devices are not owned 72 // by the master service. 73 // 74 // REQUIRES: !local_devices.empty(). 75 std::vector<Device*> local_devices; 76 77 // Factory for creating master sessions, given session options and a 78 // vector of devices. 79 // 80 // The caller of the function takes ownership of the returned 81 // `MasterSession`, which may not be null. Ownership of the 82 // `MasterEnv*` is retained by the caller. 83 std::function<MasterSession*( 84 SessionOptions, MasterEnv*, 85 std::unique_ptr<std::vector<std::unique_ptr<Device>>>, 86 std::unique_ptr<WorkerCacheInterface>, 87 std::unique_ptr<DeviceSet> device_set, 88 std::vector<string> filtered_worker_list)> 89 master_session_factory; 90 91 std::function<Status(const WorkerCacheFactoryOptions&, 92 WorkerCacheInterface**)> 93 worker_cache_factory; 94 95 // Generates per-step CollectiveExecutors and has access to utilities 96 // supporting collective operations. 97 CollectiveExecutorMgrInterface* collective_executor_mgr = nullptr; 98 }; 99 100 } // end namespace tensorflow 101 102 #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_MASTER_ENV_H_ 103