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 scope_id, const string& scope_name,
36       const gtl::ArraySlice<ScopedAllocator::Field>& fields,
37       int32 expected_call_count);
38 
39   ScopedAllocatorInstance* GetInstance(int32 scope_id);
40   ScopedAllocator* GetAllocator(int32 scope_id);
41 
42   // Retire the scope_id.
43   void Drop(int32 scope_id, ScopedAllocator* sa);
44 
45  protected:
46   friend class ScopedAllocatorMgr;
ScopedAllocatorContainer(const ScopedAllocatorMgr * mgr,int64 step_id)47   ScopedAllocatorContainer(const ScopedAllocatorMgr* mgr, int64 step_id)
48       : mgr_(mgr), step_id_(step_id) {}
49   ~ScopedAllocatorContainer();
50 
51  private:
52   const ScopedAllocatorMgr* mgr_;
53   int64 step_id_;
54   mutex mu_;
55   struct SAField {
56     int32 field_index;
57     union {
58       ScopedAllocator* scoped_allocator;
59       ScopedAllocatorInstance* instance;
60     };
SAFieldSAField61     SAField(int32 fi, ScopedAllocatorInstance* sai)
62         : field_index(fi), instance(sai) {}
SAFieldSAField63     SAField(int32 fi, ScopedAllocator* sa)
64         : field_index(fi), scoped_allocator(sa) {}
SAFieldSAField65     SAField()
66         : field_index(ScopedAllocator::kBackingIndex),
67           scoped_allocator(nullptr) {}
68   };
69   std::unordered_map<int32, SAField> allocators_ GUARDED_BY(mu_);
70 };
71 
72 // At most one of these exists per device.
73 class ScopedAllocatorMgr {
74  public:
ScopedAllocatorMgr(const string & device_name)75   explicit ScopedAllocatorMgr(const string& device_name)
76       : device_name_(device_name) {}
77   ~ScopedAllocatorMgr();
78 
79   ScopedAllocatorContainer* GetContainer(int64 step_id);
80 
81   // Establishes a reachable ScopedAllocator.
82   Status AddScopedAllocator(
83       const Tensor& backing_tensor, int64 step_id, int32 scope_id,
84       const string& scope_name,
85       const gtl::ArraySlice<ScopedAllocator::Field>& fields,
86       int32 expected_call_count);
87 
88   void Cleanup(int64 step_id);
89 
90   // Populate the bytes and offset members of Field.  Instance allocaters get
91   // consecutive scope_id values following that of the base ScopedAllocator.
92   // Returns the total number of bytes required to be allocated in the
93   // backing tensor, for convenience.  (The same value can be obtained
94   // by summing offset and bytes in the last field.)
95   static size_t PopulateFields(int32 scope_id,
96                                const gtl::ArraySlice<TensorShape>& shapes,
97                                const DataType dtype,
98                                std::vector<ScopedAllocator::Field>* fields);
99 
device_name()100   const string& device_name() const { return device_name_; }
101 
102  private:
103   string device_name_;
104   mutex mu_;
105   std::unordered_map<int64, ScopedAllocatorContainer*> per_step_map_
106       GUARDED_BY(mu_);
107 };
108 
109 }  // namespace tensorflow
110 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_SCOPED_ALLOCATOR_MGR_H_
111