Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
var_stack.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__VAR_STACK_HPP
2 #define STAN__AGRAD__REV__VAR_STACK_HPP
3 
4 #include <stdexcept>
5 #include <vector>
7 
8 namespace stan {
9  namespace agrad {
10 
11  // forward declaration of chainable
12  class chainable;
13  class chainable_alloc;
14 
15  extern std::vector<chainable*> var_stack_;
16  extern std::vector<chainable*> var_nochain_stack_;
17  extern std::vector<chainable_alloc*> var_alloc_stack_;
18  extern memory::stack_alloc memalloc_;
19 
20  // nested positions
21  extern std::vector<size_t> nested_var_stack_sizes_;
22  extern std::vector<size_t> nested_var_nochain_stack_sizes_;
23  extern std::vector<size_t> nested_var_alloc_stack_starts_;
24 
32  public:
34  var_alloc_stack_.push_back(this);
35  }
36  virtual ~chainable_alloc() { };
37  };
38 
42  static inline bool empty_nested() {
43  return nested_var_stack_sizes_.empty();
44  }
45 
52  static inline void recover_memory() {
53  if (!empty_nested())
54  throw std::logic_error("empty_nested() must be true"
55  " before calling recover_memory()");
56  var_stack_.clear();
57  var_nochain_stack_.clear();
58  for (size_t i = 0; i < var_alloc_stack_.size(); i++)
59  delete var_alloc_stack_[i];
60  var_alloc_stack_.clear();
62  }
63 
72  static inline void recover_memory_nested() {
73  if (empty_nested())
74  throw std::logic_error("empty_nested() must be false"
75  " before calling recover_memory_nested()");
76 
77  var_stack_.resize(nested_var_stack_sizes_.back());
78  nested_var_stack_sizes_.pop_back();
79 
82 
83  for (size_t i = nested_var_alloc_stack_starts_.back();
84  i < var_alloc_stack_.size();
85  ++i)
86  delete var_alloc_stack_[i];
88 
90  }
91 
96  static inline void start_nested() {
97  nested_var_stack_sizes_.push_back(var_stack_.size());
101  }
102 
103  static inline size_t nested_size() {
104  return var_stack_.size() - nested_var_stack_sizes_.back();
105  }
106 
107  }
108 }
109 #endif
static void recover_memory()
Recover memory used for all variables for reuse.
Definition: var_stack.hpp:52
static void start_nested()
Record the current position so that recover_memory_nested() can find it.
Definition: var_stack.hpp:96
memory::stack_alloc memalloc_
Definition: var_stack.cpp:16
static size_t nested_size()
Definition: var_stack.hpp:103
std::vector< size_t > nested_var_stack_sizes_
Definition: var_stack.cpp:18
A chainable_alloc is an object which is constructed and destructed normally but the memory lifespan i...
Definition: var_stack.hpp:31
std::vector< size_t > nested_var_alloc_stack_starts_
Definition: var_stack.cpp:20
std::vector< chainable * > var_stack_
Definition: var_stack.cpp:13
void start_nested()
Store current positions before doing nested operation so can recover back to start.
std::vector< chainable_alloc * > var_alloc_stack_
Definition: var_stack.cpp:15
static void recover_memory_nested()
Recover only the memory used for the top nested call.
Definition: var_stack.hpp:72
void recover_nested()
recover memory back to the last start_nested call.
static bool empty_nested()
Return true if there is no nested autodiff being executed.
Definition: var_stack.hpp:42
std::vector< size_t > nested_var_nochain_stack_sizes_
Definition: var_stack.cpp:19
void recover_all()
Recover all the memory used by the stack allocator.
std::vector< chainable * > var_nochain_stack_
Definition: var_stack.cpp:14

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