From e534199bfc7decea9335ed5159b0c5406dacb686 Mon Sep 17 00:00:00 2001 From: Deepanshu <90187896+Deepanshu9376@users.noreply.github.com> Date: Thu, 21 Mar 2024 21:09:07 +0530 Subject: [PATCH] Update Tape.h issue #793 --- include/clad/Differentiator/Tape.h | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/include/clad/Differentiator/Tape.h b/include/clad/Differentiator/Tape.h index 50a7ac0d9..b6ed99377 100644 --- a/include/clad/Differentiator/Tape.h +++ b/include/clad/Differentiator/Tape.h @@ -115,22 +115,25 @@ namespace clad { } } /// Initial capacity (allocated whenever a value is pushed into empty tape). - constexpr static std::size_t _init_capacity = 32; - CUDA_HOST_DEVICE void grow() { - // If empty, use initial capacity. - if (!_capacity) +constexpr static std::size_t _init_capacity = 32; +CUDA_HOST_DEVICE void grow() { + // If empty, use initial capacity. + if (!_capacity) _capacity = _init_capacity; - else + else // Double the capacity on each reallocation. _capacity *= 2; - T* new_data = AllocateRawStorage(_capacity); - if (!new_data) { - // clean up the memory mess just in case! + T* new_data = AllocateRawStorage(_capacity); + + if (!new_data) { + // Clean up the memory mess just in case! destroy(begin(), end()); printf("Allocation failure during tape resize! Aborting.\n"); trap(EXIT_FAILURE); - } + } +} + // Move values from old storage to the new storage. Should call move // constructors on non-trivial types, otherwise is expected to use