Automatic Differentiation
 
Loading...
Searching...
No Matches
scoped_chainablestack.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CORE_SCOPED_CHAINABLESTACK_HPP
2#define STAN_MATH_REV_CORE_SCOPED_CHAINABLESTACK_HPP
3
5
6#include <mutex>
7#include <stdexcept>
8#include <thread>
9
10namespace stan {
11namespace math {
12
34
38
39 explicit activate_scope(ScopedChainableStack& scoped_stack)
40 : parent_stack_(ChainableStack::instance_),
41 scoped_stack_(scoped_stack) {
42 if (!scoped_stack_.local_stack_mutex_.try_lock()) {
43 throw std::logic_error{"Cannot recurse same instance scoped stacks."};
44 }
46 }
47
51 }
52 };
53
54 public:
56
67 template <typename F, typename... Args>
68 decltype(auto) execute(F&& f, Args&&... args) {
69 const activate_scope active_scope(*this);
70 return std::forward<F>(f)(std::forward<Args>(args)...);
71 }
72
73 // Prevent undesirable operations
76};
77
78} // namespace math
79} // namespace stan
80#endif
decltype(auto) execute(F &&f, Args &&... args)
Execute in the current thread a function and write the AD tape to local_stack_ of this instance.
ScopedChainableStack & operator=(const ScopedChainableStack &)=delete
ScopedChainableStack(const ScopedChainableStack &)=delete
ChainableStack::AutodiffStackStorage local_stack_
The AD tape of reverse mode AD is by default stored globally within the process (or thread).
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
static thread_local AutodiffStackStorage * instance_
This struct always provides access to the autodiff stack using the singleton pattern.
ChainableStack::AutodiffStackStorage * parent_stack_