diff --git a/stan/math/rev/arr/fun/sum.hpp b/stan/math/rev/arr/fun/sum.hpp index fd5fe2e0d3b..5f1df50ee9b 100644 --- a/stan/math/rev/arr/fun/sum.hpp +++ b/stan/math/rev/arr/fun/sum.hpp @@ -29,8 +29,8 @@ class sum_v_vari : public vari { explicit sum_v_vari(const std::vector& v1) : vari(sum_of_val(v1)), - v_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( - v1.size() * sizeof(vari*)))), + v_(reinterpret_cast( + chainable_stack.memalloc_.alloc(v1.size() * sizeof(vari*)))), length_(v1.size()) { for (size_t i = 0; i < length_; i++) v_[i] = v1[i].vi_; diff --git a/stan/math/rev/core/autodiffstackstorage.hpp b/stan/math/rev/core/autodiffstackstorage.hpp index 4ae8739ec81..31936909aac 100644 --- a/stan/math/rev/core/autodiffstackstorage.hpp +++ b/stan/math/rev/core/autodiffstackstorage.hpp @@ -9,19 +9,6 @@ namespace math { template struct AutodiffStackStorage { - typedef AutodiffStackStorage - AutodiffStackStorage_t; - - static AutodiffStackStorage_t& context() { -#ifndef STAN_THREADS - static AutodiffStackStorage_t ad_stack = AutodiffStackStorage_t(); -#else - static thread_local AutodiffStackStorage_t ad_stack - = AutodiffStackStorage_t(); -#endif - return ad_stack; - } - std::vector var_stack_; std::vector var_nochain_stack_; std::vector var_alloc_stack_; diff --git a/stan/math/rev/core/chainable_alloc.hpp b/stan/math/rev/core/chainable_alloc.hpp index 01ec4e5bb4e..8d533b9fdda 100644 --- a/stan/math/rev/core/chainable_alloc.hpp +++ b/stan/math/rev/core/chainable_alloc.hpp @@ -15,9 +15,7 @@ namespace math { */ class chainable_alloc { public: - chainable_alloc() { - ChainableStack::context().var_alloc_stack_.push_back(this); - } + chainable_alloc() { chainable_stack.var_alloc_stack_.push_back(this); } virtual ~chainable_alloc() {} }; diff --git a/stan/math/rev/core/chainablestack.hpp b/stan/math/rev/core/chainablestack.hpp index b1f2fc0e70d..75e8fc2d9a5 100644 --- a/stan/math/rev/core/chainablestack.hpp +++ b/stan/math/rev/core/chainablestack.hpp @@ -11,6 +11,11 @@ class chainable_alloc; typedef AutodiffStackStorage ChainableStack; +#ifdef STAN_THREADS +thread_local +#endif + static ChainableStack chainable_stack; + } // namespace math } // namespace stan #endif diff --git a/stan/math/rev/core/empty_nested.hpp b/stan/math/rev/core/empty_nested.hpp index 29de91562fd..361d3d4dd5a 100644 --- a/stan/math/rev/core/empty_nested.hpp +++ b/stan/math/rev/core/empty_nested.hpp @@ -10,7 +10,7 @@ namespace math { * Return true if there is no nested autodiff being executed. */ static inline bool empty_nested() { - return ChainableStack::context().nested_var_stack_sizes_.empty(); + return chainable_stack.nested_var_stack_sizes_.empty(); } } // namespace math diff --git a/stan/math/rev/core/gevv_vvv_vari.hpp b/stan/math/rev/core/gevv_vvv_vari.hpp index 55946692f6e..260e5f939eb 100644 --- a/stan/math/rev/core/gevv_vvv_vari.hpp +++ b/stan/math/rev/core/gevv_vvv_vari.hpp @@ -33,7 +33,7 @@ class gevv_vvv_vari : public vari { alpha_ = alpha->vi_; // TODO(carpenter): replace this with array alloc fun call v1_ = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(2 * length_ * sizeof(vari*))); + chainable_stack.memalloc_.alloc(2 * length_ * sizeof(vari*))); v2_ = v1_ + length_; for (size_t i = 0; i < length_; i++) v1_[i] = v1[i * stride1].vi_; diff --git a/stan/math/rev/core/grad.hpp b/stan/math/rev/core/grad.hpp index 8b070ea0110..7624a6bb058 100644 --- a/stan/math/rev/core/grad.hpp +++ b/stan/math/rev/core/grad.hpp @@ -37,8 +37,8 @@ static void grad(vari* vi) { typedef std::vector::reverse_iterator it_t; vi->init_dependent(); - it_t begin = ChainableStack::context().var_stack_.rbegin(); - it_t end = empty_nested() ? ChainableStack::context().var_stack_.rend() + it_t begin = chainable_stack.var_stack_.rbegin(); + it_t end = empty_nested() ? chainable_stack.var_stack_.rend() : begin + nested_size(); for (it_t it = begin; it < end; ++it) { (*it)->chain(); diff --git a/stan/math/rev/core/nested_size.hpp b/stan/math/rev/core/nested_size.hpp index 617be0b804b..e3ab58fa2e5 100644 --- a/stan/math/rev/core/nested_size.hpp +++ b/stan/math/rev/core/nested_size.hpp @@ -8,8 +8,8 @@ namespace stan { namespace math { static inline size_t nested_size() { - return ChainableStack::context().var_stack_.size() - - ChainableStack::context().nested_var_stack_sizes_.back(); + return chainable_stack.var_stack_.size() + - chainable_stack.nested_var_stack_sizes_.back(); } } // namespace math diff --git a/stan/math/rev/core/precomputed_gradients.hpp b/stan/math/rev/core/precomputed_gradients.hpp index ef036ac9f2d..92494f31a4f 100644 --- a/stan/math/rev/core/precomputed_gradients.hpp +++ b/stan/math/rev/core/precomputed_gradients.hpp @@ -52,10 +52,8 @@ class precomputed_gradients_vari : public vari { const std::vector& gradients) : vari(val), size_(vars.size()), - varis_(ChainableStack::context().memalloc_.alloc_array( - vars.size())), - gradients_(ChainableStack::context().memalloc_.alloc_array( - vars.size())) { + varis_(chainable_stack.memalloc_.alloc_array(vars.size())), + gradients_(chainable_stack.memalloc_.alloc_array(vars.size())) { check_consistent_sizes("precomputed_gradients_vari", "vars", vars, "gradients", gradients); for (size_t i = 0; i < vars.size(); ++i) diff --git a/stan/math/rev/core/print_stack.hpp b/stan/math/rev/core/print_stack.hpp index 73faa922eed..96d62d26357 100644 --- a/stan/math/rev/core/print_stack.hpp +++ b/stan/math/rev/core/print_stack.hpp @@ -18,15 +18,12 @@ namespace math { * @param o ostream to modify */ inline void print_stack(std::ostream& o) { - o << "STACK, size=" << ChainableStack::context().var_stack_.size() - << std::endl; + o << "STACK, size=" << chainable_stack.var_stack_.size() << std::endl; // TODO(carpenter): this shouldn't need to be cast any more - for (size_t i = 0; i < ChainableStack::context().var_stack_.size(); ++i) - o << i << " " << ChainableStack::context().var_stack_[i] << " " - << (static_cast(ChainableStack::context().var_stack_[i]))->val_ - << " : " - << (static_cast(ChainableStack::context().var_stack_[i]))->adj_ - << std::endl; + for (size_t i = 0; i < chainable_stack.var_stack_.size(); ++i) + o << i << " " << chainable_stack.var_stack_[i] << " " + << (static_cast(chainable_stack.var_stack_[i]))->val_ << " : " + << (static_cast(chainable_stack.var_stack_[i]))->adj_ << std::endl; } } // namespace math diff --git a/stan/math/rev/core/recover_memory.hpp b/stan/math/rev/core/recover_memory.hpp index 5cf9ffc16ca..45f353fdce4 100644 --- a/stan/math/rev/core/recover_memory.hpp +++ b/stan/math/rev/core/recover_memory.hpp @@ -20,13 +20,13 @@ static inline void recover_memory() { throw std::logic_error( "empty_nested() must be true" " before calling recover_memory()"); - ChainableStack::context().var_stack_.clear(); - ChainableStack::context().var_nochain_stack_.clear(); - for (auto &x : ChainableStack::context().var_alloc_stack_) { + chainable_stack.var_stack_.clear(); + chainable_stack.var_nochain_stack_.clear(); + for (auto &x : chainable_stack.var_alloc_stack_) { delete x; } - ChainableStack::context().var_alloc_stack_.clear(); - ChainableStack::context().memalloc_.recover_all(); + chainable_stack.var_alloc_stack_.clear(); + chainable_stack.memalloc_.recover_all(); } } // namespace math diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index 472395bb0c7..a2b55e5c454 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -23,24 +23,23 @@ static inline void recover_memory_nested() { "empty_nested() must be false" " before calling recover_memory_nested()"); - ChainableStack::context().var_stack_.resize( - ChainableStack::context().nested_var_stack_sizes_.back()); - ChainableStack::context().nested_var_stack_sizes_.pop_back(); - - ChainableStack::context().var_nochain_stack_.resize( - ChainableStack::context().nested_var_nochain_stack_sizes_.back()); - ChainableStack::context().nested_var_nochain_stack_sizes_.pop_back(); - - for (size_t i - = ChainableStack::context().nested_var_alloc_stack_starts_.back(); - i < ChainableStack::context().var_alloc_stack_.size(); ++i) { - delete ChainableStack::context().var_alloc_stack_[i]; + chainable_stack.var_stack_.resize( + chainable_stack.nested_var_stack_sizes_.back()); + chainable_stack.nested_var_stack_sizes_.pop_back(); + + chainable_stack.var_nochain_stack_.resize( + chainable_stack.nested_var_nochain_stack_sizes_.back()); + chainable_stack.nested_var_nochain_stack_sizes_.pop_back(); + + for (size_t i = chainable_stack.nested_var_alloc_stack_starts_.back(); + i < chainable_stack.var_alloc_stack_.size(); ++i) { + delete chainable_stack.var_alloc_stack_[i]; } - ChainableStack::context().var_alloc_stack_.resize( - ChainableStack::context().nested_var_alloc_stack_starts_.back()); - ChainableStack::context().nested_var_alloc_stack_starts_.pop_back(); + chainable_stack.var_alloc_stack_.resize( + chainable_stack.nested_var_alloc_stack_starts_.back()); + chainable_stack.nested_var_alloc_stack_starts_.pop_back(); - ChainableStack::context().memalloc_.recover_nested(); + chainable_stack.memalloc_.recover_nested(); } } // namespace math diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index b97b1e49f54..d7f7596c463 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -12,9 +12,9 @@ namespace math { * Reset all adjoint values in the stack to zero. */ static void set_zero_all_adjoints() { - for (auto &x : ChainableStack::context().var_stack_) + for (auto &x : chainable_stack.var_stack_) x->set_zero_adjoint(); - for (auto &x : ChainableStack::context().var_nochain_stack_) + for (auto &x : chainable_stack.var_nochain_stack_) x->set_zero_adjoint(); } diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index ed35d93b37f..377707c6624 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -19,17 +19,16 @@ static void set_zero_all_adjoints_nested() { throw std::logic_error( "empty_nested() must be false before calling" " set_zero_all_adjoints_nested()"); - size_t start1 = ChainableStack::context().nested_var_stack_sizes_.back(); + size_t start1 = chainable_stack.nested_var_stack_sizes_.back(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); - i < ChainableStack::context().var_stack_.size(); ++i) - ChainableStack::context().var_stack_[i]->set_zero_adjoint(); + i < chainable_stack.var_stack_.size(); ++i) + chainable_stack.var_stack_[i]->set_zero_adjoint(); - size_t start2 - = ChainableStack::context().nested_var_nochain_stack_sizes_.back(); + size_t start2 = chainable_stack.nested_var_nochain_stack_sizes_.back(); for (size_t i = (start2 == 0U) ? 0U : (start2 - 1); - i < ChainableStack::context().var_nochain_stack_.size(); ++i) { - ChainableStack::context().var_nochain_stack_[i]->set_zero_adjoint(); + i < chainable_stack.var_nochain_stack_.size(); ++i) { + chainable_stack.var_nochain_stack_[i]->set_zero_adjoint(); } } diff --git a/stan/math/rev/core/start_nested.hpp b/stan/math/rev/core/start_nested.hpp index 244f523a448..3d2b1b4ed0c 100644 --- a/stan/math/rev/core/start_nested.hpp +++ b/stan/math/rev/core/start_nested.hpp @@ -11,13 +11,13 @@ namespace math { * can find it. */ static inline void start_nested() { - ChainableStack::context().nested_var_stack_sizes_.push_back( - ChainableStack::context().var_stack_.size()); - ChainableStack::context().nested_var_nochain_stack_sizes_.push_back( - ChainableStack::context().var_nochain_stack_.size()); - ChainableStack::context().nested_var_alloc_stack_starts_.push_back( - ChainableStack::context().var_alloc_stack_.size()); - ChainableStack::context().memalloc_.start_nested(); + chainable_stack.nested_var_stack_sizes_.push_back( + chainable_stack.var_stack_.size()); + chainable_stack.nested_var_nochain_stack_sizes_.push_back( + chainable_stack.var_nochain_stack_.size()); + chainable_stack.nested_var_alloc_stack_starts_.push_back( + chainable_stack.var_alloc_stack_.size()); + chainable_stack.memalloc_.start_nested(); } } // namespace math diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index fa9be73f7eb..509d6bbf0e0 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -56,14 +56,14 @@ class vari { * @param x Value of the constructed variable. */ explicit vari(double x) : val_(x), adj_(0.0) { - ChainableStack::context().var_stack_.push_back(this); + chainable_stack.var_stack_.push_back(this); } vari(double x, bool stacked) : val_(x), adj_(0.0) { if (stacked) - ChainableStack::context().var_stack_.push_back(this); + chainable_stack.var_stack_.push_back(this); else - ChainableStack::context().var_nochain_stack_.push_back(this); + chainable_stack.var_nochain_stack_.push_back(this); } /** @@ -123,7 +123,7 @@ class vari { * @return Pointer to allocated bytes. */ static inline void* operator new(size_t nbytes) { - return ChainableStack::context().memalloc_.alloc(nbytes); + return chainable_stack.memalloc_.alloc(nbytes); } /** diff --git a/stan/math/rev/mat/fun/cholesky_decompose.hpp b/stan/math/rev/mat/fun/cholesky_decompose.hpp index b3f7fed2ef7..851876f6385 100644 --- a/stan/math/rev/mat/fun/cholesky_decompose.hpp +++ b/stan/math/rev/mat/fun/cholesky_decompose.hpp @@ -44,9 +44,9 @@ class cholesky_block : public vari { const Eigen::Matrix& L_A) : vari(0.0), M_(A.rows()), - variRefA_(ChainableStack::context().memalloc_.alloc_array( + variRefA_(chainable_stack.memalloc_.alloc_array( A.rows() * (A.rows() + 1) / 2)), - variRefL_(ChainableStack::context().memalloc_.alloc_array( + variRefL_(chainable_stack.memalloc_.alloc_array( A.rows() * (A.rows() + 1) / 2)) { size_t pos = 0; block_size_ = std::max((M_ / 8 / 16) * 16, 8); @@ -159,9 +159,9 @@ class cholesky_scalar : public vari { const Eigen::Matrix& L_A) : vari(0.0), M_(A.rows()), - variRefA_(ChainableStack::context().memalloc_.alloc_array( + variRefA_(chainable_stack.memalloc_.alloc_array( A.rows() * (A.rows() + 1) / 2)), - variRefL_(ChainableStack::context().memalloc_.alloc_array( + variRefL_(chainable_stack.memalloc_.alloc_array( A.rows() * (A.rows() + 1) / 2)) { size_t accum = 0; size_t accum_i = accum; diff --git a/stan/math/rev/mat/fun/cov_exp_quad.hpp b/stan/math/rev/mat/fun/cov_exp_quad.hpp index 75fe30ca405..19431c1d12b 100644 --- a/stan/math/rev/mat/fun/cov_exp_quad.hpp +++ b/stan/math/rev/mat/fun/cov_exp_quad.hpp @@ -72,14 +72,11 @@ class cov_exp_quad_vari : public vari { l_d_(value_of(l)), sigma_d_(value_of(sigma)), sigma_sq_d_(sigma_d_ * sigma_d_), - dist_(ChainableStack::context().memalloc_.alloc_array( - size_ltri_)), + dist_(chainable_stack.memalloc_.alloc_array(size_ltri_)), l_vari_(l.vi_), sigma_vari_(sigma.vi_), - cov_lower_( - ChainableStack::context().memalloc_.alloc_array(size_ltri_)), - cov_diag_( - ChainableStack::context().memalloc_.alloc_array(size_)) { + cov_lower_(chainable_stack.memalloc_.alloc_array(size_ltri_)), + cov_diag_(chainable_stack.memalloc_.alloc_array(size_)) { double inv_half_sq_l_d = 0.5 / (l_d_ * l_d_); size_t pos = 0; for (size_t j = 0; j < size_ - 1; ++j) { @@ -164,13 +161,10 @@ class cov_exp_quad_vari : public vari { l_d_(value_of(l)), sigma_d_(value_of(sigma)), sigma_sq_d_(sigma_d_ * sigma_d_), - dist_(ChainableStack::context().memalloc_.alloc_array( - size_ltri_)), + dist_(chainable_stack.memalloc_.alloc_array(size_ltri_)), l_vari_(l.vi_), - cov_lower_( - ChainableStack::context().memalloc_.alloc_array(size_ltri_)), - cov_diag_( - ChainableStack::context().memalloc_.alloc_array(size_)) { + cov_lower_(chainable_stack.memalloc_.alloc_array(size_ltri_)), + cov_diag_(chainable_stack.memalloc_.alloc_array(size_)) { double inv_half_sq_l_d = 0.5 / (l_d_ * l_d_); size_t pos = 0; for (size_t j = 0; j < size_ - 1; ++j) { diff --git a/stan/math/rev/mat/fun/determinant.hpp b/stan/math/rev/mat/fun/determinant.hpp index 20c530494ee..656625d55ec 100644 --- a/stan/math/rev/mat/fun/determinant.hpp +++ b/stan/math/rev/mat/fun/determinant.hpp @@ -24,11 +24,10 @@ class determinant_vari : public vari { : vari(determinant_vari_calc(A)), rows_(A.rows()), cols_(A.cols()), - A_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + A_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * A.rows() * A.cols()))), - adjARef_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari*) * A.rows() * A.cols()))) { + adjARef_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari*) * A.rows() * A.cols()))) { size_t pos = 0; for (size_type j = 0; j < cols_; j++) { for (size_type i = 0; i < rows_; i++) { diff --git a/stan/math/rev/mat/fun/dot_product.hpp b/stan/math/rev/mat/fun/dot_product.hpp index f920bc0208d..2fbf2470763 100644 --- a/stan/math/rev/mat/fun/dot_product.hpp +++ b/stan/math/rev/mat/fun/dot_product.hpp @@ -85,7 +85,7 @@ class dot_product_vari : public vari { vari** shared = nullptr) { if (shared == nullptr) { mem_v = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(length_ * sizeof(vari*))); + chainable_stack.memalloc_.alloc(length_ * sizeof(vari*))); for (size_t i = 0; i < length_; i++) mem_v[i] = inv[i].vi_; } else { @@ -97,7 +97,7 @@ class dot_product_vari : public vari { vari** shared = nullptr) { if (shared == nullptr) { mem_v = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(length_ * sizeof(vari*))); + chainable_stack.memalloc_.alloc(length_ * sizeof(vari*))); for (size_t i = 0; i < length_; i++) mem_v[i] = inv(i).vi_; } else { @@ -109,7 +109,7 @@ class dot_product_vari : public vari { double* shared = nullptr) { if (shared == nullptr) { mem_d = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(length_ * sizeof(double))); + chainable_stack.memalloc_.alloc(length_ * sizeof(double))); for (size_t i = 0; i < length_; i++) mem_d[i] = ind[i]; } else { @@ -121,7 +121,7 @@ class dot_product_vari : public vari { double* shared = nullptr) { if (shared == nullptr) { mem_d = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(length_ * sizeof(double))); + chainable_stack.memalloc_.alloc(length_ * sizeof(double))); for (size_t i = 0; i < length_; i++) mem_d[i] = ind(i); } else { diff --git a/stan/math/rev/mat/fun/dot_self.hpp b/stan/math/rev/mat/fun/dot_self.hpp index 013e65d1fe0..723d1ad4027 100644 --- a/stan/math/rev/mat/fun/dot_self.hpp +++ b/stan/math/rev/mat/fun/dot_self.hpp @@ -24,7 +24,7 @@ class dot_self_vari : public vari { explicit dot_self_vari(const Eigen::DenseBase& v) : vari(var_dot_self(v)), size_(v.size()) { v_ = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(size_ * sizeof(vari*))); + chainable_stack.memalloc_.alloc(size_ * sizeof(vari*))); for (size_t i = 0; i < size_; i++) v_[i] = v[i].vi_; } @@ -32,7 +32,7 @@ class dot_self_vari : public vari { explicit dot_self_vari(const Eigen::Matrix& v) : vari(var_dot_self(v)), size_(v.size()) { v_ = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(size_ * sizeof(vari*))); + chainable_stack.memalloc_.alloc(size_ * sizeof(vari*))); for (size_t i = 0; i < size_; ++i) v_[i] = v(i).vi_; } diff --git a/stan/math/rev/mat/fun/log_determinant.hpp b/stan/math/rev/mat/fun/log_determinant.hpp index 17b635bf568..906765d488d 100644 --- a/stan/math/rev/mat/fun/log_determinant.hpp +++ b/stan/math/rev/mat/fun/log_determinant.hpp @@ -23,14 +23,12 @@ inline var log_determinant(const Eigen::Matrix& m) { double val = hh.logAbsDeterminant(); - vari** varis - = ChainableStack::context().memalloc_.alloc_array(m.size()); + vari** varis = chainable_stack.memalloc_.alloc_array(m.size()); for (int i = 0; i < m.size(); ++i) varis[i] = m(i).vi_; Matrix m_inv_transpose = hh.inverse().transpose(); - double* gradients - = ChainableStack::context().memalloc_.alloc_array(m.size()); + double* gradients = chainable_stack.memalloc_.alloc_array(m.size()); for (int i = 0; i < m.size(); ++i) gradients[i] = m_inv_transpose(i); diff --git a/stan/math/rev/mat/fun/log_determinant_spd.hpp b/stan/math/rev/mat/fun/log_determinant_spd.hpp index c8e2ff4da45..57aec3f7179 100644 --- a/stan/math/rev/mat/fun/log_determinant_spd.hpp +++ b/stan/math/rev/mat/fun/log_determinant_spd.hpp @@ -42,13 +42,11 @@ inline var log_determinant_spd(const Eigen::Matrix& m) { check_finite("log_determinant_spd", "log determininant of the matrix argument", val); - vari** operands - = ChainableStack::context().memalloc_.alloc_array(m.size()); + vari** operands = chainable_stack.memalloc_.alloc_array(m.size()); for (int i = 0; i < m.size(); ++i) operands[i] = m(i).vi_; - double* gradients - = ChainableStack::context().memalloc_.alloc_array(m.size()); + double* gradients = chainable_stack.memalloc_.alloc_array(m.size()); for (int i = 0; i < m.size(); ++i) gradients[i] = m_d(i); diff --git a/stan/math/rev/mat/fun/mdivide_left.hpp b/stan/math/rev/mat/fun/mdivide_left.hpp index 886175d5ad4..578207d9885 100644 --- a/stan/math/rev/mat/fun/mdivide_left.hpp +++ b/stan/math/rev/mat/fun/mdivide_left.hpp @@ -29,19 +29,16 @@ class mdivide_left_vv_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - A_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + A_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * A.rows() * A.cols()))), - C_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + C_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * B.rows() * B.cols()))), - variRefA_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * A.rows() * A.cols()))), - variRefB_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))) { + variRefA_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * A.rows() * A.cols()))), + variRefB_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))) { using Eigen::Map; using Eigen::Matrix; @@ -122,16 +119,14 @@ class mdivide_left_dv_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - A_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + A_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * A.rows() * A.cols()))), - C_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + C_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * B.rows() * B.cols()))), - variRefB_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))) { + variRefB_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))) { using Eigen::Map; using Eigen::Matrix; @@ -203,16 +198,14 @@ class mdivide_left_vd_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - A_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + A_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * A.rows() * A.cols()))), - C_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + C_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * B.rows() * B.cols()))), - variRefA_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * A.rows() * A.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))) { + variRefA_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * A.rows() * A.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))) { using Eigen::Map; using Eigen::Matrix; diff --git a/stan/math/rev/mat/fun/mdivide_left_ldlt.hpp b/stan/math/rev/mat/fun/mdivide_left_ldlt.hpp index 83e190bf20d..c86a7c224c5 100644 --- a/stan/math/rev/mat/fun/mdivide_left_ldlt.hpp +++ b/stan/math/rev/mat/fun/mdivide_left_ldlt.hpp @@ -49,12 +49,10 @@ class mdivide_left_ldlt_vv_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - variRefB_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), + variRefB_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), alloc_(new mdivide_left_ldlt_alloc()), alloc_ldlt_(A.alloc_) { int pos = 0; @@ -125,12 +123,10 @@ class mdivide_left_ldlt_dv_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - variRefB_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), + variRefB_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), alloc_(new mdivide_left_ldlt_alloc()) { using Eigen::Map; using Eigen::Matrix; @@ -198,9 +194,8 @@ class mdivide_left_ldlt_vd_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), alloc_(new mdivide_left_ldlt_alloc()), alloc_ldlt_(A.alloc_) { alloc_->C_ = B; diff --git a/stan/math/rev/mat/fun/mdivide_left_spd.hpp b/stan/math/rev/mat/fun/mdivide_left_spd.hpp index a31e2dee2d9..5d8232dc589 100644 --- a/stan/math/rev/mat/fun/mdivide_left_spd.hpp +++ b/stan/math/rev/mat/fun/mdivide_left_spd.hpp @@ -37,15 +37,12 @@ class mdivide_left_spd_vv_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - variRefA_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * A.rows() * A.cols()))), - variRefB_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), + variRefA_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * A.rows() * A.cols()))), + variRefB_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), alloc_(new mdivide_left_spd_alloc()) { using Eigen::Map; using Eigen::Matrix; @@ -123,12 +120,10 @@ class mdivide_left_spd_dv_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - variRefB_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), + variRefB_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), alloc_(new mdivide_left_spd_alloc()) { using Eigen::Map; using Eigen::Matrix; @@ -188,12 +183,10 @@ class mdivide_left_spd_vd_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - variRefA_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * A.rows() * A.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), + variRefA_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * A.rows() * A.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), alloc_(new mdivide_left_spd_alloc()) { using Eigen::Map; using Eigen::Matrix; diff --git a/stan/math/rev/mat/fun/mdivide_left_tri.hpp b/stan/math/rev/mat/fun/mdivide_left_tri.hpp index 22a0561f30a..ad25694dfd5 100644 --- a/stan/math/rev/mat/fun/mdivide_left_tri.hpp +++ b/stan/math/rev/mat/fun/mdivide_left_tri.hpp @@ -29,19 +29,16 @@ class mdivide_left_tri_vv_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - A_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + A_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * A.rows() * A.cols()))), - C_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + C_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * B.rows() * B.cols()))), - variRefA_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * A.rows() * (A.rows() + 1) / 2))), - variRefB_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))) { + variRefA_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * A.rows() * (A.rows() + 1) / 2))), + variRefB_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))) { using Eigen::Map; using Eigen::Matrix; @@ -140,16 +137,14 @@ class mdivide_left_tri_dv_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - A_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + A_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * A.rows() * A.cols()))), - C_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + C_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * B.rows() * B.cols()))), - variRefB_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))) { + variRefB_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))) { using Eigen::Map; using Eigen::Matrix; @@ -223,16 +218,14 @@ class mdivide_left_tri_vd_vari : public vari { : vari(0.0), M_(A.rows()), N_(B.cols()), - A_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + A_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * A.rows() * A.cols()))), - C_(reinterpret_cast(ChainableStack::context().memalloc_.alloc( + C_(reinterpret_cast(chainable_stack.memalloc_.alloc( sizeof(double) * B.rows() * B.cols()))), - variRefA_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * A.rows() * (A.rows() + 1) / 2))), - variRefC_( - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(vari *) * B.rows() * B.cols()))) { + variRefA_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * A.rows() * (A.rows() + 1) / 2))), + variRefC_(reinterpret_cast(chainable_stack.memalloc_.alloc( + sizeof(vari *) * B.rows() * B.cols()))) { using Eigen::Map; using Eigen::Matrix; diff --git a/stan/math/rev/mat/fun/multiply.hpp b/stan/math/rev/mat/fun/multiply.hpp index 248dc18830f..3f6c41bf14f 100644 --- a/stan/math/rev/mat/fun/multiply.hpp +++ b/stan/math/rev/mat/fun/multiply.hpp @@ -72,14 +72,12 @@ class multiply_mat_vari : public vari { B_cols_(B.cols()), A_size_(A.size()), B_size_(B.size()), - Ad_(ChainableStack::context().memalloc_.alloc_array(A_size_)), - Bd_(ChainableStack::context().memalloc_.alloc_array(B_size_)), - variRefA_( - ChainableStack::context().memalloc_.alloc_array(A_size_)), - variRefB_( - ChainableStack::context().memalloc_.alloc_array(B_size_)), - variRefAB_(ChainableStack::context().memalloc_.alloc_array( - A_rows_ * B_cols_)) { + Ad_(chainable_stack.memalloc_.alloc_array(A_size_)), + Bd_(chainable_stack.memalloc_.alloc_array(B_size_)), + variRefA_(chainable_stack.memalloc_.alloc_array(A_size_)), + variRefB_(chainable_stack.memalloc_.alloc_array(B_size_)), + variRefAB_( + chainable_stack.memalloc_.alloc_array(A_rows_ * B_cols_)) { using Eigen::Map; using Eigen::MatrixXd; for (size_type i = 0; i < A.size(); ++i) { @@ -159,12 +157,10 @@ class multiply_mat_vari : public vari { const Eigen::Matrix& B) : vari(0.0), size_(A.cols()), - Ad_(ChainableStack::context().memalloc_.alloc_array(size_)), - Bd_(ChainableStack::context().memalloc_.alloc_array(size_)), - variRefA_( - ChainableStack::context().memalloc_.alloc_array(size_)), - variRefB_( - ChainableStack::context().memalloc_.alloc_array(size_)) { + Ad_(chainable_stack.memalloc_.alloc_array(size_)), + Bd_(chainable_stack.memalloc_.alloc_array(size_)), + variRefA_(chainable_stack.memalloc_.alloc_array(size_)), + variRefB_(chainable_stack.memalloc_.alloc_array(size_)) { using Eigen::Map; using Eigen::RowVectorXd; using Eigen::VectorXd; @@ -249,12 +245,11 @@ class multiply_mat_vari : public vari { B_cols_(B.cols()), A_size_(A.size()), B_size_(B.size()), - Ad_(ChainableStack::context().memalloc_.alloc_array(A_size_)), - Bd_(ChainableStack::context().memalloc_.alloc_array(B_size_)), - variRefB_( - ChainableStack::context().memalloc_.alloc_array(B_size_)), - variRefAB_(ChainableStack::context().memalloc_.alloc_array( - A_rows_ * B_cols_)) { + Ad_(chainable_stack.memalloc_.alloc_array(A_size_)), + Bd_(chainable_stack.memalloc_.alloc_array(B_size_)), + variRefB_(chainable_stack.memalloc_.alloc_array(B_size_)), + variRefAB_( + chainable_stack.memalloc_.alloc_array(A_rows_ * B_cols_)) { using Eigen::Map; using Eigen::MatrixXd; for (size_type i = 0; i < A.size(); ++i) @@ -327,10 +322,9 @@ class multiply_mat_vari : public vari { const Eigen::Matrix& B) : vari(0.0), size_(A.cols()), - Ad_(ChainableStack::context().memalloc_.alloc_array(size_)), - Bd_(ChainableStack::context().memalloc_.alloc_array(size_)), - variRefB_( - ChainableStack::context().memalloc_.alloc_array(size_)) { + Ad_(chainable_stack.memalloc_.alloc_array(size_)), + Bd_(chainable_stack.memalloc_.alloc_array(size_)), + variRefB_(chainable_stack.memalloc_.alloc_array(size_)) { using Eigen::Map; using Eigen::RowVectorXd; using Eigen::VectorXd; @@ -411,12 +405,11 @@ class multiply_mat_vari : public vari { B_cols_(B.cols()), A_size_(A.size()), B_size_(B.size()), - Ad_(ChainableStack::context().memalloc_.alloc_array(A_size_)), - Bd_(ChainableStack::context().memalloc_.alloc_array(B_size_)), - variRefA_( - ChainableStack::context().memalloc_.alloc_array(A_size_)), - variRefAB_(ChainableStack::context().memalloc_.alloc_array( - A_rows_ * B_cols_)) { + Ad_(chainable_stack.memalloc_.alloc_array(A_size_)), + Bd_(chainable_stack.memalloc_.alloc_array(B_size_)), + variRefA_(chainable_stack.memalloc_.alloc_array(A_size_)), + variRefAB_( + chainable_stack.memalloc_.alloc_array(A_rows_ * B_cols_)) { using Eigen::Map; using Eigen::MatrixXd; for (size_type i = 0; i < A_size_; ++i) { @@ -493,10 +486,9 @@ class multiply_mat_vari : public vari { const Eigen::Matrix& B) : vari(0.0), size_(A.cols()), - Ad_(ChainableStack::context().memalloc_.alloc_array(size_)), - Bd_(ChainableStack::context().memalloc_.alloc_array(size_)), - variRefA_( - ChainableStack::context().memalloc_.alloc_array(size_)) { + Ad_(chainable_stack.memalloc_.alloc_array(size_)), + Bd_(chainable_stack.memalloc_.alloc_array(size_)), + variRefA_(chainable_stack.memalloc_.alloc_array(size_)) { using Eigen::Map; using Eigen::RowVectorXd; using Eigen::VectorXd; diff --git a/stan/math/rev/mat/fun/multiply_lower_tri_self_transpose.hpp b/stan/math/rev/mat/fun/multiply_lower_tri_self_transpose.hpp index ad049d55d2f..7a8ad9add53 100644 --- a/stan/math/rev/mat/fun/multiply_lower_tri_self_transpose.hpp +++ b/stan/math/rev/mat/fun/multiply_lower_tri_self_transpose.hpp @@ -33,7 +33,7 @@ inline matrix_v multiply_lower_tri_self_transpose(const matrix_v& L) { else // if (K < J) Knz = (K * (K + 1)) / 2; vari** vs = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(Knz * sizeof(vari*))); + chainable_stack.memalloc_.alloc(Knz * sizeof(vari*))); int pos = 0; for (int m = 0; m < K; ++m) for (int n = 0; n < ((J < (m + 1)) ? J : (m + 1)); ++n) { diff --git a/stan/math/rev/mat/fun/sd.hpp b/stan/math/rev/mat/fun/sd.hpp index cb29aed47c9..f78505e0ac6 100644 --- a/stan/math/rev/mat/fun/sd.hpp +++ b/stan/math/rev/mat/fun/sd.hpp @@ -20,7 +20,7 @@ namespace { // anonymous var calc_sd(size_t size, const var* dtrs) { using std::sqrt; vari** varis = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(size * sizeof(vari*))); + chainable_stack.memalloc_.alloc(size * sizeof(vari*))); for (size_t i = 0; i < size; ++i) varis[i] = dtrs[i].vi_; double sum = 0.0; @@ -35,7 +35,7 @@ var calc_sd(size_t size, const var* dtrs) { double variance = sum_of_squares / (size - 1); double sd = sqrt(variance); double* partials = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(size * sizeof(double))); + chainable_stack.memalloc_.alloc(size * sizeof(double))); if (sum_of_squares < 1e-20) { double grad_limit = 1 / std::sqrt(static_cast(size)); for (size_t i = 0; i < size; ++i) diff --git a/stan/math/rev/mat/fun/softmax.hpp b/stan/math/rev/mat/fun/softmax.hpp index 1c8dad80719..09707f5dd4d 100644 --- a/stan/math/rev/mat/fun/softmax.hpp +++ b/stan/math/rev/mat/fun/softmax.hpp @@ -57,7 +57,7 @@ inline Eigen::Matrix softmax( check_nonzero_size("softmax", "alpha", alpha); vari** alpha_vi_array = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(sizeof(vari*) * alpha.size())); + chainable_stack.memalloc_.alloc(sizeof(vari*) * alpha.size())); for (int i = 0; i < alpha.size(); ++i) alpha_vi_array[i] = alpha(i).vi_; @@ -67,9 +67,8 @@ inline Eigen::Matrix softmax( Matrix softmax_alpha_d = softmax(alpha_d); - double* softmax_alpha_d_array - = reinterpret_cast(ChainableStack::context().memalloc_.alloc( - sizeof(double) * alpha_d.size())); + double* softmax_alpha_d_array = reinterpret_cast( + chainable_stack.memalloc_.alloc(sizeof(double) * alpha_d.size())); for (int i = 0; i < alpha_d.size(); ++i) softmax_alpha_d_array[i] = softmax_alpha_d(i); diff --git a/stan/math/rev/mat/fun/squared_distance.hpp b/stan/math/rev/mat/fun/squared_distance.hpp index e2f28ca08f2..752ce3e481c 100644 --- a/stan/math/rev/mat/fun/squared_distance.hpp +++ b/stan/math/rev/mat/fun/squared_distance.hpp @@ -44,12 +44,12 @@ class squared_distance_vv_vari : public vari { const Eigen::Matrix& v2) : vari(var_squared_distance(v1, v2)), length_(v1.size()) { v1_ = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(length_ * sizeof(vari*))); + chainable_stack.memalloc_.alloc(length_ * sizeof(vari*))); for (size_t i = 0; i < length_; i++) v1_[i] = v1(i).vi_; v2_ = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(length_ * sizeof(vari*))); + chainable_stack.memalloc_.alloc(length_ * sizeof(vari*))); for (size_t i = 0; i < length_; i++) v2_[i] = v2(i).vi_; } @@ -88,12 +88,12 @@ class squared_distance_vd_vari : public vari { const Eigen::Matrix& v2) : vari(var_squared_distance(v1, v2)), length_(v1.size()) { v1_ = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(length_ * sizeof(vari*))); + chainable_stack.memalloc_.alloc(length_ * sizeof(vari*))); for (size_t i = 0; i < length_; i++) v1_[i] = v1(i).vi_; v2_ = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(length_ * sizeof(double))); + chainable_stack.memalloc_.alloc(length_ * sizeof(double))); for (size_t i = 0; i < length_; i++) v2_[i] = v2(i); } diff --git a/stan/math/rev/mat/fun/sum.hpp b/stan/math/rev/mat/fun/sum.hpp index 152c9c6a5d4..57ee1ba5106 100644 --- a/stan/math/rev/mat/fun/sum.hpp +++ b/stan/math/rev/mat/fun/sum.hpp @@ -27,11 +27,10 @@ class sum_eigen_v_vari : public sum_v_vari { public: template explicit sum_eigen_v_vari(const Eigen::Matrix& v1) - : sum_v_vari( - sum_of_val(v1), - reinterpret_cast(ChainableStack::context().memalloc_.alloc( - v1.size() * sizeof(vari*))), - v1.size()) { + : sum_v_vari(sum_of_val(v1), + reinterpret_cast(chainable_stack.memalloc_.alloc( + v1.size() * sizeof(vari*))), + v1.size()) { for (size_t i = 0; i < length_; i++) v_[i] = v1(i).vi_; } diff --git a/stan/math/rev/mat/fun/tcrossprod.hpp b/stan/math/rev/mat/fun/tcrossprod.hpp index 9978259ce38..fb77a491573 100644 --- a/stan/math/rev/mat/fun/tcrossprod.hpp +++ b/stan/math/rev/mat/fun/tcrossprod.hpp @@ -33,9 +33,8 @@ inline matrix_v tcrossprod(const matrix_v& M) { matrix_v MMt(M.rows(), M.rows()); - vari** vs - = reinterpret_cast(ChainableStack::context().memalloc_.alloc( - (M.rows() * M.cols()) * sizeof(vari*))); + vari** vs = reinterpret_cast( + chainable_stack.memalloc_.alloc((M.rows() * M.cols()) * sizeof(vari*))); int pos = 0; for (int m = 0; m < M.rows(); ++m) for (int n = 0; n < M.cols(); ++n) diff --git a/stan/math/rev/mat/fun/unit_vector_constrain.hpp b/stan/math/rev/mat/fun/unit_vector_constrain.hpp index ca98cfaabb3..6dbd08e9afc 100644 --- a/stan/math/rev/mat/fun/unit_vector_constrain.hpp +++ b/stan/math/rev/mat/fun/unit_vector_constrain.hpp @@ -58,7 +58,7 @@ Eigen::Matrix unit_vector_constrain( check_nonzero_size("unit_vector", "y", y); vari** y_vi_array = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(sizeof(vari*) * y.size())); + chainable_stack.memalloc_.alloc(sizeof(vari*) * y.size())); for (int i = 0; i < y.size(); ++i) y_vi_array[i] = y.coeff(i).vi_; @@ -71,7 +71,7 @@ Eigen::Matrix unit_vector_constrain( Eigen::VectorXd unit_vector_d = y_d / norm; double* unit_vector_y_d_array = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(sizeof(double) * y_d.size())); + chainable_stack.memalloc_.alloc(sizeof(double) * y_d.size())); for (int i = 0; i < y_d.size(); ++i) unit_vector_y_d_array[i] = unit_vector_d.coeff(i); diff --git a/stan/math/rev/mat/fun/variance.hpp b/stan/math/rev/mat/fun/variance.hpp index e72be5af53c..bf0be73fd43 100644 --- a/stan/math/rev/mat/fun/variance.hpp +++ b/stan/math/rev/mat/fun/variance.hpp @@ -15,7 +15,7 @@ namespace { inline var calc_variance(size_t size, const var* dtrs) { vari** varis = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(size * sizeof(vari*))); + chainable_stack.memalloc_.alloc(size * sizeof(vari*))); for (size_t i = 0; i < size; ++i) varis[i] = dtrs[i].vi_; double sum = 0.0; @@ -29,7 +29,7 @@ inline var calc_variance(size_t size, const var* dtrs) { } double variance = sum_of_squares / (size - 1); double* partials = reinterpret_cast( - ChainableStack::context().memalloc_.alloc(size * sizeof(double))); + chainable_stack.memalloc_.alloc(size * sizeof(double))); double two_over_size_m1 = 2 / (size - 1); for (size_t i = 0; i < size; ++i) partials[i] = two_over_size_m1 * (dtrs[i].vi_->val_ - mean); diff --git a/stan/math/rev/mat/functor/algebra_solver.hpp b/stan/math/rev/mat/functor/algebra_solver.hpp index c3f49dc2512..79b75706bf7 100644 --- a/stan/math/rev/mat/functor/algebra_solver.hpp +++ b/stan/math/rev/mat/functor/algebra_solver.hpp @@ -44,12 +44,12 @@ struct algebra_solver_vari : public vari { const Eigen::VectorXd& theta_dbl, Fx& fx, std::ostream* msgs) : vari(theta_dbl(0)), - y_(ChainableStack::context().memalloc_.alloc_array(y.size())), + y_(chainable_stack.memalloc_.alloc_array(y.size())), y_size_(y.size()), x_size_(x.size()), - theta_(ChainableStack::context().memalloc_.alloc_array(x_size_)), - Jx_y_(ChainableStack::context().memalloc_.alloc_array( - x_size_ * y_size_)) { + theta_(chainable_stack.memalloc_.alloc_array(x_size_)), + Jx_y_( + chainable_stack.memalloc_.alloc_array(x_size_ * y_size_)) { using Eigen::Map; using Eigen::MatrixXd; for (int i = 0; i < y.size(); ++i) diff --git a/stan/math/rev/scal/meta/operands_and_partials.hpp b/stan/math/rev/scal/meta/operands_and_partials.hpp index eab941168e3..aa369833c71 100644 --- a/stan/math/rev/scal/meta/operands_and_partials.hpp +++ b/stan/math/rev/scal/meta/operands_and_partials.hpp @@ -94,9 +94,8 @@ class operands_and_partials { var build(double value) { size_t size = edge1_.size() + edge2_.size() + edge3_.size() + edge4_.size() + edge5_.size(); - vari** varis = ChainableStack::context().memalloc_.alloc_array(size); - double* partials - = ChainableStack::context().memalloc_.alloc_array(size); + vari** varis = chainable_stack.memalloc_.alloc_array(size); + double* partials = chainable_stack.memalloc_.alloc_array(size); int idx = 0; edge1_.dump_operands(&varis[idx]); edge1_.dump_partials(&partials[idx]); diff --git a/test/unit/math/rev/arr/err/check_bounded_test.cpp b/test/unit/math/rev/arr/err/check_bounded_test.cpp index 2017db19279..d5d8809e9b3 100644 --- a/test/unit/math/rev/arr/err/check_bounded_test.cpp +++ b/test/unit/math/rev/arr/err/check_bounded_test.cpp @@ -14,13 +14,12 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_bounded(function, "a", a, -1.0, 6.0)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/arr/err/check_consistent_size_test.cpp b/test/unit/math/rev/arr/err/check_consistent_size_test.cpp index 2e94b29ed79..b389cdf0d4c 100644 --- a/test/unit/math/rev/arr/err/check_consistent_size_test.cpp +++ b/test/unit/math/rev/arr/err/check_consistent_size_test.cpp @@ -14,13 +14,12 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizeVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_consistent_size(function, "a", a, 5U)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/arr/err/check_consistent_sizes_test.cpp b/test/unit/math/rev/arr/err/check_consistent_sizes_test.cpp index 806a9f489d0..d1c94ea12a9 100644 --- a/test/unit/math/rev/arr/err/check_consistent_sizes_test.cpp +++ b/test/unit/math/rev/arr/err/check_consistent_sizes_test.cpp @@ -17,13 +17,12 @@ TEST(AgradRevErrorHandlingScalar, CheckConsistentSizeVarCheckVectorized) { a.push_back(var(i)); } - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(10U, stack_size); EXPECT_NO_THROW(check_consistent_sizes(function, "a", a, "b", b)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(10U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/arr/err/check_finite_test.cpp b/test/unit/math/rev/arr/err/check_finite_test.cpp index 1913302d747..5d59934401c 100644 --- a/test/unit/math/rev/arr/err/check_finite_test.cpp +++ b/test/unit/math/rev/arr/err/check_finite_test.cpp @@ -15,19 +15,17 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_finite(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); a[1] = std::numeric_limits::infinity(); EXPECT_THROW(check_finite(function, "a", a), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(6U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/arr/err/check_greater_or_equal_test.cpp b/test/unit/math/rev/arr/err/check_greater_or_equal_test.cpp index 9fd17d420fc..1d5ae911683 100644 --- a/test/unit/math/rev/arr/err/check_greater_or_equal_test.cpp +++ b/test/unit/math/rev/arr/err/check_greater_or_equal_test.cpp @@ -17,19 +17,17 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_greater_or_equal(function, "a", a, -1.0)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_greater_or_equal(function, "a", a, 2.0), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/arr/err/check_greater_test.cpp b/test/unit/math/rev/arr/err/check_greater_test.cpp index 2eeac1f7a40..d53ba03b7d7 100644 --- a/test/unit/math/rev/arr/err/check_greater_test.cpp +++ b/test/unit/math/rev/arr/err/check_greater_test.cpp @@ -17,18 +17,16 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_greater(function, "a", a, -1.0)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 2.0), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/arr/err/check_less_or_equal_test.cpp b/test/unit/math/rev/arr/err/check_less_or_equal_test.cpp index 786489c187b..722007290ae 100644 --- a/test/unit/math/rev/arr/err/check_less_or_equal_test.cpp +++ b/test/unit/math/rev/arr/err/check_less_or_equal_test.cpp @@ -17,18 +17,16 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 10.0)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less_or_equal(function, "a", a, 2.0), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/arr/err/check_less_test.cpp b/test/unit/math/rev/arr/err/check_less_test.cpp index c8bbfb0d927..e087a421fc6 100644 --- a/test/unit/math/rev/arr/err/check_less_test.cpp +++ b/test/unit/math/rev/arr/err/check_less_test.cpp @@ -17,18 +17,16 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_less(function, "a", a, 10.0)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); EXPECT_THROW(check_less(function, "a", a, 2.0), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/arr/err/check_nonnegative_test.cpp b/test/unit/math/rev/arr/err/check_nonnegative_test.cpp index 8c793f4e991..38606900e0c 100644 --- a/test/unit/math/rev/arr/err/check_nonnegative_test.cpp +++ b/test/unit/math/rev/arr/err/check_nonnegative_test.cpp @@ -46,31 +46,27 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); a[1] = std::numeric_limits::infinity(); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(6U, stack_size_after_call); a[1] = -1.0; EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(7U, stack_size_after_call); a[1] = 0.0; EXPECT_NO_THROW(check_nonnegative(function, "a", a)); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(8U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/arr/err/check_not_nan_test.cpp b/test/unit/math/rev/arr/err/check_not_nan_test.cpp index 8911947ce2c..31c01a4e53b 100644 --- a/test/unit/math/rev/arr/err/check_not_nan_test.cpp +++ b/test/unit/math/rev/arr/err/check_not_nan_test.cpp @@ -14,13 +14,12 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } @@ -37,13 +36,12 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/arr/err/check_positive_finite_test.cpp b/test/unit/math/rev/arr/err/check_positive_finite_test.cpp index 65406334dda..5fae99f66a3 100644 --- a/test/unit/math/rev/arr/err/check_positive_finite_test.cpp +++ b/test/unit/math/rev/arr/err/check_positive_finite_test.cpp @@ -66,20 +66,18 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckVectorized) { for (int i = 0; i < N; ++i) a.push_back(var(i)); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size); EXPECT_THROW(check_positive_finite(function, "a", a), std::domain_error); EXPECT_NO_THROW(check_positive_finite(function, "a", a[2])); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(5U, stack_size_after_call); a[2] = std::numeric_limits::infinity(); EXPECT_THROW(check_positive_finite(function, "a", a), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(6U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/arr/util.hpp b/test/unit/math/rev/arr/util.hpp index bbbaecc53ef..a23c6d698aa 100644 --- a/test/unit/math/rev/arr/util.hpp +++ b/test/unit/math/rev/arr/util.hpp @@ -10,8 +10,7 @@ namespace test { void check_varis_on_stack(const std::vector& x) { for (size_t n = 0; n < x.size(); ++n) - EXPECT_TRUE( - stan::math::ChainableStack::context().memalloc_.in_stack(x[n].vi_)) + EXPECT_TRUE(stan::math::chainable_stack.memalloc_.in_stack(x[n].vi_)) << n << " is not on the stack"; } diff --git a/test/unit/math/rev/mat/err/check_pos_semidefinite_test.cpp b/test/unit/math/rev/mat/err/check_pos_semidefinite_test.cpp index fa4549351c6..493559813de 100644 --- a/test/unit/math/rev/mat/err/check_pos_semidefinite_test.cpp +++ b/test/unit/math/rev/mat/err/check_pos_semidefinite_test.cpp @@ -51,13 +51,11 @@ TEST(AgradRevErrorHandlingMatrix, CheckPosDefiniteMatrixVarCheck) { y.resize(3, 3); y << 2, -1, 0, -1, 2, -1, 0, -1, 2; - size_t stack_before_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_before_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(10U, stack_before_call); EXPECT_NO_THROW(check_pos_semidefinite("checkPosDefiniteMatrix", "y", y)); - size_t stack_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(10U, stack_after_call); } diff --git a/test/unit/math/rev/mat/fun/log_softmax_test.cpp b/test/unit/math/rev/mat/fun/log_softmax_test.cpp index 7c92c509744..209f5f64810 100644 --- a/test/unit/math/rev/mat/fun/log_softmax_test.cpp +++ b/test/unit/math/rev/mat/fun/log_softmax_test.cpp @@ -23,8 +23,7 @@ TEST(AgradRevMatrix, logSoftmaxLeak) { } Matrix theta = log_softmax(x); } - EXPECT_GT(stan::math::ChainableStack::context().memalloc_.bytes_allocated(), - 4000000); + EXPECT_GT(stan::math::chainable_stack.memalloc_.bytes_allocated(), 4000000); } TEST(AgradRevMatrix, log_softmax) { diff --git a/test/unit/math/rev/mat/fun/softmax_test.cpp b/test/unit/math/rev/mat/fun/softmax_test.cpp index a347e2e562c..4e3739b58a8 100644 --- a/test/unit/math/rev/mat/fun/softmax_test.cpp +++ b/test/unit/math/rev/mat/fun/softmax_test.cpp @@ -24,8 +24,7 @@ TEST(AgradRevMatrix, softmaxLeak) { Matrix theta = softmax(x); } // test is greater than because leak is on heap, not stack - EXPECT_GT(stan::math::ChainableStack::context().memalloc_.bytes_allocated(), - 200000); + EXPECT_GT(stan::math::chainable_stack.memalloc_.bytes_allocated(), 200000); } TEST(AgradRevMatrix, softmax) { diff --git a/test/unit/math/rev/mat/fun/stored_gradient_vari_test.cpp b/test/unit/math/rev/mat/fun/stored_gradient_vari_test.cpp index 14df5e0dc0d..736ed4537f9 100644 --- a/test/unit/math/rev/mat/fun/stored_gradient_vari_test.cpp +++ b/test/unit/math/rev/mat/fun/stored_gradient_vari_test.cpp @@ -7,7 +7,7 @@ TEST(StoredGradientVari, propagate3) { using stan::math::var; using stan::math::vari; vari** xs = reinterpret_cast( - stan::math::ChainableStack::context().memalloc_.alloc(3 * sizeof(vari*))); + stan::math::chainable_stack.memalloc_.alloc(3 * sizeof(vari*))); // value not used here var xs1 = 1; // value not used here @@ -18,8 +18,7 @@ TEST(StoredGradientVari, propagate3) { xs[1] = xs2.vi_; xs[2] = xs3.vi_; double* partials = reinterpret_cast( - stan::math::ChainableStack::context().memalloc_.alloc(3 - * sizeof(double))); + stan::math::chainable_stack.memalloc_.alloc(3 * sizeof(double))); partials[0] = 10; partials[1] = 100; partials[2] = 1000; @@ -68,7 +67,7 @@ TEST(AgradRevMatrix, check_varis_on_stack) { using stan::math::var; using stan::math::vari; vari** xs = reinterpret_cast( - stan::math::ChainableStack::context().memalloc_.alloc(3 * sizeof(vari*))); + stan::math::chainable_stack.memalloc_.alloc(3 * sizeof(vari*))); // value not used here var xs1 = 1; // value not used here @@ -79,8 +78,7 @@ TEST(AgradRevMatrix, check_varis_on_stack) { xs[1] = xs2.vi_; xs[2] = xs3.vi_; double* partials = reinterpret_cast( - stan::math::ChainableStack::context().memalloc_.alloc(3 - * sizeof(double))); + stan::math::chainable_stack.memalloc_.alloc(3 * sizeof(double))); partials[0] = 10; partials[1] = 100; partials[2] = 1000; diff --git a/test/unit/math/rev/mat/functor/gradient_test.cpp b/test/unit/math/rev/mat/functor/gradient_test.cpp index 86aa738bd54..f08a8e61c06 100644 --- a/test/unit/math/rev/mat/functor/gradient_test.cpp +++ b/test/unit/math/rev/mat/functor/gradient_test.cpp @@ -138,6 +138,5 @@ TEST(AgradAutoDiff, RecoverMemory) { } // depends on starting allocation of 65K not being exceeded // without recovery_memory in autodiff::apply_recover(), takes 67M - EXPECT_LT(stan::math::ChainableStack::context().memalloc_.bytes_allocated(), - 100000); + EXPECT_LT(stan::math::chainable_stack.memalloc_.bytes_allocated(), 100000); } diff --git a/test/unit/math/rev/mat/util.hpp b/test/unit/math/rev/mat/util.hpp index 26153fe02b2..a69943bfa45 100644 --- a/test/unit/math/rev/mat/util.hpp +++ b/test/unit/math/rev/mat/util.hpp @@ -11,8 +11,7 @@ template void check_varis_on_stack(const Eigen::Matrix& x) { for (int j = 0; j < x.cols(); ++j) for (int i = 0; i < x.rows(); ++i) - EXPECT_TRUE( - stan::math::ChainableStack::context().memalloc_.in_stack(x(i, j).vi_)) + EXPECT_TRUE(stan::math::chainable_stack.memalloc_.in_stack(x(i, j).vi_)) << i << ", " << j << " is not on the stack"; } diff --git a/test/unit/math/rev/scal/err/check_bounded_test.cpp b/test/unit/math/rev/scal/err/check_bounded_test.cpp index 15b6929b654..5f9f15ffadf 100644 --- a/test/unit/math/rev/scal/err/check_bounded_test.cpp +++ b/test/unit/math/rev/scal/err/check_bounded_test.cpp @@ -121,13 +121,12 @@ TEST(AgradRevErrorHandlingScalar, CheckBoundedVarCheckUnivariate) { const char* function = "check_bounded"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_bounded(function, "a", a, 4.0, 6.0)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/scal/err/check_finite_test.cpp b/test/unit/math/rev/scal/err/check_finite_test.cpp index f42c487fef2..e8f7b4f0f9a 100644 --- a/test/unit/math/rev/scal/err/check_finite_test.cpp +++ b/test/unit/math/rev/scal/err/check_finite_test.cpp @@ -34,19 +34,17 @@ TEST(AgradRevErrorHandlingScalar, CheckFiniteVarCheckUnivariate) { const char* function = "check_finite"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_finite(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); a = std::numeric_limits::infinity(); EXPECT_THROW(check_finite(function, "a", a), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(2U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/scal/err/check_greater_or_equal_test.cpp b/test/unit/math/rev/scal/err/check_greater_or_equal_test.cpp index ceb60c23ff5..24f32a41059 100644 --- a/test/unit/math/rev/scal/err/check_greater_or_equal_test.cpp +++ b/test/unit/math/rev/scal/err/check_greater_or_equal_test.cpp @@ -45,19 +45,17 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterOrEqualVarCheckUnivariate) { const char* function = "check_greater_or_equal"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_greater_or_equal(function, "a", a, 2.0)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_THROW(check_greater_or_equal(function, "a", a, 10.0), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/scal/err/check_greater_test.cpp b/test/unit/math/rev/scal/err/check_greater_test.cpp index 528d883dc0b..26db2a899e7 100644 --- a/test/unit/math/rev/scal/err/check_greater_test.cpp +++ b/test/unit/math/rev/scal/err/check_greater_test.cpp @@ -44,18 +44,16 @@ TEST(AgradRevErrorHandlingScalar, CheckGreaterVarCheckUnivariate) { const char* function = "check_greater"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_greater(function, "a", a, 2.0)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_THROW(check_greater(function, "a", a, 10.0), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/scal/err/check_less_or_equal_test.cpp b/test/unit/math/rev/scal/err/check_less_or_equal_test.cpp index 6417fad7e4a..c20a8a93b2b 100644 --- a/test/unit/math/rev/scal/err/check_less_or_equal_test.cpp +++ b/test/unit/math/rev/scal/err/check_less_or_equal_test.cpp @@ -45,24 +45,21 @@ TEST(AgradRevErrorHandlingScalar, CheckLessOrEqualVarCheckUnivariate) { const char* function = "check_less_or_equal"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_THROW(check_less_or_equal(function, "a", a, 2.0), std::domain_error); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 5.0)); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less_or_equal(function, "a", a, 10.0)); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/scal/err/check_less_test.cpp b/test/unit/math/rev/scal/err/check_less_test.cpp index 8b6e64cfed9..3a853244f81 100644 --- a/test/unit/math/rev/scal/err/check_less_test.cpp +++ b/test/unit/math/rev/scal/err/check_less_test.cpp @@ -44,18 +44,16 @@ TEST(AgradRevErrorHandlingScalar, CheckLessVarCheckUnivariate) { const char* function = "check_less"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_THROW(check_less(function, "a", a, 2.0), std::domain_error); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); EXPECT_NO_THROW(check_less(function, "a", a, 10.0)); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/scal/err/check_nonnegative_test.cpp b/test/unit/math/rev/scal/err/check_nonnegative_test.cpp index de7bb37d17d..47be7bd5c64 100644 --- a/test/unit/math/rev/scal/err/check_nonnegative_test.cpp +++ b/test/unit/math/rev/scal/err/check_nonnegative_test.cpp @@ -37,31 +37,27 @@ TEST(AgradRevErrorHandlingScalar, CheckNonnegativeVarCheckUnivariate) { const char* function = "check_nonnegative"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); a = std::numeric_limits::infinity(); EXPECT_NO_THROW(check_nonnegative(function, "a", a)); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(2U, stack_size_after_call); a = 0.0; EXPECT_NO_THROW(check_nonnegative(function, "a", a)); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(3U, stack_size_after_call); a = -1.1; EXPECT_THROW(check_nonnegative(function, "a", a), std::domain_error); - stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(4U, stack_size_after_call); stan::math::recover_memory(); } diff --git a/test/unit/math/rev/scal/err/check_not_nan_test.cpp b/test/unit/math/rev/scal/err/check_not_nan_test.cpp index de947f25a5b..e7faba12b44 100644 --- a/test/unit/math/rev/scal/err/check_not_nan_test.cpp +++ b/test/unit/math/rev/scal/err/check_not_nan_test.cpp @@ -45,13 +45,12 @@ TEST(AgradRevErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { const char* function = "check_not_nan"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); @@ -64,13 +63,12 @@ TEST(ErrorHandlingScalar, CheckNotNanVarCheckUnivariate) { const char* function = "check_not_nan"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_not_nan(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/scal/err/check_positive_finite_test.cpp b/test/unit/math/rev/scal/err/check_positive_finite_test.cpp index 01bc31c9df4..33bf4761108 100644 --- a/test/unit/math/rev/scal/err/check_positive_finite_test.cpp +++ b/test/unit/math/rev/scal/err/check_positive_finite_test.cpp @@ -37,13 +37,12 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveFiniteVarCheckUnivariate) { const char* function = "check_positive_finite"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_positive_finite(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/scal/err/check_positive_test.cpp b/test/unit/math/rev/scal/err/check_positive_test.cpp index 56a097da2da..d82ccd02740 100644 --- a/test/unit/math/rev/scal/err/check_positive_test.cpp +++ b/test/unit/math/rev/scal/err/check_positive_test.cpp @@ -22,13 +22,12 @@ TEST(AgradRevErrorHandlingScalar, CheckPositiveVarCheckUnivariate) { const char* function = "check_positive"; var a(5.0); - size_t stack_size = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size); EXPECT_NO_THROW(check_positive(function, "a", a)); - size_t stack_size_after_call - = stan::math::ChainableStack::context().var_stack_.size(); + size_t stack_size_after_call = stan::math::chainable_stack.var_stack_.size(); EXPECT_EQ(1U, stack_size_after_call); stan::math::recover_memory(); diff --git a/test/unit/math/rev/scal/util.hpp b/test/unit/math/rev/scal/util.hpp index 37c604cccb2..0efaf0e469d 100644 --- a/test/unit/math/rev/scal/util.hpp +++ b/test/unit/math/rev/scal/util.hpp @@ -7,7 +7,7 @@ namespace test { void check_varis_on_stack(const stan::math::var& x) { - EXPECT_TRUE(stan::math::ChainableStack::context().memalloc_.in_stack(x.vi_)) + EXPECT_TRUE(stan::math::chainable_stack.memalloc_.in_stack(x.vi_)) << "not on the stack"; }