set_zero_all_adjoints_nested.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_SET_ZERO_ALL_ADJOINTS_NESTED_HPP
2 #define STAN_MATH_REV_CORE_SET_ZERO_ALL_ADJOINTS_NESTED_HPP
3 
8 #include <stdexcept>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Reset all adjoint values in the top nested portion of the stack
15  * to zero.
16  */
18  if (empty_nested())
19  throw std::logic_error(
20  "empty_nested() must be false before calling"
21  " set_zero_all_adjoints_nested()");
22  size_t start1 = ChainableStack::instance().nested_var_stack_sizes_.back();
23  // avoid wrap with unsigned when start1 == 0
24  for (size_t i = (start1 == 0U) ? 0U : (start1 - 1);
25  i < ChainableStack::instance().var_stack_.size(); ++i)
26  ChainableStack::instance().var_stack_[i]->set_zero_adjoint();
27 
28  size_t start2
30  for (size_t i = (start2 == 0U) ? 0U : (start2 - 1);
32  ChainableStack::instance().var_nochain_stack_[i]->set_zero_adjoint();
33  }
34 }
35 
36 } // namespace math
37 } // namespace stan
38 #endif
static bool empty_nested()
static void set_zero_all_adjoints_nested()
static AutodiffStackStorage & instance()