• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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 #ifndef TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_CLUSTER_FUNCTION_LIBRARY_RUNTIME_H_
16 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_CLUSTER_FUNCTION_LIBRARY_RUNTIME_H_
17 
18 #include "tensorflow/core/distributed_runtime/worker_interface.h"
19 #include "tensorflow/core/distributed_runtime/worker_session.h"
20 #include "tensorflow/core/framework/function.h"
21 
22 namespace tensorflow {
23 
24 struct WorkerSession;
25 
26 // ClusterFunctionLibraryRuntime contains methods to Instantiate and Run
27 // functions across processes by making RPCs.
28 class ClusterFunctionLibraryRuntime : public DistributedFunctionLibraryRuntime {
29  public:
ClusterFunctionLibraryRuntime(WorkerSession * worker_session,bool create_worker_session_called)30   ClusterFunctionLibraryRuntime(WorkerSession* worker_session,
31                                 bool create_worker_session_called)
32       : worker_session_(worker_session),
33         create_worker_session_called_(create_worker_session_called) {}
34 
35   ~ClusterFunctionLibraryRuntime() override;
36 
37   Status Instantiate(const string& function_name,
38                      const FunctionLibraryDefinition& lib_def, AttrSlice attrs,
39                      const FunctionLibraryRuntime::InstantiateOptions& options,
40                      FunctionLibraryRuntime::LocalHandle* handle) override;
41 
42   void Run(const FunctionLibraryRuntime::Options& opts,
43            FunctionLibraryRuntime::LocalHandle handle,
44            gtl::ArraySlice<Tensor> args, std::vector<Tensor>* rets,
45            FunctionLibraryRuntime::DoneCallback done) override;
46 
47  private:
48   static Status ConstructFunctionGraph(
49       const OpDef& sig, AttrSlice attrs,
50       const FunctionLibraryRuntime::InstantiateOptions& options, GraphDef* g,
51       std::vector<string>* send_keys, std::vector<string>* recv_keys);
52   friend class ClusterFunctionLibraryRuntimeTest;
53 
54   mutable mutex mu_;
55   WorkerSession* const worker_session_ = nullptr;  // not owned.
56   const bool create_worker_session_called_;
57 
58   struct FunctionData {
59     const string graph_handle;
60     const string target;
61     WorkerInterface* wi = nullptr;
62     const std::vector<string> send_keys;
63     const std::vector<string> recv_keys;
64 
FunctionDataFunctionData65     FunctionData(const string& graph_handle, const string& target,
66                  WorkerInterface* wi, const std::vector<string>& send_keys,
67                  const std::vector<string>& recv_keys)
68         : graph_handle(graph_handle),
69           target(target),
70           wi(wi),
71           send_keys(send_keys),
72           recv_keys(recv_keys) {}
73   };
74 
75   std::vector<FunctionData> function_data_ GUARDED_BY(mu_);
76 };
77 
78 }  // namespace tensorflow
79 
80 #endif  // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_CLUSTER_FUNCTION_LIBRARY_RUNTIME_H_
81