1 /* Copyright 2018 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_COMMON_RUNTIME_BUF_RENDEZVOUS_H_ 16 #define TENSORFLOW_CORE_COMMON_RUNTIME_BUF_RENDEZVOUS_H_ 17 18 #include <functional> 19 #include <string> 20 21 #include "tensorflow/core/framework/allocator.h" 22 #include "tensorflow/core/lib/core/status.h" 23 #include "tensorflow/core/lib/gtl/flatmap.h" 24 #include "tensorflow/core/platform/mutex.h" 25 26 namespace tensorflow { 27 class Device; 28 class DeviceContext; 29 class Tensor; 30 31 // EXPERIMENTAL: RDMA oriented producer/consumer rendezvous on a local 32 // Tensor value for which DMAHelper::CanUseDMA() is true, i.e. dense 33 // numeric types. Similar to Rendezvous but never owns a Ref on the 34 // tensor, instead it uses an explicit callback to the producer when 35 // the consumer side is finished with the value. This allows the 36 // producer to perform in-place updates on the source buffer or to take 37 // other actions that depend on knowing the consumer has passed a certain 38 // execution point. 39 class BufRendezvous { 40 public: BufRendezvous(uint64 step_id)41 explicit BufRendezvous(uint64 step_id) : step_id_(step_id) {} 42 43 ~BufRendezvous(); 44 45 // Inform all all waiting parties that this BufRendezvous is defunct 46 // because of an error Status interrupting the Step. 47 void StartAbort(const Status& s); 48 49 struct Hook; 50 // Provided by the consumer to be called when access to the buffer 51 // is available. If the Status arg is not OK, then hook will not 52 // be populated. Ownership of Hook passes to consumer with the 53 // callback. 54 typedef std::function<void(const Status&, Hook*)> ConsumerCallback; 55 // Provided by the producer to be called when the consumer has finished 56 // reading the buffer and will no longer access it. 57 typedef std::function<void(const Status&)> ProducerCallback; 58 59 struct Hook { 60 Device* prod_dev; 61 DeviceContext* prod_ctx; 62 const Tensor* prod_value; 63 AllocatorAttributes prod_attr; 64 ProducerCallback prod_cb; 65 ConsumerCallback cons_cb; HookHook66 Hook() 67 : prod_dev(nullptr), 68 prod_ctx(nullptr), 69 prod_value(nullptr), 70 prod_cb(nullptr), 71 cons_cb(nullptr) {} 72 string DebugString() const; 73 }; 74 75 // Called to advertise availability of a Tensor value corresponding 76 // to key. That value must stay valid until done is called. 77 void ProvideBuf(const string& key, Device* dev, DeviceContext* dev_ctx, 78 const Tensor* v, const AllocatorAttributes& attr, 79 const ProducerCallback& done); 80 81 // Called to request access to a Tensor value corresponding to key. 82 // Consumer is provide with a Hook as soon as available. 83 void ConsumeBuf(const string& key, const ConsumerCallback& done); 84 85 // Consumer must call this function when it's done reading the Hook provided 86 // by the ConsumerCallback. This function will invoke the producer callback 87 // and then delete h. 88 static void DoneWithHook(Hook* h); 89 90 // Write the current contents of the table to the INFO log. 91 void LogContents(); 92 93 protected: 94 const uint64 step_id_; 95 mutex mu_; 96 Status status_ GUARDED_BY(mu_); 97 typedef gtl::FlatMap<string, Hook*> HookTable; 98 HookTable hook_table_ GUARDED_BY(mu_); 99 100 void PurgeTable(const Status& s, HookTable* table); 101 }; 102 } // namespace tensorflow 103 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_BUF_RENDEZVOUS_H_ 104