-
Notifications
You must be signed in to change notification settings - Fork 145
Support std::list iteration & modification
#2370
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
Open
ax3l
wants to merge
7
commits into
EnzymeAD:main
Choose a base branch
from
ax3l:topic-std-list-insertion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
016b16d
Support `std::list` insertion
ax3l cd6640d
Unit Test
ax3l 9b1793a
Skip `-O0`
ax3l c14abdc
Rename Function
ax3l e063006
Add `test_modify_list`
ax3l 32e6481
FW: Modify Does not Yet Pass
ax3l a2c1b8d
Fix Simple Examples
ax3l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // FIXME: -O0 fails reverse mode (wrong result) https://github.com/EnzymeAD/Enzyme/pull/2370#issuecomment-3046307237 | ||
| // RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O1 %s -S -emit-llvm -o - | %opt - %OPloadEnzyme %enzyme -S | %lli - | ||
| // RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O2 %s -S -emit-llvm -o - | %opt - %OPloadEnzyme %enzyme -S | %lli - | ||
| // RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O3 %s -S -emit-llvm -o - | %opt - %OPloadEnzyme %enzyme -S | %lli - | ||
|
|
||
| #include "../test_utils.h" | ||
|
|
||
| #include <iostream> | ||
| #include <list> | ||
|
|
||
|
|
||
| struct S { | ||
| S(double r) : x(r) {}; | ||
| double x = 0.0; | ||
| }; | ||
|
|
||
| extern double __enzyme_fwddiff(void*, int, std::list<double>&, int, ...); | ||
| extern double __enzyme_autodiff(void*, int, std::list<double>&, int, ...); | ||
| extern double __enzyme_fwddiff(void*, int, std::list<S>&, int, ...); | ||
| extern double __enzyme_autodiff(void*, int, std::list<S>&, int, ...); | ||
|
|
||
|
|
||
| double test_iterate_list(std::list<double>& vals, double const & x) { | ||
| // iterate over list | ||
| double result = 0.0; | ||
| for (const auto& val : vals) { | ||
| result += val * val * x; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| double test_modify_list(std::list<S> & vals, double const & x) { | ||
| // simplified function for comparison: | ||
| //return x*x; | ||
|
|
||
| vals.front().x = x; | ||
ax3l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // iterate over list | ||
| double result = 0.0; | ||
| for (const auto& val : vals) { | ||
| result += val.x * val.x; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| void test_forward_list() { | ||
| // iterate all values of a list | ||
| { | ||
| std::list<double> vals = {1.0, 2.0, 3.0}; | ||
| double x = 3.0; | ||
| double dx = 1.0; | ||
|
|
||
| double ret = __enzyme_fwddiff((void*)test_iterate_list, enzyme_const, vals, enzyme_dup, &x, &dx); | ||
| std::cout << "FW test_iterate_list ret=" << ret << "\n"; | ||
| APPROX_EQ(ret, 14., 1e-10); | ||
| } | ||
|
|
||
| // list is const, then first value set to active | ||
| { | ||
| std::list<S> vals = {S{1.0}, S{2.0}, S{3.0}}; | ||
| std::list<S> vals = {S{0.0}, S{0.0}, S{0.0}}; | ||
| double x = 3.0; | ||
| double dx = 1.0; | ||
|
|
||
| double ret = __enzyme_fwddiff((void*)test_modify_list, enzyme_dup, vals, dvals, enzyme_dup, &x, &dx); | ||
| std::cout << "FW test_modify_list ret=" << ret << " x=" << x << " dx=" << dx << "\n"; | ||
| APPROX_EQ(ret, 6., 1e-10); | ||
| } | ||
| } | ||
|
|
||
| void test_reverse_list() { | ||
| // iterate all values of a list | ||
| { | ||
| std::list<double> vals = {1.0, 2.0, 3.0}; | ||
| double x = 3.0; | ||
| double dx = 0.0; | ||
|
|
||
| __enzyme_autodiff((void*)test_iterate_list, enzyme_const, vals, enzyme_dup, &x, &dx); | ||
| std::cout << "x=" << x << "dx=" << dx << "\n"; | ||
| APPROX_EQ(dx, 14., 1e-10); | ||
| if (dx > 14.1 || dx < 14.9) { fprintf(stderr, "AD test_iterate_list: ret is wrong.\n"); abort(); } | ||
| } | ||
|
|
||
| // list is const, then first value set to active | ||
| { | ||
| std::list<S> vals = {S{1.0}, S{2.0}, S{3.0}}; | ||
| double x = 3.5; | ||
| double dx = 1.0; | ||
|
|
||
| __enzyme_autodiff((void*)test_modify_list, enzyme_const, vals, enzyme_dup, &x, &dx); | ||
| std::cout << "x=" << x << "dx=" << dx << "\n"; | ||
| APPROX_EQ(dx, 6., 1e-10); | ||
| if (dx > 6.1 || dx < 5.9) { fprintf(stderr, "AD test_modify_list: ret is wrong.\n"); abort(); } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| int main() { | ||
| test_forward_list(); | ||
| // FIXME: all wrong so far | ||
| //test_reverse_list(); | ||
| return 0; | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.