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 
     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 Device;
     30 class DeviceSet;
     31 class Env;
     32 class MasterSession;
     33 class OpRegistryInterface;
     34 
     35 // Options passed to the worker_cache_factory function.
     36 struct WorkerCacheFactoryOptions {
     37   const ClusterDef* cluster_def = nullptr;
     38   const string* job_name = nullptr;
     39   int task_index;
     40   const string* protocol = nullptr;
     41 
     42   WorkerCacheFactoryOptions() {}
     43 
     44   // Construct from a ServerDef proto.
     45   //
     46   // Note: server_def must outlive WorkerCacheFactoryOptions!
     47   WorkerCacheFactoryOptions(const ServerDef& server_def) {
     48     if (server_def.has_cluster() && !server_def.job_name().empty()) {
     49       cluster_def = &server_def.cluster();
     50       job_name = &server_def.job_name();
     51       task_index = server_def.task_index();
     52       protocol = &server_def.protocol();
     53     }
     54   }
     55 };
     56 
     57 // The master environment class, which holds a bag of pointers to
     58 // per-master state.
     59 //
     60 // MasterEnv does not own its member pointers.
     61 struct MasterEnv {
     62   Env* env = nullptr;
     63 
     64   // Object from which WorkerInterface instances can be obtained.
     65   WorkerCacheInterface* worker_cache = nullptr;
     66 
     67   // The operation definitions to use.  Must be filled before use.
     68   const OpRegistryInterface* ops = nullptr;
     69 
     70   // Local devices co-located with this master.  Devices are not owned
     71   // by the master service.
     72   //
     73   // REQUIRES: !local_devices.empty().
     74   std::vector<Device*> local_devices;
     75 
     76   // Factory for creating master sessions, given session options and a
     77   // vector of devices.
     78   //
     79   // The caller of the function takes ownership of the returned
     80   // `MasterSession`, which may not be null. Ownership of the
     81   // `MasterEnv*` is retained by the caller.
     82   std::function<MasterSession*(
     83       SessionOptions, MasterEnv*,
     84       std::unique_ptr<std::vector<std::unique_ptr<Device>>>,
     85       std::unique_ptr<WorkerCacheInterface>,
     86       std::unique_ptr<DeviceSet> device_set)>
     87       master_session_factory;
     88 
     89   std::function<Status(const WorkerCacheFactoryOptions&,
     90                        WorkerCacheInterface**)>
     91       worker_cache_factory;
     92 };
     93 
     94 }  // end namespace tensorflow
     95 
     96 #endif  // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_MASTER_H_
     97