Skip to content

Commit 981e21c

Browse files
MaxSagebaumhsutter
andauthored
Feature/autodiff (#1413)
* Update for regex 20 lookbehind test. Removed cpp file and added cpp2 file. * Fix for name lookup issues with MSVC. * Add missing files. * Refactor of autodiff with generalized traversal. Added test for autodiff. * Handling of expression lists. * Handling of expresson terms. * Handling of function calls. * Added special handling of math functions. * Added declarations and statements to simple_traversal. * Handling if/else statements. * Added handling of direct return. * Stub handling of value declarations. * Added example for non differential variable. * Added handling of while and do while loops. * Handling of for loops and added special functions. * Unified function call handling. * Proper handling of initializer expressions. * Fix initializer problem. * Suffix can now be user defined. * Added second order test. * Remove initialization order workaround. * Taylor polynomial propagation implementation. * Basic handling of higher order derivatives and handling of add and minus. * Added handling for multiply and division. * Higher order handling for special functions. * Remaining tests for higher order derivatives. * Declaration lookup and lookup of function return name. * Basic changes for adding new things for the differentiation. * Moved handling of types and functions to autodiff_declaration_handler. * Added handling for differentiation of symbols outside of metafunction type declaration. * Basic differentation for type and namespace value declarations. * Refactor of autodiff_expression_handler. The expression handler no longer generates the assignments. It stores the primal and forward expression. The assignments can now be generated with helper functions or by accessing the expressions. * Handling of member access. * Moved assignment handling code to proper traverse function. * Added type differentiation for types without member functions. * Handling of member function calls. First basic setup for declared variable lookup. * Handling of prefix + and -. * Basic preperations for reverse mode AD. * Refactor of diff string to structure. Currently placeholder that defaults to the forward derivatives. * Reverse handling of function declaration. * Reverse differentiation of additive expressions. * Basic handling of multiplication and division. * Fix for to_string of expressions. * Activity analysis for variables and functions. * Update for tests and acitivity analysis. * Added tests for combined binary expressions. * Handling of special functions for reverse mode. * Added handling of function calls for reverse. * Handling of statement parameters for loops in forward mode. * Reverse handling of for loops. * Temp. * Build clean with GCC 10 and Clang 21, and update regression test results * Bugfix for passive variables in addition or subtraction statements. * Add an AD unit test --------- Co-authored-by: Herb Sutter <[email protected]>
1 parent ced2de1 commit 981e21c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+17660
-2859
lines changed

build_h2.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ cd source
33
cppfront reflect.h2 -verb %1
44
cd ..\include
55
cppfront cpp2regex.h2 -verb %1
6+
cppfront cpp2taylor.h2 -verb %1
67
cd..

include/cpp2ad_stack.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef CPP2_CPP2AD_STACK_H
2+
#define CPP2_CPP2AD_STACK_H
3+
4+
#include <vector>
5+
6+
namespace cpp2 {
7+
8+
struct ad_stack {
9+
10+
template<typename T>
11+
static void push(T const& v) {
12+
std::vector<T>& stack = get_stack<T>();
13+
14+
stack.push_back(v);
15+
}
16+
17+
template<typename T>
18+
static T pop() {
19+
std::vector<T>& stack = get_stack<T>();
20+
21+
T v = stack.back();
22+
stack.pop_back();
23+
24+
return v;
25+
}
26+
27+
private:
28+
29+
template<typename T>
30+
static std::vector<T>& get_stack() {
31+
static std::vector<T> stack = {};
32+
33+
return stack;
34+
}
35+
};
36+
} // cpp2 namespace
37+
38+
#endif // CPP2_CPP2AD_STACK_H

include/cpp2regex.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ template<typename CharT, typename Iter, int max_groups> [[nodiscard]] auto make_
261261
#line 184 "cpp2regex.h2"
262262
// Helpers for creating wrappers of the iterators.
263263
//
264-
template <typename Iter> [[nodiscard]] auto make_forward_iterator(Iter const& pos) -> auto;
265-
template <typename Iter> [[nodiscard]] auto make_forward_iterator(std::reverse_iterator<Iter> const& pos) -> auto;
266-
template <typename Iter> [[nodiscard]] auto make_reverse_iterator(Iter const& pos) -> auto;
267-
template<typename Iter> [[nodiscard]] auto make_reverse_iterator(std::reverse_iterator<Iter> const& pos) -> auto;
264+
template <typename Iter> [[nodiscard]] auto cpp2_make_forward_iterator(Iter const& pos) -> auto;
265+
template<typename Iter> [[nodiscard]] auto cpp2_make_forward_iterator(std::reverse_iterator<Iter> const& pos) -> auto;
266+
template <typename Iter> [[nodiscard]] auto cpp2_make_reverse_iterator(Iter const& pos) -> auto;
267+
template<typename Iter> [[nodiscard]] auto cpp2_make_reverse_iterator(std::reverse_iterator<Iter> const& pos) -> auto;
268268

269269
#line 192 "cpp2regex.h2"
270270
// End function that returns a valid match.
@@ -927,13 +927,13 @@ template<typename CharT, typename Iter, int max_groups> [[nodiscard]] auto make_
927927
}
928928

