1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_LOCAL_MASTER_H_
17#define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_LOCAL_MASTER_H_
18
19#include <memory>
20
21#include "tensorflow/core/distributed_runtime/master_interface.h"
22
23namespace tensorflow {
24
25class Master;
26
27// An implementation of the TensorFlow master interface that enables direct
28// intraprocess communication between the client and the master implementation.
29//
30// This master implementation is intended to provide more efficient access to
31// a master service that has been created in the same process as the client.
32//
33// TODO(mrry): Add methods that avoid protobuf encoding the request/response
34// objects where this affects performance.
35// TODO(mrry): Avoid closure creation/context switch overhead for synchronous
36// invocation of Master methods.
37// TODO(mrry): Make all potentially blocking Master methods take CallOptions
38// for cancellation.
39class LocalMaster : public MasterInterface {
40 public:
41  ~LocalMaster() {}
42
43  Status CreateSession(CallOptions* call_options,
44                       const CreateSessionRequest* request,
45                       CreateSessionResponse* response) override;
46
47  Status ExtendSession(CallOptions* call_options,
48                       const ExtendSessionRequest* request,
49                       ExtendSessionResponse* response) override;
50
51  Status PartialRunSetup(CallOptions* call_options,
52                         const PartialRunSetupRequest* request,
53                         PartialRunSetupResponse* response) override;
54
55  Status RunStep(CallOptions* call_options, RunStepRequestWrapper* request,
56                 MutableRunStepResponseWrapper* response) override;
57
58  MutableRunStepRequestWrapper* CreateRunStepRequest() override;
59
60  MutableRunStepResponseWrapper* CreateRunStepResponse() override;
61
62  Status CloseSession(CallOptions* call_options,
63                      const CloseSessionRequest* request,
64                      CloseSessionResponse* response) override;
65
66  Status ListDevices(CallOptions* call_options,
67                     const ListDevicesRequest* request,
68                     ListDevicesResponse* response) override;
69
70  // See tensorflow::Reset() and the comment on ResetRequest.
71  Status Reset(CallOptions* call_options, const ResetRequest* request,
72               ResetResponse* response) override;
73
74  // Registers the mapping from the given `target` to the given `master`.
75  //
76  // WARNING: The `master` pointer remains owned by the caller. It is
77  // the responsibility of the caller to ensure that `master` outlives
78  // any LocalMaster objects that may wrap this master. There is no
79  // corresponding deregister method, since clean server shutdown is
80  // not currently implemented for any server type.
81  static void Register(const string& target, Master* master,
82                       int64 default_timeout_in_ms);
83
84  // Returns a pointer to the local master associated with the given
85  // `target`, or nullptr if none exists.
86  static std::unique_ptr<LocalMaster> Lookup(const string& target);
87
88 private:
89  Master* master_impl_;  // Not owned.
90  const int64 default_timeout_in_ms_;
91
92  // See `LocalMaster::Lookup` for the factory function that creates
93  // objects of this type.
94  LocalMaster(Master* master_impl, const int64 default_timeout_in_ms);
95
96  TF_DISALLOW_COPY_AND_ASSIGN(LocalMaster);
97};
98
99}  // namespace tensorflow
100
101#endif  // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_LOCAL_MASTER_H_
102