diff --git a/paddle/fluid/memory/allocation/allocator_facade.cc b/paddle/fluid/memory/allocation/allocator_facade.cc index 99152607158eb436a70ae37b07655000fbc6c35e..46e1a500e48705b6cceeab566622d232cda445f3 100644 --- a/paddle/fluid/memory/allocation/allocator_facade.cc +++ b/paddle/fluid/memory/allocation/allocator_facade.cc @@ -931,7 +931,13 @@ class AllocatorFacadePrivate { void WrapStatAllocator() { for (auto& pair : allocators_) { - pair.second = std::make_shared(pair.second); + // Now memory stats is only supported for CPU and GPU + const platform::Place& place = pair.first; + if (platform::is_cpu_place(place) || + platform::is_cuda_pinned_place(place) || + platform::is_gpu_place(place)) { + pair.second = std::make_shared(pair.second); + } } } diff --git a/paddle/fluid/memory/allocation/pinned_allocator.cc b/paddle/fluid/memory/allocation/pinned_allocator.cc index 276c6bb0e69b867bef66f6954e78736a279780f2..5e5aea6dab2cc411a7b4570cddac67c605db3202 100644 --- a/paddle/fluid/memory/allocation/pinned_allocator.cc +++ b/paddle/fluid/memory/allocation/pinned_allocator.cc @@ -13,7 +13,7 @@ // limitations under the License. #include "paddle/fluid/memory/allocation/pinned_allocator.h" - +#include "paddle/fluid/memory/stats.h" namespace paddle { namespace memory { namespace allocation { @@ -24,6 +24,7 @@ void CPUPinnedAllocator::FreeImpl(phi::Allocation *allocation) { #else PADDLE_ENFORCE_GPU_SUCCESS(cudaFreeHost(allocation->ptr())); #endif + HOST_MEMORY_STAT_UPDATE(Reserved, 0, -allocation->size()); delete allocation; } phi::Allocation *CPUPinnedAllocator::AllocateImpl(size_t size) { @@ -33,6 +34,7 @@ phi::Allocation *CPUPinnedAllocator::AllocateImpl(size_t size) { #else PADDLE_ENFORCE_GPU_SUCCESS(cudaHostAlloc(&ptr, size, cudaHostAllocPortable)); #endif + HOST_MEMORY_STAT_UPDATE(Reserved, 0, size); return new Allocation(ptr, size, platform::CUDAPinnedPlace()); } } // namespace allocation diff --git a/paddle/fluid/memory/allocation/stat_allocator.h b/paddle/fluid/memory/allocation/stat_allocator.h index 68209bbaabecad81ceb2609a6e0347c5f0be4f09..8b54b961596c2609688ad77de2e354d3092e3067 100644 --- a/paddle/fluid/memory/allocation/stat_allocator.h +++ b/paddle/fluid/memory/allocation/stat_allocator.h @@ -45,11 +45,13 @@ class StatAllocator : public Allocator { phi::Allocator::AllocationPtr allocation = underlying_allocator_->Allocate(size); - if (platform::is_cpu_place(allocation->place())) { - HOST_MEMORY_STAT_UPDATE(Allocated, allocation->place().GetDeviceId(), + const platform::Place& place = allocation->place(); + if (platform::is_cpu_place(place) || + platform::is_cuda_pinned_place(place)) { + HOST_MEMORY_STAT_UPDATE(Allocated, place.GetDeviceId(), allocation->size()); } else { - DEVICE_MEMORY_STAT_UPDATE(Allocated, allocation->place().GetDeviceId(), + DEVICE_MEMORY_STAT_UPDATE(Allocated, place.GetDeviceId(), allocation->size()); } return allocation.release(); diff --git a/paddle/fluid/memory/detail/system_allocator.cc b/paddle/fluid/memory/detail/system_allocator.cc index 06038804e6efeae18370374b3d7dbe9413b02153..e1077d66c54ecd65cbc4c0c2a4bd8bf93c59fd48 100644 --- a/paddle/fluid/memory/detail/system_allocator.cc +++ b/paddle/fluid/memory/detail/system_allocator.cc @@ -211,6 +211,7 @@ void* CUDAPinnedAllocator::Alloc(size_t* index, size_t size) { if (result == gpuSuccess) { *index = 1; // PINNED memory cuda_pinnd_alloc_size_ += size; + HOST_MEMORY_STAT_UPDATE(Reserved, 0, size); return p; } else { LOG(WARNING) << "cudaHostAlloc failed."; @@ -255,6 +256,7 @@ void CUDAPinnedAllocator::Free(void* p, size_t size, size_t index) { err)); } #endif + HOST_MEMORY_STAT_UPDATE(Reserved, 0, -size); } bool CUDAPinnedAllocator::UseGpu() const { return false; }