From a079c63a48c38d620c4f544ba4af9f6b98f93cdd Mon Sep 17 00:00:00 2001 From: Max Sagebaum Date: Wed, 4 Sep 2024 15:25:37 +0200 Subject: [PATCH 1/2] Bugfix: Over allocation of adjoint vector decreased performance for small tapes. --- documentation/Changelog.md | 3 +++ include/codi/tapes/jacobianBaseTape.hpp | 2 +- include/codi/tapes/misc/internalAdjointsInterface.hpp | 4 ++-- include/codi/tapes/misc/localAdjoints.hpp | 7 ++++--- include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp | 7 ++++--- include/codi/tapes/primalValueBaseTape.hpp | 5 +++-- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/documentation/Changelog.md b/documentation/Changelog.md index fb875945..001d3119 100644 --- a/documentation/Changelog.md +++ b/documentation/Changelog.md @@ -8,6 +8,9 @@ Changelog {#Changelog} * Edit identifiers in recorded tapes. * Preaccumulation with a local adjoint vector after tape editing. + - Bugfix: + * Improved performance for tape reset of small tapes. + ### v 2.2.0 - 2024-01-30 - Features: * New helper for adding Enzyme-generated derivative functions to the tape. See \ref Example_24_Enzyme_external_function_helper. diff --git a/include/codi/tapes/jacobianBaseTape.hpp b/include/codi/tapes/jacobianBaseTape.hpp index 4da62ed1..c03af0d2 100644 --- a/include/codi/tapes/jacobianBaseTape.hpp +++ b/include/codi/tapes/jacobianBaseTape.hpp @@ -471,7 +471,7 @@ namespace codi { adjoints.beginUse(); } - adjoints.zeroAll(); + adjoints.zeroAll(indexManager.get().getLargestCreatedIndex()); if (AdjointsManagement::Automatic == adjointsManagement) { adjoints.endUse(); diff --git a/include/codi/tapes/misc/internalAdjointsInterface.hpp b/include/codi/tapes/misc/internalAdjointsInterface.hpp index d4d39242..774ab532 100644 --- a/include/codi/tapes/misc/internalAdjointsInterface.hpp +++ b/include/codi/tapes/misc/internalAdjointsInterface.hpp @@ -110,8 +110,8 @@ namespace codi { /// Ensure that identifiers up to newSize can be passed to operator[] without error. CODI_NO_INLINE void resize(Identifier const& newSize); - /// Set all adjoint variables to Gradient(). - CODI_INLINE void zeroAll(); + /// Set all adjoint variables up to and including maxIndex to Gradient(). + CODI_INLINE void zeroAll(Identifier const& maxIndex); /// Swap two sets of adjoint variables. Internally, declares usage of the adjoints. template diff --git a/include/codi/tapes/misc/localAdjoints.hpp b/include/codi/tapes/misc/localAdjoints.hpp index 0eaf0b1a..1b7ae0ff 100644 --- a/include/codi/tapes/misc/localAdjoints.hpp +++ b/include/codi/tapes/misc/localAdjoints.hpp @@ -95,9 +95,10 @@ namespace codi { } /// \copydoc InternalAdjointsInterface::zeroAll - CODI_INLINE void zeroAll() { - for (Gradient& gradient : adjoints) { - gradient = Gradient(); + CODI_INLINE void zeroAll(Identifier const& maxIndex) { + Identifier maxSize = std::min(maxIndex + 1, (Identifier)adjoints.size()); + for (Identifier i = 0; i < maxSize; i += 1) { + adjoints[i] = Gradient(); } } diff --git a/include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp b/include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp index 41070d18..88de3a70 100644 --- a/include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp +++ b/include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp @@ -109,9 +109,10 @@ namespace codi { } /// \copydoc InternalAdjointsInterface::zeroAll - CODI_INLINE void zeroAll() { - for (Gradient& gradient : adjoints) { - gradient = Gradient(); + CODI_INLINE void zeroAll(Identifier const& maxIndex) { + Identifier maxSize = std::min(maxIndex + 1, (Identifier)adjoints.size()); + for (Identifier i = 0; i < maxSize; i += 1) { + adjoints[i] = Gradient(); } } diff --git a/include/codi/tapes/primalValueBaseTape.hpp b/include/codi/tapes/primalValueBaseTape.hpp index 995dc0b4..aec9a201 100644 --- a/include/codi/tapes/primalValueBaseTape.hpp +++ b/include/codi/tapes/primalValueBaseTape.hpp @@ -522,8 +522,9 @@ namespace codi { CODI_INLINE void clearAdjoints(AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) { CODI_UNUSED(adjointsManagement); - for (Gradient& gradient : adjoints) { - gradient = Gradient(); + size_t maxSize = std::min((size_t)indexManager.get().getLargestCreatedIndex() + 1, adjoints.size()); + for (size_t i = 0; i < maxSize; i += 1) { + adjoints[i] = Gradient(); } } From 01c1ba505492345b9d80ba6ddc0414e0646a9bad Mon Sep 17 00:00:00 2001 From: Max Sagebaum Date: Tue, 10 Sep 2024 09:10:31 +0200 Subject: [PATCH 2/2] Applied suggested change from Johannes. --- include/codi/tapes/misc/internalAdjointsInterface.hpp | 4 ++-- include/codi/tapes/misc/localAdjoints.hpp | 4 ++-- include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/codi/tapes/misc/internalAdjointsInterface.hpp b/include/codi/tapes/misc/internalAdjointsInterface.hpp index 774ab532..fdb63b22 100644 --- a/include/codi/tapes/misc/internalAdjointsInterface.hpp +++ b/include/codi/tapes/misc/internalAdjointsInterface.hpp @@ -110,8 +110,8 @@ namespace codi { /// Ensure that identifiers up to newSize can be passed to operator[] without error. CODI_NO_INLINE void resize(Identifier const& newSize); - /// Set all adjoint variables up to and including maxIndex to Gradient(). - CODI_INLINE void zeroAll(Identifier const& maxIndex); + /// Set all adjoint variables up to and including maxIdentifier to Gradient(). + CODI_INLINE void zeroAll(Identifier const& maxIdentifier); /// Swap two sets of adjoint variables. Internally, declares usage of the adjoints. template diff --git a/include/codi/tapes/misc/localAdjoints.hpp b/include/codi/tapes/misc/localAdjoints.hpp index 1b7ae0ff..68589db2 100644 --- a/include/codi/tapes/misc/localAdjoints.hpp +++ b/include/codi/tapes/misc/localAdjoints.hpp @@ -95,8 +95,8 @@ namespace codi { } /// \copydoc InternalAdjointsInterface::zeroAll - CODI_INLINE void zeroAll(Identifier const& maxIndex) { - Identifier maxSize = std::min(maxIndex + 1, (Identifier)adjoints.size()); + CODI_INLINE void zeroAll(Identifier const& maxIdentifier) { + Identifier maxSize = std::min(maxIdentifier + 1, (Identifier)adjoints.size()); for (Identifier i = 0; i < maxSize; i += 1) { adjoints[i] = Gradient(); } diff --git a/include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp b/include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp index 88de3a70..843e88ba 100644 --- a/include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp +++ b/include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp @@ -109,8 +109,8 @@ namespace codi { } /// \copydoc InternalAdjointsInterface::zeroAll - CODI_INLINE void zeroAll(Identifier const& maxIndex) { - Identifier maxSize = std::min(maxIndex + 1, (Identifier)adjoints.size()); + CODI_INLINE void zeroAll(Identifier const& maxIdentifier) { + Identifier maxSize = std::min(maxIdentifier + 1, (Identifier)adjoints.size()); for (Identifier i = 0; i < maxSize; i += 1) { adjoints[i] = Gradient(); }