Stan Math Library  2.20.0
reverse mode automatic differentiation
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 
7 namespace stan {
8 namespace 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 
88 template <typename ChainableT, typename ChainableAllocT>
92 
93  AutodiffStackSingleton() : own_instance_(init()) {}
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_;
111  std::vector<size_t> nested_var_nochain_stack_sizes_;
112  std::vector<size_t> nested_var_alloc_stack_starts_;
113  };
114 
115  explicit AutodiffStackSingleton(AutodiffStackSingleton_t const &) = delete;
117 
119 
120  private:
121  static bool init() {
122  if (!instance_) {
123  instance_ = new AutodiffStackStorage();
124  return true;
125  }
126  return false;
127  }
128 
129  bool own_instance_;
130 };
131 
132 template <typename ChainableT, typename ChainableAllocT>
134  typename AutodiffStackSingleton<ChainableT,
135  ChainableAllocT>::AutodiffStackStorage
137  = nullptr;
138 
139 } // namespace math
140 } // namespace stan
141 #endif
AutodiffStackStorage & operator=(const AutodiffStackStorage &)=delete
static STAN_THREADS_DEF AutodiffStackStorage * instance_
AutodiffStackSingleton< ChainableT, ChainableAllocT > AutodiffStackSingleton_t
#define STAN_THREADS_DEF
This struct always provides access to the autodiff stack using the singleton pattern.
An instance of this class provides a memory pool through which blocks of raw memory may be allocated ...
Definition: stack_alloc.hpp:71

     [ Stan Home Page ] © 2011–2018, Stan Development Team.