memory_block.h 4.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
liaogang 已提交
2

L
Luo Tao 已提交
3 4 5
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
L
liaogang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
L
liaogang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
L
liaogang 已提交
14 15
#pragma once

L
Leding Li 已提交
16
#include <cstddef>
Y
Yi Wang 已提交
17 18
#include <cstdint>
#include <unordered_map>
L
liaogang 已提交
19 20 21 22 23

namespace paddle {
namespace memory {
namespace detail {

Y
Yi Wang 已提交
24
// Forward declaration.
25
class MetadataCache;
L
liaogang 已提交
26

Y
Yi Wang 已提交
27
// MemoryBlock represents Each allocated memory block, which contains
Y
Yi Wang 已提交
28 29
// MemoryBlock::Desc and the payload.
struct MemoryBlock {
L
liaogang 已提交
30 31 32 33 34 35 36
  enum Type {
    FREE_CHUNK,    // memory is free and idle
    ARENA_CHUNK,   // memory is being occupied
    HUGE_CHUNK,    // memory is out of management
    INVALID_CHUNK  // memory is invalid
  };

Y
Yi Wang 已提交
37
  // init saves the MemoryBlock::Desc of the memory block in a MetadataCache.
Y
Yi Wang 已提交
38
  // If it is a CPU memory block, the MetadataCache writes the
Y
Yi Wang 已提交
39
  // MemoryBlock::Desc to the beginning of the block; or, if it is a GPU memory
Y
Yi Wang 已提交
40 41
  // block, the MetadataCache writes the Meatadata to a std::map in
  // the CPU.
42 43 44 45 46 47
  void Init(MetadataCache* cache,
            Type t,
            size_t index,
            size_t size,
            void* left_buddy,
            void* right_buddy);
L
liaogang 已提交
48

49 50
  MemoryBlock* GetLeftBuddy(MetadataCache* cache);
  MemoryBlock* GetRightBuddy(MetadataCache* cache);
L
liaogang 已提交
51

Y
Yi Wang 已提交
52
  // Split the allocation into left/right blocks.
53
  void Split(MetadataCache* cache, size_t size);
L
liaogang 已提交
54

Y
Yi Wang 已提交
55
  // Merge left and right blocks together.
56
  void Merge(MetadataCache* cache, MemoryBlock* right_buddy);
L
liaogang 已提交
57

Y
Yi Wang 已提交
58
  // Mark the allocation as free.
59
  void MarkAsFree(MetadataCache* cache);
L
liaogang 已提交
60

61 62
  void* Data() const;
  MemoryBlock* Metadata() const;
L
liaogang 已提交
63

Y
Yi Wang 已提交
64 65
  // MemoryBlock::Desc describes a MemoryBlock.
  struct Desc {
66 67 68 69 70
    Desc(MemoryBlock::Type t,
         size_t i,
         size_t s,
         size_t ts,
         MemoryBlock* l,
Y
Yi Wang 已提交
71 72
         MemoryBlock* r);
    Desc();
Y
Yi Wang 已提交
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    // mutator for type
    inline void set_type(const MemoryBlock::Type& type) {
      this->type = type;
      this->UpdateGuards();
    }

    // accessor for type
    inline const MemoryBlock::Type& get_type() const { return this->type; }

    // accessor for index
    inline const size_t& get_index() const { return this->index; }

    // accessor for size
    inline const size_t& get_size() const { return this->size; }

    // accessor for total_size
    inline const size_t& get_total_size() const { return this->total_size; }

Y
Yi Wang 已提交
92
    // Updates guard_begin and guard_end by hashes of the Metadata object.
93
    void UpdateGuards();
Y
Yi Wang 已提交
94 95

    // Checks that guard_begin and guard_end are hashes of the Metadata object.
96
    bool CheckGuards() const;
Y
Yi Wang 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110

    // TODO(gangliao): compress this
    size_t guard_begin = 0;
    MemoryBlock::Type type = MemoryBlock::INVALID_CHUNK;
    size_t index = 0;
    size_t size = 0;
    size_t total_size = 0;
    MemoryBlock* left_buddy = nullptr;
    MemoryBlock* right_buddy = nullptr;
    size_t guard_end = 0;
  };
};

// A cache for accessing memory block meta-data that may be expensive
Y
Yi Wang 已提交
111 112 113 114
// to access directly.  This class exists to unify the
// MemoryBlock::Desc format between GPU and CPU allocations. It should
// be removed when the CPU can access all GPU allocations directly via
// UVM.
Y
Yi Wang 已提交
115
class MetadataCache {
L
liaogang 已提交
116
 public:
Y
Yi Wang 已提交
117 118 119 120 121 122
  explicit MetadataCache(bool uses_gpu);

  // Disable copying and assignment.
  MetadataCache(const MetadataCache&) = delete;
  MetadataCache& operator=(const MetadataCache&) = delete;

Y
Yi Wang 已提交
123 124
  // Returns the MemoryBlock::Desc for a memory block.  When MetadataCache is
  // used to manage CPU memory, the MemoryBlock::Desc resides at the beginning
Y
Yi Wang 已提交
125 126
  // of the memory block; when used to manage GPU memory, the
  // Meatadata resides in CPU memory indexed by cache_.
127
  MemoryBlock::Desc* LoadDesc(MemoryBlock* memory_block);
Y
Yi Wang 已提交
128

Y
Yi Wang 已提交
129 130
  // Saves the MemoryBlock::Desc of a memory block into the cache.  For CPU
  // memory block, writes the MemoryBlock::Desc to the beginning of the memory
Y
Yi Wang 已提交
131
  // block; whereas for GPU memory, writes it to cache_.
132
  void Save(MemoryBlock* memory_block, const MemoryBlock::Desc& meta_data);
Y
Yi Wang 已提交
133

Y
Yi Wang 已提交
134
  // For GPU memory block, erases its MemoryBlock::Desc from cache_.
135
  void Invalidate(MemoryBlock* memory_block);
Y
Yi Wang 已提交
136 137

 private:
Y
Yi Wang 已提交
138
  typedef std::unordered_map<const MemoryBlock*, MemoryBlock::Desc> MetadataMap;
Y
Yi Wang 已提交
139 140
  MetadataMap cache_;
  bool uses_gpu_;
L
liaogang 已提交
141 142 143 144 145
};

}  // namespace detail
}  // namespace memory
}  // namespace paddle