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_RENDEZVOUS_MGR_INTERFACE_H_ 17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RENDEZVOUS_MGR_INTERFACE_H_ 18 19 #include <string> 20 21 #include "tensorflow/core/distributed_runtime/worker_env.h" 22 #include "tensorflow/core/framework/rendezvous.h" 23 #include "tensorflow/core/lib/core/status.h" 24 #include "tensorflow/core/platform/types.h" 25 26 namespace tensorflow { 27 28 class WorkerSession; 29 30 // RemoteRendezvous follow a 2-part initialization. First the objects are 31 // constructed. Eventually, they will be initialized. Clients of the 32 // RendezvousMgrInterface must guarantee to call Initialize on the returned 33 // RemoteRendezvous eventually. 34 // 35 // Partially initialized RemoteRendezvous must respect the Rendezvous interface 36 // (i.e. Send() must never block), however implementations are not expected to 37 // actually perform the underlying operations until after the RemoteRendezvous 38 // has been Initialize'd. 39 class RemoteRendezvous : public Rendezvous { 40 public: 41 // Fully construct the RemoteRendezvous. 42 virtual Status Initialize(WorkerSession* session) = 0; 43 44 protected: is_cross_process()45 bool is_cross_process() override { return true; } 46 }; 47 48 // RendezvousMgr keeps track of a set of local rendezvous instances. 49 // All tensors sent by this worker are buffered in a RendezvousMgr 50 // until the tensor is received. Each global unique "step_id" 51 // corresponds to one local rendezvous instance managed by a 52 // RendezvousMgr. 53 // 54 // E.g., 55 // Rendezvous* rendez = worker_env->rendezvous_mgr->Find(0x8935); 56 // fork execution of an graph executor using "rendez" on thread 1; 57 // fork execution of another graph executor using "rendez" on thread 2; 58 // ... 59 // join threads 1 and 2; 60 // 61 // In the example above, execution in thread 1 and 2 communicates with 62 // each other by send/recv operations through the "rend". 63 // 64 // Tensors sent and recved through rendezvous managed by this 65 // RendezvousMgr must have keys generated by Rendezvous::CreateKey. 66 class RendezvousMgrInterface { 67 public: RendezvousMgrInterface()68 RendezvousMgrInterface() {} ~RendezvousMgrInterface()69 virtual ~RendezvousMgrInterface() {} 70 71 // Returns Rendezvous supporting send and recv among workers in the 72 // "step_id". The caller takes ownership of one reference on the 73 // returned Rendezvous instance. 74 // 75 // Note: the caller must guarantee to eventually call Initialize on the 76 // returned RemoteRendezvous 77 virtual RemoteRendezvous* Find(int64 step_id) = 0; 78 79 // Finds the local rendezvous instance for the "step_id". Runs 80 // "done" when the tensor for "key" is produced or an error occurs. 81 // 82 // This method is used by the rpc handler of RecvTensor. 83 virtual void RecvLocalAsync(int64 step_id, 84 const Rendezvous::ParsedKey& parsed, 85 Rendezvous::DoneCallback done) = 0; 86 87 // Synchronous wrapper for RecvLocalAsync. 88 virtual Status RecvLocal(int64 step_id, const Rendezvous::ParsedKey& parsed, 89 Tensor* val, bool* is_dead) = 0; 90 91 // Removes rendezvous for "step_id". 92 // 93 // TODO(zhifengc): Have a background thread in worker that 94 // periodically calls CleanupAll(). 95 virtual void Cleanup(int64 step_id) = 0; 96 }; 97 98 } // end namespace tensorflow 99 100 #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RENDEZVOUS_MGR_INTERFACE_H_ 101