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_SCOPED_ALLOCATOR_MGR_H_ 16 #define TENSORFLOW_CORE_COMMON_RUNTIME_SCOPED_ALLOCATOR_MGR_H_ 17 18 #include <string> 19 #include <unordered_map> 20 21 #include "tensorflow/core/common_runtime/scoped_allocator.h" 22 #include "tensorflow/core/lib/core/refcount.h" 23 #include "tensorflow/core/lib/core/status.h" 24 #include "tensorflow/core/platform/mutex.h" 25 26 namespace tensorflow { 27 class ScopedAllocatorMgr; 28 29 // At most one of these exists per <device, step_id> pair. 30 // A Ref is held by every ScopedAllocator and also by the ScopedAllocatorMgr. 31 class ScopedAllocatorContainer : public core::RefCounted { 32 public: 33 // Establishes a reachable ScopedAllocator. 34 Status AddScopedAllocator( 35 const Tensor& backing_tensor, int32_t scope_id, 36 const std::string& scope_name, 37 const gtl::ArraySlice<ScopedAllocator::Field>& fields, 38 int32_t expected_call_count); 39 40 ScopedAllocatorInstance* GetInstance(int32_t scope_id); 41 ScopedAllocator* GetAllocator(int32_t scope_id); 42 43 // Retire the scope_id. 44 void Drop(int32_t scope_id, ScopedAllocator* sa); 45 46 protected: 47 friend class ScopedAllocatorMgr; ScopedAllocatorContainer(const ScopedAllocatorMgr * mgr,int64_t step_id)48 ScopedAllocatorContainer(const ScopedAllocatorMgr* mgr, int64_t step_id) 49 : mgr_(mgr), step_id_(step_id) {} 50 ~ScopedAllocatorContainer(); 51 52 private: 53 const ScopedAllocatorMgr* mgr_; 54 int64_t step_id_; 55 mutex mu_; 56 struct SAField { 57 int32 field_index; 58 union { 59 ScopedAllocator* scoped_allocator; 60 ScopedAllocatorInstance* instance; 61 }; SAFieldSAField62 SAField(int32_t fi, ScopedAllocatorInstance* sai) 63 : field_index(fi), instance(sai) {} SAFieldSAField64 SAField(int32_t fi, ScopedAllocator* sa) 65 : field_index(fi), scoped_allocator(sa) {} SAFieldSAField66 SAField() 67 : field_index(ScopedAllocator::kBackingIndex), 68 scoped_allocator(nullptr) {} 69 }; 70 std::unordered_map<int32, SAField> allocators_ TF_GUARDED_BY(mu_); 71 }; 72 73 // At most one of these exists per device. 74 class ScopedAllocatorMgr { 75 public: ScopedAllocatorMgr(const std::string & device_name)76 explicit ScopedAllocatorMgr(const std::string& device_name) 77 : device_name_(device_name) {} 78 ~ScopedAllocatorMgr(); 79 80 ScopedAllocatorContainer* GetContainer(int64_t step_id); 81 82 // Establishes a reachable ScopedAllocator. 83 Status AddScopedAllocator( 84 const Tensor& backing_tensor, int64_t step_id, int32_t scope_id, 85 const std::string& scope_name, 86 const gtl::ArraySlice<ScopedAllocator::Field>& fields, 87 int32_t expected_call_count); 88 89 void Cleanup(int64_t step_id); 90 91 // Populate the bytes and offset members of Field. Instance allocaters get 92 // consecutive scope_id values following that of the base ScopedAllocator. 93 // Returns the total number of bytes required to be allocated in the 94 // backing tensor, for convenience. (The same value can be obtained 95 // by summing offset and bytes in the last field.) 96 static size_t PopulateFields(int32_t scope_id, 97 const gtl::ArraySlice<TensorShape>& shapes, 98 const DataType dtype, 99 std::vector<ScopedAllocator::Field>* fields); 100 device_name()101 const std::string& device_name() const { return device_name_; } 102 103 private: 104 std::string device_name_; 105 mutex mu_; 106 std::unordered_map<int64_t, ScopedAllocatorContainer*> per_step_map_ 107 TF_GUARDED_BY(mu_); 108 }; 109 110 } // namespace tensorflow 111 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_SCOPED_ALLOCATOR_MGR_H_ 112