Automatic Differentiation
 
Loading...
Searching...
No Matches
autodiffstackstorage.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CORE_AUTODIFFSTACKSTORAGE_HPP
2#define STAN_MATH_REV_CORE_AUTODIFFSTACKSTORAGE_HPP
3
5#include <vector>
6
7namespace stan {
8namespace math {
9
10// Internal macro used to modify global pointer definition to the
11// global AD instance.
12#ifdef STAN_THREADS
13// Whenever STAN_THREADS is set a TLS keyword is used. For reasons
14// explained below we use the GNU compiler extension __thread if
15// supported by the compiler while the generic thread_local C++11
16// keyword is used otherwise.
17#ifdef __GNUC__
18#define STAN_THREADS_DEF __thread
19#else
20#define STAN_THREADS_DEF thread_local
21#endif
22#else
23// In case STAN_THREADS is not set, then no modifier is needed.
24#define STAN_THREADS_DEF
25#endif
26
88template <typename ChainableT, typename ChainableAllocT>
92
95 if (own_instance_) {
96 delete instance_;
97 instance_ = nullptr;
98 }
99 }
100
103
104 std::vector<ChainableT *> var_stack_;
105 std::vector<ChainableT *> var_nochain_stack_;
106 std::vector<ChainableAllocT *> var_alloc_stack_;
108
109 // nested positions
110 std::vector<size_t> nested_var_stack_sizes_;
113
114 STAN_STRONG_INLINE void push_var_stack(ChainableT *vi) {
115 push(var_stack_, vi);
116 }
119 }
120
132 std::vector<ChainableT *> &stack, ChainableT *vi) {
133 if (likely(stack.size() != stack.capacity())) {
134 stack.push_back(vi);
135 } else {
136 grow_and_push(stack, vi);
137 }
138 }
139 STAN_COLD_PATH static void grow_and_push(std::vector<ChainableT *> &stack,
140 ChainableT *vi) {
141 stack.push_back(vi);
142 }
143 };
144
147
149
150 private:
151 static bool init() {
152 static STAN_THREADS_DEF bool is_initialized = false;
153 if (!is_initialized) {
154 is_initialized = true;
156 return true;
157 }
158 if (!instance_) {
159 is_initialized = true;
161 return true;
162 }
163 return false;
164 }
165
167};
168
169template <typename ChainableT, typename ChainableAllocT>
171 typename AutodiffStackSingleton<ChainableT,
172 ChainableAllocT>::AutodiffStackStorage
174
175} // namespace math
176} // namespace stan
177#endif
#define STAN_THREADS_DEF
An instance of this class provides a memory pool through which blocks of raw memory may be allocated ...
#define STAN_STRONG_INLINE
#define likely(x)
#define STAN_HOT_PATH
#define STAN_COLD_PATH
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
static void grow_and_push(std::vector< ChainableT * > &stack, ChainableT *vi)
static void push(std::vector< ChainableT * > &stack, ChainableT *vi)
Once we create memory in stack we do not release it till the end of the program.
AutodiffStackStorage & operator=(const AutodiffStackStorage &)=delete
AutodiffStackSingleton(AutodiffStackSingleton_t const &)=delete
AutodiffStackSingleton & operator=(const AutodiffStackSingleton_t &)=delete
static thread_local AutodiffStackStorage * instance_
This struct always provides access to the autodiff stack using the singleton pattern.