提交 a2730d1e 编写于 作者: Y Yi Wang

Rename Metadata into MemoryBlock::Desc

上级 eebb2053
cc_library(memory_block SRCS memory_block.cc meta_data.cc meta_cache.cc) cc_library(memory_block SRCS memory_block.cc memory_block_desc.cc meta_cache.cc)
if(${WITH_GPU}) if(${WITH_GPU})
nv_library(system_allocator SRCS system_allocator.cc DEPS gflags cpu_info gpu_info) nv_library(system_allocator SRCS system_allocator.cc DEPS gflags cpu_info gpu_info)
......
...@@ -21,9 +21,10 @@ namespace detail { ...@@ -21,9 +21,10 @@ namespace detail {
void MemoryBlock::init(MetadataCache* cache, Type t, size_t index, size_t size, void MemoryBlock::init(MetadataCache* cache, Type t, size_t index, size_t size,
void* left_buddy, void* right_buddy) { void* left_buddy, void* right_buddy) {
cache->save(this, Metadata(t, index, size - sizeof(Metadata), size, cache->save(
static_cast<MemoryBlock*>(left_buddy), this, MemoryBlock::Desc(t, index, size - sizeof(MemoryBlock::Desc), size,
static_cast<MemoryBlock*>(right_buddy))); static_cast<MemoryBlock*>(left_buddy),
static_cast<MemoryBlock*>(right_buddy)));
} }
MemoryBlock::Type MemoryBlock::type(MetadataCache& cache) const { MemoryBlock::Type MemoryBlock::type(MetadataCache& cache) const {
...@@ -63,7 +64,7 @@ void MemoryBlock::split(MetadataCache* cache, size_t size) { ...@@ -63,7 +64,7 @@ void MemoryBlock::split(MetadataCache* cache, size_t size) {
PADDLE_ASSERT(total_size(*cache) >= size); PADDLE_ASSERT(total_size(*cache) >= size);
// bail out if there is no room for another partition // bail out if there is no room for another partition
if (total_size(*cache) - size <= sizeof(Metadata)) { if (total_size(*cache) - size <= sizeof(MemoryBlock::Desc)) {
return; return;
} }
...@@ -78,13 +79,13 @@ void MemoryBlock::split(MetadataCache* cache, size_t size) { ...@@ -78,13 +79,13 @@ void MemoryBlock::split(MetadataCache* cache, size_t size) {
// Write the metadata for the new block // Write the metadata for the new block
auto new_block_right_buddy = metadata.right_buddy; auto new_block_right_buddy = metadata.right_buddy;
cache->save( cache->save(static_cast<MemoryBlock*>(right_partition),
static_cast<MemoryBlock*>(right_partition), MemoryBlock::Desc(FREE_CHUNK, index(*cache),
Metadata(FREE_CHUNK, index(*cache), remaining_size - sizeof(Metadata), remaining_size - sizeof(MemoryBlock::Desc),
remaining_size, this, new_block_right_buddy)); remaining_size, this, new_block_right_buddy));
metadata.right_buddy = static_cast<MemoryBlock*>(right_partition); metadata.right_buddy = static_cast<MemoryBlock*>(right_partition);
metadata.size = size - sizeof(Metadata); metadata.size = size - sizeof(MemoryBlock::Desc);
metadata.total_size = size; metadata.total_size = size;
cache->save(this, metadata); cache->save(this, metadata);
...@@ -122,7 +123,8 @@ void MemoryBlock::merge(MetadataCache* cache, MemoryBlock* right_buddy) { ...@@ -122,7 +123,8 @@ void MemoryBlock::merge(MetadataCache* cache, MemoryBlock* right_buddy) {
metadata.total_size += right_buddy->total_size(*cache); metadata.total_size += right_buddy->total_size(*cache);
cache->save(this, metadata); cache->save(this, metadata);
cache->save(right_buddy, Metadata(INVALID_CHUNK, 0, 0, 0, nullptr, nullptr)); cache->save(right_buddy,
MemoryBlock::Desc(INVALID_CHUNK, 0, 0, 0, nullptr, nullptr));
} }
void MemoryBlock::mark_as_free(MetadataCache* cache) { void MemoryBlock::mark_as_free(MetadataCache* cache) {
...@@ -139,12 +141,14 @@ void MemoryBlock::set_type(MetadataCache* cache, Type t) { ...@@ -139,12 +141,14 @@ void MemoryBlock::set_type(MetadataCache* cache, Type t) {
} }
void* MemoryBlock::data() const { void* MemoryBlock::data() const {
return const_cast<Metadata*>(reinterpret_cast<const Metadata*>(this)) + 1; return const_cast<MemoryBlock::Desc*>(
reinterpret_cast<const MemoryBlock::Desc*>(this)) +
1;
} }
MemoryBlock* MemoryBlock::metadata() const { MemoryBlock* MemoryBlock::metadata() const {
return const_cast<MemoryBlock*>(reinterpret_cast<const MemoryBlock*>( return const_cast<MemoryBlock*>(reinterpret_cast<const MemoryBlock*>(
reinterpret_cast<const Metadata*>(this) - 1)); reinterpret_cast<const MemoryBlock::Desc*>(this) - 1));
} }
} // namespace detail } // namespace detail
......
...@@ -23,9 +23,8 @@ namespace detail { ...@@ -23,9 +23,8 @@ namespace detail {
class MetadataCache; class MetadataCache;
// MemoryBlock represents Each allocated memory block, which contains // MemoryBlock represents Each allocated memory block, which contains
// Metadata and the payload. // MemoryBlock::Desc and the payload.
class MemoryBlock { struct MemoryBlock {
public:
enum Type { enum Type {
FREE_CHUNK, // memory is free and idle FREE_CHUNK, // memory is free and idle
ARENA_CHUNK, // memory is being occupied ARENA_CHUNK, // memory is being occupied
...@@ -33,17 +32,17 @@ class MemoryBlock { ...@@ -33,17 +32,17 @@ class MemoryBlock {
INVALID_CHUNK // memory is invalid INVALID_CHUNK // memory is invalid
}; };
// init saves the Metadata of the memory block in a MetadataCache. // init saves the MemoryBlock::Desc of the memory block in a MetadataCache.
// If it is a CPU memory block, the MetadataCache writes the // If it is a CPU memory block, the MetadataCache writes the
// Metadata to the beginning of the block; or, if it is a GPU memory // MemoryBlock::Desc to the beginning of the block; or, if it is a GPU memory
// block, the MetadataCache writes the Meatadata to a std::map in // block, the MetadataCache writes the Meatadata to a std::map in
// the CPU. // the CPU.
void init(MetadataCache* cache, Type t, size_t index, size_t size, void init(MemoryBlock::DescCache* cache, Type t, size_t index, size_t size,
void* left_buddy, void* right_buddy); void* left_buddy, void* right_buddy);
// All these accessors returns fields in the Metadata of the memory // All these accessors returns fields in the MemoryBlock::Desc of the memory
// block. They all need a MetadataCache instance as their first // block. They all need a MetadataCache instance as their first
// parameter because they read the Metadata from the cache. // parameter because they read the MemoryBlock::Desc from the cache.
Type type(const MetadataCache& cache) const; Type type(const MetadataCache& cache) const;
size_t size(const MetadataCache& cache) const; size_t size(const MetadataCache& cache) const;
size_t index(const MetadataCache& cache) const; size_t index(const MetadataCache& cache) const;
...@@ -68,12 +67,11 @@ class MemoryBlock { ...@@ -68,12 +67,11 @@ class MemoryBlock {
void* data() const; void* data() const;
MemoryBlock* metadata() const; MemoryBlock* metadata() const;
private: // MemoryBlock::Desc describes a MemoryBlock.
// Metadata describes a MemoryBlock. struct Desc {
struct Metadata { Desc(MemoryBlock::Type t, size_t i, size_t s, size_t ts, MemoryBlock* l,
Metadata(MemoryBlock::Type t, size_t i, size_t s, size_t ts, MemoryBlock* l, MemoryBlock* r);
MemoryBlock* r); Desc();
Metadata();
// Updates guard_begin and guard_end by hashes of the Metadata object. // Updates guard_begin and guard_end by hashes of the Metadata object.
void update_guards(); void update_guards();
...@@ -94,9 +92,10 @@ class MemoryBlock { ...@@ -94,9 +92,10 @@ class MemoryBlock {
}; };
// A cache for accessing memory block meta-data that may be expensive // A cache for accessing memory block meta-data that may be expensive
// to access directly. This class exists to unify the metadata format // to access directly. This class exists to unify the
// between GPU and CPU allocations. It should be removed when the CPU // MemoryBlock::Desc format between GPU and CPU allocations. It should
// can access all GPU allocations directly via UVM. // be removed when the CPU can access all GPU allocations directly via
// UVM.
class MetadataCache { class MetadataCache {
public: public:
explicit MetadataCache(bool uses_gpu); explicit MetadataCache(bool uses_gpu);
...@@ -105,22 +104,22 @@ class MetadataCache { ...@@ -105,22 +104,22 @@ class MetadataCache {
MetadataCache(const MetadataCache&) = delete; MetadataCache(const MetadataCache&) = delete;
MetadataCache& operator=(const MetadataCache&) = delete; MetadataCache& operator=(const MetadataCache&) = delete;
// Returns the Metadata for a memory block. When MetadataCache is // Returns the MemoryBlock::Desc for a memory block. When MetadataCache is
// used to manage CPU memory, the Metadata resides at the beginning // used to manage CPU memory, the MemoryBlock::Desc resides at the beginning
// of the memory block; when used to manage GPU memory, the // of the memory block; when used to manage GPU memory, the
// Meatadata resides in CPU memory indexed by cache_. // Meatadata resides in CPU memory indexed by cache_.
Metadata load(const MemoryBlock* memory_block) const; MemoryBlock::Desc load(const MemoryBlock* memory_block) const;
// Saves the Metadata of a memory block into the cache. For CPU // Saves the MemoryBlock::Desc of a memory block into the cache. For CPU
// memory block, writes the Metadata to the beginning of the memory // memory block, writes the MemoryBlock::Desc to the beginning of the memory
// block; whereas for GPU memory, writes it to cache_. // block; whereas for GPU memory, writes it to cache_.
void save(MemoryBlock* memory_block, const Metadata& meta_data); void save(MemoryBlock* memory_block, const MemoryBlock::Desc& meta_data);
// For GPU memory block, erases its Metadata from cache_. // For GPU memory block, erases its MemoryBlock::Desc from cache_.
void invalidate(MemoryBlock* memory_block); void invalidate(MemoryBlock* memory_block);
private: private:
typedef std::unordered_map<const MemoryBlock*, Metadata> MetadataMap; typedef std::unordered_map<const MemoryBlock*, MemoryBlock::Desc> MetadataMap;
MetadataMap cache_; MetadataMap cache_;
bool uses_gpu_; bool uses_gpu_;
}; };
......
...@@ -20,8 +20,8 @@ namespace paddle { ...@@ -20,8 +20,8 @@ namespace paddle {
namespace memory { namespace memory {
namespace detail { namespace detail {
Metadata::Metadata(MemoryBlock::Type t, size_t i, size_t s, size_t ts, MemoryBlock::Desc::Desc(MemoryBlock::Type t, size_t i, size_t s, size_t ts,
MemoryBlock* l, MemoryBlock* r) MemoryBlock* l, MemoryBlock* r)
: type(t), : type(t),
index(i), index(i),
size(s), size(s),
...@@ -29,7 +29,7 @@ Metadata::Metadata(MemoryBlock::Type t, size_t i, size_t s, size_t ts, ...@@ -29,7 +29,7 @@ Metadata::Metadata(MemoryBlock::Type t, size_t i, size_t s, size_t ts,
left_buddy(l), left_buddy(l),
right_buddy(r) {} right_buddy(r) {}
Metadata::Metadata() MemoryBlock::Desc::Desc()
: type(MemoryBlock::INVALID_CHUNK), : type(MemoryBlock::INVALID_CHUNK),
index(0), index(0),
size(0), size(0),
...@@ -45,7 +45,7 @@ inline void hash_combine(std::size_t* seed, const T& v) { ...@@ -45,7 +45,7 @@ inline void hash_combine(std::size_t* seed, const T& v) {
(*seed) ^= hasher(v) + 0x9e3779b9 + ((*seed) << 6) + ((*seed) >> 2); (*seed) ^= hasher(v) + 0x9e3779b9 + ((*seed) << 6) + ((*seed) >> 2);
} }
inline size_t hash(const Metadata& metadata, size_t initial_seed) { inline size_t hash(const MemoryBlock::Desc& metadata, size_t initial_seed) {
size_t seed = initial_seed; size_t seed = initial_seed;
hash_combine(&seed, static_cast<size_t>(metadata.type)); hash_combine(&seed, static_cast<size_t>(metadata.type));
...@@ -60,12 +60,12 @@ inline size_t hash(const Metadata& metadata, size_t initial_seed) { ...@@ -60,12 +60,12 @@ inline size_t hash(const Metadata& metadata, size_t initial_seed) {
} // namespace } // namespace
void Metadata::update_guards() { void MemoryBlock::Desc::update_guards() {
guard_begin = hash(this, 1); guard_begin = hash(this, 1);
guard_end = hash(this, 2); guard_end = hash(this, 2);
} }
bool Metadata::check_guards() const { bool MemoryBlock::Desc::check_guards() const {
return guard_begin == hash(this, 1) && guard_end == hash(this, 2); return guard_begin == hash(this, 1) && guard_end == hash(this, 2);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册