diff --git a/paddle/fluid/memory/detail/CMakeLists.txt b/paddle/fluid/memory/detail/CMakeLists.txt index 1636b120ae3d94e7c522d9236cb5198f6f658187..c725dba5e98c200c2542d97cb8f53a938f6b614a 100644 --- a/paddle/fluid/memory/detail/CMakeLists.txt +++ b/paddle/fluid/memory/detail/CMakeLists.txt @@ -1,4 +1,4 @@ -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}) nv_library(system_allocator SRCS system_allocator.cc DEPS gflags cpu_info gpu_info) diff --git a/paddle/fluid/memory/detail/memory_block.cc b/paddle/fluid/memory/detail/memory_block.cc index f744450f480b48a149d4e8adfa1eb68d6cf83c91..79f65f1eb8beec7fb06811c693fe544a6b2eec29 100644 --- a/paddle/fluid/memory/detail/memory_block.cc +++ b/paddle/fluid/memory/detail/memory_block.cc @@ -21,9 +21,10 @@ namespace detail { void MemoryBlock::init(MetadataCache* cache, Type t, size_t index, size_t size, void* left_buddy, void* right_buddy) { - cache->save(this, Metadata(t, index, size - sizeof(Metadata), size, - static_cast(left_buddy), - static_cast(right_buddy))); + cache->save( + this, MemoryBlock::Desc(t, index, size - sizeof(MemoryBlock::Desc), size, + static_cast(left_buddy), + static_cast(right_buddy))); } MemoryBlock::Type MemoryBlock::type(MetadataCache& cache) const { @@ -63,7 +64,7 @@ void MemoryBlock::split(MetadataCache* cache, size_t size) { PADDLE_ASSERT(total_size(*cache) >= size); // 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; } @@ -78,13 +79,13 @@ void MemoryBlock::split(MetadataCache* cache, size_t size) { // Write the metadata for the new block auto new_block_right_buddy = metadata.right_buddy; - cache->save( - static_cast(right_partition), - Metadata(FREE_CHUNK, index(*cache), remaining_size - sizeof(Metadata), - remaining_size, this, new_block_right_buddy)); + cache->save(static_cast(right_partition), + MemoryBlock::Desc(FREE_CHUNK, index(*cache), + remaining_size - sizeof(MemoryBlock::Desc), + remaining_size, this, new_block_right_buddy)); metadata.right_buddy = static_cast(right_partition); - metadata.size = size - sizeof(Metadata); + metadata.size = size - sizeof(MemoryBlock::Desc); metadata.total_size = size; cache->save(this, metadata); @@ -122,7 +123,8 @@ void MemoryBlock::merge(MetadataCache* cache, MemoryBlock* right_buddy) { metadata.total_size += right_buddy->total_size(*cache); 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) { @@ -139,12 +141,14 @@ void MemoryBlock::set_type(MetadataCache* cache, Type t) { } void* MemoryBlock::data() const { - return const_cast(reinterpret_cast(this)) + 1; + return const_cast( + reinterpret_cast(this)) + + 1; } MemoryBlock* MemoryBlock::metadata() const { return const_cast(reinterpret_cast( - reinterpret_cast(this) - 1)); + reinterpret_cast(this) - 1)); } } // namespace detail diff --git a/paddle/fluid/memory/detail/memory_block.h b/paddle/fluid/memory/detail/memory_block.h index 5e83a2f8991b224833e6f1997a2563e98564264f..f0a06577a507bb2315c7c26c78dc0a4e437f786c 100644 --- a/paddle/fluid/memory/detail/memory_block.h +++ b/paddle/fluid/memory/detail/memory_block.h @@ -23,9 +23,8 @@ namespace detail { class MetadataCache; // MemoryBlock represents Each allocated memory block, which contains -// Metadata and the payload. -class MemoryBlock { - public: +// MemoryBlock::Desc and the payload. +struct MemoryBlock { enum Type { FREE_CHUNK, // memory is free and idle ARENA_CHUNK, // memory is being occupied @@ -33,17 +32,17 @@ class MemoryBlock { 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 - // 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 // 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); - // 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 - // 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; size_t size(const MetadataCache& cache) const; size_t index(const MetadataCache& cache) const; @@ -68,12 +67,11 @@ class MemoryBlock { void* data() const; MemoryBlock* metadata() const; - private: - // Metadata describes a MemoryBlock. - struct Metadata { - Metadata(MemoryBlock::Type t, size_t i, size_t s, size_t ts, MemoryBlock* l, - MemoryBlock* r); - Metadata(); + // MemoryBlock::Desc describes a MemoryBlock. + struct Desc { + Desc(MemoryBlock::Type t, size_t i, size_t s, size_t ts, MemoryBlock* l, + MemoryBlock* r); + Desc(); // Updates guard_begin and guard_end by hashes of the Metadata object. void update_guards(); @@ -94,9 +92,10 @@ class MemoryBlock { }; // A cache for accessing memory block meta-data that may be expensive -// to access directly. This class exists to unify the metadata format -// between GPU and CPU allocations. It should be removed when the CPU -// can access all GPU allocations directly via UVM. +// 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. class MetadataCache { public: explicit MetadataCache(bool uses_gpu); @@ -105,22 +104,22 @@ class MetadataCache { MetadataCache(const MetadataCache&) = delete; MetadataCache& operator=(const MetadataCache&) = delete; - // Returns the Metadata for a memory block. When MetadataCache is - // used to manage CPU memory, the Metadata resides at the beginning + // Returns the MemoryBlock::Desc for a memory block. When MetadataCache is + // used to manage CPU memory, the MemoryBlock::Desc resides at the beginning // of the memory block; when used to manage GPU memory, the // 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 - // memory block, writes the Metadata to the beginning of the memory + // 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 // 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); private: - typedef std::unordered_map MetadataMap; + typedef std::unordered_map MetadataMap; MetadataMap cache_; bool uses_gpu_; }; diff --git a/paddle/fluid/memory/detail/meta_data.cc b/paddle/fluid/memory/detail/memory_block_desc.cc similarity index 84% rename from paddle/fluid/memory/detail/meta_data.cc rename to paddle/fluid/memory/detail/memory_block_desc.cc index 02f5235d14fd06c031ac89ca0e5de3d0043c7ed8..0ad2ef4d7cae9034c38b4ae53483b3db49d676cc 100644 --- a/paddle/fluid/memory/detail/meta_data.cc +++ b/paddle/fluid/memory/detail/memory_block_desc.cc @@ -20,8 +20,8 @@ namespace paddle { namespace memory { namespace detail { -Metadata::Metadata(MemoryBlock::Type t, size_t i, size_t s, size_t ts, - MemoryBlock* l, MemoryBlock* r) +MemoryBlock::Desc::Desc(MemoryBlock::Type t, size_t i, size_t s, size_t ts, + MemoryBlock* l, MemoryBlock* r) : type(t), index(i), size(s), @@ -29,7 +29,7 @@ Metadata::Metadata(MemoryBlock::Type t, size_t i, size_t s, size_t ts, left_buddy(l), right_buddy(r) {} -Metadata::Metadata() +MemoryBlock::Desc::Desc() : type(MemoryBlock::INVALID_CHUNK), index(0), size(0), @@ -45,7 +45,7 @@ inline void hash_combine(std::size_t* seed, const T& v) { (*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; hash_combine(&seed, static_cast(metadata.type)); @@ -60,12 +60,12 @@ inline size_t hash(const Metadata& metadata, size_t initial_seed) { } // namespace -void Metadata::update_guards() { +void MemoryBlock::Desc::update_guards() { guard_begin = hash(this, 1); 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); }