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