Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use move semantics to push objects on tapes #1249

Merged
merged 1 commit into from
Feb 25, 2025

Conversation

PetroZarytskyi
Copy link
Collaborator

This PR introduces move semantics to clad::pop and, where possible, clad::push. The latter is done when local variables inside loops get promoted to the function scope, e.g.

while (cond) {
   some_type x = init;
   ...
}

In the reverse mode, it becomes

some_type x;
while (cond) {
   clad::push(_tape, x), x = init; // the value will be usefull in the reverse pass, we need to store it
   ...
}

In such cases, the PR introduces std::move:

some_type x;
while (cond) {
   clad::push(_tape, std::move(x)), x = init; // the value is stored right before it's overwritten so we can move it
   ...
}

This is useful for 2 reasons:

  1. Moving objects is move efficient than copying.
  2. This way, we can expect the underlying data (e.g. of stl containers) to stay at the same memory location in the reverse pass.

This PR is opened to make #1247 smaller.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

assert(E && "must be provided");
E = E->IgnoreImplicit();
if (type.isNull())
type = E->getType();
QualType TapeType =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: misleading indentation: statement is indented too deeply [readability-misleading-indentation]

    QualType TapeType =
    ^
Additional context

lib/Differentiator/ReverseModeVisitor.cpp:97: did you mean this line to be inside this 'if'

    if (type.isNull())
    ^

Copy link
Collaborator Author

@PetroZarytskyi PetroZarytskyi Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vgvassilev Is it broken or am I not seeing something?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is the indentation of the entire function.

This commit introduces move semantics to ``clad::pop`` and, where possible, ``clad::push``. The latter is done when local variables inside loops get promoted to the function scope, e.g.
```
while (cond) {
   some_type x = init;
   ...
}
```
In the reverse mode, it becomes
```
some_type x;
while (cond) {
   clad::push(_tape, x), x = init; // the value will be usefull in the reverse pass, we need to store it
   ...
}
```
In such cases, the commit introduces ``std::move``:
```
some_type x;
while (cond) {
   clad::push(_tape, std::move(x)), x = init; // the value is stored right before it's overwritten so we can move it
   ...
}
```
This is useful for 2 reasons:
1) Moving objects is move efficient than copying.
2) This way, we can expect the underlying data (e.g. of stl containers) to stay at the same memory location in the reverse pass.
Copy link

codecov bot commented Feb 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.66%. Comparing base (2e32138) to head (0ce2536).
Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1249   +/-   ##
=======================================
  Coverage   94.66%   94.66%           
=======================================
  Files          51       51           
  Lines        8903     8910    +7     
=======================================
+ Hits         8428     8435    +7     
  Misses        475      475           
Files with missing lines Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 98.09% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 95.71% <100.00%> (+0.01%) ⬆️
Files with missing lines Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 98.09% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 95.71% <100.00%> (+0.01%) ⬆️

Copy link
Owner

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

assert(E && "must be provided");
E = E->IgnoreImplicit();
if (type.isNull())
type = E->getType();
QualType TapeType =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is the indentation of the entire function.

@vgvassilev vgvassilev merged commit 1a24e1b into vgvassilev:master Feb 25, 2025
97 checks passed
@PetroZarytskyi PetroZarytskyi deleted the move branch February 25, 2025 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants