From 67ec31a2e591735ad8e6162b5e1f08c496ad8227 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Wed, 21 Dec 2022 20:54:32 -0500 Subject: [PATCH 1/4] umpire_allocator_impl::deallocate(ptr, n) deallocated n bytes instead of n*sizeof(T) bytes --- src/TiledArray/external/umpire.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/TiledArray/external/umpire.h b/src/TiledArray/external/umpire.h index 9419968585..1bd574bb77 100644 --- a/src/TiledArray/external/umpire.h +++ b/src/TiledArray/external/umpire.h @@ -68,26 +68,28 @@ class umpire_allocator_impl { umpire_allocator_impl(const umpire_allocator_impl& rhs) noexcept : umpalloc_(rhs.umpalloc_) {} - /// allocates um memory using umpire dynamic pool + /// allocates memory using umpire dynamic pool pointer allocate(size_t n) { - pointer result = nullptr; - TA_ASSERT(umpalloc_); + size_t nbytes = n * sizeof(T); + // this, instead of umpalloc_->allocate(n*sizeof(T)), profiles memory use // even if introspection is off + pointer result = nullptr; result = static_cast( - umpalloc_->getAllocationStrategy()->allocate_internal(n * sizeof(T))); + umpalloc_->getAllocationStrategy()->allocate_internal(nbytes)); return result; } - /// deallocate um memory using umpire dynamic pool - void deallocate(pointer ptr, size_t size) { + /// deallocate memory using umpire dynamic pool + void deallocate(pointer ptr, size_t n) { TA_ASSERT(umpalloc_); - // this, instead of umpalloc_->deallocate(ptr, size), profiles mmeory use + const auto nbytes = n * sizeof(T); + // this, instead of umpalloc_->deallocate(ptr, nbytes), profiles mmeory use // even if introspection is off - umpalloc_->getAllocationStrategy()->deallocate_internal(ptr, size); + umpalloc_->getAllocationStrategy()->deallocate_internal(ptr, nbytes); } const umpire::Allocator* umpire_allocator() const { return umpalloc_; } From 0aa5e93607194f96b3315b3b2335167a1c149ca3 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Thu, 22 Dec 2022 11:31:08 -0500 Subject: [PATCH 2/4] umpire_allocator: moar deallocation sanity checks --- src/TiledArray/external/umpire.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/TiledArray/external/umpire.h b/src/TiledArray/external/umpire.h index 1bd574bb77..96f7ac40e3 100644 --- a/src/TiledArray/external/umpire.h +++ b/src/TiledArray/external/umpire.h @@ -87,8 +87,11 @@ class umpire_allocator_impl { void deallocate(pointer ptr, size_t n) { TA_ASSERT(umpalloc_); const auto nbytes = n * sizeof(T); - // this, instead of umpalloc_->deallocate(ptr, nbytes), profiles mmeory use + // this, instead of umpalloc_->deallocate(ptr, nbytes), profiles memory use // even if introspection is off + // N.B. with multiple threads would have to do this test in + // the critical section of Umpire's ThreadSafeAllocator::deallocate + // TA_ASSERT(nbytes <= umpalloc_->getCurrentSize()); umpalloc_->getAllocationStrategy()->deallocate_internal(ptr, nbytes); } From 168f116166a8df8c4f8a7520a4926e40a6240a1d Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Thu, 22 Dec 2022 11:31:36 -0500 Subject: [PATCH 3/4] cleanup --- src/TiledArray/tensor/tensor.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TiledArray/tensor/tensor.h b/src/TiledArray/tensor/tensor.h index 19452eb233..0e8f3851f1 100644 --- a/src/TiledArray/tensor/tensor.h +++ b/src/TiledArray/tensor/tensor.h @@ -107,7 +107,7 @@ class Tensor { std::uninitialized_default_construct_n(ptr, size); // std::uninitialized_value_construct_n(ptr, size); } - auto deleter = [this, allocator = std::move(allocator), + auto deleter = [allocator = std::move(allocator), size](auto&& ptr) mutable { std::destroy_n(ptr, size); allocator.deallocate(ptr, size); @@ -124,7 +124,7 @@ class Tensor { std::uninitialized_default_construct_n(ptr, size); // std::uninitialized_value_construct_n(ptr, size); } - auto deleter = [this, allocator = std::move(allocator), + auto deleter = [allocator = std::move(allocator), size](auto&& ptr) mutable { std::destroy_n(ptr, size); allocator.deallocate(ptr, size * sizeof(T)); From 3b73a9d6d3d0e1fd76a4fbe5f77d9f06b8b44369 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Thu, 22 Dec 2022 11:32:52 -0500 Subject: [PATCH 4/4] TA::Tensor: allocator.deallocate() sometimes invoked with incorrect size --- src/TiledArray/tensor/tensor.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TiledArray/tensor/tensor.h b/src/TiledArray/tensor/tensor.h index 0e8f3851f1..94e59425cf 100644 --- a/src/TiledArray/tensor/tensor.h +++ b/src/TiledArray/tensor/tensor.h @@ -127,7 +127,7 @@ class Tensor { auto deleter = [allocator = std::move(allocator), size](auto&& ptr) mutable { std::destroy_n(ptr, size); - allocator.deallocate(ptr, size * sizeof(T)); + allocator.deallocate(ptr, size); }; this->data_ = std::shared_ptr(ptr, std::move(deleter)); }