Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
chainable.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__CHAINABLE_HPP
2 #define STAN__AGRAD__REV__CHAINABLE_HPP
3 
4 #include <vector>
6 
7 namespace stan {
8  namespace agrad {
9 
14  class chainable {
15 
16  public:
17 
22  chainable() { }
23 
28  virtual ~chainable() {
29  // handled automatically
30  }
31 
37  virtual void chain() {
38  }
39 
44  virtual void init_dependent() {
45  }
46 
51  virtual void set_zero_adjoint() {
52  }
53 
63  static inline void* operator new(size_t nbytes) {
64  return memalloc_.alloc(nbytes);
65  }
66 
77  static inline void operator delete(void* /* ignore arg */) {
78  /* no op */
79  }
80  };
81 
82 
83 
87  static void set_zero_all_adjoints() {
88  for (size_t i = 0; i < var_stack_.size(); ++i)
89  var_stack_[i]->set_zero_adjoint();
90  for (size_t i = 0; i < var_nochain_stack_.size(); ++i)
91  var_nochain_stack_[i]->set_zero_adjoint();
92  }
93 
110  static void grad(chainable* vi) {
111 
112  // simple reference implementation (intended as doc):
113  // vi->init_dependent();
114  // size_t end = var_stack_.size();
115  // size_t begin = empty_nested() ? 0 : end - nested_size();
116  // for (size_t i = end; --i > begin; )
117  // var_stack_[i]->chain();
118 
119  typedef std::vector<chainable*>::reverse_iterator it_t;
120  vi->init_dependent();
121  it_t begin = var_stack_.rbegin();
122  it_t end = empty_nested() ? var_stack_.rend() : begin + nested_size();
123  for (it_t it = begin; it < end; ++it) {
124  (*it)->chain();
125  }
126  }
127 
128  }
129 }
130 #endif
virtual void init_dependent()
Initialize this chainable's adjoint value to make it the dependent variable in a gradient calculation...
Definition: chainable.hpp:44
static void set_zero_all_adjoints()
Reset all adjoint values in the stack to zero.
Definition: chainable.hpp:87
Abstract base class for variable implementations that handles memory management and applying the chai...
Definition: chainable.hpp:14
memory::stack_alloc memalloc_
Definition: var_stack.cpp:16
static void grad(chainable *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: chainable.hpp:110
static size_t nested_size()
Definition: var_stack.hpp:103
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: chainable.hpp:37
virtual ~chainable()
Chainables are not destructible and should go on the function call stack or be allocated with operato...
Definition: chainable.hpp:28
chainable()
Construct a chainable object.
Definition: chainable.hpp:22
std::vector< chainable * > var_stack_
Definition: var_stack.cpp:13
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
virtual void set_zero_adjoint()
Set the value of the adjoint for this chainable to its initial value.
Definition: chainable.hpp:51
static bool empty_nested()
Return true if there is no nested autodiff being executed.
Definition: var_stack.hpp:42
std::vector< chainable * > var_nochain_stack_
Definition: var_stack.cpp:14

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