Automatic Differentiation
 
Loading...
Searching...
No Matches
callback_vari.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CORE_CALLBACK_VARI_HPP
2#define STAN_MATH_REV_CORE_CALLBACK_VARI_HPP
3
5#include <new>
6#include <utility>
7
8namespace stan {
9namespace math {
10namespace internal {
11
12template <typename T, typename F>
13struct callback_vari : public vari_value<T> {
15
16 template <typename S,
18 explicit callback_vari(S&& value, F&& rev_functor)
19 : vari_value<T>(std::move(value), true),
20 rev_functor_(std::forward<F>(rev_functor)) {}
21
22 inline void chain() final { rev_functor_(*this); }
23};
24
25} // namespace internal
26
41template <typename T, typename F>
43 T&& value, F&& functor) {
44 // Captures such as fixed-size Eigen matrices can need more than the 8 byte
45 // alignment of `vari_base::operator new`.
47 return ::new (ChainableStack::instance_->memalloc_.alloc(sizeof(vari_t),
48 alignof(vari_t)))
49 vari_t(std::move(value), std::forward<F>(functor));
50}
51
65template <typename T, typename F>
66inline var_value<plain_type_t<T>> make_callback_var(T&& value, F&& functor) {
68 make_callback_vari(std::move(value), std::forward<F>(functor)));
69}
70
71} // namespace math
72} // namespace stan
73
74#endif
require_t< std::is_same< std::decay_t< T >, std::decay_t< S > > > require_same_t
Require types T and S satisfies std::is_same.
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
internal::callback_vari< plain_type_t< T >, F > * make_callback_vari(T &&value, F &&functor)
Creates a new vari with given value and a callback that implements the reverse pass (chain).
typename plain_type< std::decay_t< T > >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
STL namespace.
static thread_local AutodiffStackStorage * instance_
callback_vari(S &&value, F &&rev_functor)