Skip to content

Commit

Permalink
Bugfix: Over allocation of adjoint vector decreased performance for s…
Browse files Browse the repository at this point in the history
…mall tapes.
  • Loading branch information
MaxSagebaum committed Sep 5, 2024
1 parent 762ba76 commit a079c63
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
3 changes: 3 additions & 0 deletions documentation/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion include/codi/tapes/jacobianBaseTape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ namespace codi {
adjoints.beginUse();
}

adjoints.zeroAll();
adjoints.zeroAll(indexManager.get().getLargestCreatedIndex());

if (AdjointsManagement::Automatic == adjointsManagement) {
adjoints.endUse();
Expand Down
4 changes: 2 additions & 2 deletions include/codi/tapes/misc/internalAdjointsInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename Impl>
Expand Down
7 changes: 4 additions & 3 deletions include/codi/tapes/misc/localAdjoints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
7 changes: 4 additions & 3 deletions include/codi/tapes/misc/threadSafeGlobalAdjoints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
5 changes: 3 additions & 2 deletions include/codi/tapes/primalValueBaseTape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit a079c63

Please sign in to comment.