-
Notifications
You must be signed in to change notification settings - Fork 122
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
Add support for differentiation of immediate functions #1109
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0638cc9
CladFunction `constexpr`
MihailMihov b13cc77
Process `DiffRequest`s to immediate functions earlier
MihailMihov 34eccfc
Add tests for constexpr and consteval
MihailMihov c213194
Do not assume index of `derivedFn` and `code` parameters
MihailMihov 294aea7
Mark `clad::array_ref` methods as constexpr
MihailMihov 24dbec6
Keep track of whether a request is immediate in `DiffRequest`
MihailMihov b9d5bbf
Fix ForwardMode/NotEnoughArgError.C
MihailMihov 7e7aa96
Fix cuda device host constexpr execute functions declarations
kchristin22 d7dcb34
Store argPtrs of cuda kernels in a std array instead of a vector
kchristin22 21f62ce
Rename `__CLAD_SO_LOADED` to `__CLAD__`
MihailMihov 27f1892
Add page to documentation explaining `clad::immediate_mode`
MihailMihov 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 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 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,71 @@ | ||
Using Clad-generated derivatives in an immediate context | ||
********************************************************** | ||
|
||
The derivatives that Clad generates are valid C++ code, which could in theory | ||
be executed at compile-time (or in an immediate context as the C++ standard | ||
calls it). When a function is differentiated all specifiers, such as | ||
`constexpr` and `consteval` are kept, but it is important to understand the | ||
interface that Clad provides for those derivatives to the user. | ||
|
||
When Clad differentiates a function (e.g. with `clad::differentiate`) the user | ||
receives a `CladFunction`, which contains a function pointer to the generated | ||
derivative, among many other things. Unfortunately due to how the C++ standard | ||
is written handling function pointers in an immediate context is very | ||
restricted and care needs to be taken to not violate the rules or the compiler | ||
won't be able to evaluate our `constexpr`/`consteval` functions during | ||
translation. | ||
|
||
Currently to get a `CladFunction` that is usable in immediate mode the user has | ||
to pass `clad::immediate_mode` to the differentiation function and that removes | ||
the ability to dump the generated derivative, but it may be possible to add | ||
support for that in the future. | ||
|
||
Usage of Clad's immediate mode | ||
================================================ | ||
|
||
The following code snippet shows how one can request Clad to use the immediate | ||
mode for differentiation:: | ||
|
||
#include "clad/Differentiator/Differentiator.h" | ||
|
||
constexpr double fn(double x, double y) { | ||
return (x + y) / 2; | ||
} | ||
|
||
constexpr double fn_test() { | ||
auto dx = clad::differentiate<clad::immediate_mode>(fn, "x"); | ||
|
||
return dx.execute(4, 7); | ||
} | ||
|
||
int main(){ | ||
constexpr double fn_result = fn_test(); | ||
|
||
printf("%.2f\n", fn_result); | ||
} | ||
|
||
It is neccessary both to pass the `clad::immediate_mode` option to | ||
`clad::differentiate` and to keep both the call to `clad::differentiate` and | ||
all it's `.execute(...)` calls in the same immediate context, as the C++ | ||
standard forbids having a function pointer to an immediate function outside of | ||
an immediate context. (It is not possible to do the differentiation and | ||
executions in main as `dx` would contain such a pointer, but `main` is not and | ||
can not be immediate) | ||
|
||
When using `constexpr` there is no easy way to tell whether the functions are | ||
actually being evaluated during translation, so it is a good idea to use either | ||
`consteval` or an `if consteval` (in C++23 and newer) to check if the immediate | ||
contexts are behaving as expected or assign the results to a variable marked | ||
`constexpr` as that would fail if the expression that is being assigned isn't | ||
immediate. | ||
|
||
Use cases supported by Clad's immediate mode | ||
================================================ | ||
|
||
Currently Clad's immediate mode is primarily meant to be used in the forward | ||
mode (`clad::differentiate`) as internal data structures that Clad needs for | ||
differentiating loops, etc. are not yet usable in an immediate context. | ||
|
||
Both `constexpr` and `consteval` are supported as Clad doesn't actually rely on | ||
these specific keywords for its support, but instead uses clang's API to | ||
determine if the functions are immediate and should be differentiated eariler. |
This file contains 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 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 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 |
---|---|---|
|
@@ -65,6 +65,9 @@ struct DiffRequest { | |
/// A flag to enable TBR analysis during reverse-mode differentiation. | ||
bool EnableTBRAnalysis = false; | ||
bool EnableVariedAnalysis = false; | ||
/// A flag specifying whether this differentiation is to be used | ||
/// in immediate contexts. | ||
bool ImmediateMode = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member variable 'ImmediateMode' has public visibility [cppcoreguidelines-non-private-member-variables-in-classes] bool ImmediateMode = false;
^ |
||
/// Puts the derived function and its code in the diff call | ||
void updateCall(clang::FunctionDecl* FD, clang::FunctionDecl* OverloadedFD, | ||
clang::Sema& SemaRef); | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: function 'size' should be marked [[nodiscard]] [modernize-use-nodiscard]