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

[WIP] adds contributor guides to the docs #2350

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added section on make_callback_ptr
  • Loading branch information
bbbales2 committed Feb 5, 2021
commit e6ac525a69803f597032de8228976ff60a91e3de
24 changes: 21 additions & 3 deletions doxygen/contributor_help_pages/common_pitfalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,27 @@ The general rules to follow for passing values to a function are:
2. If you are writing a function for reverse mode, pass values by `const&`
3. In prim, if you are confident and working with larger types, use perfect forwarding to pass values that can be moved from. Otherwise simply pass values by `const&`.

### Copying non-arena variables to lambdas used in the reverse pass
### Copying non-arena variables to lambdas used in the reverse pass (`make_callback_ptr`)

When possible, non-arena variables should be copied to the arena to be
used in the reverse pass. The two tools for that are `arena_t<T>` and
`to_arena`.

When these tools do not work, there is `make_callback_ptr(x)`.
`make_callback_ptr(x)` constructs a copy of the argument `x` and returns
a pointer to that copy. The copy of `x` will only be destructed when the
enclosing `recover_memory` is called.

The pointer is cheap to copy around and is safe to copy into lambdas for
`reverse_pass_callback` and `make_callback_var`.

As an example, see the implementation of `mdivide_left`
[here](https://github.com/stan-dev/math/blob/develop/stan/math/rev/fun/mdivide_left.hpp)
where `make_callback_ptr` is used to save the result of an Eigen
Householder QR decomposition for use in the reverse pass.

The implementation is in
[stan/math/rev/core/chainable_object.hpp](https://github.com/stan-dev/math/blob/develop/stan/math/rev/core/chainable_object.hpp)

### Returning arena types

Expand All @@ -223,8 +243,6 @@ The general rules to follow for passing values to a function are:

## Handy tricks

### `make_callback_ptr`

### `forward_as`

### Nested Stacks
Expand Down