aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/common_runtime/scoped_allocator_mgr.h
blob: 8c5e853472d1317dcc5e13583d4c4ea1f4a91956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_CORE_COMMON_RUNTIME_SCOPED_ALLOCATOR_MGR_H_
#define TENSORFLOW_CORE_COMMON_RUNTIME_SCOPED_ALLOCATOR_MGR_H_

#include <string>
#include <unordered_map>

#include "tensorflow/core/common_runtime/scoped_allocator.h"
#include "tensorflow/core/lib/core/refcount.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/platform/mutex.h"

namespace tensorflow {
class ScopedAllocatorMgr;

// At most one of these exists per <device, step_id> pair.
// A Ref is held by every ScopedAllocator and also by the ScopedAllocatorMgr.
class ScopedAllocatorContainer : public core::RefCounted {
 public:
  // Establishes a reachable ScopedAllocator.
  Status AddScopedAllocator(
      const Tensor& backing_tensor, int32 scope_id, const string& scope_name,
      const gtl::ArraySlice<ScopedAllocator::Field>& fields,
      int32 expected_call_count);

  ScopedAllocatorInstance* GetInstance(int32 scope_id);
  ScopedAllocator* GetAllocator(int32 scope_id);

  // Retire the scope_id.
  void Drop(int32 scope_id, ScopedAllocator* sa);

 protected:
  friend class ScopedAllocatorMgr;
  ScopedAllocatorContainer(const ScopedAllocatorMgr* mgr, int64 step_id)
      : mgr_(mgr), step_id_(step_id) {}
  ~ScopedAllocatorContainer();

 private:
  const ScopedAllocatorMgr* mgr_;
  int64 step_id_;
  mutex mu_;
  struct SAField {
    int32 field_index;
    union {
      ScopedAllocator* scoped_allocator;
      ScopedAllocatorInstance* instance;
    };
    SAField(int32 fi, ScopedAllocatorInstance* sai)
        : field_index(fi), instance(sai) {}
    SAField(int32 fi, ScopedAllocator* sa)
        : field_index(fi), scoped_allocator(sa) {}
    SAField()
        : field_index(ScopedAllocator::kBackingIndex),
          scoped_allocator(nullptr) {}
  };
  std::unordered_map<int32, SAField> allocators_ GUARDED_BY(mu_);
};

// At most one of these exists per device.
class ScopedAllocatorMgr {
 public:
  explicit ScopedAllocatorMgr(const string& device_name)
      : device_name_(device_name) {}
  ~ScopedAllocatorMgr();

  ScopedAllocatorContainer* GetContainer(int64 step_id);

  // Establishes a reachable ScopedAllocator.
  Status AddScopedAllocator(
      const Tensor& backing_tensor, int64 step_id, int32 scope_id,
      const string& scope_name,
      const gtl::ArraySlice<ScopedAllocator::Field>& fields,
      int32 expected_call_count);

  void Cleanup(int64 step_id);

  // Populate the bytes and offset members of Field.  Instance allocaters get
  // consecutive scope_id values following that of the base ScopedAllocator.
  // Returns the total number of bytes required to be allocated in the
  // backing tensor, for convenience.  (The same value can be obtained
  // by summing offset and bytes in the last field.)
  static size_t PopulateFields(int32 scope_id,
                               const gtl::ArraySlice<TensorShape>& shapes,
                               const DataType dtype,
                               std::vector<ScopedAllocator::Field>* fields);

  const string& device_name() const { return device_name_; }

 private:
  string device_name_;
  mutex mu_;
  std::unordered_map<int64, ScopedAllocatorContainer*> per_step_map_
      GUARDED_BY(mu_);
};

}  // namespace tensorflow
#endif  // TENSORFLOW_CORE_COMMON_RUNTIME_SCOPED_ALLOCATOR_MGR_H_