929929
#line 186 "cpp2regex.h2"
930-
template <typename Iter> [[nodiscard]] auto make_forward_iterator(Iter const& pos) -> auto { return pos; }
930+
template <typename Iter> [[nodiscard]] auto cpp2_make_forward_iterator(Iter const& pos) -> auto { return pos; }
931931
#line 187 "cpp2regex.h2"
932-
template <typename Iter> [[nodiscard]] auto make_forward_iterator(std::reverse_iterator<Iter> const& pos) -> auto { return CPP2_UFCS(base)(pos); }
932+
template<typename Iter> [[nodiscard]] auto cpp2_make_forward_iterator(std::reverse_iterator<Iter> const& pos) -> auto { return CPP2_UFCS(base)(pos); }
933933
#line 188 "cpp2regex.h2"
934-
template <typename Iter> [[nodiscard]] auto make_reverse_iterator(Iter const& pos) -> auto { return std::make_reverse_iterator(pos); }
934+
template <typename Iter> [[nodiscard]] auto cpp2_make_reverse_iterator(Iter const& pos) -> auto { return std::make_reverse_iterator(pos); }
935935
#line 189 "cpp2regex.h2"
936-
template<typename Iter> [[nodiscard]] auto make_reverse_iterator(std::reverse_iterator<Iter> const& pos) -> auto { return pos; }
936+
template<typename Iter> [[nodiscard]] auto cpp2_make_reverse_iterator(std::reverse_iterator<Iter> const& pos) -> auto { return pos; }
937937

938938
#line 196 "cpp2regex.h2"
939939
[[nodiscard]] auto true_end_func::operator()(auto const& cur, auto& ctx) const& -> decltype(auto) { return ctx.pass(cur); }
@@ -1153,7 +1153,7 @@ template<typename CharT, bool match_new_line> [[nodiscard]] auto line_start_toke
11531153
#line 575 "cpp2regex.h2"
11541154
template<typename CharT, bool positive> [[nodiscard]] auto lookahead_token_matcher(auto const& cur, auto& ctx, auto const& func) -> bool
11551155
{
1156-
auto r {func(make_forward_iterator(cur), make_forward_match_context(ctx), true_end_func())};
1156+
auto r {func(cpp2_make_forward_iterator(cur), make_forward_match_context(ctx), true_end_func())};
11571157
if (!(positive)) {
11581158
r.matched = !(r.matched);
11591159
}
@@ -1164,7 +1164,7 @@ template<typename CharT, bool positive> [[nodiscard]] auto lookahead_token_match
11641164
#line 589 "cpp2regex.h2"
11651165
template<typename CharT, bool positive> [[nodiscard]] auto lookbehind_token_matcher(auto const& cur, auto& ctx, auto const& func) -> bool
11661166
{
1167-
auto r {func(make_reverse_iterator(cur), make_reverse_match_context(ctx), true_end_func())};
1167+
auto r {func(cpp2_make_reverse_iterator(cur), make_reverse_match_context(ctx), true_end_func())};
11681168
if (!(positive)) {
11691169
r.matched = !(r.matched);
11701170
}

include/cpp2regex.h2

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ make_reverse_match_context: <CharT, Iter, max_groups: int> (inout ctx: reverse_m
183183

184184
// Helpers for creating wrappers of the iterators.
185185
//
186-
make_forward_iterator: <Iter> (pos: Iter) -> _ = pos;
187-
make_forward_iterator: <Iter> (pos: std::reverse_iterator<Iter>) -> _ = pos.base();
188-
make_reverse_iterator: <Iter> (pos: Iter) -> _ = std::make_reverse_iterator(pos);
189-
make_reverse_iterator: <Iter> (pos: std::reverse_iterator<Iter>) -> _ = pos;
186+
cpp2_make_forward_iterator: <Iter> (pos: Iter) -> _ = pos;
187+
cpp2_make_forward_iterator: <Iter> (pos: std::reverse_iterator<Iter>) -> _ = pos.base();
188+
cpp2_make_reverse_iterator: <Iter> (pos: Iter) -> _ = std::make_reverse_iterator(pos);
189+
cpp2_make_reverse_iterator: <Iter> (pos: std::reverse_iterator<Iter>) -> _ = pos;
190190

191191

192192
// End function that returns a valid match.
@@ -574,7 +574,7 @@ line_start_token_matcher: <CharT, match_new_line: bool> (cur, inout ctx) -> bool
574574
//
575575
lookahead_token_matcher: <CharT, positive: bool> (cur, inout ctx, func) -> bool =
576576
{
577-
r := func(make_forward_iterator(cur), make_forward_match_context(ctx), true_end_func());
577+
r := func(cpp2_make_forward_iterator(cur), make_forward_match_context(ctx), true_end_func());
578578
if !positive {
579579
r.matched = !r.matched;
580580
}
@@ -588,7 +588,7 @@ lookahead_token_matcher: <CharT, positive: bool> (cur, inout ctx, func) -> bool
588588
//
589589
lookbehind_token_matcher: <CharT, positive: bool> (cur, inout ctx, func) -> bool =
590590
{
591-
r := func(make_reverse_iterator(cur), make_reverse_match_context(ctx), true_end_func());
591+
r := func(cpp2_make_reverse_iterator(cur), make_reverse_match_context(ctx), true_end_func());
592592
if !positive {
593593
r.matched = !r.matched;
594594
}

0 commit comments

Comments
 (0)