From 7fe241ed371108d50957f8866ce3f39a062ba26c Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 31 Mar 2020 14:10:17 -0400 Subject: [PATCH 01/41] adds reduce_sum and tests --- stan/math/prim/functor/reduce_sum.hpp | 272 ++++++++++ stan/math/prim/functor/reduce_sum_static.hpp | 57 +++ stan/math/rev/functor/reduce_sum.hpp | 243 +++++++++ .../mix/functor/reduce_sum_static_test.cpp | 457 +++++++++++++++++ .../unit/math/mix/functor/reduce_sum_test.cpp | 409 +++++++++++++++ .../prim/functor/reduce_sum_static_test.cpp | 484 ++++++++++++++++++ .../math/prim/functor/reduce_sum_test.cpp | 436 ++++++++++++++++ .../unit/math/rev/functor/reduce_sum_test.cpp | 397 ++++++++++++++ 8 files changed, 2755 insertions(+) create mode 100644 stan/math/prim/functor/reduce_sum.hpp create mode 100644 stan/math/prim/functor/reduce_sum_static.hpp create mode 100644 stan/math/rev/functor/reduce_sum.hpp create mode 100644 test/unit/math/mix/functor/reduce_sum_static_test.cpp create mode 100644 test/unit/math/mix/functor/reduce_sum_test.cpp create mode 100644 test/unit/math/prim/functor/reduce_sum_static_test.cpp create mode 100644 test/unit/math/prim/functor/reduce_sum_test.cpp create mode 100644 test/unit/math/rev/functor/reduce_sum_test.cpp diff --git a/stan/math/prim/functor/reduce_sum.hpp b/stan/math/prim/functor/reduce_sum.hpp new file mode 100644 index 00000000000..25dc700b8eb --- /dev/null +++ b/stan/math/prim/functor/reduce_sum.hpp @@ -0,0 +1,272 @@ +#ifndef STAN_MATH_PRIM_FUNCTOR_REDUCE_SUM_HPP +#define STAN_MATH_PRIM_FUNCTOR_REDUCE_SUM_HPP + +#include + +#include +#include +#include + +#include +#include + +namespace stan { +namespace math { + +namespace internal { + +/** + * reduce_sum_impl implementation for any autodiff type. + * + * @tparam ReduceFunction Type of reducer function + * @tparam ReturnType An arithmetic type + * @tparam Vec Type of sliced argument + * @tparam Args Types of shared arguments + */ +template +struct reduce_sum_impl { + /** + * Call an instance of the function `ReduceFunction` on every element + * of an input sequence and sum these terms. + * + * This specialization is not parallelized and works for any autodiff types. + * + * An instance, f, of `ReduceFunction` should have the signature: + * T f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, + * Args&&... args) + * + * `ReduceFunction` must be default constructible without any arguments + * + * Each call to `ReduceFunction` is responsible for computing the + * start through end terms (inclusive) of the overall sum. All args are + * passed from this function through to the `ReduceFunction` instances. + * However, only the start through end (inclusive) elements of the vmapped + * argument are passed to the `ReduceFunction` instances (as the + * `vmapped_subset` argument). + * + * If auto partitioning is true, do the calculation with one + * ReduceFunction call. If false, break work into pieces strictly smaller + * than grainsize. + * + * grainsize must be greater than or equal to 1 + * + * @param vmapped Sliced arguments used only in some sum terms + * @param auto_partitioning Work partitioning style (ignored) + * @param grainsize Suggested grainsize for tbb + * @param[in, out] msgs The print stream for warning messages + * @param args Shared arguments used in every sum term + * @return Summation of all terms + */ + return_type_t operator()(Vec&& vmapped, bool auto_partitioning, + int grainsize, std::ostream* msgs, + Args&&... args) const { + const std::size_t num_jobs = vmapped.size(); + + if (num_jobs == 0) { + return 0.0; + } + + if (auto_partitioning) { + return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), + msgs, std::forward(args)...); + } else { + return_type_t sum = 0.0; + for (size_t i = 0; i < (vmapped.size() + grainsize - 1) / grainsize; + ++i) { + size_t start = i * grainsize; + size_t end = std::min((i + 1) * grainsize, vmapped.size()) - 1; + + std::decay_t sub_slice; + sub_slice.reserve(end - start + 1); + for (int i = start; i <= end; ++i) { + sub_slice.emplace_back(vmapped[i]); + } + + sum += ReduceFunction()(start, end, std::forward(sub_slice), msgs, + std::forward(args)...); + } + return sum; + } + } +}; + +/** + * Specialization of reduce_sum_impl for arithmetic types + * + * @tparam ReduceFunction Type of reducer function + * @tparam ReturnType An arithmetic type + * @tparam Vec Type of sliced argument + * @tparam Args Types of shared arguments + */ +template +struct reduce_sum_impl, + ReturnType, Vec, Args...> { + /** + * Internal object meeting the Imperative form requirements of + * `tbb::parallel_reduce` + * + * @note see link [here](https://tinyurl.com/vp7xw2t) for requirements. + */ + struct recursive_reducer { + Vec vmapped_; + std::ostream* msgs_; + std::tuple args_tuple_; + return_type_t sum_{0.0}; + + recursive_reducer(Vec&& vmapped, std::ostream* msgs, Args&&... args) + : vmapped_(std::forward(vmapped)), + msgs_(msgs), + args_tuple_(std::forward(args)...) {} + + /* + * This is the copy operator as required for tbb::parallel_reduce + * Imperative form. This requires sum_ be reset to zero. + */ + recursive_reducer(recursive_reducer& other, tbb::split) + : vmapped_(other.vmapped_), + msgs_(other.msgs_), + args_tuple_(other.args_tuple_) {} + + /** + * Compute the value and of `ReduceFunction` over the range defined by r + * and accumulate those in member variable sum_. This function may + * be called multiple times per object instantiation (so the sum_ + * must be accumulated, not just assigned). + * + * @param r Range over which to compute `ReduceFunction` + */ + void operator()(const tbb::blocked_range& r) { + if (r.empty()) { + return; + } + + std::decay_t sub_slice; + sub_slice.reserve(r.size()); + for (int i = r.begin(); i < r.end(); ++i) { + sub_slice.emplace_back(vmapped_[i]); + } + + sum_ += apply( + [&](auto&&... args) { + return ReduceFunction()(r.begin(), r.end() - 1, sub_slice, msgs_, + args...); + }, + this->args_tuple_); + } + + /** + * Join reducers. Accumuluate the value (sum_) of the other reducer. + * + * @param rhs Another partial sum + */ + void join(const recursive_reducer& child) { this->sum_ += child.sum_; } + }; + + /** + * Call an instance of the function `ReduceFunction` on every element + * of an input sequence and sum these terms. + * + * This specialization is parallelized using tbb and works only for + * arithmetic types. + * + * An instance, f, of `ReduceFunction` should have the signature: + * double f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, + * Args&&... args) + * + * `ReduceFunction` must be default constructible without any arguments + * + * Each call to `ReduceFunction` is responsible for computing the + * start through end (inclusive) terms of the overall sum. All args are + * passed from this function through to the `ReduceFunction` instances. + * However, only the start through end (inclusive) elements of the vmapped + * argument are passed to the `ReduceFunction` instances (as the + * `vmapped_subset` argument). + * + * This function distributes computation of the desired sum + * over multiple threads by coordinating calls to `ReduceFunction` + * instances. + * + * If auto partitioning is true, break work into pieces automatically, + * taking grainsize as a recommended work size (this process + * is not deterministic). If false, break work deterministically + * into pieces smaller than or equal to grainsize. The execution + * order is non-deterministic. + * + * grainsize must be greater than or equal to 1 + * + * @param vmapped Sliced arguments used only in some sum terms + * @param auto_partitioning Work partitioning style + * @param grainsize Suggested grainsize for tbb + * @param[in, out] msgs The print stream for warning messages + * @param args Shared arguments used in every sum term + * @return Summation of all terms + */ + ReturnType operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, + std::ostream* msgs, Args&&... args) const { + const std::size_t num_jobs = vmapped.size(); + if (num_jobs == 0) { + return 0.0; + } + recursive_reducer worker(std::forward(vmapped), msgs, + std::forward(args)...); + + if (auto_partitioning) { + tbb::parallel_reduce( + tbb::blocked_range(0, num_jobs, grainsize), worker); + } else { + tbb::simple_partitioner partitioner; + tbb::parallel_deterministic_reduce( + tbb::blocked_range(0, num_jobs, grainsize), worker, + partitioner); + } + + return worker.sum_; + } +}; + +} // namespace internal + +/** + * Call an instance of the function `ReduceFunction` on every element + * of an input sequence and sum these terms. + * + * This defers to reduce_sum_impl for the appropriate implementation + * + * An instance, f, of `ReduceFunction` should have the signature: + * T f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, Args&&... + * args) + * + * `ReduceFunction` must be default constructible without any arguments + * + * grainsize must be greater than or equal to 1 + * + * @tparam ReduceFunction Type of reducer function + * @tparam ReturnType An arithmetic type + * @tparam Vec Type of sliced argument + * @tparam Args Types of shared arguments + * @param vmapped Sliced arguments used only in some sum terms + * @param grainsize Suggested grainsize for tbb + * @param[in, out] msgs The print stream for warning messages + * @param args Shared arguments used in every sum term + * @return Sum of terms + */ +template , typename... Args> +auto reduce_sum(Vec&& vmapped, int grainsize, std::ostream* msgs, + Args&&... args) { + using return_type = return_type_t; + + check_positive("reduce_sum", "grainsize", grainsize); + + return internal::reduce_sum_impl()(std::forward(vmapped), true, + grainsize, msgs, + std::forward(args)...); +} + +} // namespace math +} // namespace stan + +#endif diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp new file mode 100644 index 00000000000..5b13701df93 --- /dev/null +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -0,0 +1,57 @@ +#ifndef STAN_MATH_PRIM_FUNCTOR_REDUCE_SUM_STATIC_HPP +#define STAN_MATH_PRIM_FUNCTOR_REDUCE_SUM_STATIC_HPP + +#include + +#include +#include +#include + +#include +#include + +namespace stan { +namespace math { + +/** + * Call an instance of the function `ReduceFunction` on every element + * of an input sequence and sum these terms. + * + * This defers to reduce_sum_impl for the appropriate implementation + * + * An instance, f, of `ReduceFunction` should have the signature: + * T f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, Args&&... + * args) + * + * `ReduceFunction` must be default constructible without any arguments + * + * grainsize must be greater than or equal to 1 + * + * @tparam ReduceFunction Type of reducer function + * @tparam ReturnType An arithmetic type + * @tparam Vec Type of sliced argument + * @tparam Args Types of shared arguments + * @param vmapped Sliced arguments used only in some sum terms + * @param grainsize Suggested grainsize for tbb + * @param[in, out] msgs The print stream for warning messages + * @param args Shared arguments used in every sum term + * @return Sum of terms + */ +template , typename... Args> +auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs, + Args&&... args) { + using return_type = return_type_t; + + check_positive("reduce_sum", "grainsize", grainsize); + + return internal::reduce_sum_impl()(std::forward(vmapped), false, + grainsize, msgs, + std::forward(args)...); +} + +} // namespace math +} // namespace stan + +#endif diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp new file mode 100644 index 00000000000..aec94c6692a --- /dev/null +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -0,0 +1,243 @@ +#ifndef STAN_MATH_REV_FUNCTOR_REDUCE_SUM_HPP +#define STAN_MATH_REV_FUNCTOR_REDUCE_SUM_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace stan { +namespace math { +namespace internal { + +/** + * Var specialization of reduce_sum_impl + * + * @tparam ReduceFunction Type of reducer function + * @tparam ReturnType Must be var + * @tparam Vec Type of sliced argument + * @tparam Args Types of shared arguments + */ +template +struct reduce_sum_impl, ReturnType, + Vec, Args...> { + /** + * Internal object meeting the Imperative form requirements of + * `tbb::parallel_reduce` + * + * @note see link [here](https://tinyurl.com/vp7xw2t) for requirements. + */ + struct recursive_reducer { + size_t per_job_sliced_terms_; + size_t num_shared_terms_; // Number of terms shared across threads + double* sliced_partials_; // Points to adjoints of the partial calculations + Vec vmapped_; + std::ostream* msgs_; + std::tuple args_tuple_; + double sum_{0.0}; + Eigen::VectorXd args_adjoints_{0}; + + template + recursive_reducer(size_t per_job_sliced_terms, size_t num_shared_terms, + double* sliced_partials, VecT&& vmapped, + std::ostream* msgs, ArgsT&&... args) + : per_job_sliced_terms_(per_job_sliced_terms), + num_shared_terms_(num_shared_terms), + sliced_partials_(sliced_partials), + vmapped_(std::forward(vmapped)), + msgs_(msgs), + args_tuple_(std::forward(args)...) {} + + /* + * This is the copy operator as required for tbb::parallel_reduce + * Imperative form. This requires the reduced values (sum_ and + * arg_adjoints_) be reset to zero. + */ + recursive_reducer(recursive_reducer& other, tbb::split) + : per_job_sliced_terms_(other.per_job_sliced_terms_), + num_shared_terms_(other.num_shared_terms_), + sliced_partials_(other.sliced_partials_), + vmapped_(other.vmapped_), + msgs_(other.msgs_), + args_tuple_(other.args_tuple_) {} + + /** + * Compute, using nested autodiff, the value and Jacobian of + * `ReduceFunction` called over the range defined by r and accumulate those + * in member variable sum_ (for the value) and args_adjoints_ (for the + * Jacobian). This function may be called multiple times per object + * instantiation (so the sum_ and args_adjoints_ must be accumulated, not + * just assigned). + * + * @param r Range over which to compute reduce_sum + */ + inline void operator()(const tbb::blocked_range& r) { + if (r.empty()) { + return; + } + + if (args_adjoints_.size() == 0) { + args_adjoints_ = Eigen::VectorXd::Zero(this->num_shared_terms_); + } + + // Initialize nested autodiff stack + const nested_rev_autodiff begin_nest; + + // Create nested autodiff copies of sliced argument that do not point + // back to main autodiff stack + std::decay_t local_sub_slice; + local_sub_slice.reserve(r.size()); + for (int i = r.begin(); i < r.end(); ++i) { + local_sub_slice.emplace_back(deep_copy_vars(vmapped_[i])); + } + + // Create nested autodiff copies of all shared arguments that do not point + // back to main autodiff stack + auto args_tuple_local_copy = apply( + [&](auto&&... args) { + return std::tuple( + deep_copy_vars(args)...); + }, + this->args_tuple_); + + // Perform calculation + var sub_sum_v = apply( + [&](auto&&... args) { + return ReduceFunction()(r.begin(), r.end() - 1, local_sub_slice, + this->msgs_, args...); + }, + args_tuple_local_copy); + + // Compute Jacobian + sub_sum_v.grad(); + + // Accumulate value of reduce_sum + sum_ += sub_sum_v.val(); + + // Accumulate adjoints of sliced_arguments + accumulate_adjoints( + this->sliced_partials_ + r.begin() * per_job_sliced_terms_, + local_sub_slice); + + // Accumulate adjoints of shared_arguments + apply( + [&](auto&&... args) { + accumulate_adjoints(args_adjoints_.data(), + std::forward(args)...); + }, + std::move(args_tuple_local_copy)); + } + + /** + * Join reducers. Accumuluate the value (sum_) and Jacobian (arg_adoints_) + * of the other reducer. + * + * @param rhs Another partial sum + */ + void join(const recursive_reducer& rhs) { + this->sum_ += rhs.sum_; + if (this->args_adjoints_.size() != 0 && rhs.args_adjoints_.size() != 0) { + this->args_adjoints_ += rhs.args_adjoints_; + } else if (this->args_adjoints_.size() == 0 + && rhs.args_adjoints_.size() != 0) { + this->args_adjoints_ = rhs.args_adjoints_; + } + } + }; + + /** + * Call an instance of the function `ReduceFunction` on every element + * of an input sequence and sum these terms. + * + * This specialization is parallelized using tbb and works for reverse + * mode autodiff. + * + * An instance, f, of `ReduceFunction` should have the signature: + * var f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, + * Args&&... args) + * + * `ReduceFunction` must be default constructible without any arguments + * + * Each call to `ReduceFunction` is responsible for computing the + * start through end (inclusive) terms of the overall sum. All args are + * passed from this function through to the `ReduceFunction` instances. + * However, only the start through end (inclusive) elements of the vmapped + * argument are passed to the `ReduceFunction` instances (as the + * `vmapped_subset` argument). + * + * This function distributes computation of the desired sum and the Jacobian + * of that sum over multiple threads by coordinating calls to `ReduceFunction` + * instances. Results are stored as precomputed varis in the autodiff tree. + * + * If auto partitioning is true, break work into pieces automatically, + * taking grainsize as a recommended work size (this process + * is not deterministic). If false, break work deterministically + * into pieces smaller than or equal to grainsize. The execution + * order is non-deterministic. + * + * @param vmapped Sliced arguments used only in some sum terms + * @param auto_partitioning Work partitioning style + * @param grainsize Suggested grainsize for tbb + * @param[in, out] msgs The print stream for warning messages + * @param args Shared arguments used in every sum term + * @return Summation of all terms + */ + inline var operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, + std::ostream* msgs, Args&&... args) const { + const std::size_t num_jobs = vmapped.size(); + + if (num_jobs == 0) { + return var(0.0); + } + + const std::size_t per_job_sliced_terms = count_vars(vmapped[0]); + const std::size_t num_sliced_terms = num_jobs * per_job_sliced_terms; + const std::size_t num_shared_terms = count_vars(args...); + + vari** varis = ChainableStack::instance_->memalloc_.alloc_array( + num_sliced_terms + num_shared_terms); + double* partials = ChainableStack::instance_->memalloc_.alloc_array( + num_sliced_terms + num_shared_terms); + + for (size_t i = 0; i < num_sliced_terms; ++i) { + partials[i] = 0.0; + } + recursive_reducer worker(per_job_sliced_terms, num_shared_terms, partials, + vmapped, msgs, args...); + + if (auto_partitioning) { + tbb::parallel_reduce( + tbb::blocked_range(0, num_jobs, grainsize), worker); + } else { + tbb::simple_partitioner partitioner; + tbb::parallel_deterministic_reduce( + tbb::blocked_range(0, num_jobs, grainsize), worker, + partitioner); + } + + save_varis(varis, std::forward(vmapped)); + save_varis(varis + num_sliced_terms, std::forward(args)...); + + for (size_t i = 0; i < num_shared_terms; ++i) { + partials[num_sliced_terms + i] = worker.args_adjoints_(i); + } + + return var(new precomputed_gradients_vari( + worker.sum_, num_sliced_terms + num_shared_terms, varis, partials)); + } +}; +} // namespace internal + +} // namespace math +} // namespace stan + +#endif diff --git a/test/unit/math/mix/functor/reduce_sum_static_test.cpp b/test/unit/math/mix/functor/reduce_sum_static_test.cpp new file mode 100644 index 00000000000..844fdc16ebb --- /dev/null +++ b/test/unit/math/mix/functor/reduce_sum_static_test.cpp @@ -0,0 +1,457 @@ +#include +#include + +#include +#include + +std::ostream* msgs = nullptr; + +template * = nullptr> +T sum_(T arg) { + return arg; +} + +template * = nullptr> +auto sum_(EigMat&& arg) { + return stan::math::sum(arg); +} + +template * = nullptr> +auto sum_(Vec&& arg) { + stan::scalar_type_t sum = 0; + for (size_t i = 0; i < arg.size(); ++i) { + sum += sum_(arg[i]); + } + return sum; +} + +struct sum_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, + std::ostream* msgs, Args&&... args) const { + using return_type = stan::return_type_t; + + return sum_(sub_slice) + + sub_slice.size() + * stan::math::sum(std::vector{ + return_type(sum_(std::forward(args)))...}); + } +}; + +TEST(MathMix_reduce_sum_static, grainsize) { + auto f1 = [](auto&& data) { + return stan::math::reduce_sum_static(data, 0, msgs); + }; + auto f2 = [](auto&& data) { + return stan::math::reduce_sum_static(data, -1, msgs); + }; + auto f3 = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + auto f4 = [](auto&& data) { + return stan::math::reduce_sum_static(data, 20, msgs); + }; + + std::vector data(5, 10.0); + + stan::test::expect_ad(f1, data); + stan::test::expect_ad(f2, data); + stan::test::expect_ad(f3, data); + stan::test::expect_ad(f4, data); +} + +TEST(MathMix_reduce_sum_static, std_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + + std::vector data(5, 10.0); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum_static, std_vector_std_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + + std::vector> data(3, std::vector(2, 10.0)); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum_static, std_vector_eigen_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + + std::vector data(3, Eigen::VectorXd::Ones(2)); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum_static, std_vector_eigen_row_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + + std::vector data(3, Eigen::RowVectorXd::Ones(2)); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum_static, std_vector_eigen_matrix_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + + std::vector data(3, Eigen::MatrixXd::Ones(2, 4)); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum_static, std_vector_std_vector_std_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + + std::vector>> data( + 3, std::vector>(2, std::vector(2, 10.0))); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum_static, + std_vector_std_vector_eigen_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + + std::vector> data( + 3, std::vector(2, Eigen::VectorXd::Ones(2))); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum_static, + std_vector_std_vector_eigen_row_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + + std::vector> data( + 3, std::vector(2, Eigen::RowVectorXd::Ones(2))); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum_static, + std_vector_std_vector_eigen_matrix_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, msgs); + }; + + std::vector> data( + 3, std::vector(2, Eigen::MatrixXd::Ones(2, 4))); + + stan::test::expect_ad(f, data); +} + +struct start_end_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, T1&&, + std::ostream* msgs, T2&& data) const { + stan::return_type_t sum = 0; + EXPECT_GE(start, 0); + EXPECT_LE(end, data.size() - 1); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } +}; + +TEST(StanMath_reduce_sum_static, start_end_slice) { + auto start_end = [](auto&& arg) { + return stan::math::reduce_sum_static(arg, 1, msgs, arg); + }; + + std::vector data(5, 1.0); + + stan::test::expect_ad(start_end, data); +} + +auto fi = [](auto&&... args) { + return stan::math::reduce_sum_static(std::vector(2, 10.0), 1, + msgs, args...); +}; + +auto fd = [](auto&& data, auto&&... args) { + return stan::math::reduce_sum_static(data, 1, msgs, args...); +}; + +TEST(MathMix_reduce_sum_static, int_arg) { + std::vector data(2, 1.0); + int arg = 5.0; + + stan::test::expect_ad([&](auto&& data) { return fd(data, arg); }, data); +} + +TEST(MathMix_reduce_sum_static, double_arg) { + std::vector data(2, 10.0); + double arg = 5.0; + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, std_vector_int_arg) { + std::vector data(2, 10.0); + std::vector arg(2, 10); + + stan::test::expect_ad([&](auto&& data) { return fd(data, arg); }, data); +} + +TEST(MathMix_reduce_sum_static, std_vector_double_arg) { + std::vector data(2, 10.0); + std::vector arg(2, 10.0); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, eigen_vector_arg) { + std::vector data(2, 10.0); + Eigen::VectorXd arg = Eigen::VectorXd::Ones(2); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, eigen_row_vector_arg) { + std::vector data(2, 10.0); + Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(2); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, eigen_matrix_arg) { + std::vector data(2, 10.0); + Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, std_vector_std_vector_double_arg) { + std::vector data(2, 10.0); + std::vector> arg(2, std::vector(2, 10.0)); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, std_vector_eigen_vector_arg) { + std::vector data(2, 10.0); + std::vector arg(2, Eigen::VectorXd::Ones(2)); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, std_vector_eigen_row_vector_arg) { + std::vector data(2, 10.0); + std::vector arg(2, Eigen::RowVectorXd::Ones(2)); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, std_vector_eigen_matrix_arg) { + std::vector data(2, 10.0); + std::vector arg(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, std_vector_std_vector_std_vector_double_arg) { + std::vector data(2, 10.0); + std::vector>> arg( + 2, std::vector>(2, std::vector(2, 10.0))); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, std_vector_std_vector_eigen_vector_arg) { + std::vector data(2, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::VectorXd::Ones(2))); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, std_vector_std_vector_eigen_row_vector_arg) { + std::vector data(2, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::RowVectorXd::Ones(2))); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, std_vector_std_vector_eigen_matrix_arg) { + std::vector data(2, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum_static, eigen_three_args1) { + Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); + Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad(fi, arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum_static, eigen_three_args2) { + double arg1 = 1.0; + std::vector arg2(2, 1.0); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad(fi, arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum_static, eigen_three_args3) { + double arg1 = 1.0; + std::vector> arg2(2, std::vector(2, 1.0)); + std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad(fi, arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum_static, eigen_three_args_with_ints1) { + Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); + Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum_static, eigen_three_args_with_ints2) { + double arg1 = 1.0; + std::vector arg2(2, 1.0); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum_static, eigen_three_args_with_ints3) { + double arg1 = 1.0; + std::vector> arg2(2, std::vector(2, 1.0)); + std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum_static, eigen_three_args_with_doubles1) { + Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); + Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum_static, eigen_three_args_with_doubles2) { + double arg1 = 1.0; + std::vector arg2(2, 1.0); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum_static, eigen_three_args_with_doubles3) { + double arg1 = 1.0; + std::vector> arg2(2, std::vector(2, 1.0)); + std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); +} + +template +struct static_check_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, + const std::vector&, std::ostream* msgs, + const std::vector& data) const { + T sum = 0; + // std::cout << "start: " << start << ", end: " << end << ", grainsize: " << + // grainsize << std::endl; + EXPECT_LE(end - start + 1, grainsize); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } +}; + +TEST(StanMathPrim_reduce_sum_static, static_check) { + stan::math::init_threadpool_tbb(); + + for (auto size : {1, 3, 6, 11}) { + std::vector data(size, 10); + std::vector arg(size, 10); + + auto fi1 = [&](auto&&... args) { + return stan::math::reduce_sum_static>(data, 1, msgs, + args...); + }; + + auto fi2 = [&](auto&&... args) { + return stan::math::reduce_sum_static>(data, 2, msgs, + args...); + }; + + auto fi3 = [&](auto&&... args) { + return stan::math::reduce_sum_static>(data, 3, msgs, + args...); + }; + + stan::test::expect_ad(fi1, arg); + stan::test::expect_ad(fi2, arg); + stan::test::expect_ad(fi3, arg); + } +} diff --git a/test/unit/math/mix/functor/reduce_sum_test.cpp b/test/unit/math/mix/functor/reduce_sum_test.cpp new file mode 100644 index 00000000000..ed520821d33 --- /dev/null +++ b/test/unit/math/mix/functor/reduce_sum_test.cpp @@ -0,0 +1,409 @@ +#include +#include + +#include +#include + +std::ostream* msgs = nullptr; + +template * = nullptr> +T sum_(T arg) { + return arg; +} + +template * = nullptr> +auto sum_(EigMat&& arg) { + return stan::math::sum(arg); +} + +template * = nullptr> +auto sum_(Vec&& arg) { + stan::scalar_type_t sum = 0; + for (size_t i = 0; i < arg.size(); ++i) { + sum += sum_(arg[i]); + } + return sum; +} + +struct sum_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, + std::ostream* msgs, Args&&... args) const { + using return_type = stan::return_type_t; + + return sum_(sub_slice) + + sub_slice.size() + * stan::math::sum(std::vector{ + return_type(sum_(std::forward(args)))...}); + } +}; + +TEST(MathMix_reduce_sum, grainsize) { + auto f1 = [](auto&& data) { + return stan::math::reduce_sum(data, 0, msgs); + }; + auto f2 = [](auto&& data) { + return stan::math::reduce_sum(data, -1, msgs); + }; + auto f3 = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + auto f4 = [](auto&& data) { + return stan::math::reduce_sum(data, 20, msgs); + }; + + std::vector data(5, 10.0); + + stan::test::expect_ad(f1, data); + stan::test::expect_ad(f2, data); + stan::test::expect_ad(f3, data); + stan::test::expect_ad(f4, data); +} + +TEST(MathMix_reduce_sum, std_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + + std::vector data(5, 10.0); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + + std::vector> data(3, std::vector(2, 10.0)); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + + std::vector data(3, Eigen::VectorXd::Ones(2)); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_row_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + + std::vector data(3, Eigen::RowVectorXd::Ones(2)); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_matrix_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + + std::vector data(3, Eigen::MatrixXd::Ones(2, 4)); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_std_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + + std::vector>> data( + 3, std::vector>(2, std::vector(2, 10.0))); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + + std::vector> data( + 3, std::vector(2, Eigen::VectorXd::Ones(2))); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_row_vector_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + + std::vector> data( + 3, std::vector(2, Eigen::RowVectorXd::Ones(2))); + + stan::test::expect_ad(f, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_double_slice) { + auto f = [](auto&& data) { + return stan::math::reduce_sum(data, 1, msgs); + }; + + std::vector> data( + 3, std::vector(2, Eigen::MatrixXd::Ones(2, 4))); + + stan::test::expect_ad(f, data); +} + +struct start_end_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, T1&&, + std::ostream* msgs, T2&& data) const { + stan::return_type_t sum = 0; + EXPECT_GE(start, 0); + EXPECT_LE(end, data.size() - 1); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } +}; + +TEST(StanMath_reduce_sum, start_end_slice) { + auto start_end = [](auto&& arg) { + return stan::math::reduce_sum(arg, 1, msgs, arg); + }; + + std::vector data(5, 1.0); + + stan::test::expect_ad(start_end, data); +} + +auto fi = [](auto&&... args) { + return stan::math::reduce_sum(std::vector(2, 10.0), 1, msgs, + args...); +}; + +auto fd = [](auto&& data, auto&&... args) { + return stan::math::reduce_sum(data, 1, msgs, args...); +}; + +TEST(MathMix_reduce_sum, int_arg) { + std::vector data(2, 1.0); + int arg = 5.0; + + stan::test::expect_ad([&](auto&& data) { return fd(data, arg); }, data); +} + +TEST(MathMix_reduce_sum, double_arg) { + std::vector data(2, 10.0); + double arg = 5.0; + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_int_arg) { + std::vector data(2, 10.0); + std::vector arg(2, 10); + + stan::test::expect_ad([&](auto&& data) { return fd(data, arg); }, data); +} + +TEST(MathMix_reduce_sum, std_vector_double_arg) { + std::vector data(2, 10.0); + std::vector arg(2, 10.0); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, eigen_vector_arg) { + std::vector data(2, 10.0); + Eigen::VectorXd arg = Eigen::VectorXd::Ones(2); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, eigen_row_vector_arg) { + std::vector data(2, 10.0); + Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(2); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, eigen_matrix_arg) { + std::vector data(2, 10.0); + Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_double_arg) { + std::vector data(2, 10.0); + std::vector> arg(2, std::vector(2, 10.0)); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_vector_arg) { + std::vector data(2, 10.0); + std::vector arg(2, Eigen::VectorXd::Ones(2)); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_row_vector_arg) { + std::vector data(2, 10.0); + std::vector arg(2, Eigen::RowVectorXd::Ones(2)); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_matrix_arg) { + std::vector data(2, 10.0); + std::vector arg(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_std_vector_double_arg) { + std::vector data(2, 10.0); + std::vector>> arg( + 2, std::vector>(2, std::vector(2, 10.0))); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_vector_arg) { + std::vector data(2, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::VectorXd::Ones(2))); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_row_vector_arg) { + std::vector data(2, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::RowVectorXd::Ones(2))); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_arg) { + std::vector data(2, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); + + stan::test::expect_ad(fi, arg); + stan::test::expect_ad(fd, data, arg); +} + +TEST(MathMix_reduce_sum, eigen_three_args1) { + Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); + Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad(fi, arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args2) { + double arg1 = 1.0; + std::vector arg2(2, 1.0); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad(fi, arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args3) { + double arg1 = 1.0; + std::vector> arg2(2, std::vector(2, 1.0)); + std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad(fi, arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_ints1) { + Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); + Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_ints2) { + double arg1 = 1.0; + std::vector arg2(2, 1.0); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_ints3) { + double arg1 = 1.0; + std::vector> arg2(2, std::vector(2, 1.0)); + std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_doubles1) { + Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); + Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_doubles2) { + double arg1 = 1.0; + std::vector arg2(2, 1.0); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_doubles3) { + double arg1 = 1.0; + std::vector> arg2(2, std::vector(2, 1.0)); + std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); +} diff --git a/test/unit/math/prim/functor/reduce_sum_static_test.cpp b/test/unit/math/prim/functor/reduce_sum_static_test.cpp new file mode 100644 index 00000000000..97350a1284a --- /dev/null +++ b/test/unit/math/prim/functor/reduce_sum_static_test.cpp @@ -0,0 +1,484 @@ +#include +#include +#include +#include +#include +#include +#include + +std::ostream* msgs = nullptr; + +// reduce functor which is the BinaryFunction +// here we use iterators which represent integer indices +template +struct count_lpdf { + count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::poisson_lpmf(sub_slice, lambda[0]); + } +}; + +TEST(StanMathPrim_reduce_sum_static, value) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t elems = 10000; + const std::size_t num_iter = 1000; + std::vector data(elems); + + for (std::size_t i = 0; i != elems; ++i) + data[i] = i; + + std::vector idata; + std::vector vlambda_d(1, lambda_d); + + double poisson_lpdf = stan::math::reduce_sum_static>( + data, 5, msgs, vlambda_d, idata); + + double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); + + EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref); +} + +// ******************************** +// test if nested parallelism works +// ******************************** + +template +struct nesting_count_lpdf { + nesting_count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::reduce_sum_static>(sub_slice, 5, msgs, + lambda, idata); + } +}; + +TEST(StanMathPrim_reduce_sum_static, nesting_value) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t elems = 10000; + const std::size_t num_iter = 1000; + std::vector data(elems); + + for (std::size_t i = 0; i != elems; ++i) + data[i] = i; + + std::vector idata; + std::vector vlambda_d(1, lambda_d); + + double poisson_lpdf = stan::math::reduce_sum_static>( + data, 5, msgs, vlambda_d, idata); + + double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); + + EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref); +} + +namespace stan { + namespace math { + namespace test { + + template * = nullptr> + T sum_(T arg) { + return arg; + } + + template * = nullptr> + auto sum_(EigMat&& arg) { + return stan::math::sum(arg); + } + + template * = nullptr> + auto sum_(Vec&& arg) { + stan::scalar_type_t sum = 0; + for (size_t i = 0; i < arg.size(); ++i) { + sum += sum_(arg[i]); + } + return sum; + } + + struct sum_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, + std::ostream* msgs, Args&&... args) const { + using return_type = stan::return_type_t; + + return sum_(sub_slice) + + sub_slice.size() + * stan::math::sum(std::vector{ + return_type(sum_(std::forward(args)))...}); + } + }; + + } + } +} + +TEST(StanMathPrim_reduce_sum_static, grainsize) { + stan::math::init_threadpool_tbb(); + using stan::math::sum_lpdf; + std::vector data(5, 10); + + EXPECT_THROW(stan::math::reduce_sum_static(data, 0, msgs), + std::domain_error); + + EXPECT_THROW(stan::math::reduce_sum_static(data, -1, msgs), + std::domain_error); + + EXPECT_NO_THROW(stan::math::reduce_sum_static(data, 1, msgs)); + + EXPECT_NO_THROW( + stan::math::reduce_sum_static(data, 3 * data.size(), msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_int_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10); + + EXPECT_EQ(50, stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_double_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + + EXPECT_DOUBLE_EQ(50.0, + stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_double_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data(5, std::vector(2, 10.0)); + + EXPECT_DOUBLE_EQ(100.0, + stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_int_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data(5, std::vector(2, 10)); + + EXPECT_EQ(100, stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_vector_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, Eigen::VectorXd::Ones(2)); + + EXPECT_DOUBLE_EQ(10.0, + stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_row_vector_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, Eigen::RowVectorXd::Ones(2)); + + EXPECT_DOUBLE_EQ(10.0, + stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_matrix_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, Eigen::MatrixXd::Ones(2, 2)); + + EXPECT_DOUBLE_EQ(20.0, + stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, + std_vector_std_vector_std_vector_int_slice) { + stan::math::init_threadpool_tbb(); + + std::vector>> data( + 5, std::vector>(2, std::vector(2, 10.0))); + + EXPECT_DOUBLE_EQ(200, stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, + std_vector_std_vector_std_vector_double_slice) { + stan::math::init_threadpool_tbb(); + + std::vector>> data( + 5, std::vector>(2, std::vector(2, 10.0))); + + EXPECT_DOUBLE_EQ(200.0, + stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_eigen_vector_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data( + 5, std::vector(2, Eigen::VectorXd::Ones(2))); + + EXPECT_DOUBLE_EQ(20.0, + stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, + std_vector_std_vector_eigen_row_vector_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data( + 5, std::vector(2, Eigen::RowVectorXd::Ones(2))); + + EXPECT_DOUBLE_EQ(20.0, + stan::math::reduce_sum_static(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_eigen_matrix_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data( + 5, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); + + EXPECT_DOUBLE_EQ(40.0, + stan::math::reduce_sum_static(data, 1, msgs)); +} + +struct start_end_lpdf { + inline auto operator()(std::size_t start, std::size_t end, + const std::vector&, std::ostream* msgs, + const std::vector& data) const { + int sum = 0; + EXPECT_GE(start, 0); + EXPECT_LE(end, data.size() - 1); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } +}; + +TEST(StanMathPrim_reduce_sum_static, start_end_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10); + + EXPECT_EQ(50, + stan::math::reduce_sum_static(data, 1, msgs, data)); +} + +TEST(StanMathPrim_reduce_sum_static, int_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + int arg = 5; + + EXPECT_DOUBLE_EQ(5 * (10 + 5), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, double_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + double arg = 5.0; + + EXPECT_DOUBLE_EQ(5 * (10.0 + 5.0), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_int_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(5, 10); + + EXPECT_DOUBLE_EQ(5 * (10 + 5 * 10), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_double_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(5, 10.0); + + EXPECT_DOUBLE_EQ(5 * (10 + 5 * 10), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, eigen_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + Eigen::VectorXd arg = Eigen::VectorXd::Ones(5); + + EXPECT_DOUBLE_EQ(5 * (10 + 5), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, eigen_row_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(5); + + EXPECT_DOUBLE_EQ(5 * (10 + 5), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, eigen_matrix_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(5, 5); + + EXPECT_DOUBLE_EQ(5 * (10 + 5 * 5), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_double_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector> arg(5, std::vector(5, 10.0)); + + EXPECT_DOUBLE_EQ(5 * (10 + 250), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(2, Eigen::VectorXd::Ones(5)); + + EXPECT_DOUBLE_EQ(5 * (10 + 10), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_row_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(2, Eigen::RowVectorXd::Ones(5)); + + EXPECT_DOUBLE_EQ(5 * (10 + 10), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_matrix_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(2, Eigen::MatrixXd::Ones(5, 5)); + + EXPECT_DOUBLE_EQ(5 * (10 + 50), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, + std_vector_std_vector_std_vector_double_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector>> arg( + 5, std::vector>(5, std::vector(5, 10.0))); + + EXPECT_DOUBLE_EQ(5 * (10 + 1250), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_eigen_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::VectorXd::Ones(5))); + + EXPECT_DOUBLE_EQ(5 * (10 + 20), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, + std_vector_std_vector_eigen_row_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::RowVectorXd::Ones(5))); + + EXPECT_DOUBLE_EQ(5 * (10 + 20), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_eigen_matrix_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::MatrixXd::Ones(5, 3))); + + EXPECT_DOUBLE_EQ(5 * (10 + 60), + stan::math::reduce_sum_static(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum_static, sum) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 1.0); + int arg1 = 1; + double arg2 = 1.0; + std::vector arg3(5, 1); + std::vector arg4(5, 1.0); + Eigen::VectorXd arg5 = Eigen::VectorXd::Ones(5); + Eigen::RowVectorXd arg6 = Eigen::RowVectorXd::Ones(5); + Eigen::MatrixXd arg7 = Eigen::MatrixXd::Ones(5, 5); + std::vector> arg8(2, arg4); + std::vector arg9(2, arg5); + + EXPECT_DOUBLE_EQ( + 5 + 5 * (1 + 1 + 5 + 5 + 5 + 5 + 25 + 10 + 10), + stan::math::reduce_sum_static( + data, 1, msgs, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)); +} + +template +struct static_check_lpdf { + inline auto operator()(std::size_t start, std::size_t end, + const std::vector&, std::ostream* msgs, + const std::vector& data) const { + int sum = 0; + EXPECT_LE(end - start + 1, grainsize); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } +}; + +TEST(StanMathPrim_reduce_sum_static, static_check) { + stan::math::init_threadpool_tbb(); + + for (auto size : {10, 16, 19, 31}) { + std::vector data(size, 10); + + EXPECT_NO_THROW(stan::math::reduce_sum_static>( + data, 1, msgs, data)); + EXPECT_NO_THROW(stan::math::reduce_sum_static>( + data, 2, msgs, data)); + EXPECT_NO_THROW(stan::math::reduce_sum_static>( + data, 3, msgs, data)); + } +} diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp new file mode 100644 index 00000000000..0ed9bc262ae --- /dev/null +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -0,0 +1,436 @@ +#include +#include +#include +#include +#include +#include +#include + +std::ostream* msgs = nullptr; + +// reduce functor which is the BinaryFunction +// here we use iterators which represent integer indices +template +struct count_lpdf { + count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::poisson_lpmf(sub_slice, lambda[0]); + } +}; + +TEST(StanMathPrim_reduce_sum, value) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t elems = 10000; + const std::size_t num_iter = 1000; + std::vector data(elems); + + for (std::size_t i = 0; i != elems; ++i) + data[i] = i; + + std::vector idata; + std::vector vlambda_d(1, lambda_d); + + double poisson_lpdf = stan::math::reduce_sum>( + data, 5, msgs, vlambda_d, idata); + + double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); + + EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref); +} + +// ******************************** +// test if nested parallelism works +// ******************************** + +template +struct nesting_count_lpdf { + nesting_count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::reduce_sum>(sub_slice, 5, msgs, lambda, + idata); + } +}; + +TEST(StanMathPrim_reduce_sum, nesting_value) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t elems = 10000; + const std::size_t num_iter = 1000; + std::vector data(elems); + + for (std::size_t i = 0; i != elems; ++i) + data[i] = i; + + std::vector idata; + std::vector vlambda_d(1, lambda_d); + + double poisson_lpdf = stan::math::reduce_sum>( + data, 5, msgs, vlambda_d, idata); + + double poisson_static_lpdf = stan::math::reduce_sum_static>( + data, 5, msgs, vlambda_d, idata); + + double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); + + EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref); + EXPECT_FLOAT_EQ(poisson_static_lpdf, poisson_lpdf_ref); +} + +template * = nullptr> +T sum_(T arg) { + return arg; +} + +template * = nullptr> +auto sum_(EigMat&& arg) { + return stan::math::sum(arg); +} + +template * = nullptr> +auto sum_(Vec&& arg) { + stan::scalar_type_t sum = 0; + for (size_t i = 0; i < arg.size(); ++i) { + sum += sum_(arg[i]); + } + return sum; +} + +struct sum_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, + std::ostream* msgs, Args&&... args) const { + using return_type = stan::return_type_t; + + return sum_(sub_slice) + + sub_slice.size() + * stan::math::sum(std::vector{ + return_type(sum_(std::forward(args)))...}); + } +}; + +TEST(StanMathPrim_reduce_sum, grainsize) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10); + + EXPECT_THROW(stan::math::reduce_sum(data, 0, msgs), + std::domain_error); + + EXPECT_THROW(stan::math::reduce_sum(data, -1, msgs), + std::domain_error); + + EXPECT_NO_THROW(stan::math::reduce_sum(data, 1, msgs)); + + EXPECT_NO_THROW( + stan::math::reduce_sum(data, 3 * data.size(), msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_int_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10); + + EXPECT_EQ(50, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_double_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + + EXPECT_DOUBLE_EQ(50.0, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_double_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data(5, std::vector(2, 10.0)); + + EXPECT_DOUBLE_EQ(100.0, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_int_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data(5, std::vector(2, 10)); + + EXPECT_EQ(100, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_eigen_vector_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, Eigen::VectorXd::Ones(2)); + + EXPECT_DOUBLE_EQ(10.0, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_eigen_row_vector_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, Eigen::RowVectorXd::Ones(2)); + + EXPECT_DOUBLE_EQ(10.0, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_eigen_matrix_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, Eigen::MatrixXd::Ones(2, 2)); + + EXPECT_DOUBLE_EQ(20.0, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_std_vector_int_slice) { + stan::math::init_threadpool_tbb(); + + std::vector>> data( + 5, std::vector>(2, std::vector(2, 10.0))); + + EXPECT_DOUBLE_EQ(200, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_std_vector_double_slice) { + stan::math::init_threadpool_tbb(); + + std::vector>> data( + 5, std::vector>(2, std::vector(2, 10.0))); + + EXPECT_DOUBLE_EQ(200.0, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_vector_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data( + 5, std::vector(2, Eigen::VectorXd::Ones(2))); + + EXPECT_DOUBLE_EQ(20.0, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_row_vector_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data( + 5, std::vector(2, Eigen::RowVectorXd::Ones(2))); + + EXPECT_DOUBLE_EQ(20.0, stan::math::reduce_sum(data, 1, msgs)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_matrix_slice) { + stan::math::init_threadpool_tbb(); + + std::vector> data( + 5, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); + + EXPECT_DOUBLE_EQ(40.0, stan::math::reduce_sum(data, 1, msgs)); +} + +struct start_end_lpdf { + inline auto operator()(std::size_t start, std::size_t end, + const std::vector&, std::ostream* msgs, + const std::vector& data) const { + int sum = 0; + EXPECT_GE(start, 0); + EXPECT_LE(end, data.size() - 1); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } +}; + +TEST(StanMathPrim_reduce_sum, start_end_slice) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10); + + EXPECT_EQ(50, stan::math::reduce_sum(data, 1, msgs, data)); +} + +TEST(StanMathPrim_reduce_sum, int_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + int arg = 5; + + EXPECT_DOUBLE_EQ(5 * (10 + 5), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, double_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + double arg = 5.0; + + EXPECT_DOUBLE_EQ(5 * (10.0 + 5.0), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_int_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(5, 10); + + EXPECT_DOUBLE_EQ(5 * (10 + 5 * 10), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_double_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(5, 10.0); + + EXPECT_DOUBLE_EQ(5 * (10 + 5 * 10), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, eigen_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + Eigen::VectorXd arg = Eigen::VectorXd::Ones(5); + + EXPECT_DOUBLE_EQ(5 * (10 + 5), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, eigen_row_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(5); + + EXPECT_DOUBLE_EQ(5 * (10 + 5), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, eigen_matrix_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(5, 5); + + EXPECT_DOUBLE_EQ(5 * (10 + 5 * 5), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_double_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector> arg(5, std::vector(5, 10.0)); + + EXPECT_DOUBLE_EQ(5 * (10 + 250), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_eigen_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(2, Eigen::VectorXd::Ones(5)); + + EXPECT_DOUBLE_EQ(5 * (10 + 10), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_eigen_row_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(2, Eigen::RowVectorXd::Ones(5)); + + EXPECT_DOUBLE_EQ(5 * (10 + 10), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_eigen_matrix_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector arg(2, Eigen::MatrixXd::Ones(5, 5)); + + EXPECT_DOUBLE_EQ(5 * (10 + 50), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_std_vector_double_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector>> arg( + 5, std::vector>(5, std::vector(5, 10.0))); + + EXPECT_DOUBLE_EQ(5 * (10 + 1250), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::VectorXd::Ones(5))); + + EXPECT_DOUBLE_EQ(5 * (10 + 20), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_row_vector_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::RowVectorXd::Ones(5))); + + EXPECT_DOUBLE_EQ(5 * (10 + 20), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_matrix_arg) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 10.0); + std::vector> arg( + 2, std::vector(2, Eigen::MatrixXd::Ones(5, 3))); + + EXPECT_DOUBLE_EQ(5 * (10 + 60), + stan::math::reduce_sum(data, 1, msgs, arg)); +} + +TEST(StanMathPrim_reduce_sum, sum) { + stan::math::init_threadpool_tbb(); + + std::vector data(5, 1.0); + int arg1 = 1; + double arg2 = 1.0; + std::vector arg3(5, 1); + std::vector arg4(5, 1.0); + Eigen::VectorXd arg5 = Eigen::VectorXd::Ones(5); + Eigen::RowVectorXd arg6 = Eigen::RowVectorXd::Ones(5); + Eigen::MatrixXd arg7 = Eigen::MatrixXd::Ones(5, 5); + std::vector> arg8(2, arg4); + std::vector arg9(2, arg5); + + EXPECT_DOUBLE_EQ( + 5 + 5 * (1 + 1 + 5 + 5 + 5 + 5 + 25 + 10 + 10), + stan::math::reduce_sum(data, 1, msgs, arg1, arg2, arg3, arg4, + arg5, arg6, arg7, arg8, arg9)); +} diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp new file mode 100644 index 00000000000..810f4b2d52c --- /dev/null +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -0,0 +1,397 @@ +#include +#include +#include +#include +#include +#include +#include + +std::ostream* msgs = nullptr; + +// reduce functor which is the BinaryFunction +// here we use iterators which represent integer indices +template +struct count_lpdf { + count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::poisson_lpmf(sub_slice, lambda[0]); + } +}; + +TEST(StanMathRev_reduce_sum, value) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t elems = 10000; + const std::size_t num_iter = 1000; + std::vector data(elems); + + for (std::size_t i = 0; i != elems; ++i) + data[i] = i; + + std::vector idata; + std::vector vlambda_d(1, lambda_d); + + double poisson_lpdf = stan::math::reduce_sum>( + data, 5, msgs, vlambda_d, idata); + + double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); + + EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref) + << "ref value of poisson lpdf : " << poisson_lpdf_ref << std::endl + << "value of poisson lpdf : " << poisson_lpdf << std::endl; +} + +TEST(StanMathRev_reduce_sum, gradient) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t elems = 10000; + const std::size_t num_iter = 1000; + std::vector data(elems); + + for (std::size_t i = 0; i != elems; ++i) + data[i] = i; + + using stan::math::var; + + var lambda_v = lambda_d; + + std::vector idata; + std::vector vlambda_v(1, lambda_v); + + var poisson_lpdf = stan::math::reduce_sum>(data, 5, msgs, + vlambda_v, idata); + + var lambda_ref = lambda_d; + var poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_ref); + + EXPECT_FLOAT_EQ(value_of(poisson_lpdf), value_of(poisson_lpdf_ref)); + + stan::math::grad(poisson_lpdf_ref.vi_); + const double lambda_ref_adj = lambda_ref.adj(); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf.vi_); + const double lambda_adj = lambda_v.adj(); + + EXPECT_FLOAT_EQ(lambda_adj, lambda_ref_adj) + << "ref value of poisson lpdf : " << poisson_lpdf_ref.val() << std::endl + << "ref gradient wrt to lambda: " << lambda_ref_adj << std::endl + << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl + << "gradient wrt to lambda: " << lambda_adj << std::endl; + + stan::math::recover_memory(); +} +TEST(StanMathRev_reduce_sum, grainsize) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t elems = 10000; + const std::size_t num_iter = 1000; + std::vector data(elems); + + for (std::size_t i = 0; i != elems; ++i) + data[i] = i; + + using stan::math::var; + + var lambda_v = lambda_d; + + std::vector idata; + std::vector vlambda_v(1, lambda_v); + + EXPECT_THROW( + stan::math::reduce_sum>(data, 0, msgs, vlambda_v, idata), + std::domain_error); + + EXPECT_THROW( + stan::math::reduce_sum>(data, -1, msgs, vlambda_v, idata), + std::domain_error); + + EXPECT_NO_THROW( + stan::math::reduce_sum>(data, 1, msgs, vlambda_v, idata)); + + EXPECT_NO_THROW(stan::math::reduce_sum>(data, 2 * elems, msgs, + vlambda_v, idata)); + + stan::math::recover_memory(); +} + +// ******************************** +// test if nested parallelism works +// ******************************** +template +struct nesting_count_lpdf { + nesting_count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::reduce_sum>(sub_slice, 5, msgs, lambda, + idata); + } +}; + +TEST(StanMathRev_reduce_sum, nesting_gradient) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t elems = 10000; + const std::size_t num_iter = 1000; + std::vector data(elems); + + for (std::size_t i = 0; i != elems; ++i) + data[i] = i; + + using stan::math::var; + + var lambda_v = lambda_d; + + std::vector idata; + std::vector vlambda_v(1, lambda_v); + + var poisson_lpdf = stan::math::reduce_sum>( + data, 5, msgs, vlambda_v, idata); + + var lambda_ref = lambda_d; + var poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_ref); + + EXPECT_FLOAT_EQ(value_of(poisson_lpdf), value_of(poisson_lpdf_ref)); + + stan::math::grad(poisson_lpdf_ref.vi_); + const double lambda_ref_adj = lambda_ref.adj(); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf.vi_); + const double lambda_adj = lambda_v.adj(); + + EXPECT_FLOAT_EQ(lambda_adj, lambda_ref_adj) + << "ref value of poisson lpdf : " << poisson_lpdf_ref.val() << std::endl + << "ref gradient wrt to lambda: " << lambda_ref_adj << std::endl + << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl + << "gradient wrt to lambda: " << lambda_adj << std::endl; + + stan::math::recover_memory(); +} + +// ******************************** +// basic performance test for a hierarchical model +// ******************************** + +template +struct grouped_count_lpdf { + grouped_count_lpdf() {} + + // does the reduction in the sub-slice start to end + template + inline T operator()(std::size_t start, std::size_t end, VecInt1&& sub_slice, + std::ostream* msgs, VecT&& lambda, VecInt2&& gidx) const { + const std::size_t num_terms = end - start + 1; + // std::cout << "sub-slice " << start << " - " << end << "; num_terms = " << + // num_terms << "; size = " << sub_slice.size() << std::endl; + std::decay_t lambda_slice(num_terms); + for (std::size_t i = 0; i != num_terms; ++i) + lambda_slice[i] = lambda[gidx[start + i]]; + + return stan::math::poisson_lpmf(sub_slice, lambda_slice); + } +}; + +TEST(StanMathRev_reduce_sum, grouped_gradient) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t groups = 10; + const std::size_t elems_per_group = 1000; + const std::size_t elems = groups * elems_per_group; + const std::size_t num_iter = 1000; + + std::vector data(elems); + std::vector gidx(elems); + + for (std::size_t i = 0; i != elems; ++i) { + data[i] = i; + gidx[i] = i / elems_per_group; + } + + using stan::math::var; + + std::vector vlambda_v; + + for (std::size_t i = 0; i != groups; ++i) + vlambda_v.push_back(i + 0.2); + + var lambda_v = vlambda_v[0]; + + var poisson_lpdf = stan::math::reduce_sum>( + data, 5, msgs, vlambda_v, gidx); + + std::vector vref_lambda_v; + for (std::size_t i = 0; i != elems; ++i) { + vref_lambda_v.push_back(vlambda_v[gidx[i]]); + } + var lambda_ref = vlambda_v[0]; + var poisson_lpdf_ref = stan::math::poisson_lpmf(data, vref_lambda_v); + + EXPECT_FLOAT_EQ(value_of(poisson_lpdf), value_of(poisson_lpdf_ref)); + + stan::math::grad(poisson_lpdf_ref.vi_); + const double lambda_ref_adj = lambda_ref.adj(); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf.vi_); + const double lambda_adj = lambda_v.adj(); + + EXPECT_FLOAT_EQ(lambda_adj, lambda_ref_adj) + << "ref value of poisson lpdf : " << poisson_lpdf_ref.val() << std::endl + << "ref gradient wrt to lambda: " << lambda_ref_adj << std::endl + << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl + << "gradient wrt to lambda: " << lambda_adj << std::endl; + + stan::math::recover_memory(); +} + +TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t groups = 10; + const std::size_t elems_per_group = 1000; + const std::size_t elems = groups * elems_per_group; + const std::size_t num_iter = 1000; + + std::vector data(elems); + std::vector gidx(elems); + + for (std::size_t i = 0; i != elems; ++i) { + data[i] = i; + gidx[i] = i / elems_per_group; + } + using stan::math::var; + + Eigen::Matrix vlambda_v(groups); + + for (std::size_t i = 0; i != groups; ++i) + vlambda_v[i] = i + 0.2; + var lambda_v = vlambda_v[0]; + + var poisson_lpdf = stan::math::reduce_sum>( + data, 5, msgs, vlambda_v, gidx); + + std::vector vref_lambda_v; + for (std::size_t i = 0; i != elems; ++i) { + vref_lambda_v.push_back(vlambda_v[gidx[i]]); + } + var lambda_ref = vlambda_v[0]; + + var poisson_lpdf_ref = stan::math::poisson_lpmf(data, vref_lambda_v); + + EXPECT_FLOAT_EQ(value_of(poisson_lpdf), value_of(poisson_lpdf_ref)); + + stan::math::grad(poisson_lpdf_ref.vi_); + const double lambda_ref_adj = lambda_ref.adj(); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf.vi_); + const double lambda_adj = lambda_v.adj(); + + EXPECT_FLOAT_EQ(lambda_adj, lambda_ref_adj) + << "ref value of poisson lpdf : " << poisson_lpdf_ref.val() << std::endl + << "ref gradient wrt to lambda: " << lambda_ref_adj << std::endl + << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl + << "gradient wrt to lambda: " << lambda_adj << std::endl; + + stan::math::recover_memory(); +} + +// ******************************** +// slice over the grouping variable which is a var +// ******************************** + +template +struct slice_group_count_lpdf { + slice_group_count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& lambda_slice, std::ostream* msgs, + const std::vector& y, + const std::vector& gsidx) const { + const std::size_t num_groups = end - start + 1; + T result = 0.0; + for (std::size_t i = 0; i != num_groups; ++i) { + std::vector y_group(y.begin() + gsidx[start + i], + y.begin() + gsidx[start + i + 1]); + result += stan::math::poisson_lpmf(y_group, lambda_slice[i]); + } + return result; + } +}; + +TEST(StanMathRev_reduce_sum, slice_group_gradient) { + stan::math::init_threadpool_tbb(); + + double lambda_d = 10.0; + const std::size_t groups = 10; + const std::size_t elems_per_group = 1000; + const std::size_t elems = groups * elems_per_group; + const std::size_t num_iter = 1000; + + std::vector data(elems); + std::vector gidx(elems); + std::vector gsidx(groups + 1); + + for (std::size_t i = 0, k = 0; i != groups; ++i) { + gsidx[i] = k; + for (std::size_t j = 0; j != elems_per_group; ++j, ++k) { + data[k] = k; + gidx[k] = i; + } + gsidx[i + 1] = k; + } + + using stan::math::var; + + std::vector vlambda_v; + + for (std::size_t i = 0; i != groups; ++i) + vlambda_v.push_back(i + 0.2); + + var lambda_v = vlambda_v[0]; + + var poisson_lpdf = stan::math::reduce_sum>( + vlambda_v, 5, msgs, data, gsidx); + + std::vector vref_lambda_v; + for (std::size_t i = 0; i != elems; ++i) { + vref_lambda_v.push_back(vlambda_v[gidx[i]]); + } + var lambda_ref = vlambda_v[0]; + + var poisson_lpdf_ref = stan::math::poisson_lpmf(data, vref_lambda_v); + + EXPECT_FLOAT_EQ(value_of(poisson_lpdf), value_of(poisson_lpdf_ref)); + + stan::math::grad(poisson_lpdf_ref.vi_); + const double lambda_ref_adj = lambda_ref.adj(); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf.vi_); + const double lambda_adj = lambda_v.adj(); + + EXPECT_FLOAT_EQ(lambda_adj, lambda_ref_adj) + << "ref value of poisson lpdf : " << poisson_lpdf_ref.val() << std::endl + << "ref gradient wrt to lambda: " << lambda_ref_adj << std::endl + << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl + << "gradient wrt to lambda: " << lambda_adj << std::endl; + + stan::math::recover_memory(); +} From ec78eb67d3d86a9e00feb895e9e40be334191387 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 31 Mar 2020 14:11:37 -0400 Subject: [PATCH 02/41] remove stan::math::test namespace for now --- .../prim/functor/reduce_sum_static_test.cpp | 65 +++++++++---------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/test/unit/math/prim/functor/reduce_sum_static_test.cpp b/test/unit/math/prim/functor/reduce_sum_static_test.cpp index 97350a1284a..87d3585fcc9 100644 --- a/test/unit/math/prim/functor/reduce_sum_static_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_static_test.cpp @@ -85,46 +85,39 @@ TEST(StanMathPrim_reduce_sum_static, nesting_value) { EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref); } -namespace stan { - namespace math { - namespace test { - - template * = nullptr> - T sum_(T arg) { - return arg; - } - - template * = nullptr> - auto sum_(EigMat&& arg) { - return stan::math::sum(arg); - } - - template * = nullptr> - auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; - } - - struct sum_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, - std::ostream* msgs, Args&&... args) const { - using return_type = stan::return_type_t; - - return sum_(sub_slice) - + sub_slice.size() - * stan::math::sum(std::vector{ - return_type(sum_(std::forward(args)))...}); - } - }; +template * = nullptr> +T sum_(T arg) { + return arg; +} - } +template * = nullptr> +auto sum_(EigMat&& arg) { + return stan::math::sum(arg); +} + +template * = nullptr> +auto sum_(Vec&& arg) { + stan::scalar_type_t sum = 0; + for (size_t i = 0; i < arg.size(); ++i) { + sum += sum_(arg[i]); } + return sum; } +struct sum_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, + std::ostream* msgs, Args&&... args) const { + using return_type = stan::return_type_t; + + return sum_(sub_slice) + + sub_slice.size() + * stan::math::sum(std::vector{ + return_type(sum_(std::forward(args)))...}); + } +}; + + TEST(StanMathPrim_reduce_sum_static, grainsize) { stan::math::init_threadpool_tbb(); using stan::math::sum_lpdf; From 65e65262e2cc66e23a8d05e66189e4bb62a628d1 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 31 Mar 2020 18:51:22 +0000 Subject: [PATCH 03/41] [Jenkins] auto-formatting by clang-format version 5.0.0-3~16.04.1 (tags/RELEASE_500/final) --- test/unit/math/prim/functor/reduce_sum_static_test.cpp | 1 - test/unit/math/prim/functor/reduce_sum_test.cpp | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/math/prim/functor/reduce_sum_static_test.cpp b/test/unit/math/prim/functor/reduce_sum_static_test.cpp index 87d3585fcc9..fbc0ff72b5a 100644 --- a/test/unit/math/prim/functor/reduce_sum_static_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_static_test.cpp @@ -117,7 +117,6 @@ struct sum_lpdf { } }; - TEST(StanMathPrim_reduce_sum_static, grainsize) { stan::math::init_threadpool_tbb(); using stan::math::sum_lpdf; diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index 0ed9bc262ae..d02a24b4133 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -80,8 +80,9 @@ TEST(StanMathPrim_reduce_sum, nesting_value) { double poisson_lpdf = stan::math::reduce_sum>( data, 5, msgs, vlambda_d, idata); - double poisson_static_lpdf = stan::math::reduce_sum_static>( - data, 5, msgs, vlambda_d, idata); + double poisson_static_lpdf + = stan::math::reduce_sum_static>(data, 5, msgs, + vlambda_d, idata); double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); From 928898bb3d534571ed6d0f298cac477d8f32de33 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 31 Mar 2020 18:59:36 -0400 Subject: [PATCH 04/41] cpplint --- stan/math/prim/functor/reduce_sum.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/stan/math/prim/functor/reduce_sum.hpp b/stan/math/prim/functor/reduce_sum.hpp index 25dc700b8eb..d451f1fc94c 100644 --- a/stan/math/prim/functor/reduce_sum.hpp +++ b/stan/math/prim/functor/reduce_sum.hpp @@ -7,6 +7,7 @@ #include #include +#include #include #include From 9d48fab16948df8ae66f2aaeb06c4cb4ad2d2d35 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 31 Mar 2020 23:31:41 -0400 Subject: [PATCH 05/41] merge static and normal reduce_sum tests for mix and prim --- stan/math/prim/functor.hpp | 3 +- .../mix/functor/reduce_sum_static_test.cpp | 457 ----------------- .../unit/math/mix/functor/reduce_sum_test.cpp | 343 ++++++++----- .../prim/functor/reduce_sum_static_test.cpp | 476 ------------------ .../math/prim/functor/reduce_sum_test.cpp | 385 +++----------- test/unit/math/reduce_sum_util.hpp | 202 ++++++++ .../unit/math/rev/functor/reduce_sum_test.cpp | 118 +---- 7 files changed, 506 insertions(+), 1478 deletions(-) delete mode 100644 test/unit/math/mix/functor/reduce_sum_static_test.cpp delete mode 100644 test/unit/math/prim/functor/reduce_sum_static_test.cpp create mode 100644 test/unit/math/reduce_sum_util.hpp diff --git a/stan/math/prim/functor.hpp b/stan/math/prim/functor.hpp index bfa72de41af..f33209dcac3 100644 --- a/stan/math/prim/functor.hpp +++ b/stan/math/prim/functor.hpp @@ -18,5 +18,6 @@ #include #include #include - +#include +#include #endif diff --git a/test/unit/math/mix/functor/reduce_sum_static_test.cpp b/test/unit/math/mix/functor/reduce_sum_static_test.cpp deleted file mode 100644 index 844fdc16ebb..00000000000 --- a/test/unit/math/mix/functor/reduce_sum_static_test.cpp +++ /dev/null @@ -1,457 +0,0 @@ -#include -#include - -#include -#include - -std::ostream* msgs = nullptr; - -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct sum_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, - std::ostream* msgs, Args&&... args) const { - using return_type = stan::return_type_t; - - return sum_(sub_slice) - + sub_slice.size() - * stan::math::sum(std::vector{ - return_type(sum_(std::forward(args)))...}); - } -}; - -TEST(MathMix_reduce_sum_static, grainsize) { - auto f1 = [](auto&& data) { - return stan::math::reduce_sum_static(data, 0, msgs); - }; - auto f2 = [](auto&& data) { - return stan::math::reduce_sum_static(data, -1, msgs); - }; - auto f3 = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - auto f4 = [](auto&& data) { - return stan::math::reduce_sum_static(data, 20, msgs); - }; - - std::vector data(5, 10.0); - - stan::test::expect_ad(f1, data); - stan::test::expect_ad(f2, data); - stan::test::expect_ad(f3, data); - stan::test::expect_ad(f4, data); -} - -TEST(MathMix_reduce_sum_static, std_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - - std::vector data(5, 10.0); - - stan::test::expect_ad(f, data); -} - -TEST(MathMix_reduce_sum_static, std_vector_std_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - - std::vector> data(3, std::vector(2, 10.0)); - - stan::test::expect_ad(f, data); -} - -TEST(MathMix_reduce_sum_static, std_vector_eigen_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - - std::vector data(3, Eigen::VectorXd::Ones(2)); - - stan::test::expect_ad(f, data); -} - -TEST(MathMix_reduce_sum_static, std_vector_eigen_row_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - - std::vector data(3, Eigen::RowVectorXd::Ones(2)); - - stan::test::expect_ad(f, data); -} - -TEST(MathMix_reduce_sum_static, std_vector_eigen_matrix_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - - std::vector data(3, Eigen::MatrixXd::Ones(2, 4)); - - stan::test::expect_ad(f, data); -} - -TEST(MathMix_reduce_sum_static, std_vector_std_vector_std_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - - std::vector>> data( - 3, std::vector>(2, std::vector(2, 10.0))); - - stan::test::expect_ad(f, data); -} - -TEST(MathMix_reduce_sum_static, - std_vector_std_vector_eigen_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - - std::vector> data( - 3, std::vector(2, Eigen::VectorXd::Ones(2))); - - stan::test::expect_ad(f, data); -} - -TEST(MathMix_reduce_sum_static, - std_vector_std_vector_eigen_row_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - - std::vector> data( - 3, std::vector(2, Eigen::RowVectorXd::Ones(2))); - - stan::test::expect_ad(f, data); -} - -TEST(MathMix_reduce_sum_static, - std_vector_std_vector_eigen_matrix_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, msgs); - }; - - std::vector> data( - 3, std::vector(2, Eigen::MatrixXd::Ones(2, 4))); - - stan::test::expect_ad(f, data); -} - -struct start_end_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, T1&&, - std::ostream* msgs, T2&& data) const { - stan::return_type_t sum = 0; - EXPECT_GE(start, 0); - EXPECT_LE(end, data.size() - 1); - for (size_t i = start; i <= end; i++) { - sum += data[i]; - } - return sum; - } -}; - -TEST(StanMath_reduce_sum_static, start_end_slice) { - auto start_end = [](auto&& arg) { - return stan::math::reduce_sum_static(arg, 1, msgs, arg); - }; - - std::vector data(5, 1.0); - - stan::test::expect_ad(start_end, data); -} - -auto fi = [](auto&&... args) { - return stan::math::reduce_sum_static(std::vector(2, 10.0), 1, - msgs, args...); -}; - -auto fd = [](auto&& data, auto&&... args) { - return stan::math::reduce_sum_static(data, 1, msgs, args...); -}; - -TEST(MathMix_reduce_sum_static, int_arg) { - std::vector data(2, 1.0); - int arg = 5.0; - - stan::test::expect_ad([&](auto&& data) { return fd(data, arg); }, data); -} - -TEST(MathMix_reduce_sum_static, double_arg) { - std::vector data(2, 10.0); - double arg = 5.0; - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, std_vector_int_arg) { - std::vector data(2, 10.0); - std::vector arg(2, 10); - - stan::test::expect_ad([&](auto&& data) { return fd(data, arg); }, data); -} - -TEST(MathMix_reduce_sum_static, std_vector_double_arg) { - std::vector data(2, 10.0); - std::vector arg(2, 10.0); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, eigen_vector_arg) { - std::vector data(2, 10.0); - Eigen::VectorXd arg = Eigen::VectorXd::Ones(2); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, eigen_row_vector_arg) { - std::vector data(2, 10.0); - Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(2); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, eigen_matrix_arg) { - std::vector data(2, 10.0); - Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, std_vector_std_vector_double_arg) { - std::vector data(2, 10.0); - std::vector> arg(2, std::vector(2, 10.0)); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, std_vector_eigen_vector_arg) { - std::vector data(2, 10.0); - std::vector arg(2, Eigen::VectorXd::Ones(2)); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, std_vector_eigen_row_vector_arg) { - std::vector data(2, 10.0); - std::vector arg(2, Eigen::RowVectorXd::Ones(2)); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, std_vector_eigen_matrix_arg) { - std::vector data(2, 10.0); - std::vector arg(2, Eigen::MatrixXd::Ones(2, 2)); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, std_vector_std_vector_std_vector_double_arg) { - std::vector data(2, 10.0); - std::vector>> arg( - 2, std::vector>(2, std::vector(2, 10.0))); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, std_vector_std_vector_eigen_vector_arg) { - std::vector data(2, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::VectorXd::Ones(2))); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, std_vector_std_vector_eigen_row_vector_arg) { - std::vector data(2, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::RowVectorXd::Ones(2))); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, std_vector_std_vector_eigen_matrix_arg) { - std::vector data(2, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); -} - -TEST(MathMix_reduce_sum_static, eigen_three_args1) { - Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); - Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad(fi, arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum_static, eigen_three_args2) { - double arg1 = 1.0; - std::vector arg2(2, 1.0); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad(fi, arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum_static, eigen_three_args3) { - double arg1 = 1.0; - std::vector> arg2(2, std::vector(2, 1.0)); - std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); - - stan::test::expect_ad(fi, arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum_static, eigen_three_args_with_ints1) { - Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); - Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum_static, eigen_three_args_with_ints2) { - double arg1 = 1.0; - std::vector arg2(2, 1.0); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum_static, eigen_three_args_with_ints3) { - double arg1 = 1.0; - std::vector> arg2(2, std::vector(2, 1.0)); - std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum_static, eigen_three_args_with_doubles1) { - Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); - Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum_static, eigen_three_args_with_doubles2) { - double arg1 = 1.0; - std::vector arg2(2, 1.0); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum_static, eigen_three_args_with_doubles3) { - double arg1 = 1.0; - std::vector> arg2(2, std::vector(2, 1.0)); - std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); - }, - arg1, arg2, arg3); -} - -template -struct static_check_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, - const std::vector&, std::ostream* msgs, - const std::vector& data) const { - T sum = 0; - // std::cout << "start: " << start << ", end: " << end << ", grainsize: " << - // grainsize << std::endl; - EXPECT_LE(end - start + 1, grainsize); - for (size_t i = start; i <= end; i++) { - sum += data[i]; - } - return sum; - } -}; - -TEST(StanMathPrim_reduce_sum_static, static_check) { - stan::math::init_threadpool_tbb(); - - for (auto size : {1, 3, 6, 11}) { - std::vector data(size, 10); - std::vector arg(size, 10); - - auto fi1 = [&](auto&&... args) { - return stan::math::reduce_sum_static>(data, 1, msgs, - args...); - }; - - auto fi2 = [&](auto&&... args) { - return stan::math::reduce_sum_static>(data, 2, msgs, - args...); - }; - - auto fi3 = [&](auto&&... args) { - return stan::math::reduce_sum_static>(data, 3, msgs, - args...); - }; - - stan::test::expect_ad(fi1, arg); - stan::test::expect_ad(fi2, arg); - stan::test::expect_ad(fi3, arg); - } -} diff --git a/test/unit/math/mix/functor/reduce_sum_test.cpp b/test/unit/math/mix/functor/reduce_sum_test.cpp index ed520821d33..57a3f07ed49 100644 --- a/test/unit/math/mix/functor/reduce_sum_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_test.cpp @@ -1,55 +1,51 @@ #include #include +#include #include #include -std::ostream* msgs = nullptr; -template * = nullptr> -T sum_(T arg) { - return arg; -} -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} +TEST(MathMix_reduce_sum, grainsize) { + using stan::math::test::sum_lpdf; + using stan::math::test::get_new_msg; -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} + auto f1 = [](auto&& data) { + return stan::math::reduce_sum_static(data, 0, get_new_msg()); + }; + auto f2 = [](auto&& data) { + return stan::math::reduce_sum_static(data, -1, get_new_msg()); + }; + auto f3 = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, get_new_msg()); + }; + auto f4 = [](auto&& data) { + return stan::math::reduce_sum_static(data, 20, get_new_msg()); + }; -struct sum_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, - std::ostream* msgs, Args&&... args) const { - using return_type = stan::return_type_t; + std::vector data(5, 10.0); - return sum_(sub_slice) - + sub_slice.size() - * stan::math::sum(std::vector{ - return_type(sum_(std::forward(args)))...}); - } -}; + stan::test::expect_ad(f1, data); + stan::test::expect_ad(f2, data); + stan::test::expect_ad(f3, data); + stan::test::expect_ad(f4, data); +} TEST(MathMix_reduce_sum, grainsize) { + using stan::math::test::sum_lpdf; + using stan::math::test::get_new_msg; auto f1 = [](auto&& data) { - return stan::math::reduce_sum(data, 0, msgs); + return stan::math::reduce_sum(data, 0, get_new_msg()); }; auto f2 = [](auto&& data) { - return stan::math::reduce_sum(data, -1, msgs); + return stan::math::reduce_sum(data, -1, get_new_msg()); }; auto f3 = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); + return stan::math::reduce_sum(data, 1, get_new_msg()); }; auto f4 = [](auto&& data) { - return stan::math::reduce_sum(data, 20, msgs); + return stan::math::reduce_sum(data, 20, get_new_msg()); }; std::vector data(5, 10.0); @@ -61,216 +57,196 @@ TEST(MathMix_reduce_sum, grainsize) { } TEST(MathMix_reduce_sum, std_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); - }; + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector data(5, 10.0); - stan::test::expect_ad(f, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } TEST(MathMix_reduce_sum, std_vector_std_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); - }; + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector> data(3, std::vector(2, 10.0)); - stan::test::expect_ad(f, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } TEST(MathMix_reduce_sum, std_vector_eigen_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); - }; + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector data(3, Eigen::VectorXd::Ones(2)); - stan::test::expect_ad(f, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } TEST(MathMix_reduce_sum, std_vector_eigen_row_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); - }; + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector data(3, Eigen::RowVectorXd::Ones(2)); - stan::test::expect_ad(f, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } TEST(MathMix_reduce_sum, std_vector_eigen_matrix_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); - }; + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector data(3, Eigen::MatrixXd::Ones(2, 4)); - stan::test::expect_ad(f, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } TEST(MathMix_reduce_sum, std_vector_std_vector_std_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); - }; + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector>> data( 3, std::vector>(2, std::vector(2, 10.0))); - stan::test::expect_ad(f, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } -TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); - }; +TEST(MathMix_reduce_sum, + std_vector_std_vector_eigen_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector> data( 3, std::vector(2, Eigen::VectorXd::Ones(2))); - stan::test::expect_ad(f, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } -TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_row_vector_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); - }; +TEST(MathMix_reduce_sum, + std_vector_std_vector_eigen_row_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector> data( 3, std::vector(2, Eigen::RowVectorXd::Ones(2))); - stan::test::expect_ad(f, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } -TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_double_slice) { - auto f = [](auto&& data) { - return stan::math::reduce_sum(data, 1, msgs); - }; +TEST(MathMix_reduce_sum, + std_vector_std_vector_eigen_matrix_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector> data( 3, std::vector(2, Eigen::MatrixXd::Ones(2, 4))); - stan::test::expect_ad(f, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } -struct start_end_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, T1&&, - std::ostream* msgs, T2&& data) const { - stan::return_type_t sum = 0; - EXPECT_GE(start, 0); - EXPECT_LE(end, data.size() - 1); - for (size_t i = start; i <= end; i++) { - sum += data[i]; - } - return sum; - } -}; +TEST(StanMath_reduce_sum_static, start_end_slice) { + using stan::math::test::start_end_lpdf; + using stan::math::test::get_new_msg; + auto start_end_static = [](auto&& arg) { + return stan::math::reduce_sum_static(arg, 1, get_new_msg(), arg); + }; -TEST(StanMath_reduce_sum, start_end_slice) { auto start_end = [](auto&& arg) { - return stan::math::reduce_sum(arg, 1, msgs, arg); + return stan::math::reduce_sum_static(arg, 1, get_new_msg(), arg); }; std::vector data(5, 1.0); stan::test::expect_ad(start_end, data); + stan::test::expect_ad(start_end_static, data); } -auto fi = [](auto&&... args) { - return stan::math::reduce_sum(std::vector(2, 10.0), 1, msgs, - args...); -}; -auto fd = [](auto&& data, auto&&... args) { - return stan::math::reduce_sum(data, 1, msgs, args...); -}; TEST(MathMix_reduce_sum, int_arg) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector data(2, 1.0); int arg = 5.0; - stan::test::expect_ad([&](auto&& data) { return fd(data, arg); }, data); -} + stan::test::expect_ad([&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); + stan::test::expect_ad([&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); -TEST(MathMix_reduce_sum, double_arg) { - std::vector data(2, 10.0); - double arg = 5.0; - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); } TEST(MathMix_reduce_sum, std_vector_int_arg) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector data(2, 10.0); std::vector arg(2, 10); - stan::test::expect_ad([&](auto&& data) { return fd(data, arg); }, data); + stan::test::expect_ad([&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); + stan::test::expect_ad([&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); } -TEST(MathMix_reduce_sum, std_vector_double_arg) { - std::vector data(2, 10.0); - std::vector arg(2, 10.0); +TEST(MathMix_reduce_sum, double_arg) { + stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), 5.0); +} - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); +TEST(MathMix_reduce_sum, std_vector_double_arg) { + stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), std::vector(2, 10.0)); } TEST(MathMix_reduce_sum, eigen_vector_arg) { std::vector data(2, 10.0); Eigen::VectorXd arg = Eigen::VectorXd::Ones(2); - - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, eigen_row_vector_arg) { std::vector data(2, 10.0); Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(2); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, eigen_matrix_arg) { std::vector data(2, 10.0); Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(2, 2); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, std_vector_std_vector_double_arg) { std::vector data(2, 10.0); std::vector> arg(2, std::vector(2, 10.0)); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, std_vector_eigen_vector_arg) { std::vector data(2, 10.0); std::vector arg(2, Eigen::VectorXd::Ones(2)); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, std_vector_eigen_row_vector_arg) { std::vector data(2, 10.0); std::vector arg(2, Eigen::RowVectorXd::Ones(2)); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, std_vector_eigen_matrix_arg) { std::vector data(2, 10.0); std::vector arg(2, Eigen::MatrixXd::Ones(2, 2)); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, std_vector_std_vector_std_vector_double_arg) { @@ -278,8 +254,7 @@ TEST(MathMix_reduce_sum, std_vector_std_vector_std_vector_double_arg) { std::vector>> arg( 2, std::vector>(2, std::vector(2, 10.0))); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_vector_arg) { @@ -287,8 +262,7 @@ TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_vector_arg) { std::vector> arg( 2, std::vector(2, Eigen::VectorXd::Ones(2))); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_row_vector_arg) { @@ -296,8 +270,7 @@ TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_row_vector_arg) { std::vector> arg( 2, std::vector(2, Eigen::RowVectorXd::Ones(2))); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_arg) { @@ -305,105 +278,197 @@ TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_arg) { std::vector> arg( 2, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); - stan::test::expect_ad(fi, arg); - stan::test::expect_ad(fd, data, arg); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); } TEST(MathMix_reduce_sum, eigen_three_args1) { + using stan::math::test::reduce_sum_static_int_sum_lpdf; + using stan::math::test::reduce_sum_int_sum_lpdf; Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - stan::test::expect_ad(fi, arg1, arg2, arg3); + stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg1, arg2, arg3); + stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg1, arg2, arg3); } TEST(MathMix_reduce_sum, eigen_three_args2) { + using stan::math::test::reduce_sum_static_int_sum_lpdf; + using stan::math::test::reduce_sum_int_sum_lpdf; double arg1 = 1.0; std::vector arg2(2, 1.0); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - stan::test::expect_ad(fi, arg1, arg2, arg3); + stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg1, arg2, arg3); + stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg1, arg2, arg3); } TEST(MathMix_reduce_sum, eigen_three_args3) { + using stan::math::test::reduce_sum_static_int_sum_lpdf; + using stan::math::test::reduce_sum_int_sum_lpdf; double arg1 = 1.0; std::vector> arg2(2, std::vector(2, 1.0)); std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); - stan::test::expect_ad(fi, arg1, arg2, arg3); + stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg1, arg2, arg3); + stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg1, arg2, arg3); } TEST(MathMix_reduce_sum, eigen_three_args_with_ints1) { + using stan::math::test::reduce_sum_static_int_sum_lpdf; + using stan::math::test::reduce_sum_int_sum_lpdf; Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + return reduce_sum_static_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); }, arg1, arg2, arg3); } TEST(MathMix_reduce_sum, eigen_three_args_with_ints2) { + using stan::math::test::reduce_sum_static_int_sum_lpdf; + using stan::math::test::reduce_sum_int_sum_lpdf; double arg1 = 1.0; std::vector arg2(2, 1.0); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + return reduce_sum_static_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); }, arg1, arg2, arg3); + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); + } TEST(MathMix_reduce_sum, eigen_three_args_with_ints3) { + using stan::math::test::reduce_sum_static_int_sum_lpdf; + using stan::math::test::reduce_sum_int_sum_lpdf; double arg1 = 1.0; std::vector> arg2(2, std::vector(2, 1.0)); std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + return reduce_sum_static_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); }, arg1, arg2, arg3); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); + } TEST(MathMix_reduce_sum, eigen_three_args_with_doubles1) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + return reduce_sum_static_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, std::vector{1.0, 2.0, 3.0}, arg3); }, arg1, arg2, arg3); } TEST(MathMix_reduce_sum, eigen_three_args_with_doubles2) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; double arg1 = 1.0; std::vector arg2(2, 1.0); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + return reduce_sum_static_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, std::vector{1.0, 2.0, 3.0}, arg3); }, arg1, arg2, arg3); + } TEST(MathMix_reduce_sum, eigen_three_args_with_doubles3) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; double arg1 = 1.0; std::vector> arg2(2, std::vector(2, 1.0)); std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return fi(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + return reduce_sum_static_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, std::vector{1.0, 2.0, 3.0}, arg3); }, arg1, arg2, arg3); + +} + + +TEST(MathMix_reduce_sum, static_check) { + stan::math::init_threadpool_tbb(); + using stan::math::test::get_new_msg; + using stan::math::test::static_check_lpdf; + + for (auto size : {1, 3, 6, 11}) { + std::vector data(size, 10); + std::vector arg(size, 10); + + auto fi1 = [&](auto&&... args) { + return stan::math::reduce_sum_static>(data, 1, get_new_msg(), + args...); + }; + + auto fi2 = [&](auto&&... args) { + return stan::math::reduce_sum_static>(data, 2, get_new_msg(), + args...); + }; + + auto fi3 = [&](auto&&... args) { + return stan::math::reduce_sum_static>(data, 3, get_new_msg(), + args...); + }; + + stan::test::expect_ad(fi1, arg); + stan::test::expect_ad(fi2, arg); + stan::test::expect_ad(fi3, arg); + } } diff --git a/test/unit/math/prim/functor/reduce_sum_static_test.cpp b/test/unit/math/prim/functor/reduce_sum_static_test.cpp deleted file mode 100644 index fbc0ff72b5a..00000000000 --- a/test/unit/math/prim/functor/reduce_sum_static_test.cpp +++ /dev/null @@ -1,476 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -std::ostream* msgs = nullptr; - -// reduce functor which is the BinaryFunction -// here we use iterators which represent integer indices -template -struct count_lpdf { - count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& sub_slice, std::ostream* msgs, - const std::vector& lambda, - const std::vector& idata) const { - return stan::math::poisson_lpmf(sub_slice, lambda[0]); - } -}; - -TEST(StanMathPrim_reduce_sum_static, value) { - stan::math::init_threadpool_tbb(); - - double lambda_d = 10.0; - const std::size_t elems = 10000; - const std::size_t num_iter = 1000; - std::vector data(elems); - - for (std::size_t i = 0; i != elems; ++i) - data[i] = i; - - std::vector idata; - std::vector vlambda_d(1, lambda_d); - - double poisson_lpdf = stan::math::reduce_sum_static>( - data, 5, msgs, vlambda_d, idata); - - double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); - - EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref); -} - -// ******************************** -// test if nested parallelism works -// ******************************** - -template -struct nesting_count_lpdf { - nesting_count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& sub_slice, std::ostream* msgs, - const std::vector& lambda, - const std::vector& idata) const { - return stan::math::reduce_sum_static>(sub_slice, 5, msgs, - lambda, idata); - } -}; - -TEST(StanMathPrim_reduce_sum_static, nesting_value) { - stan::math::init_threadpool_tbb(); - - double lambda_d = 10.0; - const std::size_t elems = 10000; - const std::size_t num_iter = 1000; - std::vector data(elems); - - for (std::size_t i = 0; i != elems; ++i) - data[i] = i; - - std::vector idata; - std::vector vlambda_d(1, lambda_d); - - double poisson_lpdf = stan::math::reduce_sum_static>( - data, 5, msgs, vlambda_d, idata); - - double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); - - EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref); -} - -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct sum_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, - std::ostream* msgs, Args&&... args) const { - using return_type = stan::return_type_t; - - return sum_(sub_slice) - + sub_slice.size() - * stan::math::sum(std::vector{ - return_type(sum_(std::forward(args)))...}); - } -}; - -TEST(StanMathPrim_reduce_sum_static, grainsize) { - stan::math::init_threadpool_tbb(); - using stan::math::sum_lpdf; - std::vector data(5, 10); - - EXPECT_THROW(stan::math::reduce_sum_static(data, 0, msgs), - std::domain_error); - - EXPECT_THROW(stan::math::reduce_sum_static(data, -1, msgs), - std::domain_error); - - EXPECT_NO_THROW(stan::math::reduce_sum_static(data, 1, msgs)); - - EXPECT_NO_THROW( - stan::math::reduce_sum_static(data, 3 * data.size(), msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_int_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10); - - EXPECT_EQ(50, stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_double_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - - EXPECT_DOUBLE_EQ(50.0, - stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_double_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data(5, std::vector(2, 10.0)); - - EXPECT_DOUBLE_EQ(100.0, - stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_int_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data(5, std::vector(2, 10)); - - EXPECT_EQ(100, stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_vector_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, Eigen::VectorXd::Ones(2)); - - EXPECT_DOUBLE_EQ(10.0, - stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_row_vector_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, Eigen::RowVectorXd::Ones(2)); - - EXPECT_DOUBLE_EQ(10.0, - stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_matrix_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, Eigen::MatrixXd::Ones(2, 2)); - - EXPECT_DOUBLE_EQ(20.0, - stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, - std_vector_std_vector_std_vector_int_slice) { - stan::math::init_threadpool_tbb(); - - std::vector>> data( - 5, std::vector>(2, std::vector(2, 10.0))); - - EXPECT_DOUBLE_EQ(200, stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, - std_vector_std_vector_std_vector_double_slice) { - stan::math::init_threadpool_tbb(); - - std::vector>> data( - 5, std::vector>(2, std::vector(2, 10.0))); - - EXPECT_DOUBLE_EQ(200.0, - stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_eigen_vector_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data( - 5, std::vector(2, Eigen::VectorXd::Ones(2))); - - EXPECT_DOUBLE_EQ(20.0, - stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, - std_vector_std_vector_eigen_row_vector_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data( - 5, std::vector(2, Eigen::RowVectorXd::Ones(2))); - - EXPECT_DOUBLE_EQ(20.0, - stan::math::reduce_sum_static(data, 1, msgs)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_eigen_matrix_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data( - 5, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); - - EXPECT_DOUBLE_EQ(40.0, - stan::math::reduce_sum_static(data, 1, msgs)); -} - -struct start_end_lpdf { - inline auto operator()(std::size_t start, std::size_t end, - const std::vector&, std::ostream* msgs, - const std::vector& data) const { - int sum = 0; - EXPECT_GE(start, 0); - EXPECT_LE(end, data.size() - 1); - for (size_t i = start; i <= end; i++) { - sum += data[i]; - } - return sum; - } -}; - -TEST(StanMathPrim_reduce_sum_static, start_end_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10); - - EXPECT_EQ(50, - stan::math::reduce_sum_static(data, 1, msgs, data)); -} - -TEST(StanMathPrim_reduce_sum_static, int_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - int arg = 5; - - EXPECT_DOUBLE_EQ(5 * (10 + 5), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, double_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - double arg = 5.0; - - EXPECT_DOUBLE_EQ(5 * (10.0 + 5.0), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_int_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(5, 10); - - EXPECT_DOUBLE_EQ(5 * (10 + 5 * 10), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_double_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(5, 10.0); - - EXPECT_DOUBLE_EQ(5 * (10 + 5 * 10), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, eigen_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - Eigen::VectorXd arg = Eigen::VectorXd::Ones(5); - - EXPECT_DOUBLE_EQ(5 * (10 + 5), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, eigen_row_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(5); - - EXPECT_DOUBLE_EQ(5 * (10 + 5), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, eigen_matrix_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(5, 5); - - EXPECT_DOUBLE_EQ(5 * (10 + 5 * 5), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_double_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector> arg(5, std::vector(5, 10.0)); - - EXPECT_DOUBLE_EQ(5 * (10 + 250), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(2, Eigen::VectorXd::Ones(5)); - - EXPECT_DOUBLE_EQ(5 * (10 + 10), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_row_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(2, Eigen::RowVectorXd::Ones(5)); - - EXPECT_DOUBLE_EQ(5 * (10 + 10), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_eigen_matrix_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(2, Eigen::MatrixXd::Ones(5, 5)); - - EXPECT_DOUBLE_EQ(5 * (10 + 50), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, - std_vector_std_vector_std_vector_double_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector>> arg( - 5, std::vector>(5, std::vector(5, 10.0))); - - EXPECT_DOUBLE_EQ(5 * (10 + 1250), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_eigen_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::VectorXd::Ones(5))); - - EXPECT_DOUBLE_EQ(5 * (10 + 20), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, - std_vector_std_vector_eigen_row_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::RowVectorXd::Ones(5))); - - EXPECT_DOUBLE_EQ(5 * (10 + 20), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, std_vector_std_vector_eigen_matrix_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::MatrixXd::Ones(5, 3))); - - EXPECT_DOUBLE_EQ(5 * (10 + 60), - stan::math::reduce_sum_static(data, 1, msgs, arg)); -} - -TEST(StanMathPrim_reduce_sum_static, sum) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 1.0); - int arg1 = 1; - double arg2 = 1.0; - std::vector arg3(5, 1); - std::vector arg4(5, 1.0); - Eigen::VectorXd arg5 = Eigen::VectorXd::Ones(5); - Eigen::RowVectorXd arg6 = Eigen::RowVectorXd::Ones(5); - Eigen::MatrixXd arg7 = Eigen::MatrixXd::Ones(5, 5); - std::vector> arg8(2, arg4); - std::vector arg9(2, arg5); - - EXPECT_DOUBLE_EQ( - 5 + 5 * (1 + 1 + 5 + 5 + 5 + 5 + 25 + 10 + 10), - stan::math::reduce_sum_static( - data, 1, msgs, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)); -} - -template -struct static_check_lpdf { - inline auto operator()(std::size_t start, std::size_t end, - const std::vector&, std::ostream* msgs, - const std::vector& data) const { - int sum = 0; - EXPECT_LE(end - start + 1, grainsize); - for (size_t i = start; i <= end; i++) { - sum += data[i]; - } - return sum; - } -}; - -TEST(StanMathPrim_reduce_sum_static, static_check) { - stan::math::init_threadpool_tbb(); - - for (auto size : {10, 16, 19, 31}) { - std::vector data(size, 10); - - EXPECT_NO_THROW(stan::math::reduce_sum_static>( - data, 1, msgs, data)); - EXPECT_NO_THROW(stan::math::reduce_sum_static>( - data, 2, msgs, data)); - EXPECT_NO_THROW(stan::math::reduce_sum_static>( - data, 3, msgs, data)); - } -} diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index d02a24b4133..812b0dd0ecd 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -1,71 +1,11 @@ -#include #include +#include #include -#include -#include -#include -#include - -std::ostream* msgs = nullptr; - -// reduce functor which is the BinaryFunction -// here we use iterators which represent integer indices -template -struct count_lpdf { - count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& sub_slice, std::ostream* msgs, - const std::vector& lambda, - const std::vector& idata) const { - return stan::math::poisson_lpmf(sub_slice, lambda[0]); - } -}; TEST(StanMathPrim_reduce_sum, value) { stan::math::init_threadpool_tbb(); - - double lambda_d = 10.0; - const std::size_t elems = 10000; - const std::size_t num_iter = 1000; - std::vector data(elems); - - for (std::size_t i = 0; i != elems; ++i) - data[i] = i; - - std::vector idata; - std::vector vlambda_d(1, lambda_d); - - double poisson_lpdf = stan::math::reduce_sum>( - data, 5, msgs, vlambda_d, idata); - - double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); - - EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref); -} - -// ******************************** -// test if nested parallelism works -// ******************************** - -template -struct nesting_count_lpdf { - nesting_count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& sub_slice, std::ostream* msgs, - const std::vector& lambda, - const std::vector& idata) const { - return stan::math::reduce_sum>(sub_slice, 5, msgs, lambda, - idata); - } -}; - -TEST(StanMathPrim_reduce_sum, nesting_value) { - stan::math::init_threadpool_tbb(); - + using stan::math::test::get_new_msg; + using stan::math::test::count_lpdf; double lambda_d = 10.0; const std::size_t elems = 10000; const std::size_t num_iter = 1000; @@ -78,360 +18,179 @@ TEST(StanMathPrim_reduce_sum, nesting_value) { std::vector vlambda_d(1, lambda_d); double poisson_lpdf = stan::math::reduce_sum>( - data, 5, msgs, vlambda_d, idata); - - double poisson_static_lpdf - = stan::math::reduce_sum_static>(data, 5, msgs, - vlambda_d, idata); + data, 5, get_new_msg(), vlambda_d, idata); + double poisson_static_lpdf = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_d, idata); double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); - + // NOTE:(Steve) This fails with EXPECT_DOUBLE_EQ at about 10e-7 EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref); EXPECT_FLOAT_EQ(poisson_static_lpdf, poisson_lpdf_ref); } -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct sum_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, - std::ostream* msgs, Args&&... args) const { - using return_type = stan::return_type_t; - - return sum_(sub_slice) - + sub_slice.size() - * stan::math::sum(std::vector{ - return_type(sum_(std::forward(args)))...}); - } -}; TEST(StanMathPrim_reduce_sum, grainsize) { stan::math::init_threadpool_tbb(); + using stan::math::test::get_new_msg; + using stan::math::test::sum_lpdf; std::vector data(5, 10); - EXPECT_THROW(stan::math::reduce_sum(data, 0, msgs), + EXPECT_THROW(stan::math::reduce_sum(data, 0, get_new_msg()), + std::domain_error); + + EXPECT_THROW(stan::math::reduce_sum(data, -1, get_new_msg()), std::domain_error); - EXPECT_THROW(stan::math::reduce_sum(data, -1, msgs), + EXPECT_NO_THROW(stan::math::reduce_sum(data, 1, get_new_msg())); + + EXPECT_NO_THROW( + stan::math::reduce_sum(data, 3 * data.size(), get_new_msg())); + EXPECT_THROW(stan::math::reduce_sum_static(data, 0, get_new_msg()), + std::domain_error); + + EXPECT_THROW(stan::math::reduce_sum_static(data, -1, get_new_msg()), std::domain_error); - EXPECT_NO_THROW(stan::math::reduce_sum(data, 1, msgs)); + EXPECT_NO_THROW(stan::math::reduce_sum_static(data, 1, get_new_msg())); EXPECT_NO_THROW( - stan::math::reduce_sum(data, 3 * data.size(), msgs)); + stan::math::reduce_sum_static(data, 3 * data.size(), get_new_msg())); } -TEST(StanMathPrim_reduce_sum, std_vector_int_slice) { +TEST(StanMathPrim_reduce_sum, start_end_slice) { stan::math::init_threadpool_tbb(); + using stan::math::test::get_new_msg; + using stan::math::test::start_end_lpdf; std::vector data(5, 10); - EXPECT_EQ(50, stan::math::reduce_sum(data, 1, msgs)); + EXPECT_EQ(50, stan::math::reduce_sum(data, 1, get_new_msg(), data)); } -TEST(StanMathPrim_reduce_sum, std_vector_double_slice) { - stan::math::init_threadpool_tbb(); - std::vector data(5, 10.0); +TEST(StanMathPrim_reduce_sum, std_vector_int_slice) { + stan::math::test::test_slices(50, 10); +} - EXPECT_DOUBLE_EQ(50.0, stan::math::reduce_sum(data, 1, msgs)); +TEST(StanMathPrim_reduce_sum, std_vector_double_slice) { + stan::math::test::test_slices(50.0, 10.0); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_double_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data(5, std::vector(2, 10.0)); - - EXPECT_DOUBLE_EQ(100.0, stan::math::reduce_sum(data, 1, msgs)); + stan::math::test::test_slices(100.0, std::vector(2, 10)); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_int_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data(5, std::vector(2, 10)); - - EXPECT_EQ(100, stan::math::reduce_sum(data, 1, msgs)); + stan::math::test::test_slices(100, std::vector(2, 10)); } TEST(StanMathPrim_reduce_sum, std_vector_eigen_vector_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, Eigen::VectorXd::Ones(2)); - - EXPECT_DOUBLE_EQ(10.0, stan::math::reduce_sum(data, 1, msgs)); + Eigen::VectorXd test_x = Eigen::VectorXd::Ones(2); + stan::math::test::test_slices(10.0, test_x); } TEST(StanMathPrim_reduce_sum, std_vector_eigen_row_vector_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, Eigen::RowVectorXd::Ones(2)); - - EXPECT_DOUBLE_EQ(10.0, stan::math::reduce_sum(data, 1, msgs)); + Eigen::RowVectorXd test_x = Eigen::RowVectorXd::Ones(2); + stan::math::test::test_slices(10.0, test_x); } TEST(StanMathPrim_reduce_sum, std_vector_eigen_matrix_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, Eigen::MatrixXd::Ones(2, 2)); - - EXPECT_DOUBLE_EQ(20.0, stan::math::reduce_sum(data, 1, msgs)); + Eigen::MatrixXd test_x = Eigen::MatrixXd::Ones(2, 2); + stan::math::test::test_slices(20.0, test_x); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_std_vector_int_slice) { - stan::math::init_threadpool_tbb(); - - std::vector>> data( - 5, std::vector>(2, std::vector(2, 10.0))); - - EXPECT_DOUBLE_EQ(200, stan::math::reduce_sum(data, 1, msgs)); + stan::math::test::test_slices(200, std::vector>(2, std::vector(2, 10.0))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_std_vector_double_slice) { - stan::math::init_threadpool_tbb(); - - std::vector>> data( - 5, std::vector>(2, std::vector(2, 10.0))); - - EXPECT_DOUBLE_EQ(200.0, stan::math::reduce_sum(data, 1, msgs)); + stan::math::test::test_slices(200.0, std::vector>(2, std::vector(2, 10.0))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_vector_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data( - 5, std::vector(2, Eigen::VectorXd::Ones(2))); - - EXPECT_DOUBLE_EQ(20.0, stan::math::reduce_sum(data, 1, msgs)); + stan::math::test::test_slices(20.0, std::vector(2, Eigen::VectorXd::Ones(2))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_row_vector_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data( - 5, std::vector(2, Eigen::RowVectorXd::Ones(2))); - - EXPECT_DOUBLE_EQ(20.0, stan::math::reduce_sum(data, 1, msgs)); + stan::math::test::test_slices(20.0, std::vector(2, Eigen::RowVectorXd::Ones(2))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_matrix_slice) { - stan::math::init_threadpool_tbb(); - - std::vector> data( - 5, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); - - EXPECT_DOUBLE_EQ(40.0, stan::math::reduce_sum(data, 1, msgs)); -} - -struct start_end_lpdf { - inline auto operator()(std::size_t start, std::size_t end, - const std::vector&, std::ostream* msgs, - const std::vector& data) const { - int sum = 0; - EXPECT_GE(start, 0); - EXPECT_LE(end, data.size() - 1); - for (size_t i = start; i <= end; i++) { - sum += data[i]; - } - return sum; - } -}; - -TEST(StanMathPrim_reduce_sum, start_end_slice) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10); - - EXPECT_EQ(50, stan::math::reduce_sum(data, 1, msgs, data)); + stan::math::test::test_slices(40.0, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); } TEST(StanMathPrim_reduce_sum, int_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - int arg = 5; - - EXPECT_DOUBLE_EQ(5 * (10 + 5), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 5), 10, 5); } TEST(StanMathPrim_reduce_sum, double_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - double arg = 5.0; - - EXPECT_DOUBLE_EQ(5 * (10.0 + 5.0), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10.0 + 5.0), 10.0, 5.0); } TEST(StanMathPrim_reduce_sum, std_vector_int_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(5, 10); - - EXPECT_DOUBLE_EQ(5 * (10 + 5 * 10), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10.0 + 5.0 * 10), 10.0, std::vector(5, 10)); } TEST(StanMathPrim_reduce_sum, std_vector_double_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(5, 10.0); - - EXPECT_DOUBLE_EQ(5 * (10 + 5 * 10), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 5 * 10), 10.0, std::vector(5, 10)); } TEST(StanMathPrim_reduce_sum, eigen_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - Eigen::VectorXd arg = Eigen::VectorXd::Ones(5); - - EXPECT_DOUBLE_EQ(5 * (10 + 5), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 5), 10.0, Eigen::VectorXd::Ones(5)); } TEST(StanMathPrim_reduce_sum, eigen_row_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(5); - - EXPECT_DOUBLE_EQ(5 * (10 + 5), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 5), 10.0, Eigen::RowVectorXd::Ones(5)); } TEST(StanMathPrim_reduce_sum, eigen_matrix_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(5, 5); - - EXPECT_DOUBLE_EQ(5 * (10 + 5 * 5), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 5 * 5), 10.0, Eigen::MatrixXd::Ones(5, 5)); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_double_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector> arg(5, std::vector(5, 10.0)); - - EXPECT_DOUBLE_EQ(5 * (10 + 250), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 250), 10.0, std::vector>(5, std::vector(5, 10.0))); } TEST(StanMathPrim_reduce_sum, std_vector_eigen_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(2, Eigen::VectorXd::Ones(5)); - - EXPECT_DOUBLE_EQ(5 * (10 + 10), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 10), 10.0, std::vector(2, Eigen::VectorXd::Ones(5))); } TEST(StanMathPrim_reduce_sum, std_vector_eigen_row_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(2, Eigen::RowVectorXd::Ones(5)); - - EXPECT_DOUBLE_EQ(5 * (10 + 10), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 10), 10.0, std::vector(2, Eigen::RowVectorXd::Ones(5))); } TEST(StanMathPrim_reduce_sum, std_vector_eigen_matrix_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector arg(2, Eigen::MatrixXd::Ones(5, 5)); - - EXPECT_DOUBLE_EQ(5 * (10 + 50), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 50), 10.0, std::vector(2, Eigen::MatrixXd::Ones(5, 5))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_std_vector_double_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector>> arg( - 5, std::vector>(5, std::vector(5, 10.0))); - - EXPECT_DOUBLE_EQ(5 * (10 + 1250), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 1250), 10.0, std::vector>>( + 5, std::vector>(5, std::vector(5, 10.0)))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::VectorXd::Ones(5))); - - EXPECT_DOUBLE_EQ(5 * (10 + 20), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 20), 10.0, std::vector>( + 2, std::vector(2, Eigen::VectorXd::Ones(5)))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_row_vector_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::RowVectorXd::Ones(5))); - - EXPECT_DOUBLE_EQ(5 * (10 + 20), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 20), 10.0, std::vector>( + 2, std::vector(2, Eigen::RowVectorXd::Ones(5)))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_matrix_arg) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::MatrixXd::Ones(5, 3))); - - EXPECT_DOUBLE_EQ(5 * (10 + 60), - stan::math::reduce_sum(data, 1, msgs, arg)); + stan::math::test::test_slices(5 * (10 + 60), 10.0, std::vector>( + 2, std::vector(2, Eigen::MatrixXd::Ones(5, 3)))); } TEST(StanMathPrim_reduce_sum, sum) { - stan::math::init_threadpool_tbb(); - - std::vector data(5, 1.0); - int arg1 = 1; - double arg2 = 1.0; - std::vector arg3(5, 1); - std::vector arg4(5, 1.0); - Eigen::VectorXd arg5 = Eigen::VectorXd::Ones(5); - Eigen::RowVectorXd arg6 = Eigen::RowVectorXd::Ones(5); - Eigen::MatrixXd arg7 = Eigen::MatrixXd::Ones(5, 5); - std::vector> arg8(2, arg4); - std::vector arg9(2, arg5); - - EXPECT_DOUBLE_EQ( - 5 + 5 * (1 + 1 + 5 + 5 + 5 + 5 + 25 + 10 + 10), - stan::math::reduce_sum(data, 1, msgs, arg1, arg2, arg3, arg4, - arg5, arg6, arg7, arg8, arg9)); + double answer = 5 + 5 * (1 + 1 + 5 + 5 + 5 + 5 + 25 + 10 + 10); + stan::math::test::test_slices(answer, 1.0, 1, 1.0, std::vector(5, 1), + std::vector(5, 1.0), + Eigen::VectorXd::Ones(5), + Eigen::RowVectorXd::Ones(5), + Eigen::MatrixXd::Ones(5, 5), + std::vector>(2, std::vector(5, 1.0)), + std::vector(2, Eigen::VectorXd::Ones(5))); } diff --git a/test/unit/math/reduce_sum_util.hpp b/test/unit/math/reduce_sum_util.hpp new file mode 100644 index 00000000000..fdb706eca38 --- /dev/null +++ b/test/unit/math/reduce_sum_util.hpp @@ -0,0 +1,202 @@ +#ifndef TEST_UNIT_MATH_REDUCE_SUM_UTIL +#define TEST_UNIT_MATH_REDUCE_SUM_UTIL + +#include +#include +#include +#include +#include +#include + +namespace stan { + namespace math { + namespace test { + std::ostream* get_new_msg() { + std::ostream* msgs = nullptr; + return msgs; + } + // reduce functor which is the BinaryFunction + // here we use iterators which represent integer indices + template + struct count_lpdf { + count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::poisson_lpmf(sub_slice, lambda[0]); + } + }; + + + template + struct nesting_count_lpdf { + nesting_count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::reduce_sum>(sub_slice, 5, msgs, lambda, + idata); + } + }; + + + struct sum_lpdf { + template * = nullptr> + T sum_(T arg) const { + return arg; + } + + template * = nullptr> + auto sum_(EigMat&& arg) const { + return stan::math::sum(arg); + } + + template * = nullptr> + auto sum_(Vec&& arg) const { + stan::scalar_type_t sum = 0; + for (size_t i = 0; i < arg.size(); ++i) { + sum += sum_(arg[i]); + } + return sum; + } + + template + inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, + std::ostream* msgs, Args&&... args) const { + using return_type = stan::return_type_t; + + return sum_(sub_slice) + + sub_slice.size() + * stan::math::sum(std::vector{ + return_type(sum_(std::forward(args)))...}); + } + }; + struct start_end_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, + T1&&, std::ostream* msgs, + T2&& data) const { + stan::return_type_t sum = 0; + EXPECT_GE(start, 0); + EXPECT_LE(end, data.size() - 1); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } + }; + + /** + * slice over the grouping variable which is a var + */ + template + struct slice_group_count_lpdf { + slice_group_count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& lambda_slice, std::ostream* msgs, + const std::vector& y, + const std::vector& gsidx) const { + const std::size_t num_groups = end - start + 1; + T result = 0.0; + for (std::size_t i = 0; i != num_groups; ++i) { + std::vector y_group(y.begin() + gsidx[start + i], + y.begin() + gsidx[start + i + 1]); + result += stan::math::poisson_lpmf(y_group, lambda_slice[i]); + } + return result; + } + }; + + + /** + * basic performance test for a hierarchical model + */ + template + struct grouped_count_lpdf { + grouped_count_lpdf() {} + + // does the reduction in the sub-slice start to end + template + inline T operator()(std::size_t start, std::size_t end, VecInt1&& sub_slice, + std::ostream* msgs, VecT&& lambda, VecInt2&& gidx) const { + const std::size_t num_terms = end - start + 1; + // std::cout << "sub-slice " << start << " - " << end << "; num_terms = " << + // num_terms << "; size = " << sub_slice.size() << std::endl; + std::decay_t lambda_slice(num_terms); + for (std::size_t i = 0; i != num_terms; ++i) + lambda_slice[i] = lambda[gidx[start + i]]; + + return stan::math::poisson_lpmf(sub_slice, lambda_slice); + } + }; + + template + void test_slices(T1 result, T2&& vec_value, Args&&... args) { + stan::math::init_threadpool_tbb(); + using stan::math::test::get_new_msg; + using stan::math::test::sum_lpdf; + + std::vector> data(5, vec_value); + EXPECT_EQ(result, stan::math::reduce_sum_static(data, 1, get_new_msg(), args...)) << "Failed for reduce_sum_static"; + EXPECT_EQ(result, stan::math::reduce_sum(data, 1, get_new_msg(), args...))<< "Failed for reduce_sum"; + } + + auto reduce_sum_static_int_sum_lpdf = [](auto&&... args) { + return stan::math::reduce_sum_static(std::vector(2, 10.0), 1, + get_new_msg(), args...); + }; + + auto reduce_sum_static_sum_lpdf = [](auto&& data, auto&&... args) { + return stan::math::reduce_sum_static(data, 1, get_new_msg(), args...); + }; + + auto reduce_sum_int_sum_lpdf = [](auto&&... args) { + return stan::math::reduce_sum(std::vector(2, 10.0), 1, + get_new_msg(), args...); + }; + + auto reduce_sum_sum_lpdf = [](auto&& data, auto&&... args) { + return stan::math::reduce_sum(data, 1, get_new_msg(), args...); + }; + + template + void expect_ad_reduce_sum_lpdf(T1&& data, T2&& arg) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + using stan::math::test::reduce_sum_int_sum_lpdf; + stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data, arg); + stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg); + stan::test::expect_ad(reduce_sum_sum_lpdf, data, arg); + } + + template + struct static_check_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, + const std::vector&, std::ostream* msgs, + const std::vector& data) const { + T sum = 0; + // std::cout << "start: " << start << ", end: " << end << ", grainsize: " << + // grainsize << std::endl; + EXPECT_LE(end - start + 1, grainsize); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } + }; + + } + } +} +#endif diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 810f4b2d52c..dd53e63267a 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -1,31 +1,15 @@ -#include #include +#include #include #include #include #include #include -std::ostream* msgs = nullptr; - -// reduce functor which is the BinaryFunction -// here we use iterators which represent integer indices -template -struct count_lpdf { - count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& sub_slice, std::ostream* msgs, - const std::vector& lambda, - const std::vector& idata) const { - return stan::math::poisson_lpmf(sub_slice, lambda[0]); - } -}; - TEST(StanMathRev_reduce_sum, value) { + using stan::math::test::get_new_msg; + using stan::math::test::count_lpdf; stan::math::init_threadpool_tbb(); - double lambda_d = 10.0; const std::size_t elems = 10000; const std::size_t num_iter = 1000; @@ -38,7 +22,7 @@ TEST(StanMathRev_reduce_sum, value) { std::vector vlambda_d(1, lambda_d); double poisson_lpdf = stan::math::reduce_sum>( - data, 5, msgs, vlambda_d, idata); + data, 5, get_new_msg(), vlambda_d, idata); double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); @@ -48,6 +32,8 @@ TEST(StanMathRev_reduce_sum, value) { } TEST(StanMathRev_reduce_sum, gradient) { + using stan::math::test::get_new_msg; + using stan::math::test::count_lpdf; stan::math::init_threadpool_tbb(); double lambda_d = 10.0; @@ -65,7 +51,7 @@ TEST(StanMathRev_reduce_sum, gradient) { std::vector idata; std::vector vlambda_v(1, lambda_v); - var poisson_lpdf = stan::math::reduce_sum>(data, 5, msgs, + var poisson_lpdf = stan::math::reduce_sum>(data, 5, get_new_msg(), vlambda_v, idata); var lambda_ref = lambda_d; @@ -88,7 +74,10 @@ TEST(StanMathRev_reduce_sum, gradient) { stan::math::recover_memory(); } + TEST(StanMathRev_reduce_sum, grainsize) { + using stan::math::test::get_new_msg; + using stan::math::test::count_lpdf; stan::math::init_threadpool_tbb(); double lambda_d = 10.0; @@ -107,40 +96,25 @@ TEST(StanMathRev_reduce_sum, grainsize) { std::vector vlambda_v(1, lambda_v); EXPECT_THROW( - stan::math::reduce_sum>(data, 0, msgs, vlambda_v, idata), + stan::math::reduce_sum>(data, 0, get_new_msg(), vlambda_v, idata), std::domain_error); EXPECT_THROW( - stan::math::reduce_sum>(data, -1, msgs, vlambda_v, idata), + stan::math::reduce_sum>(data, -1, get_new_msg(), vlambda_v, idata), std::domain_error); EXPECT_NO_THROW( - stan::math::reduce_sum>(data, 1, msgs, vlambda_v, idata)); + stan::math::reduce_sum>(data, 1, get_new_msg(), vlambda_v, idata)); - EXPECT_NO_THROW(stan::math::reduce_sum>(data, 2 * elems, msgs, + EXPECT_NO_THROW(stan::math::reduce_sum>(data, 2 * elems, get_new_msg(), vlambda_v, idata)); stan::math::recover_memory(); } -// ******************************** -// test if nested parallelism works -// ******************************** -template -struct nesting_count_lpdf { - nesting_count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& sub_slice, std::ostream* msgs, - const std::vector& lambda, - const std::vector& idata) const { - return stan::math::reduce_sum>(sub_slice, 5, msgs, lambda, - idata); - } -}; - TEST(StanMathRev_reduce_sum, nesting_gradient) { + using stan::math::test::get_new_msg; + using stan::math::test::nesting_count_lpdf; stan::math::init_threadpool_tbb(); double lambda_d = 10.0; @@ -159,7 +133,7 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { std::vector vlambda_v(1, lambda_v); var poisson_lpdf = stan::math::reduce_sum>( - data, 5, msgs, vlambda_v, idata); + data, 5, get_new_msg(), vlambda_v, idata); var lambda_ref = lambda_d; var poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_ref); @@ -182,30 +156,10 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { stan::math::recover_memory(); } -// ******************************** -// basic performance test for a hierarchical model -// ******************************** - -template -struct grouped_count_lpdf { - grouped_count_lpdf() {} - - // does the reduction in the sub-slice start to end - template - inline T operator()(std::size_t start, std::size_t end, VecInt1&& sub_slice, - std::ostream* msgs, VecT&& lambda, VecInt2&& gidx) const { - const std::size_t num_terms = end - start + 1; - // std::cout << "sub-slice " << start << " - " << end << "; num_terms = " << - // num_terms << "; size = " << sub_slice.size() << std::endl; - std::decay_t lambda_slice(num_terms); - for (std::size_t i = 0; i != num_terms; ++i) - lambda_slice[i] = lambda[gidx[start + i]]; - - return stan::math::poisson_lpmf(sub_slice, lambda_slice); - } -}; TEST(StanMathRev_reduce_sum, grouped_gradient) { + using stan::math::test::get_new_msg; + using stan::math::test::grouped_count_lpdf; stan::math::init_threadpool_tbb(); double lambda_d = 10.0; @@ -232,7 +186,7 @@ TEST(StanMathRev_reduce_sum, grouped_gradient) { var lambda_v = vlambda_v[0]; var poisson_lpdf = stan::math::reduce_sum>( - data, 5, msgs, vlambda_v, gidx); + data, 5, get_new_msg(), vlambda_v, gidx); std::vector vref_lambda_v; for (std::size_t i = 0; i != elems; ++i) { @@ -260,6 +214,8 @@ TEST(StanMathRev_reduce_sum, grouped_gradient) { } TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { + using stan::math::test::get_new_msg; + using stan::math::test::grouped_count_lpdf; stan::math::init_threadpool_tbb(); double lambda_d = 10.0; @@ -284,7 +240,7 @@ TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { var lambda_v = vlambda_v[0]; var poisson_lpdf = stan::math::reduce_sum>( - data, 5, msgs, vlambda_v, gidx); + data, 5, get_new_msg(), vlambda_v, gidx); std::vector vref_lambda_v; for (std::size_t i = 0; i != elems; ++i) { @@ -312,31 +268,9 @@ TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { stan::math::recover_memory(); } -// ******************************** -// slice over the grouping variable which is a var -// ******************************** - -template -struct slice_group_count_lpdf { - slice_group_count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& lambda_slice, std::ostream* msgs, - const std::vector& y, - const std::vector& gsidx) const { - const std::size_t num_groups = end - start + 1; - T result = 0.0; - for (std::size_t i = 0; i != num_groups; ++i) { - std::vector y_group(y.begin() + gsidx[start + i], - y.begin() + gsidx[start + i + 1]); - result += stan::math::poisson_lpmf(y_group, lambda_slice[i]); - } - return result; - } -}; - TEST(StanMathRev_reduce_sum, slice_group_gradient) { + using stan::math::test::get_new_msg; + using stan::math::test::slice_group_count_lpdf; stan::math::init_threadpool_tbb(); double lambda_d = 10.0; @@ -368,7 +302,7 @@ TEST(StanMathRev_reduce_sum, slice_group_gradient) { var lambda_v = vlambda_v[0]; var poisson_lpdf = stan::math::reduce_sum>( - vlambda_v, 5, msgs, data, gsidx); + vlambda_v, 5, get_new_msg(), data, gsidx); std::vector vref_lambda_v; for (std::size_t i = 0; i != elems; ++i) { From c554db8e54ef245b55cf611e73f82dd45f06ce0e Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 31 Mar 2020 23:32:35 -0400 Subject: [PATCH 06/41] [Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/stable/2017-11-14) --- stan/math/opencl/kernel_generator/load.hpp | 2 +- .../unit/math/mix/functor/reduce_sum_test.cpp | 145 +++---- .../math/prim/functor/reduce_sum_test.cpp | 99 +++-- test/unit/math/reduce_sum_util.hpp | 373 +++++++++--------- .../unit/math/rev/functor/reduce_sum_test.cpp | 31 +- 5 files changed, 341 insertions(+), 309 deletions(-) diff --git a/stan/math/opencl/kernel_generator/load.hpp b/stan/math/opencl/kernel_generator/load.hpp index 799cdb551a0..ae0f7d1eb6d 100644 --- a/stan/math/opencl/kernel_generator/load.hpp +++ b/stan/math/opencl/kernel_generator/load.hpp @@ -49,7 +49,7 @@ class load_ * Creates a deep copy of this expression. * @return copy of \c *this */ - inline load_ deep_copy() const & { return load_(a_); } + inline load_ deep_copy() const& { return load_(a_); } inline load_ deep_copy() && { return load_(std::forward(a_)); } /** diff --git a/test/unit/math/mix/functor/reduce_sum_test.cpp b/test/unit/math/mix/functor/reduce_sum_test.cpp index 57a3f07ed49..bf32b30d831 100644 --- a/test/unit/math/mix/functor/reduce_sum_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_test.cpp @@ -5,11 +5,9 @@ #include #include - - TEST(MathMix_reduce_sum, grainsize) { - using stan::math::test::sum_lpdf; using stan::math::test::get_new_msg; + using stan::math::test::sum_lpdf; auto f1 = [](auto&& data) { return stan::math::reduce_sum_static(data, 0, get_new_msg()); @@ -33,8 +31,8 @@ TEST(MathMix_reduce_sum, grainsize) { } TEST(MathMix_reduce_sum, grainsize) { - using stan::math::test::sum_lpdf; using stan::math::test::get_new_msg; + using stan::math::test::sum_lpdf; auto f1 = [](auto&& data) { return stan::math::reduce_sum(data, 0, get_new_msg()); }; @@ -113,14 +111,13 @@ TEST(MathMix_reduce_sum, std_vector_std_vector_std_vector_double_slice) { std::vector>> data( 3, std::vector>(2, std::vector(2, 10.0))); - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } -TEST(MathMix_reduce_sum, - std_vector_std_vector_eigen_vector_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector> data( 3, std::vector(2, Eigen::VectorXd::Ones(2))); @@ -129,39 +126,39 @@ TEST(MathMix_reduce_sum, stan::test::expect_ad(reduce_sum_sum_lpdf, data); } -TEST(MathMix_reduce_sum, - std_vector_std_vector_eigen_row_vector_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_row_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector> data( 3, std::vector(2, Eigen::RowVectorXd::Ones(2))); - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } -TEST(MathMix_reduce_sum, - std_vector_std_vector_eigen_matrix_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; std::vector> data( 3, std::vector(2, Eigen::MatrixXd::Ones(2, 4))); - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); } TEST(StanMath_reduce_sum_static, start_end_slice) { - using stan::math::test::start_end_lpdf; using stan::math::test::get_new_msg; + using stan::math::test::start_end_lpdf; auto start_end_static = [](auto&& arg) { - return stan::math::reduce_sum_static(arg, 1, get_new_msg(), arg); + return stan::math::reduce_sum_static(arg, 1, get_new_msg(), + arg); }; auto start_end = [](auto&& arg) { - return stan::math::reduce_sum_static(arg, 1, get_new_msg(), arg); + return stan::math::reduce_sum_static(arg, 1, get_new_msg(), + arg); }; std::vector data(5, 1.0); @@ -170,17 +167,16 @@ TEST(StanMath_reduce_sum_static, start_end_slice) { stan::test::expect_ad(start_end_static, data); } - - TEST(MathMix_reduce_sum, int_arg) { using stan::math::test::reduce_sum_static_sum_lpdf; using stan::math::test::reduce_sum_sum_lpdf; std::vector data(2, 1.0); int arg = 5.0; - stan::test::expect_ad([&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); - stan::test::expect_ad([&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); - + stan::test::expect_ad( + [&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); + stan::test::expect_ad( + [&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); } TEST(MathMix_reduce_sum, std_vector_int_arg) { @@ -189,16 +185,20 @@ TEST(MathMix_reduce_sum, std_vector_int_arg) { std::vector data(2, 10.0); std::vector arg(2, 10); - stan::test::expect_ad([&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); - stan::test::expect_ad([&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); + stan::test::expect_ad( + [&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); + stan::test::expect_ad( + [&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); } TEST(MathMix_reduce_sum, double_arg) { - stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), 5.0); + stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), + 5.0); } TEST(MathMix_reduce_sum, std_vector_double_arg) { - stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), std::vector(2, 10.0)); + stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), + std::vector(2, 10.0)); } TEST(MathMix_reduce_sum, eigen_vector_arg) { @@ -282,8 +282,8 @@ TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_arg) { } TEST(MathMix_reduce_sum, eigen_three_args1) { - using stan::math::test::reduce_sum_static_int_sum_lpdf; using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); @@ -293,8 +293,8 @@ TEST(MathMix_reduce_sum, eigen_three_args1) { } TEST(MathMix_reduce_sum, eigen_three_args2) { - using stan::math::test::reduce_sum_static_int_sum_lpdf; using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; double arg1 = 1.0; std::vector arg2(2, 1.0); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); @@ -304,8 +304,8 @@ TEST(MathMix_reduce_sum, eigen_three_args2) { } TEST(MathMix_reduce_sum, eigen_three_args3) { - using stan::math::test::reduce_sum_static_int_sum_lpdf; using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; double arg1 = 1.0; std::vector> arg2(2, std::vector(2, 1.0)); std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); @@ -315,64 +315,68 @@ TEST(MathMix_reduce_sum, eigen_three_args3) { } TEST(MathMix_reduce_sum, eigen_three_args_with_ints1) { - using stan::math::test::reduce_sum_static_int_sum_lpdf; using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + return reduce_sum_static_int_sum_lpdf( + 1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); }, arg1, arg2, arg3); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, + 3, arg3); }, arg1, arg2, arg3); } TEST(MathMix_reduce_sum, eigen_three_args_with_ints2) { - using stan::math::test::reduce_sum_static_int_sum_lpdf; using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; double arg1 = 1.0; std::vector arg2(2, 1.0); Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + return reduce_sum_static_int_sum_lpdf( + 1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); }, arg1, arg2, arg3); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, + 3, arg3); }, arg1, arg2, arg3); - } TEST(MathMix_reduce_sum, eigen_three_args_with_ints3) { - using stan::math::test::reduce_sum_static_int_sum_lpdf; using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; double arg1 = 1.0; std::vector> arg2(2, std::vector(2, 1.0)); std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + return reduce_sum_static_int_sum_lpdf( + 1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); }, arg1, arg2, arg3); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, + 3, arg3); }, arg1, arg2, arg3); - } TEST(MathMix_reduce_sum, eigen_three_args_with_doubles1) { @@ -384,14 +388,16 @@ TEST(MathMix_reduce_sum, eigen_three_args_with_doubles1) { stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); + return reduce_sum_static_sum_lpdf( + std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); }, arg1, arg2, arg3); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); + return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, + 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); }, arg1, arg2, arg3); } @@ -405,18 +411,19 @@ TEST(MathMix_reduce_sum, eigen_three_args_with_doubles2) { stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); + return reduce_sum_static_sum_lpdf( + std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); }, arg1, arg2, arg3); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); + return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, + 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); }, arg1, arg2, arg3); - } TEST(MathMix_reduce_sum, eigen_three_args_with_doubles3) { @@ -428,21 +435,21 @@ TEST(MathMix_reduce_sum, eigen_three_args_with_doubles3) { stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); + return reduce_sum_static_sum_lpdf( + std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); }, arg1, arg2, arg3); stan::test::expect_ad( [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); + return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, + 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); }, arg1, arg2, arg3); - } - TEST(MathMix_reduce_sum, static_check) { stan::math::init_threadpool_tbb(); using stan::math::test::get_new_msg; @@ -453,18 +460,18 @@ TEST(MathMix_reduce_sum, static_check) { std::vector arg(size, 10); auto fi1 = [&](auto&&... args) { - return stan::math::reduce_sum_static>(data, 1, get_new_msg(), - args...); + return stan::math::reduce_sum_static>( + data, 1, get_new_msg(), args...); }; auto fi2 = [&](auto&&... args) { - return stan::math::reduce_sum_static>(data, 2, get_new_msg(), - args...); + return stan::math::reduce_sum_static>( + data, 2, get_new_msg(), args...); }; auto fi3 = [&](auto&&... args) { - return stan::math::reduce_sum_static>(data, 3, get_new_msg(), - args...); + return stan::math::reduce_sum_static>( + data, 3, get_new_msg(), args...); }; stan::test::expect_ad(fi1, arg); diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index 812b0dd0ecd..1df27d33750 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -4,8 +4,8 @@ TEST(StanMathPrim_reduce_sum, value) { stan::math::init_threadpool_tbb(); - using stan::math::test::get_new_msg; using stan::math::test::count_lpdf; + using stan::math::test::get_new_msg; double lambda_d = 10.0; const std::size_t elems = 10000; const std::size_t num_iter = 1000; @@ -19,8 +19,9 @@ TEST(StanMathPrim_reduce_sum, value) { double poisson_lpdf = stan::math::reduce_sum>( data, 5, get_new_msg(), vlambda_d, idata); - double poisson_static_lpdf = stan::math::reduce_sum_static>( - data, 5, get_new_msg(), vlambda_d, idata); + double poisson_static_lpdf + = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_d, idata); double poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_d); // NOTE:(Steve) This fails with EXPECT_DOUBLE_EQ at about 10e-7 @@ -28,7 +29,6 @@ TEST(StanMathPrim_reduce_sum, value) { EXPECT_FLOAT_EQ(poisson_static_lpdf, poisson_lpdf_ref); } - TEST(StanMathPrim_reduce_sum, grainsize) { stan::math::init_threadpool_tbb(); using stan::math::test::get_new_msg; @@ -52,10 +52,11 @@ TEST(StanMathPrim_reduce_sum, grainsize) { EXPECT_THROW(stan::math::reduce_sum_static(data, -1, get_new_msg()), std::domain_error); - EXPECT_NO_THROW(stan::math::reduce_sum_static(data, 1, get_new_msg())); - EXPECT_NO_THROW( - stan::math::reduce_sum_static(data, 3 * data.size(), get_new_msg())); + stan::math::reduce_sum_static(data, 1, get_new_msg())); + + EXPECT_NO_THROW(stan::math::reduce_sum_static(data, 3 * data.size(), + get_new_msg())); } TEST(StanMathPrim_reduce_sum, start_end_slice) { @@ -65,10 +66,10 @@ TEST(StanMathPrim_reduce_sum, start_end_slice) { std::vector data(5, 10); - EXPECT_EQ(50, stan::math::reduce_sum(data, 1, get_new_msg(), data)); + EXPECT_EQ( + 50, stan::math::reduce_sum(data, 1, get_new_msg(), data)); } - TEST(StanMathPrim_reduce_sum, std_vector_int_slice) { stan::math::test::test_slices(50, 10); } @@ -101,23 +102,28 @@ TEST(StanMathPrim_reduce_sum, std_vector_eigen_matrix_slice) { } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_std_vector_int_slice) { - stan::math::test::test_slices(200, std::vector>(2, std::vector(2, 10.0))); + stan::math::test::test_slices( + 200, std::vector>(2, std::vector(2, 10.0))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_std_vector_double_slice) { - stan::math::test::test_slices(200.0, std::vector>(2, std::vector(2, 10.0))); + stan::math::test::test_slices( + 200.0, std::vector>(2, std::vector(2, 10.0))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_vector_slice) { - stan::math::test::test_slices(20.0, std::vector(2, Eigen::VectorXd::Ones(2))); + stan::math::test::test_slices( + 20.0, std::vector(2, Eigen::VectorXd::Ones(2))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_row_vector_slice) { - stan::math::test::test_slices(20.0, std::vector(2, Eigen::RowVectorXd::Ones(2))); + stan::math::test::test_slices( + 20.0, std::vector(2, Eigen::RowVectorXd::Ones(2))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_matrix_slice) { - stan::math::test::test_slices(40.0, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); + stan::math::test::test_slices( + 40.0, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); } TEST(StanMathPrim_reduce_sum, int_arg) { @@ -129,11 +135,13 @@ TEST(StanMathPrim_reduce_sum, double_arg) { } TEST(StanMathPrim_reduce_sum, std_vector_int_arg) { - stan::math::test::test_slices(5 * (10.0 + 5.0 * 10), 10.0, std::vector(5, 10)); + stan::math::test::test_slices(5 * (10.0 + 5.0 * 10), 10.0, + std::vector(5, 10)); } TEST(StanMathPrim_reduce_sum, std_vector_double_arg) { - stan::math::test::test_slices(5 * (10 + 5 * 10), 10.0, std::vector(5, 10)); + stan::math::test::test_slices(5 * (10 + 5 * 10), 10.0, + std::vector(5, 10)); } TEST(StanMathPrim_reduce_sum, eigen_vector_arg) { @@ -141,56 +149,73 @@ TEST(StanMathPrim_reduce_sum, eigen_vector_arg) { } TEST(StanMathPrim_reduce_sum, eigen_row_vector_arg) { - stan::math::test::test_slices(5 * (10 + 5), 10.0, Eigen::RowVectorXd::Ones(5)); + stan::math::test::test_slices(5 * (10 + 5), 10.0, + Eigen::RowVectorXd::Ones(5)); } TEST(StanMathPrim_reduce_sum, eigen_matrix_arg) { - stan::math::test::test_slices(5 * (10 + 5 * 5), 10.0, Eigen::MatrixXd::Ones(5, 5)); + stan::math::test::test_slices(5 * (10 + 5 * 5), 10.0, + Eigen::MatrixXd::Ones(5, 5)); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_double_arg) { - stan::math::test::test_slices(5 * (10 + 250), 10.0, std::vector>(5, std::vector(5, 10.0))); + stan::math::test::test_slices( + 5 * (10 + 250), 10.0, + std::vector>(5, std::vector(5, 10.0))); } TEST(StanMathPrim_reduce_sum, std_vector_eigen_vector_arg) { - stan::math::test::test_slices(5 * (10 + 10), 10.0, std::vector(2, Eigen::VectorXd::Ones(5))); + stan::math::test::test_slices( + 5 * (10 + 10), 10.0, + std::vector(2, Eigen::VectorXd::Ones(5))); } TEST(StanMathPrim_reduce_sum, std_vector_eigen_row_vector_arg) { - stan::math::test::test_slices(5 * (10 + 10), 10.0, std::vector(2, Eigen::RowVectorXd::Ones(5))); + stan::math::test::test_slices( + 5 * (10 + 10), 10.0, + std::vector(2, Eigen::RowVectorXd::Ones(5))); } TEST(StanMathPrim_reduce_sum, std_vector_eigen_matrix_arg) { - stan::math::test::test_slices(5 * (10 + 50), 10.0, std::vector(2, Eigen::MatrixXd::Ones(5, 5))); + stan::math::test::test_slices( + 5 * (10 + 50), 10.0, + std::vector(2, Eigen::MatrixXd::Ones(5, 5))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_std_vector_double_arg) { - stan::math::test::test_slices(5 * (10 + 1250), 10.0, std::vector>>( - 5, std::vector>(5, std::vector(5, 10.0)))); + stan::math::test::test_slices(5 * (10 + 1250), 10.0, + std::vector>>( + 5, std::vector>( + 5, std::vector(5, 10.0)))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_vector_arg) { - stan::math::test::test_slices(5 * (10 + 20), 10.0, std::vector>( - 2, std::vector(2, Eigen::VectorXd::Ones(5)))); + stan::math::test::test_slices( + 5 * (10 + 20), 10.0, + std::vector>( + 2, std::vector(2, Eigen::VectorXd::Ones(5)))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_row_vector_arg) { - stan::math::test::test_slices(5 * (10 + 20), 10.0, std::vector>( - 2, std::vector(2, Eigen::RowVectorXd::Ones(5)))); + stan::math::test::test_slices( + 5 * (10 + 20), 10.0, + std::vector>( + 2, std::vector(2, Eigen::RowVectorXd::Ones(5)))); } TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_matrix_arg) { - stan::math::test::test_slices(5 * (10 + 60), 10.0, std::vector>( - 2, std::vector(2, Eigen::MatrixXd::Ones(5, 3)))); + stan::math::test::test_slices( + 5 * (10 + 60), 10.0, + std::vector>( + 2, std::vector(2, Eigen::MatrixXd::Ones(5, 3)))); } TEST(StanMathPrim_reduce_sum, sum) { double answer = 5 + 5 * (1 + 1 + 5 + 5 + 5 + 5 + 25 + 10 + 10); - stan::math::test::test_slices(answer, 1.0, 1, 1.0, std::vector(5, 1), - std::vector(5, 1.0), - Eigen::VectorXd::Ones(5), - Eigen::RowVectorXd::Ones(5), - Eigen::MatrixXd::Ones(5, 5), - std::vector>(2, std::vector(5, 1.0)), - std::vector(2, Eigen::VectorXd::Ones(5))); + stan::math::test::test_slices( + answer, 1.0, 1, 1.0, std::vector(5, 1), std::vector(5, 1.0), + Eigen::VectorXd::Ones(5), Eigen::RowVectorXd::Ones(5), + Eigen::MatrixXd::Ones(5, 5), + std::vector>(2, std::vector(5, 1.0)), + std::vector(2, Eigen::VectorXd::Ones(5))); } diff --git a/test/unit/math/reduce_sum_util.hpp b/test/unit/math/reduce_sum_util.hpp index fdb706eca38..6e1153b433d 100644 --- a/test/unit/math/reduce_sum_util.hpp +++ b/test/unit/math/reduce_sum_util.hpp @@ -9,194 +9,195 @@ #include namespace stan { - namespace math { - namespace test { - std::ostream* get_new_msg() { - std::ostream* msgs = nullptr; - return msgs; - } - // reduce functor which is the BinaryFunction - // here we use iterators which represent integer indices - template - struct count_lpdf { - count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& sub_slice, std::ostream* msgs, - const std::vector& lambda, - const std::vector& idata) const { - return stan::math::poisson_lpmf(sub_slice, lambda[0]); - } - }; - - - template - struct nesting_count_lpdf { - nesting_count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& sub_slice, std::ostream* msgs, - const std::vector& lambda, - const std::vector& idata) const { - return stan::math::reduce_sum>(sub_slice, 5, msgs, lambda, - idata); - } - }; - - - struct sum_lpdf { - template * = nullptr> - T sum_(T arg) const { - return arg; - } - - template * = nullptr> - auto sum_(EigMat&& arg) const { - return stan::math::sum(arg); - } - - template * = nullptr> - auto sum_(Vec&& arg) const { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; - } - - template - inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, - std::ostream* msgs, Args&&... args) const { - using return_type = stan::return_type_t; - - return sum_(sub_slice) - + sub_slice.size() - * stan::math::sum(std::vector{ - return_type(sum_(std::forward(args)))...}); - } - }; - struct start_end_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, - T1&&, std::ostream* msgs, - T2&& data) const { - stan::return_type_t sum = 0; - EXPECT_GE(start, 0); - EXPECT_LE(end, data.size() - 1); - for (size_t i = start; i <= end; i++) { - sum += data[i]; - } - return sum; - } - }; - - /** - * slice over the grouping variable which is a var - */ - template - struct slice_group_count_lpdf { - slice_group_count_lpdf() {} - - // does the reduction in the sub-slice start to end - inline T operator()(std::size_t start, std::size_t end, - const std::vector& lambda_slice, std::ostream* msgs, - const std::vector& y, - const std::vector& gsidx) const { - const std::size_t num_groups = end - start + 1; - T result = 0.0; - for (std::size_t i = 0; i != num_groups; ++i) { - std::vector y_group(y.begin() + gsidx[start + i], - y.begin() + gsidx[start + i + 1]); - result += stan::math::poisson_lpmf(y_group, lambda_slice[i]); - } - return result; - } - }; - - - /** - * basic performance test for a hierarchical model - */ - template - struct grouped_count_lpdf { - grouped_count_lpdf() {} - - // does the reduction in the sub-slice start to end - template - inline T operator()(std::size_t start, std::size_t end, VecInt1&& sub_slice, - std::ostream* msgs, VecT&& lambda, VecInt2&& gidx) const { - const std::size_t num_terms = end - start + 1; - // std::cout << "sub-slice " << start << " - " << end << "; num_terms = " << - // num_terms << "; size = " << sub_slice.size() << std::endl; - std::decay_t lambda_slice(num_terms); - for (std::size_t i = 0; i != num_terms; ++i) - lambda_slice[i] = lambda[gidx[start + i]]; - - return stan::math::poisson_lpmf(sub_slice, lambda_slice); - } - }; - - template - void test_slices(T1 result, T2&& vec_value, Args&&... args) { - stan::math::init_threadpool_tbb(); - using stan::math::test::get_new_msg; - using stan::math::test::sum_lpdf; - - std::vector> data(5, vec_value); - EXPECT_EQ(result, stan::math::reduce_sum_static(data, 1, get_new_msg(), args...)) << "Failed for reduce_sum_static"; - EXPECT_EQ(result, stan::math::reduce_sum(data, 1, get_new_msg(), args...))<< "Failed for reduce_sum"; - } - - auto reduce_sum_static_int_sum_lpdf = [](auto&&... args) { - return stan::math::reduce_sum_static(std::vector(2, 10.0), 1, - get_new_msg(), args...); - }; - - auto reduce_sum_static_sum_lpdf = [](auto&& data, auto&&... args) { - return stan::math::reduce_sum_static(data, 1, get_new_msg(), args...); - }; - - auto reduce_sum_int_sum_lpdf = [](auto&&... args) { - return stan::math::reduce_sum(std::vector(2, 10.0), 1, - get_new_msg(), args...); - }; - - auto reduce_sum_sum_lpdf = [](auto&& data, auto&&... args) { - return stan::math::reduce_sum(data, 1, get_new_msg(), args...); - }; - - template - void expect_ad_reduce_sum_lpdf(T1&& data, T2&& arg) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_static_int_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - using stan::math::test::reduce_sum_int_sum_lpdf; - stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg); - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data, arg); - stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg); - stan::test::expect_ad(reduce_sum_sum_lpdf, data, arg); - } - - template - struct static_check_lpdf { - template - inline auto operator()(std::size_t start, std::size_t end, - const std::vector&, std::ostream* msgs, - const std::vector& data) const { - T sum = 0; - // std::cout << "start: " << start << ", end: " << end << ", grainsize: " << - // grainsize << std::endl; - EXPECT_LE(end - start + 1, grainsize); - for (size_t i = start; i <= end; i++) { - sum += data[i]; - } - return sum; - } - }; +namespace math { +namespace test { +std::ostream* get_new_msg() { + std::ostream* msgs = nullptr; + return msgs; +} +// reduce functor which is the BinaryFunction +// here we use iterators which represent integer indices +template +struct count_lpdf { + count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::poisson_lpmf(sub_slice, lambda[0]); + } +}; + +template +struct nesting_count_lpdf { + nesting_count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& sub_slice, std::ostream* msgs, + const std::vector& lambda, + const std::vector& idata) const { + return stan::math::reduce_sum>(sub_slice, 5, msgs, lambda, + idata); + } +}; +struct sum_lpdf { + template * = nullptr> + T sum_(T arg) const { + return arg; + } + + template * = nullptr> + auto sum_(EigMat&& arg) const { + return stan::math::sum(arg); + } + + template * = nullptr> + auto sum_(Vec&& arg) const { + stan::scalar_type_t sum = 0; + for (size_t i = 0; i < arg.size(); ++i) { + sum += sum_(arg[i]); } + return sum; } + + template + inline auto operator()(std::size_t start, std::size_t end, T&& sub_slice, + std::ostream* msgs, Args&&... args) const { + using return_type = stan::return_type_t; + + return sum_(sub_slice) + + sub_slice.size() + * stan::math::sum(std::vector{ + return_type(sum_(std::forward(args)))...}); + } +}; +struct start_end_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, T1&&, + std::ostream* msgs, T2&& data) const { + stan::return_type_t sum = 0; + EXPECT_GE(start, 0); + EXPECT_LE(end, data.size() - 1); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } +}; + +/** + * slice over the grouping variable which is a var + */ +template +struct slice_group_count_lpdf { + slice_group_count_lpdf() {} + + // does the reduction in the sub-slice start to end + inline T operator()(std::size_t start, std::size_t end, + const std::vector& lambda_slice, std::ostream* msgs, + const std::vector& y, + const std::vector& gsidx) const { + const std::size_t num_groups = end - start + 1; + T result = 0.0; + for (std::size_t i = 0; i != num_groups; ++i) { + std::vector y_group(y.begin() + gsidx[start + i], + y.begin() + gsidx[start + i + 1]); + result += stan::math::poisson_lpmf(y_group, lambda_slice[i]); + } + return result; + } +}; + +/** + * basic performance test for a hierarchical model + */ +template +struct grouped_count_lpdf { + grouped_count_lpdf() {} + + // does the reduction in the sub-slice start to end + template + inline T operator()(std::size_t start, std::size_t end, VecInt1&& sub_slice, + std::ostream* msgs, VecT&& lambda, VecInt2&& gidx) const { + const std::size_t num_terms = end - start + 1; + // std::cout << "sub-slice " << start << " - " << end << "; num_terms = " << + // num_terms << "; size = " << sub_slice.size() << std::endl; + std::decay_t lambda_slice(num_terms); + for (std::size_t i = 0; i != num_terms; ++i) + lambda_slice[i] = lambda[gidx[start + i]]; + + return stan::math::poisson_lpmf(sub_slice, lambda_slice); + } +}; + +template +void test_slices(T1 result, T2&& vec_value, Args&&... args) { + stan::math::init_threadpool_tbb(); + using stan::math::test::get_new_msg; + using stan::math::test::sum_lpdf; + + std::vector> data(5, vec_value); + EXPECT_EQ(result, stan::math::reduce_sum_static( + data, 1, get_new_msg(), args...)) + << "Failed for reduce_sum_static"; + EXPECT_EQ(result, + stan::math::reduce_sum(data, 1, get_new_msg(), args...)) + << "Failed for reduce_sum"; +} + +auto reduce_sum_static_int_sum_lpdf = [](auto&&... args) { + return stan::math::reduce_sum_static(std::vector(2, 10.0), 1, + get_new_msg(), args...); +}; + +auto reduce_sum_static_sum_lpdf = [](auto&& data, auto&&... args) { + return stan::math::reduce_sum_static(data, 1, get_new_msg(), + args...); +}; + +auto reduce_sum_int_sum_lpdf = [](auto&&... args) { + return stan::math::reduce_sum(std::vector(2, 10.0), 1, + get_new_msg(), args...); +}; + +auto reduce_sum_sum_lpdf = [](auto&& data, auto&&... args) { + return stan::math::reduce_sum(data, 1, get_new_msg(), args...); +}; + +template +void expect_ad_reduce_sum_lpdf(T1&& data, T2&& arg) { + using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data, arg); + stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg); + stan::test::expect_ad(reduce_sum_sum_lpdf, data, arg); } + +template +struct static_check_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, + const std::vector&, std::ostream* msgs, + const std::vector& data) const { + T sum = 0; + // std::cout << "start: " << start << ", end: " << end << ", grainsize: " << + // grainsize << std::endl; + EXPECT_LE(end - start + 1, grainsize); + for (size_t i = start; i <= end; i++) { + sum += data[i]; + } + return sum; + } +}; + +} // namespace test +} // namespace math +} // namespace stan #endif diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index dd53e63267a..1f520f8e5af 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -7,8 +7,8 @@ #include TEST(StanMathRev_reduce_sum, value) { - using stan::math::test::get_new_msg; using stan::math::test::count_lpdf; + using stan::math::test::get_new_msg; stan::math::init_threadpool_tbb(); double lambda_d = 10.0; const std::size_t elems = 10000; @@ -32,8 +32,8 @@ TEST(StanMathRev_reduce_sum, value) { } TEST(StanMathRev_reduce_sum, gradient) { - using stan::math::test::get_new_msg; using stan::math::test::count_lpdf; + using stan::math::test::get_new_msg; stan::math::init_threadpool_tbb(); double lambda_d = 10.0; @@ -51,8 +51,8 @@ TEST(StanMathRev_reduce_sum, gradient) { std::vector idata; std::vector vlambda_v(1, lambda_v); - var poisson_lpdf = stan::math::reduce_sum>(data, 5, get_new_msg(), - vlambda_v, idata); + var poisson_lpdf = stan::math::reduce_sum>( + data, 5, get_new_msg(), vlambda_v, idata); var lambda_ref = lambda_d; var poisson_lpdf_ref = stan::math::poisson_lpmf(data, lambda_ref); @@ -76,8 +76,8 @@ TEST(StanMathRev_reduce_sum, gradient) { } TEST(StanMathRev_reduce_sum, grainsize) { - using stan::math::test::get_new_msg; using stan::math::test::count_lpdf; + using stan::math::test::get_new_msg; stan::math::init_threadpool_tbb(); double lambda_d = 10.0; @@ -95,19 +95,19 @@ TEST(StanMathRev_reduce_sum, grainsize) { std::vector idata; std::vector vlambda_v(1, lambda_v); - EXPECT_THROW( - stan::math::reduce_sum>(data, 0, get_new_msg(), vlambda_v, idata), - std::domain_error); + EXPECT_THROW(stan::math::reduce_sum>(data, 0, get_new_msg(), + vlambda_v, idata), + std::domain_error); - EXPECT_THROW( - stan::math::reduce_sum>(data, -1, get_new_msg(), vlambda_v, idata), - std::domain_error); + EXPECT_THROW(stan::math::reduce_sum>(data, -1, get_new_msg(), + vlambda_v, idata), + std::domain_error); - EXPECT_NO_THROW( - stan::math::reduce_sum>(data, 1, get_new_msg(), vlambda_v, idata)); + EXPECT_NO_THROW(stan::math::reduce_sum>( + data, 1, get_new_msg(), vlambda_v, idata)); - EXPECT_NO_THROW(stan::math::reduce_sum>(data, 2 * elems, get_new_msg(), - vlambda_v, idata)); + EXPECT_NO_THROW(stan::math::reduce_sum>( + data, 2 * elems, get_new_msg(), vlambda_v, idata)); stan::math::recover_memory(); } @@ -156,7 +156,6 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { stan::math::recover_memory(); } - TEST(StanMathRev_reduce_sum, grouped_gradient) { using stan::math::test::get_new_msg; using stan::math::test::grouped_count_lpdf; From c3795dae184bfa3f513f53cbdf50fa139ce4a46e Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 31 Mar 2020 23:44:21 -0400 Subject: [PATCH 07/41] make static tests for rev --- test/unit/math/reduce_sum_util.hpp | 1 + .../unit/math/rev/functor/reduce_sum_test.cpp | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/test/unit/math/reduce_sum_util.hpp b/test/unit/math/reduce_sum_util.hpp index 6e1153b433d..c607b335cbf 100644 --- a/test/unit/math/reduce_sum_util.hpp +++ b/test/unit/math/reduce_sum_util.hpp @@ -2,6 +2,7 @@ #define TEST_UNIT_MATH_REDUCE_SUM_UTIL #include +#include #include #include #include diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 1f520f8e5af..5a34661fe21 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -29,6 +29,11 @@ TEST(StanMathRev_reduce_sum, value) { EXPECT_FLOAT_EQ(poisson_lpdf, poisson_lpdf_ref) << "ref value of poisson lpdf : " << poisson_lpdf_ref << std::endl << "value of poisson lpdf : " << poisson_lpdf << std::endl; + + double poisson_lpdf_static = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_d, idata); + + EXPECT_FLOAT_EQ(poisson_lpdf_static, poisson_lpdf_ref); } TEST(StanMathRev_reduce_sum, gradient) { @@ -72,6 +77,14 @@ TEST(StanMathRev_reduce_sum, gradient) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; + + var poisson_lpdf_static = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_v, idata); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf_static.vi_); + const double lambda_adj_static = lambda_v.adj(); + EXPECT_FLOAT_EQ(lambda_adj_static, lambda_ref_adj); stan::math::recover_memory(); } @@ -109,6 +122,20 @@ TEST(StanMathRev_reduce_sum, grainsize) { EXPECT_NO_THROW(stan::math::reduce_sum>( data, 2 * elems, get_new_msg(), vlambda_v, idata)); + EXPECT_THROW(stan::math::reduce_sum_static>(data, 0, get_new_msg(), + vlambda_v, idata), + std::domain_error); + + EXPECT_THROW(stan::math::reduce_sum_static>(data, -1, get_new_msg(), + vlambda_v, idata), + std::domain_error); + + EXPECT_NO_THROW(stan::math::reduce_sum_static>( + data, 1, get_new_msg(), vlambda_v, idata)); + + EXPECT_NO_THROW(stan::math::reduce_sum_static>( + data, 2 * elems, get_new_msg(), vlambda_v, idata)); + stan::math::recover_memory(); } @@ -153,6 +180,14 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; + var poisson_lpdf_static = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_v, idata); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf_static.vi_); + const double lambda_adj_static = lambda_v.adj(); + + EXPECT_FLOAT_EQ(lambda_adj_static, lambda_ref_adj); stan::math::recover_memory(); } @@ -209,6 +244,13 @@ TEST(StanMathRev_reduce_sum, grouped_gradient) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; + var poisson_lpdf_static = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_v, gidx); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf_static.vi_); + const double lambda_adj_static = lambda_v.adj(); + EXPECT_FLOAT_EQ(lambda_adj_static, lambda_ref_adj); stan::math::recover_memory(); } @@ -264,6 +306,15 @@ TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; + var poisson_lpdf_static = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_v, gidx); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf_static.vi_); + const double lambda_adj_static = lambda_v.adj(); + + EXPECT_FLOAT_EQ(lambda_adj_static, lambda_ref_adj); + stan::math::recover_memory(); } @@ -326,5 +377,14 @@ TEST(StanMathRev_reduce_sum, slice_group_gradient) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; + var poisson_lpdf_static = stan::math::reduce_sum_static>( + vlambda_v, 5, get_new_msg(), data, gsidx); + + stan::math::set_zero_all_adjoints(); + stan::math::grad(poisson_lpdf_static.vi_); + const double lambda_adj_static = lambda_v.adj(); + + EXPECT_FLOAT_EQ(lambda_adj_static, lambda_ref_adj); + stan::math::recover_memory(); } From 07991a5e109e5d80cc9044738c79c3d82c09b240 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 31 Mar 2020 23:45:14 -0400 Subject: [PATCH 08/41] [Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/stable/2017-11-14) --- .../unit/math/rev/functor/reduce_sum_test.cpp | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 5a34661fe21..2748025eeb0 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -30,8 +30,9 @@ TEST(StanMathRev_reduce_sum, value) { << "ref value of poisson lpdf : " << poisson_lpdf_ref << std::endl << "value of poisson lpdf : " << poisson_lpdf << std::endl; - double poisson_lpdf_static = stan::math::reduce_sum_static>( - data, 5, get_new_msg(), vlambda_d, idata); + double poisson_lpdf_static + = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_d, idata); EXPECT_FLOAT_EQ(poisson_lpdf_static, poisson_lpdf_ref); } @@ -77,7 +78,6 @@ TEST(StanMathRev_reduce_sum, gradient) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; - var poisson_lpdf_static = stan::math::reduce_sum_static>( data, 5, get_new_msg(), vlambda_v, idata); @@ -122,12 +122,12 @@ TEST(StanMathRev_reduce_sum, grainsize) { EXPECT_NO_THROW(stan::math::reduce_sum>( data, 2 * elems, get_new_msg(), vlambda_v, idata)); - EXPECT_THROW(stan::math::reduce_sum_static>(data, 0, get_new_msg(), - vlambda_v, idata), + EXPECT_THROW(stan::math::reduce_sum_static>( + data, 0, get_new_msg(), vlambda_v, idata), std::domain_error); - EXPECT_THROW(stan::math::reduce_sum_static>(data, -1, get_new_msg(), - vlambda_v, idata), + EXPECT_THROW(stan::math::reduce_sum_static>( + data, -1, get_new_msg(), vlambda_v, idata), std::domain_error); EXPECT_NO_THROW(stan::math::reduce_sum_static>( @@ -180,8 +180,9 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; - var poisson_lpdf_static = stan::math::reduce_sum_static>( - data, 5, get_new_msg(), vlambda_v, idata); + var poisson_lpdf_static + = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_v, idata); stan::math::set_zero_all_adjoints(); stan::math::grad(poisson_lpdf_static.vi_); @@ -244,8 +245,9 @@ TEST(StanMathRev_reduce_sum, grouped_gradient) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; - var poisson_lpdf_static = stan::math::reduce_sum_static>( - data, 5, get_new_msg(), vlambda_v, gidx); + var poisson_lpdf_static + = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_v, gidx); stan::math::set_zero_all_adjoints(); stan::math::grad(poisson_lpdf_static.vi_); @@ -306,8 +308,9 @@ TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; - var poisson_lpdf_static = stan::math::reduce_sum_static>( - data, 5, get_new_msg(), vlambda_v, gidx); + var poisson_lpdf_static + = stan::math::reduce_sum_static>( + data, 5, get_new_msg(), vlambda_v, gidx); stan::math::set_zero_all_adjoints(); stan::math::grad(poisson_lpdf_static.vi_); @@ -377,7 +380,8 @@ TEST(StanMathRev_reduce_sum, slice_group_gradient) { << "value of poisson lpdf : " << poisson_lpdf.val() << std::endl << "gradient wrt to lambda: " << lambda_adj << std::endl; - var poisson_lpdf_static = stan::math::reduce_sum_static>( + var poisson_lpdf_static + = stan::math::reduce_sum_static>( vlambda_v, 5, get_new_msg(), data, gsidx); stan::math::set_zero_all_adjoints(); From c0ff21db15129c7ed2c720239329ff546b629afd Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 1 Apr 2020 00:26:18 -0400 Subject: [PATCH 09/41] cpplint --- test/unit/math/mix/functor/reduce_sum_test.cpp | 2 +- test/unit/math/prim/functor/reduce_sum_test.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test/unit/math/mix/functor/reduce_sum_test.cpp b/test/unit/math/mix/functor/reduce_sum_test.cpp index bf32b30d831..c84730066e9 100644 --- a/test/unit/math/mix/functor/reduce_sum_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_test.cpp @@ -5,7 +5,7 @@ #include #include -TEST(MathMix_reduce_sum, grainsize) { +TEST(MathMix_reduce_sum, grainsize_static) { using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index 1df27d33750..27779236784 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -2,6 +2,8 @@ #include #include +#include + TEST(StanMathPrim_reduce_sum, value) { stan::math::init_threadpool_tbb(); using stan::math::test::count_lpdf; From 92e56865bd29144942db373263ddcfdd429515cd Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 1 Apr 2020 00:43:43 -0400 Subject: [PATCH 10/41] Add header includes --- stan/math/prim/functor/reduce_sum.hpp | 1 + stan/math/rev/functor/reduce_sum.hpp | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/stan/math/prim/functor/reduce_sum.hpp b/stan/math/prim/functor/reduce_sum.hpp index d451f1fc94c..5f2a16261a3 100644 --- a/stan/math/prim/functor/reduce_sum.hpp +++ b/stan/math/prim/functor/reduce_sum.hpp @@ -2,6 +2,7 @@ #define STAN_MATH_PRIM_FUNCTOR_REDUCE_SUM_HPP #include +#include #include #include diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index aec94c6692a..988b9512b67 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -2,10 +2,7 @@ #define STAN_MATH_REV_FUNCTOR_REDUCE_SUM_HPP #include -#include -#include -#include -#include +#include #include #include #include From acf754e83cdd79211a7d1aeade1f93ed2e1462db Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 1 Apr 2020 01:40:07 -0400 Subject: [PATCH 11/41] reduce_sum headers --- stan/math/prim/functor/reduce_sum_static.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp index 5b13701df93..e8fe2843693 100644 --- a/stan/math/prim/functor/reduce_sum_static.hpp +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -2,7 +2,7 @@ #define STAN_MATH_PRIM_FUNCTOR_REDUCE_SUM_STATIC_HPP #include - +#include #include #include #include From df6ab63809e9ef8e40c0d9f0bb0623a2564e4a8b Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 1 Apr 2020 01:54:51 -0400 Subject: [PATCH 12/41] header stuff --- stan/math/prim/functor/reduce_sum_static.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp index e8fe2843693..2dacdce55ba 100644 --- a/stan/math/prim/functor/reduce_sum_static.hpp +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include From 67230268e11892a0c3727d94805cd7cfa6ec40e4 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 1 Apr 2020 13:03:27 -0400 Subject: [PATCH 13/41] break reduce_sum mix tests into two seperate headers --- .../math/mix/functor/reduce_sum1_test.cpp | 204 ++++++++++++++++++ ...duce_sum_test.cpp => reduce_sum2_test.cpp} | 196 +---------------- 2 files changed, 205 insertions(+), 195 deletions(-) create mode 100644 test/unit/math/mix/functor/reduce_sum1_test.cpp rename test/unit/math/mix/functor/{reduce_sum_test.cpp => reduce_sum2_test.cpp} (59%) diff --git a/test/unit/math/mix/functor/reduce_sum1_test.cpp b/test/unit/math/mix/functor/reduce_sum1_test.cpp new file mode 100644 index 00000000000..a0205980877 --- /dev/null +++ b/test/unit/math/mix/functor/reduce_sum1_test.cpp @@ -0,0 +1,204 @@ +#include +#include +#include + +#include +#include + +// Reduce sum tests are broken up into two files to avoid windows compiler error + +TEST(MathMix_reduce_sum, grainsize_static) { + using stan::math::test::get_new_msg; + using stan::math::test::sum_lpdf; + + auto f1 = [](auto&& data) { + return stan::math::reduce_sum_static(data, 0, get_new_msg()); + }; + auto f2 = [](auto&& data) { + return stan::math::reduce_sum_static(data, -1, get_new_msg()); + }; + auto f3 = [](auto&& data) { + return stan::math::reduce_sum_static(data, 1, get_new_msg()); + }; + auto f4 = [](auto&& data) { + return stan::math::reduce_sum_static(data, 20, get_new_msg()); + }; + + std::vector data(5, 10.0); + + stan::test::expect_ad(f1, data); + stan::test::expect_ad(f2, data); + stan::test::expect_ad(f3, data); + stan::test::expect_ad(f4, data); +} + +TEST(MathMix_reduce_sum, grainsize) { + using stan::math::test::get_new_msg; + using stan::math::test::sum_lpdf; + auto f1 = [](auto&& data) { + return stan::math::reduce_sum(data, 0, get_new_msg()); + }; + auto f2 = [](auto&& data) { + return stan::math::reduce_sum(data, -1, get_new_msg()); + }; + auto f3 = [](auto&& data) { + return stan::math::reduce_sum(data, 1, get_new_msg()); + }; + auto f4 = [](auto&& data) { + return stan::math::reduce_sum(data, 20, get_new_msg()); + }; + + std::vector data(5, 10.0); + + stan::test::expect_ad(f1, data); + stan::test::expect_ad(f2, data); + stan::test::expect_ad(f3, data); + stan::test::expect_ad(f4, data); +} + +TEST(MathMix_reduce_sum, std_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector data(5, 10.0); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector> data(3, std::vector(2, 10.0)); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector data(3, Eigen::VectorXd::Ones(2)); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_row_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector data(3, Eigen::RowVectorXd::Ones(2)); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_matrix_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector data(3, Eigen::MatrixXd::Ones(2, 4)); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_std_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector>> data( + 3, std::vector>(2, std::vector(2, 10.0))); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector> data( + 3, std::vector(2, Eigen::VectorXd::Ones(2))); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_row_vector_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector> data( + 3, std::vector(2, Eigen::RowVectorXd::Ones(2))); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_double_slice) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector> data( + 3, std::vector(2, Eigen::MatrixXd::Ones(2, 4))); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + +TEST(StanMath_reduce_sum_static, start_end_slice) { + using stan::math::test::get_new_msg; + using stan::math::test::start_end_lpdf; + auto start_end_static = [](auto&& arg) { + return stan::math::reduce_sum_static(arg, 1, get_new_msg(), + arg); + }; + + auto start_end = [](auto&& arg) { + return stan::math::reduce_sum_static(arg, 1, get_new_msg(), + arg); + }; + + std::vector data(5, 1.0); + + stan::test::expect_ad(start_end, data); + stan::test::expect_ad(start_end_static, data); +} + +TEST(MathMix_reduce_sum, int_arg) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + std::vector data(2, 1.0); + int arg = 5.0; + + stan::test::expect_ad( + [&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); + stan::test::expect_ad( + [&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); +} + +TEST(MathMix_reduce_sum, std_vector_int_arg) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + std::vector data(2, 10.0); + std::vector arg(2, 10); + + stan::test::expect_ad( + [&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); + stan::test::expect_ad( + [&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); +} + +TEST(MathMix_reduce_sum, double_arg) { + stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), + 5.0); +} + +TEST(MathMix_reduce_sum, std_vector_double_arg) { + stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), + std::vector(2, 10.0)); +} diff --git a/test/unit/math/mix/functor/reduce_sum_test.cpp b/test/unit/math/mix/functor/reduce_sum2_test.cpp similarity index 59% rename from test/unit/math/mix/functor/reduce_sum_test.cpp rename to test/unit/math/mix/functor/reduce_sum2_test.cpp index c84730066e9..db7d8acaac9 100644 --- a/test/unit/math/mix/functor/reduce_sum_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum2_test.cpp @@ -5,201 +5,7 @@ #include #include -TEST(MathMix_reduce_sum, grainsize_static) { - using stan::math::test::get_new_msg; - using stan::math::test::sum_lpdf; - - auto f1 = [](auto&& data) { - return stan::math::reduce_sum_static(data, 0, get_new_msg()); - }; - auto f2 = [](auto&& data) { - return stan::math::reduce_sum_static(data, -1, get_new_msg()); - }; - auto f3 = [](auto&& data) { - return stan::math::reduce_sum_static(data, 1, get_new_msg()); - }; - auto f4 = [](auto&& data) { - return stan::math::reduce_sum_static(data, 20, get_new_msg()); - }; - - std::vector data(5, 10.0); - - stan::test::expect_ad(f1, data); - stan::test::expect_ad(f2, data); - stan::test::expect_ad(f3, data); - stan::test::expect_ad(f4, data); -} - -TEST(MathMix_reduce_sum, grainsize) { - using stan::math::test::get_new_msg; - using stan::math::test::sum_lpdf; - auto f1 = [](auto&& data) { - return stan::math::reduce_sum(data, 0, get_new_msg()); - }; - auto f2 = [](auto&& data) { - return stan::math::reduce_sum(data, -1, get_new_msg()); - }; - auto f3 = [](auto&& data) { - return stan::math::reduce_sum(data, 1, get_new_msg()); - }; - auto f4 = [](auto&& data) { - return stan::math::reduce_sum(data, 20, get_new_msg()); - }; - - std::vector data(5, 10.0); - - stan::test::expect_ad(f1, data); - stan::test::expect_ad(f2, data); - stan::test::expect_ad(f3, data); - stan::test::expect_ad(f4, data); -} - -TEST(MathMix_reduce_sum, std_vector_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - - std::vector data(5, 10.0); - - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - - std::vector> data(3, std::vector(2, 10.0)); - - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); -} - -TEST(MathMix_reduce_sum, std_vector_eigen_vector_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - - std::vector data(3, Eigen::VectorXd::Ones(2)); - - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); -} - -TEST(MathMix_reduce_sum, std_vector_eigen_row_vector_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - - std::vector data(3, Eigen::RowVectorXd::Ones(2)); - - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); -} - -TEST(MathMix_reduce_sum, std_vector_eigen_matrix_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - - std::vector data(3, Eigen::MatrixXd::Ones(2, 4)); - - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_std_vector_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - - std::vector>> data( - 3, std::vector>(2, std::vector(2, 10.0))); - - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_vector_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - - std::vector> data( - 3, std::vector(2, Eigen::VectorXd::Ones(2))); - - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_row_vector_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - - std::vector> data( - 3, std::vector(2, Eigen::RowVectorXd::Ones(2))); - - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_double_slice) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - - std::vector> data( - 3, std::vector(2, Eigen::MatrixXd::Ones(2, 4))); - - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); - stan::test::expect_ad(reduce_sum_sum_lpdf, data); -} - -TEST(StanMath_reduce_sum_static, start_end_slice) { - using stan::math::test::get_new_msg; - using stan::math::test::start_end_lpdf; - auto start_end_static = [](auto&& arg) { - return stan::math::reduce_sum_static(arg, 1, get_new_msg(), - arg); - }; - - auto start_end = [](auto&& arg) { - return stan::math::reduce_sum_static(arg, 1, get_new_msg(), - arg); - }; - - std::vector data(5, 1.0); - - stan::test::expect_ad(start_end, data); - stan::test::expect_ad(start_end_static, data); -} - -TEST(MathMix_reduce_sum, int_arg) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - std::vector data(2, 1.0); - int arg = 5.0; - - stan::test::expect_ad( - [&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); - stan::test::expect_ad( - [&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); -} - -TEST(MathMix_reduce_sum, std_vector_int_arg) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - std::vector data(2, 10.0); - std::vector arg(2, 10); - - stan::test::expect_ad( - [&](auto&& data) { return reduce_sum_static_sum_lpdf(data, arg); }, data); - stan::test::expect_ad( - [&](auto&& data) { return reduce_sum_sum_lpdf(data, arg); }, data); -} - -TEST(MathMix_reduce_sum, double_arg) { - stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), - 5.0); -} - -TEST(MathMix_reduce_sum, std_vector_double_arg) { - stan::math::test::expect_ad_reduce_sum_lpdf(std::vector(2, 10.0), - std::vector(2, 10.0)); -} +// Reduce sum tests are broken up into two files to avoid windows compiler error TEST(MathMix_reduce_sum, eigen_vector_arg) { std::vector data(2, 10.0); From bc7d174b474d16baf35320fc797907fd15195d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Ce=C5=A1novar?= Date: Thu, 2 Apr 2020 19:48:29 +0200 Subject: [PATCH 14/41] split test files --- .../math/mix/functor/reduce_sum2_test.cpp | 287 ------------------ ...um1_test.cpp => reduce_sum_part1_test.cpp} | 2 +- .../mix/functor/reduce_sum_part2_test.cpp | 49 +++ .../mix/functor/reduce_sum_part3_test.cpp | 84 +++++ .../mix/functor/reduce_sum_part4_test.cpp | 131 ++++++++ 5 files changed, 265 insertions(+), 288 deletions(-) delete mode 100644 test/unit/math/mix/functor/reduce_sum2_test.cpp rename test/unit/math/mix/functor/{reduce_sum1_test.cpp => reduce_sum_part1_test.cpp} (98%) create mode 100644 test/unit/math/mix/functor/reduce_sum_part2_test.cpp create mode 100644 test/unit/math/mix/functor/reduce_sum_part3_test.cpp create mode 100644 test/unit/math/mix/functor/reduce_sum_part4_test.cpp diff --git a/test/unit/math/mix/functor/reduce_sum2_test.cpp b/test/unit/math/mix/functor/reduce_sum2_test.cpp deleted file mode 100644 index db7d8acaac9..00000000000 --- a/test/unit/math/mix/functor/reduce_sum2_test.cpp +++ /dev/null @@ -1,287 +0,0 @@ -#include -#include -#include - -#include -#include - -// Reduce sum tests are broken up into two files to avoid windows compiler error - -TEST(MathMix_reduce_sum, eigen_vector_arg) { - std::vector data(2, 10.0); - Eigen::VectorXd arg = Eigen::VectorXd::Ones(2); - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, eigen_row_vector_arg) { - std::vector data(2, 10.0); - Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(2); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, eigen_matrix_arg) { - std::vector data(2, 10.0); - Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(2, 2); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_double_arg) { - std::vector data(2, 10.0); - std::vector> arg(2, std::vector(2, 10.0)); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, std_vector_eigen_vector_arg) { - std::vector data(2, 10.0); - std::vector arg(2, Eigen::VectorXd::Ones(2)); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, std_vector_eigen_row_vector_arg) { - std::vector data(2, 10.0); - std::vector arg(2, Eigen::RowVectorXd::Ones(2)); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, std_vector_eigen_matrix_arg) { - std::vector data(2, 10.0); - std::vector arg(2, Eigen::MatrixXd::Ones(2, 2)); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_std_vector_double_arg) { - std::vector data(2, 10.0); - std::vector>> arg( - 2, std::vector>(2, std::vector(2, 10.0))); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_vector_arg) { - std::vector data(2, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::VectorXd::Ones(2))); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_row_vector_arg) { - std::vector data(2, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::RowVectorXd::Ones(2))); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, std_vector_std_vector_eigen_matrix_arg) { - std::vector data(2, 10.0); - std::vector> arg( - 2, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); - - stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} - -TEST(MathMix_reduce_sum, eigen_three_args1) { - using stan::math::test::reduce_sum_int_sum_lpdf; - using stan::math::test::reduce_sum_static_int_sum_lpdf; - Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); - Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg1, arg2, arg3); - stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum, eigen_three_args2) { - using stan::math::test::reduce_sum_int_sum_lpdf; - using stan::math::test::reduce_sum_static_int_sum_lpdf; - double arg1 = 1.0; - std::vector arg2(2, 1.0); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg1, arg2, arg3); - stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum, eigen_three_args3) { - using stan::math::test::reduce_sum_int_sum_lpdf; - using stan::math::test::reduce_sum_static_int_sum_lpdf; - double arg1 = 1.0; - std::vector> arg2(2, std::vector(2, 1.0)); - std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); - - stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg1, arg2, arg3); - stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum, eigen_three_args_with_ints1) { - using stan::math::test::reduce_sum_int_sum_lpdf; - using stan::math::test::reduce_sum_static_int_sum_lpdf; - Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); - Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_int_sum_lpdf( - 1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); - }, - arg1, arg2, arg3); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, - 3, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum, eigen_three_args_with_ints2) { - using stan::math::test::reduce_sum_int_sum_lpdf; - using stan::math::test::reduce_sum_static_int_sum_lpdf; - double arg1 = 1.0; - std::vector arg2(2, 1.0); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_int_sum_lpdf( - 1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); - }, - arg1, arg2, arg3); - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, - 3, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum, eigen_three_args_with_ints3) { - using stan::math::test::reduce_sum_int_sum_lpdf; - using stan::math::test::reduce_sum_static_int_sum_lpdf; - double arg1 = 1.0; - std::vector> arg2(2, std::vector(2, 1.0)); - std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_int_sum_lpdf( - 1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); - }, - arg1, arg2, arg3); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, - 3, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum, eigen_three_args_with_doubles1) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); - Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_sum_lpdf( - std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); - }, - arg1, arg2, arg3); - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, - 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum, eigen_three_args_with_doubles2) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - double arg1 = 1.0; - std::vector arg2(2, 1.0); - Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_sum_lpdf( - std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); - }, - arg1, arg2, arg3); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, - 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum, eigen_three_args_with_doubles3) { - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - double arg1 = 1.0; - std::vector> arg2(2, std::vector(2, 1.0)); - std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_static_sum_lpdf( - std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); - }, - arg1, arg2, arg3); - - stan::test::expect_ad( - [&](auto&& arg1, auto&& arg2, auto&& arg3) { - return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, - 3.0, arg2, - std::vector{1.0, 2.0, 3.0}, arg3); - }, - arg1, arg2, arg3); -} - -TEST(MathMix_reduce_sum, static_check) { - stan::math::init_threadpool_tbb(); - using stan::math::test::get_new_msg; - using stan::math::test::static_check_lpdf; - - for (auto size : {1, 3, 6, 11}) { - std::vector data(size, 10); - std::vector arg(size, 10); - - auto fi1 = [&](auto&&... args) { - return stan::math::reduce_sum_static>( - data, 1, get_new_msg(), args...); - }; - - auto fi2 = [&](auto&&... args) { - return stan::math::reduce_sum_static>( - data, 2, get_new_msg(), args...); - }; - - auto fi3 = [&](auto&&... args) { - return stan::math::reduce_sum_static>( - data, 3, get_new_msg(), args...); - }; - - stan::test::expect_ad(fi1, arg); - stan::test::expect_ad(fi2, arg); - stan::test::expect_ad(fi3, arg); - } -} diff --git a/test/unit/math/mix/functor/reduce_sum1_test.cpp b/test/unit/math/mix/functor/reduce_sum_part1_test.cpp similarity index 98% rename from test/unit/math/mix/functor/reduce_sum1_test.cpp rename to test/unit/math/mix/functor/reduce_sum_part1_test.cpp index a0205980877..1cac0a2243f 100644 --- a/test/unit/math/mix/functor/reduce_sum1_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part1_test.cpp @@ -5,7 +5,7 @@ #include #include -// Reduce sum tests are broken up into two files to avoid windows compiler error +// Reduce sum tests are broken up into four files to avoid windows compiler error TEST(MathMix_reduce_sum, grainsize_static) { using stan::math::test::get_new_msg; diff --git a/test/unit/math/mix/functor/reduce_sum_part2_test.cpp b/test/unit/math/mix/functor/reduce_sum_part2_test.cpp new file mode 100644 index 00000000000..a1c572b7301 --- /dev/null +++ b/test/unit/math/mix/functor/reduce_sum_part2_test.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +#include +#include + +// Reduce sum tests are broken up into four files to avoid windows compiler error + +TEST(MathMix_reduce_sum, eigen_vector_arg) { + std::vector data(2, 10.0); + Eigen::VectorXd arg = Eigen::VectorXd::Ones(2); + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); +} + +TEST(MathMix_reduce_sum, eigen_row_vector_arg) { + std::vector data(2, 10.0); + Eigen::RowVectorXd arg = Eigen::RowVectorXd::Ones(2); + + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); +} + +TEST(MathMix_reduce_sum, eigen_matrix_arg) { + std::vector data(2, 10.0); + Eigen::MatrixXd arg = Eigen::MatrixXd::Ones(2, 2); + + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_std_vector_double_arg) { + std::vector data(2, 10.0); + std::vector> arg(2, std::vector(2, 10.0)); + + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_vector_arg) { + std::vector data(2, 10.0); + std::vector arg(2, Eigen::VectorXd::Ones(2)); + + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); +} + +TEST(MathMix_reduce_sum, std_vector_eigen_row_vector_arg) { + std::vector data(2, 10.0); + std::vector arg(2, Eigen::RowVectorXd::Ones(2)); + + stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); +} \ No newline at end of file diff --git a/test/unit/math/mix/functor/reduce_sum_part3_test.cpp b/test/unit/math/mix/functor/reduce_sum_part3_test.cpp new file mode 100644 index 00000000000..71812efbd4e --- /dev/null +++ b/test/unit/math/mix/functor/reduce_sum_part3_test.cpp @@ -0,0 +1,84 @@ +#include +#include +#include + +#include +#include + +// Reduce sum tests are broken up into four files to avoid windows compiler error + +TEST(MathMix_reduce_sum, eigen_three_args1) { + using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; + Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); + Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg1, arg2, arg3); + stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args2) { + using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; + double arg1 = 1.0; + std::vector arg2(2, 1.0); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg1, arg2, arg3); + stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args3) { + using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; + double arg1 = 1.0; + std::vector> arg2(2, std::vector(2, 1.0)); + std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg1, arg2, arg3); + stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_ints1) { + using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; + Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); + Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_static_int_sum_lpdf( + 1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, + 3, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_ints2) { + using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; + double arg1 = 1.0; + std::vector arg2(2, 1.0); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_static_int_sum_lpdf( + 1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, + 3, arg3); + }, + arg1, arg2, arg3); +} diff --git a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp new file mode 100644 index 00000000000..5798f4532b6 --- /dev/null +++ b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp @@ -0,0 +1,131 @@ +#include +#include +#include + +#include +#include + +// Reduce sum tests are broken up into four files to avoid windows compiler error + +TEST(MathMix_reduce_sum, eigen_three_args_with_ints3) { + using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; + double arg1 = 1.0; + std::vector> arg2(2, std::vector(2, 1.0)); + std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_static_int_sum_lpdf( + 1, arg1, std::vector{1, 2, 3}, arg2, 3, arg3); + }, + arg1, arg2, arg3); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_int_sum_lpdf(1, arg1, std::vector{1, 2, 3}, arg2, + 3, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_doubles1) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + Eigen::VectorXd arg1 = Eigen::VectorXd::Ones(2); + Eigen::RowVectorXd arg2 = Eigen::RowVectorXd::Ones(2); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_static_sum_lpdf( + std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, + 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_doubles2) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + double arg1 = 1.0; + std::vector arg2(2, 1.0); + Eigen::MatrixXd arg3 = Eigen::MatrixXd::Ones(2, 2); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_static_sum_lpdf( + std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, + 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, eigen_three_args_with_doubles3) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + double arg1 = 1.0; + std::vector> arg2(2, std::vector(2, 1.0)); + std::vector arg3(2, Eigen::MatrixXd::Ones(2, 2)); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_static_sum_lpdf( + std::vector{1.0, 2.0, 3.0}, arg1, 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); + + stan::test::expect_ad( + [&](auto&& arg1, auto&& arg2, auto&& arg3) { + return reduce_sum_sum_lpdf(std::vector{1.0, 2.0, 3.0}, arg1, + 3.0, arg2, + std::vector{1.0, 2.0, 3.0}, arg3); + }, + arg1, arg2, arg3); +} + +TEST(MathMix_reduce_sum, static_check) { + stan::math::init_threadpool_tbb(); + using stan::math::test::get_new_msg; + using stan::math::test::static_check_lpdf; + + for (auto size : {1, 3, 6, 11}) { + std::vector data(size, 10); + std::vector arg(size, 10); + + auto fi1 = [&](auto&&... args) { + return stan::math::reduce_sum_static>( + data, 1, get_new_msg(), args...); + }; + + auto fi2 = [&](auto&&... args) { + return stan::math::reduce_sum_static>( + data, 2, get_new_msg(), args...); + }; + + auto fi3 = [&](auto&&... args) { + return stan::math::reduce_sum_static>( + data, 3, get_new_msg(), args...); + }; + + stan::test::expect_ad(fi1, arg); + stan::test::expect_ad(fi2, arg); + stan::test::expect_ad(fi3, arg); + } +} From c9105b9718a327adfee70d7e630003bf13aa33e6 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 2 Apr 2020 15:53:03 -0400 Subject: [PATCH 15/41] [Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/stable/2017-11-14) --- test/unit/math/mix/functor/reduce_sum_part1_test.cpp | 3 ++- test/unit/math/mix/functor/reduce_sum_part2_test.cpp | 3 ++- test/unit/math/mix/functor/reduce_sum_part3_test.cpp | 3 ++- test/unit/math/mix/functor/reduce_sum_part4_test.cpp | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/test/unit/math/mix/functor/reduce_sum_part1_test.cpp b/test/unit/math/mix/functor/reduce_sum_part1_test.cpp index 1cac0a2243f..113808ce070 100644 --- a/test/unit/math/mix/functor/reduce_sum_part1_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part1_test.cpp @@ -5,7 +5,8 @@ #include #include -// Reduce sum tests are broken up into four files to avoid windows compiler error +// Reduce sum tests are broken up into four files to avoid windows compiler +// error TEST(MathMix_reduce_sum, grainsize_static) { using stan::math::test::get_new_msg; diff --git a/test/unit/math/mix/functor/reduce_sum_part2_test.cpp b/test/unit/math/mix/functor/reduce_sum_part2_test.cpp index a1c572b7301..e65d11dfcd9 100644 --- a/test/unit/math/mix/functor/reduce_sum_part2_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part2_test.cpp @@ -5,7 +5,8 @@ #include #include -// Reduce sum tests are broken up into four files to avoid windows compiler error +// Reduce sum tests are broken up into four files to avoid windows compiler +// error TEST(MathMix_reduce_sum, eigen_vector_arg) { std::vector data(2, 10.0); diff --git a/test/unit/math/mix/functor/reduce_sum_part3_test.cpp b/test/unit/math/mix/functor/reduce_sum_part3_test.cpp index 71812efbd4e..df2d3c1e4be 100644 --- a/test/unit/math/mix/functor/reduce_sum_part3_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part3_test.cpp @@ -5,7 +5,8 @@ #include #include -// Reduce sum tests are broken up into four files to avoid windows compiler error +// Reduce sum tests are broken up into four files to avoid windows compiler +// error TEST(MathMix_reduce_sum, eigen_three_args1) { using stan::math::test::reduce_sum_int_sum_lpdf; diff --git a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp index 5798f4532b6..6bb47541687 100644 --- a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp @@ -5,7 +5,8 @@ #include #include -// Reduce sum tests are broken up into four files to avoid windows compiler error +// Reduce sum tests are broken up into four files to avoid windows compiler +// error TEST(MathMix_reduce_sum, eigen_three_args_with_ints3) { using stan::math::test::reduce_sum_int_sum_lpdf; From a6ae88cd4415ea406bd0c11f0a046b724594bc6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Ce=C5=A1novar?= Date: Thu, 2 Apr 2020 20:01:29 +0200 Subject: [PATCH 16/41] newline --- test/unit/math/mix/functor/reduce_sum_part2_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/math/mix/functor/reduce_sum_part2_test.cpp b/test/unit/math/mix/functor/reduce_sum_part2_test.cpp index e65d11dfcd9..9eabefefab0 100644 --- a/test/unit/math/mix/functor/reduce_sum_part2_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part2_test.cpp @@ -47,4 +47,4 @@ TEST(MathMix_reduce_sum, std_vector_eigen_row_vector_arg) { std::vector arg(2, Eigen::RowVectorXd::Ones(2)); stan::math::test::expect_ad_reduce_sum_lpdf(data, arg); -} \ No newline at end of file +} From 6f68ab714ae9ab2a1f8a7c44823d110e9b5b58e2 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 2 Apr 2020 20:48:11 +0000 Subject: [PATCH 17/41] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp | 2 +- stan/math/prim/prob/beta_rng.hpp | 2 +- stan/math/prim/prob/cauchy_rng.hpp | 2 +- stan/math/prim/prob/chi_square_rng.hpp | 2 +- stan/math/prim/prob/dirichlet_rng.hpp | 4 ++-- stan/math/prim/prob/discrete_range_rng.hpp | 2 +- stan/math/prim/prob/double_exponential_rng.hpp | 2 +- stan/math/prim/prob/frechet_rng.hpp | 2 +- stan/math/prim/prob/hypergeometric_rng.hpp | 2 +- stan/math/prim/prob/inv_chi_square_rng.hpp | 2 +- stan/math/prim/prob/inv_gamma_rng.hpp | 2 +- stan/math/prim/prob/logistic_rng.hpp | 2 +- stan/math/prim/prob/lognormal_rng.hpp | 2 +- stan/math/prim/prob/multi_student_t_rng.hpp | 2 +- stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp | 2 +- stan/math/prim/prob/neg_binomial_2_log_rng.hpp | 2 +- stan/math/prim/prob/neg_binomial_2_rng.hpp | 2 +- stan/math/prim/prob/neg_binomial_rng.hpp | 2 +- stan/math/prim/prob/pareto_type_2_rng.hpp | 2 +- stan/math/prim/prob/poisson_log_rng.hpp | 2 +- stan/math/prim/prob/poisson_rng.hpp | 2 +- stan/math/prim/prob/rayleigh_rng.hpp | 2 +- stan/math/prim/prob/scaled_inv_chi_square_rng.hpp | 2 +- stan/math/prim/prob/skew_normal_rng.hpp | 2 +- stan/math/prim/prob/student_t_rng.hpp | 2 +- stan/math/prim/prob/uniform_rng.hpp | 2 +- stan/math/prim/prob/von_mises_rng.hpp | 2 +- stan/math/prim/prob/weibull_rng.hpp | 2 +- test/prob/student_t/student_t_test.hpp | 2 +- test/prob/test_fixture_ccdf_log.hpp | 2 +- test/prob/test_fixture_cdf.hpp | 2 +- test/prob/test_fixture_cdf_log.hpp | 2 +- test/prob/test_fixture_distr.hpp | 2 +- test/prob/uniform/uniform_ccdf_log_test.hpp | 2 +- test/prob/uniform/uniform_cdf_log_test.hpp | 2 +- test/prob/uniform/uniform_cdf_test.hpp | 2 +- test/prob/utility.hpp | 2 +- test/unit/math/fwd/core/std_numeric_limits_test.cpp | 2 +- test/unit/math/fwd/meta/partials_return_type_test.cpp | 2 +- test/unit/math/fwd/meta/partials_type_test.cpp | 2 +- test/unit/math/mix/core/std_numeric_limits_test.cpp | 2 +- test/unit/math/mix/meta/broadcast_array_test.cpp | 2 +- test/unit/math/mix/meta/partials_return_type_test.cpp | 2 +- test/unit/math/mix/meta/partials_type_test.cpp | 2 +- test/unit/math/mix/meta/return_type_test.cpp | 2 +- test/unit/math/opencl/assign_event_test.cpp | 2 +- test/unit/math/opencl/rev/triangular_transpose_test.cpp | 4 ++-- test/unit/math/prim/fun/beta_test.cpp | 2 +- test/unit/math/prim/fun/binomial_coefficient_log_test.cpp | 2 +- test/unit/math/prim/fun/inv_Phi_test.cpp | 2 +- test/unit/math/prim/fun/log_inv_logit_diff_test.cpp | 2 +- test/unit/math/prim/meta/StdVectorBuilder_test.cpp | 6 +++--- test/unit/math/prim/meta/is_detected_test.cpp | 2 +- test/unit/math/prim/meta/void_t_test.cpp | 2 +- test/unit/math/prim/prob/inv_wishart_rng_test.cpp | 2 +- test/unit/math/prim/prob/ordered_probit_test.cpp | 4 ++-- test/unit/math/prim/prob/wishart_rng_test.cpp | 2 +- test/unit/math/rev/functor/algebra_solver_fp_test.cpp | 4 ++-- test/unit/math/rev/meta/partials_return_type_test.cpp | 4 ++-- test/unit/math/rev/meta/partials_type_test.cpp | 2 +- 60 files changed, 67 insertions(+), 67 deletions(-) diff --git a/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp b/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp index d8ed7024ac1..86b1af1c5be 100644 --- a/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp +++ b/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp @@ -51,8 +51,8 @@ return_type_t bernoulli_logit_glm_lpmf( const T_alpha &alpha, const T_beta &beta) { using Eigen::Array; using Eigen::Dynamic; - using Eigen::Matrix; using Eigen::log1p; + using Eigen::Matrix; using std::exp; using T_partials_return = partials_return_t; using T_y_val = diff --git a/stan/math/prim/prob/beta_rng.hpp b/stan/math/prim/prob/beta_rng.hpp index e3188349730..e516bbed030 100644 --- a/stan/math/prim/prob/beta_rng.hpp +++ b/stan/math/prim/prob/beta_rng.hpp @@ -34,9 +34,9 @@ namespace math { template inline typename VectorBuilder::type beta_rng( const T_shape1 &alpha, const T_shape2 &beta, RNG &rng) { + using boost::variate_generator; using boost::random::gamma_distribution; using boost::random::uniform_real_distribution; - using boost::variate_generator; static const char *function = "beta_rng"; check_positive_finite(function, "First shape parameter", alpha); check_positive_finite(function, "Second shape parameter", beta); diff --git a/stan/math/prim/prob/cauchy_rng.hpp b/stan/math/prim/prob/cauchy_rng.hpp index 57fa4a4adfb..d7ad6a1137a 100644 --- a/stan/math/prim/prob/cauchy_rng.hpp +++ b/stan/math/prim/prob/cauchy_rng.hpp @@ -31,8 +31,8 @@ namespace math { template inline typename VectorBuilder::type cauchy_rng( const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::random::cauchy_distribution; using boost::variate_generator; + using boost::random::cauchy_distribution; static const char* function = "cauchy_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/chi_square_rng.hpp b/stan/math/prim/prob/chi_square_rng.hpp index 9e113cdf150..bfb1e8436e3 100644 --- a/stan/math/prim/prob/chi_square_rng.hpp +++ b/stan/math/prim/prob/chi_square_rng.hpp @@ -26,8 +26,8 @@ namespace math { template inline typename VectorBuilder::type chi_square_rng( const T_deg& nu, RNG& rng) { - using boost::random::chi_squared_distribution; using boost::variate_generator; + using boost::random::chi_squared_distribution; static const char* function = "chi_square_rng"; check_positive_finite(function, "Degrees of freedom parameter", nu); diff --git a/stan/math/prim/prob/dirichlet_rng.hpp b/stan/math/prim/prob/dirichlet_rng.hpp index 88116ec1040..bd4cd0c6232 100644 --- a/stan/math/prim/prob/dirichlet_rng.hpp +++ b/stan/math/prim/prob/dirichlet_rng.hpp @@ -38,10 +38,10 @@ namespace math { template inline Eigen::VectorXd dirichlet_rng( const Eigen::Matrix& alpha, RNG& rng) { - using Eigen::VectorXd; using boost::gamma_distribution; - using boost::random::uniform_real_distribution; using boost::variate_generator; + using boost::random::uniform_real_distribution; + using Eigen::VectorXd; using std::exp; using std::log; diff --git a/stan/math/prim/prob/discrete_range_rng.hpp b/stan/math/prim/prob/discrete_range_rng.hpp index 4348694ece9..adb976e5451 100644 --- a/stan/math/prim/prob/discrete_range_rng.hpp +++ b/stan/math/prim/prob/discrete_range_rng.hpp @@ -34,8 +34,8 @@ template inline typename VectorBuilder::type discrete_range_rng(const T_lower& lower, const T_upper& upper, RNG& rng) { static const char* function = "discrete_range_rng"; - using boost::random::uniform_int_distribution; using boost::variate_generator; + using boost::random::uniform_int_distribution; check_consistent_sizes(function, "Lower bound parameter", lower, "Upper bound parameter", upper); check_greater_or_equal(function, "Upper bound parameter", upper, lower); diff --git a/stan/math/prim/prob/double_exponential_rng.hpp b/stan/math/prim/prob/double_exponential_rng.hpp index f0fe42dbd91..b5dd6cf6b3c 100644 --- a/stan/math/prim/prob/double_exponential_rng.hpp +++ b/stan/math/prim/prob/double_exponential_rng.hpp @@ -33,8 +33,8 @@ namespace math { template inline typename VectorBuilder::type double_exponential_rng(const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::random::uniform_real_distribution; using boost::variate_generator; + using boost::random::uniform_real_distribution; static const char* function = "double_exponential_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/frechet_rng.hpp b/stan/math/prim/prob/frechet_rng.hpp index 66792cc3567..2b38832143c 100644 --- a/stan/math/prim/prob/frechet_rng.hpp +++ b/stan/math/prim/prob/frechet_rng.hpp @@ -30,8 +30,8 @@ namespace math { template inline typename VectorBuilder::type frechet_rng( const T_shape& alpha, const T_scale& sigma, RNG& rng) { - using boost::random::weibull_distribution; using boost::variate_generator; + using boost::random::weibull_distribution; static const char* function = "frechet_rng"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/hypergeometric_rng.hpp b/stan/math/prim/prob/hypergeometric_rng.hpp index 7f8b91032c4..2d4234d6c8f 100644 --- a/stan/math/prim/prob/hypergeometric_rng.hpp +++ b/stan/math/prim/prob/hypergeometric_rng.hpp @@ -12,8 +12,8 @@ namespace math { template inline int hypergeometric_rng(int N, int a, int b, RNG& rng) { - using boost::math::hypergeometric_distribution; using boost::variate_generator; + using boost::math::hypergeometric_distribution; static const char* function = "hypergeometric_rng"; check_bounded(function, "Draws parameter", N, 0, a + b); check_positive(function, "Draws parameter", N); diff --git a/stan/math/prim/prob/inv_chi_square_rng.hpp b/stan/math/prim/prob/inv_chi_square_rng.hpp index 93160aad6ff..b886bb958b4 100644 --- a/stan/math/prim/prob/inv_chi_square_rng.hpp +++ b/stan/math/prim/prob/inv_chi_square_rng.hpp @@ -26,8 +26,8 @@ namespace math { template inline typename VectorBuilder::type inv_chi_square_rng( const T_deg& nu, RNG& rng) { - using boost::random::chi_squared_distribution; using boost::variate_generator; + using boost::random::chi_squared_distribution; static const char* function = "inv_chi_square_rng"; check_positive_finite(function, "Degrees of freedom parameter", nu); diff --git a/stan/math/prim/prob/inv_gamma_rng.hpp b/stan/math/prim/prob/inv_gamma_rng.hpp index b0e809a6298..9adf2a3932f 100644 --- a/stan/math/prim/prob/inv_gamma_rng.hpp +++ b/stan/math/prim/prob/inv_gamma_rng.hpp @@ -31,8 +31,8 @@ namespace math { template inline typename VectorBuilder::type inv_gamma_rng(const T_shape& alpha, const T_scale& beta, RNG& rng) { - using boost::random::gamma_distribution; using boost::variate_generator; + using boost::random::gamma_distribution; static const char* function = "inv_gamma_rng"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", beta); diff --git a/stan/math/prim/prob/logistic_rng.hpp b/stan/math/prim/prob/logistic_rng.hpp index c87fec1ccf7..e8b27e63e3c 100644 --- a/stan/math/prim/prob/logistic_rng.hpp +++ b/stan/math/prim/prob/logistic_rng.hpp @@ -32,8 +32,8 @@ namespace math { template inline typename VectorBuilder::type logistic_rng( const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::random::exponential_distribution; using boost::variate_generator; + using boost::random::exponential_distribution; static const char* function = "logistic_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/lognormal_rng.hpp b/stan/math/prim/prob/lognormal_rng.hpp index 8985de27936..a84e26491b9 100644 --- a/stan/math/prim/prob/lognormal_rng.hpp +++ b/stan/math/prim/prob/lognormal_rng.hpp @@ -31,8 +31,8 @@ namespace math { template inline typename VectorBuilder::type lognormal_rng( const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::random::lognormal_distribution; using boost::variate_generator; + using boost::random::lognormal_distribution; static const char* function = "lognormal_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/multi_student_t_rng.hpp b/stan/math/prim/prob/multi_student_t_rng.hpp index e27e89c4cdb..e532b11bbcd 100644 --- a/stan/math/prim/prob/multi_student_t_rng.hpp +++ b/stan/math/prim/prob/multi_student_t_rng.hpp @@ -36,8 +36,8 @@ multi_student_t_rng( double nu, const T_loc& mu, const Eigen::Matrix& S, RNG& rng) { using boost::normal_distribution; - using boost::random::gamma_distribution; using boost::variate_generator; + using boost::random::gamma_distribution; static const char* function = "multi_student_t_rng"; check_not_nan(function, "Degrees of freedom parameter", nu); diff --git a/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp b/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp index e155b8935bf..713a24c0217 100644 --- a/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp @@ -63,9 +63,9 @@ neg_binomial_2_log_glm_lpmf( const T_alpha& alpha, const T_beta& beta, const T_precision& phi) { using Eigen::Array; using Eigen::Dynamic; - using Eigen::Matrix; using Eigen::exp; using Eigen::log1p; + using Eigen::Matrix; using T_partials_return = partials_return_t; using T_precision_val = typename std::conditional_t< diff --git a/stan/math/prim/prob/neg_binomial_2_log_rng.hpp b/stan/math/prim/prob/neg_binomial_2_log_rng.hpp index 1a7d85e95bb..3a5228132e5 100644 --- a/stan/math/prim/prob/neg_binomial_2_log_rng.hpp +++ b/stan/math/prim/prob/neg_binomial_2_log_rng.hpp @@ -35,8 +35,8 @@ template inline typename VectorBuilder::type neg_binomial_2_log_rng(const T_loc& eta, const T_inv& phi, RNG& rng) { using boost::gamma_distribution; - using boost::random::poisson_distribution; using boost::variate_generator; + using boost::random::poisson_distribution; static const char* function = "neg_binomial_2_log_rng"; check_finite(function, "Log-location parameter", eta); check_positive_finite(function, "Inverse dispersion parameter", phi); diff --git a/stan/math/prim/prob/neg_binomial_2_rng.hpp b/stan/math/prim/prob/neg_binomial_2_rng.hpp index 9a9cdef03cb..4f93387bdfc 100644 --- a/stan/math/prim/prob/neg_binomial_2_rng.hpp +++ b/stan/math/prim/prob/neg_binomial_2_rng.hpp @@ -34,8 +34,8 @@ template inline typename VectorBuilder::type neg_binomial_2_rng(const T_loc& mu, const T_prec& phi, RNG& rng) { using boost::gamma_distribution; - using boost::random::poisson_distribution; using boost::variate_generator; + using boost::random::poisson_distribution; static const char* function = "neg_binomial_2_rng"; check_positive_finite(function, "Location parameter", mu); check_positive_finite(function, "Precision parameter", phi); diff --git a/stan/math/prim/prob/neg_binomial_rng.hpp b/stan/math/prim/prob/neg_binomial_rng.hpp index 9557772f750..95baf77902d 100644 --- a/stan/math/prim/prob/neg_binomial_rng.hpp +++ b/stan/math/prim/prob/neg_binomial_rng.hpp @@ -34,8 +34,8 @@ template inline typename VectorBuilder::type neg_binomial_rng( const T_shape& alpha, const T_inv& beta, RNG& rng) { using boost::gamma_distribution; - using boost::random::poisson_distribution; using boost::variate_generator; + using boost::random::poisson_distribution; static const char* function = "neg_binomial_rng"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Inverse scale parameter", beta); diff --git a/stan/math/prim/prob/pareto_type_2_rng.hpp b/stan/math/prim/prob/pareto_type_2_rng.hpp index f6108671821..6b27f646352 100644 --- a/stan/math/prim/prob/pareto_type_2_rng.hpp +++ b/stan/math/prim/prob/pareto_type_2_rng.hpp @@ -39,8 +39,8 @@ template inline typename VectorBuilder::type pareto_type_2_rng(const T_loc& mu, const T_scale& lambda, const T_shape& alpha, RNG& rng) { - using boost::random::uniform_real_distribution; using boost::variate_generator; + using boost::random::uniform_real_distribution; static const char* function = "pareto_type_2_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", lambda); diff --git a/stan/math/prim/prob/poisson_log_rng.hpp b/stan/math/prim/prob/poisson_log_rng.hpp index 9d1364791f8..22c42a4345c 100644 --- a/stan/math/prim/prob/poisson_log_rng.hpp +++ b/stan/math/prim/prob/poisson_log_rng.hpp @@ -28,8 +28,8 @@ namespace math { template inline typename VectorBuilder::type poisson_log_rng( const T_rate& alpha, RNG& rng) { - using boost::random::poisson_distribution; using boost::variate_generator; + using boost::random::poisson_distribution; static const char* function = "poisson_log_rng"; static const double POISSON_MAX_LOG_RATE = 30 * LOG_TWO; check_finite(function, "Log rate parameter", alpha); diff --git a/stan/math/prim/prob/poisson_rng.hpp b/stan/math/prim/prob/poisson_rng.hpp index c1a0532ff5e..e2150d952e8 100644 --- a/stan/math/prim/prob/poisson_rng.hpp +++ b/stan/math/prim/prob/poisson_rng.hpp @@ -27,8 +27,8 @@ namespace math { template inline typename VectorBuilder::type poisson_rng( const T_rate& lambda, RNG& rng) { - using boost::random::poisson_distribution; using boost::variate_generator; + using boost::random::poisson_distribution; static const char* function = "poisson_rng"; check_not_nan(function, "Rate parameter", lambda); check_positive(function, "Rate parameter", lambda); diff --git a/stan/math/prim/prob/rayleigh_rng.hpp b/stan/math/prim/prob/rayleigh_rng.hpp index a767d0b705d..6793e6cc0ab 100644 --- a/stan/math/prim/prob/rayleigh_rng.hpp +++ b/stan/math/prim/prob/rayleigh_rng.hpp @@ -27,8 +27,8 @@ namespace math { template inline typename VectorBuilder::type rayleigh_rng( const T_scale& sigma, RNG& rng) { - using boost::random::uniform_real_distribution; using boost::variate_generator; + using boost::random::uniform_real_distribution; static const char* function = "rayleigh_rng"; check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp b/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp index f9648899f94..444e26e24a7 100644 --- a/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp +++ b/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp @@ -32,8 +32,8 @@ namespace math { template inline typename VectorBuilder::type scaled_inv_chi_square_rng(const T_deg& nu, const T_scale& s, RNG& rng) { - using boost::random::chi_squared_distribution; using boost::variate_generator; + using boost::random::chi_squared_distribution; static const char* function = "scaled_inv_chi_square_rng"; check_positive_finite(function, "Degrees of freedom parameter", nu); check_positive_finite(function, "Scale parameter", s); diff --git a/stan/math/prim/prob/skew_normal_rng.hpp b/stan/math/prim/prob/skew_normal_rng.hpp index 926b5db2466..347cf175e74 100644 --- a/stan/math/prim/prob/skew_normal_rng.hpp +++ b/stan/math/prim/prob/skew_normal_rng.hpp @@ -37,8 +37,8 @@ template inline typename VectorBuilder::type skew_normal_rng(const T_loc& mu, const T_scale& sigma, const T_shape& alpha, RNG& rng) { - using boost::random::normal_distribution; using boost::variate_generator; + using boost::random::normal_distribution; static const char* function = "skew_normal_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/student_t_rng.hpp b/stan/math/prim/prob/student_t_rng.hpp index 5edfa17f1d7..68149d1fc25 100644 --- a/stan/math/prim/prob/student_t_rng.hpp +++ b/stan/math/prim/prob/student_t_rng.hpp @@ -36,8 +36,8 @@ template inline typename VectorBuilder::type student_t_rng(const T_deg& nu, const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::random::student_t_distribution; using boost::variate_generator; + using boost::random::student_t_distribution; static const char* function = "student_t_rng"; check_positive_finite(function, "Degrees of freedom parameter", nu); check_finite(function, "Location parameter", mu); diff --git a/stan/math/prim/prob/uniform_rng.hpp b/stan/math/prim/prob/uniform_rng.hpp index 84906bc9625..05a8ec29e8b 100644 --- a/stan/math/prim/prob/uniform_rng.hpp +++ b/stan/math/prim/prob/uniform_rng.hpp @@ -33,8 +33,8 @@ namespace math { template inline typename VectorBuilder::type uniform_rng( const T_alpha& alpha, const T_beta& beta, RNG& rng) { - using boost::random::uniform_real_distribution; using boost::variate_generator; + using boost::random::uniform_real_distribution; static const char* function = "uniform_rng"; check_finite(function, "Lower bound parameter", alpha); check_finite(function, "Upper bound parameter", beta); diff --git a/stan/math/prim/prob/von_mises_rng.hpp b/stan/math/prim/prob/von_mises_rng.hpp index d36ed93bbdb..86d4a68df5a 100644 --- a/stan/math/prim/prob/von_mises_rng.hpp +++ b/stan/math/prim/prob/von_mises_rng.hpp @@ -44,8 +44,8 @@ namespace math { template inline typename VectorBuilder::type von_mises_rng( const T_loc& mu, const T_conc& kappa, RNG& rng) { - using boost::random::uniform_real_distribution; using boost::variate_generator; + using boost::random::uniform_real_distribution; static const char* function = "von_mises_rng"; check_finite(function, "Location parameter", mu); check_nonnegative(function, "Scale parameter", kappa); diff --git a/stan/math/prim/prob/weibull_rng.hpp b/stan/math/prim/prob/weibull_rng.hpp index bf91178711f..68e7c478e95 100644 --- a/stan/math/prim/prob/weibull_rng.hpp +++ b/stan/math/prim/prob/weibull_rng.hpp @@ -31,8 +31,8 @@ namespace math { template inline typename VectorBuilder::type weibull_rng( const T_shape& alpha, const T_scale& sigma, RNG& rng) { - using boost::random::weibull_distribution; using boost::variate_generator; + using boost::random::weibull_distribution; static const char* function = "weibull_rng"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", sigma); diff --git a/test/prob/student_t/student_t_test.hpp b/test/prob/student_t/student_t_test.hpp index 5dd27477997..c3f4d7e42ab 100644 --- a/test/prob/student_t/student_t_test.hpp +++ b/test/prob/student_t/student_t_test.hpp @@ -85,8 +85,8 @@ class AgradDistributionsStudentT : public AgradDistributionTest { const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& sigma, const T4&, const T5&) { using boost::math::lgamma; - using stan::math::LOG_SQRT_PI; using stan::math::log1p; + using stan::math::LOG_SQRT_PI; using stan::math::square; using std::log; diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index 3f3f0c5106d..468c5b7f139 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -9,9 +9,9 @@ using Eigen::Dynamic; using Eigen::Matrix; using stan::is_constant_all; using stan::is_vector; +using stan::scalar_type; using stan::math::value_of; using stan::math::var; -using stan::scalar_type; using std::vector; class AgradCcdfLogTest { diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index 9f80760e735..f4736bef637 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -9,9 +9,9 @@ using Eigen::Dynamic; using Eigen::Matrix; using stan::is_constant_all; using stan::is_vector; +using stan::scalar_type; using stan::math::value_of; using stan::math::var; -using stan::scalar_type; using std::vector; class AgradCdfTest { diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index 739e8a29e99..a0c133f9103 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -9,9 +9,9 @@ using Eigen::Dynamic; using Eigen::Matrix; using stan::is_constant_all; using stan::is_vector; +using stan::scalar_type; using stan::math::value_of; using stan::math::var; -using stan::scalar_type; using std::vector; class AgradCdfLogTest { diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index cfcd73e6b4e..b4278c9d05d 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -10,10 +10,10 @@ using Eigen::Dynamic; using Eigen::Matrix; using stan::is_constant_all; using stan::is_vector; +using stan::scalar_type; using stan::math::fvar; using stan::math::value_of; using stan::math::var; -using stan::scalar_type; using std::vector; /** diff --git a/test/prob/uniform/uniform_ccdf_log_test.hpp b/test/prob/uniform/uniform_ccdf_log_test.hpp index 74fcabb652d..3dd940a7ee9 100644 --- a/test/prob/uniform/uniform_ccdf_log_test.hpp +++ b/test/prob/uniform/uniform_ccdf_log_test.hpp @@ -60,8 +60,8 @@ class AgradCcdfLogUniform : public AgradCcdfLogTest { stan::return_type_t ccdf_log_function( const T_y& y, const T_low& alpha, const T_high& beta, const T3&, const T4&, const T5&) { - using stan::math::LOG_ZERO; using stan::math::include_summand; + using stan::math::LOG_ZERO; if (y < alpha || y > beta) return 0.0; diff --git a/test/prob/uniform/uniform_cdf_log_test.hpp b/test/prob/uniform/uniform_cdf_log_test.hpp index a5db47b0a12..27049d43c90 100644 --- a/test/prob/uniform/uniform_cdf_log_test.hpp +++ b/test/prob/uniform/uniform_cdf_log_test.hpp @@ -61,8 +61,8 @@ class AgradCdfLogUniform : public AgradCdfLogTest { const T_high& beta, const T3&, const T4&, const T5&) { - using stan::math::LOG_ZERO; using stan::math::include_summand; + using stan::math::LOG_ZERO; if (y < alpha || y > beta) return LOG_ZERO; diff --git a/test/prob/uniform/uniform_cdf_test.hpp b/test/prob/uniform/uniform_cdf_test.hpp index 38211532169..db02726f657 100644 --- a/test/prob/uniform/uniform_cdf_test.hpp +++ b/test/prob/uniform/uniform_cdf_test.hpp @@ -59,8 +59,8 @@ class AgradCdfUniform : public AgradCdfTest { const T_high& beta, const T3&, const T4&, const T5&) { - using stan::math::LOG_ZERO; using stan::math::include_summand; + using stan::math::LOG_ZERO; if (y < alpha || y > beta) return 0.0; diff --git a/test/prob/utility.hpp b/test/prob/utility.hpp index 6d3a4fb42f2..0925b330b03 100644 --- a/test/prob/utility.hpp +++ b/test/prob/utility.hpp @@ -5,9 +5,9 @@ using stan::is_constant_all; using stan::is_vector; +using stan::scalar_type; using stan::math::fvar; using stan::math::var; -using stan::scalar_type; using std::vector; using size_type = stan::math::index_type_t>; diff --git a/test/unit/math/fwd/core/std_numeric_limits_test.cpp b/test/unit/math/fwd/core/std_numeric_limits_test.cpp index 3b820ab876d..798736eb571 100644 --- a/test/unit/math/fwd/core/std_numeric_limits_test.cpp +++ b/test/unit/math/fwd/core/std_numeric_limits_test.cpp @@ -4,8 +4,8 @@ #include TEST(AgradFwdNumericLimits, All_Fvar) { - using stan::math::INFTY; using stan::math::fvar; + using stan::math::INFTY; using std::isnan; EXPECT_TRUE(std::numeric_limits >::is_specialized); diff --git a/test/unit/math/fwd/meta/partials_return_type_test.cpp b/test/unit/math/fwd/meta/partials_return_type_test.cpp index 0783ad14d36..54b72a7b0c7 100644 --- a/test/unit/math/fwd/meta/partials_return_type_test.cpp +++ b/test/unit/math/fwd/meta/partials_return_type_test.cpp @@ -3,8 +3,8 @@ #include #include -using stan::math::fvar; using stan::partials_return_type; +using stan::math::fvar; TEST(MathMetaFwd, PartialsReturnTypeFvarDouble) { test::expect_same_type >::type>(); diff --git a/test/unit/math/fwd/meta/partials_type_test.cpp b/test/unit/math/fwd/meta/partials_type_test.cpp index f49d091a24c..442fe99dd0d 100644 --- a/test/unit/math/fwd/meta/partials_type_test.cpp +++ b/test/unit/math/fwd/meta/partials_type_test.cpp @@ -2,8 +2,8 @@ #include TEST(MathMetaFwd, partials_type) { - using stan::math::fvar; using stan::partials_type; + using stan::math::fvar; stan::partials_type >::type a(2.0); EXPECT_EQ(2.0, a); diff --git a/test/unit/math/mix/core/std_numeric_limits_test.cpp b/test/unit/math/mix/core/std_numeric_limits_test.cpp index 2b900ce58b9..9dde1d79f78 100644 --- a/test/unit/math/mix/core/std_numeric_limits_test.cpp +++ b/test/unit/math/mix/core/std_numeric_limits_test.cpp @@ -3,8 +3,8 @@ #include TEST(AgradMixNumericLimits, All_Fvar) { - using stan::math::INFTY; using stan::math::fvar; + using stan::math::INFTY; using stan::math::var; using std::isnan; diff --git a/test/unit/math/mix/meta/broadcast_array_test.cpp b/test/unit/math/mix/meta/broadcast_array_test.cpp index e2bdbc257bf..ef9697a2edb 100644 --- a/test/unit/math/mix/meta/broadcast_array_test.cpp +++ b/test/unit/math/mix/meta/broadcast_array_test.cpp @@ -5,8 +5,8 @@ TEST(MathMetaMix, broadcast_array) { using stan::math::fvar; - using stan::math::internal::broadcast_array; using stan::math::var; + using stan::math::internal::broadcast_array; fvar fv(1.0, 2.1); broadcast_array > ba(fv); diff --git a/test/unit/math/mix/meta/partials_return_type_test.cpp b/test/unit/math/mix/meta/partials_return_type_test.cpp index f19916b0ab6..53ed911e0a0 100644 --- a/test/unit/math/mix/meta/partials_return_type_test.cpp +++ b/test/unit/math/mix/meta/partials_return_type_test.cpp @@ -3,9 +3,9 @@ #include #include +using stan::partials_return_type; using stan::math::fvar; using stan::math::var; -using stan::partials_return_type; TEST(MathMetaMix, PartialsReturnTypeFvarVar) { test::expect_same_type >::type>(); diff --git a/test/unit/math/mix/meta/partials_type_test.cpp b/test/unit/math/mix/meta/partials_type_test.cpp index ec5d5af5e0b..13c58504c7a 100644 --- a/test/unit/math/mix/meta/partials_type_test.cpp +++ b/test/unit/math/mix/meta/partials_type_test.cpp @@ -2,9 +2,9 @@ #include TEST(MathMetaMix, partials_type) { + using stan::partials_type; using stan::math::fvar; using stan::math::var; - using stan::partials_type; stan::partials_type > >::type d(7.0, 1.0); EXPECT_EQ(7.0, d.val_.val()); diff --git a/test/unit/math/mix/meta/return_type_test.cpp b/test/unit/math/mix/meta/return_type_test.cpp index fd4013b3d2c..0ded7225cc3 100644 --- a/test/unit/math/mix/meta/return_type_test.cpp +++ b/test/unit/math/mix/meta/return_type_test.cpp @@ -4,9 +4,9 @@ #include #include +using stan::return_type; using stan::math::fvar; using stan::math::var; -using stan::return_type; using d_t = double; using v_t = var; using fd_t = fvar; diff --git a/test/unit/math/opencl/assign_event_test.cpp b/test/unit/math/opencl/assign_event_test.cpp index 84674753424..cf156b4b3b5 100644 --- a/test/unit/math/opencl/assign_event_test.cpp +++ b/test/unit/math/opencl/assign_event_test.cpp @@ -7,8 +7,8 @@ using stan::math::matrix_cl; using stan::math::opencl_kernels::in_buffer; using stan::math::opencl_kernels::in_out_buffer; -using stan::math::opencl_kernels::internal::assign_events; using stan::math::opencl_kernels::out_buffer; +using stan::math::opencl_kernels::internal::assign_events; TEST(assign_event, correct_vectors) { matrix_cl m; diff --git a/test/unit/math/opencl/rev/triangular_transpose_test.cpp b/test/unit/math/opencl/rev/triangular_transpose_test.cpp index 0deb0634876..5ec8fe74bbd 100644 --- a/test/unit/math/opencl/rev/triangular_transpose_test.cpp +++ b/test/unit/math/opencl/rev/triangular_transpose_test.cpp @@ -5,8 +5,8 @@ #include TEST(MathMatrixRevCL, triangular_transpose_m_exception_pass) { - using stan::math::TriangularMapCL; using stan::math::matrix_cl; + using stan::math::TriangularMapCL; using stan::math::var; matrix_cl m1(1, 1); matrix_cl m0; @@ -28,11 +28,11 @@ TEST(MathMatrixRevCL, triangular_transpose_m_exception_pass) { } TEST(MathMatrixRevCL, triangular_transpose_m_pass) { - using stan::math::TriangularMapCL; using stan::math::from_matrix_cl; using stan::math::matrix_cl; using stan::math::matrix_d; using stan::math::matrix_v; + using stan::math::TriangularMapCL; using stan::math::var; matrix_v m0(2, 2); matrix_d m0_dst(2, 2); diff --git a/test/unit/math/prim/fun/beta_test.cpp b/test/unit/math/prim/fun/beta_test.cpp index baf3a0300aa..b8e4bb2e971 100644 --- a/test/unit/math/prim/fun/beta_test.cpp +++ b/test/unit/math/prim/fun/beta_test.cpp @@ -10,9 +10,9 @@ TEST(MathFunctions, beta) { } TEST(MathFunctions, beta_nan) { + using stan::math::beta; using stan::math::INFTY; using stan::math::NOT_A_NUMBER; - using stan::math::beta; EXPECT_TRUE(std::isnan(beta(NOT_A_NUMBER, 2.16))); EXPECT_TRUE(std::isnan(beta(1.65, NOT_A_NUMBER))); diff --git a/test/unit/math/prim/fun/binomial_coefficient_log_test.cpp b/test/unit/math/prim/fun/binomial_coefficient_log_test.cpp index e5df58947b9..91e7dd9ba04 100644 --- a/test/unit/math/prim/fun/binomial_coefficient_log_test.cpp +++ b/test/unit/math/prim/fun/binomial_coefficient_log_test.cpp @@ -48,8 +48,8 @@ TEST(MathFunctions, binomial_coefficient_log_nan) { } TEST(MathFunctions, binomial_coefficient_log_errors_edge_cases) { - using stan::math::INFTY; using stan::math::binomial_coefficient_log; + using stan::math::INFTY; EXPECT_NO_THROW(binomial_coefficient_log(10, 11)); EXPECT_THROW(binomial_coefficient_log(10, 11.01), std::domain_error); diff --git a/test/unit/math/prim/fun/inv_Phi_test.cpp b/test/unit/math/prim/fun/inv_Phi_test.cpp index 9538f7151d6..be63208746d 100644 --- a/test/unit/math/prim/fun/inv_Phi_test.cpp +++ b/test/unit/math/prim/fun/inv_Phi_test.cpp @@ -3,8 +3,8 @@ #include TEST(MathFunctions, inv_Phi) { - using stan::math::Phi; using stan::math::inv_Phi; + using stan::math::Phi; EXPECT_FLOAT_EQ(0.0, inv_Phi(0.5)); double p = 0.123456789; EXPECT_FLOAT_EQ(p, Phi(inv_Phi(p))); diff --git a/test/unit/math/prim/fun/log_inv_logit_diff_test.cpp b/test/unit/math/prim/fun/log_inv_logit_diff_test.cpp index c9287744fe1..51eb4b2b1b4 100644 --- a/test/unit/math/prim/fun/log_inv_logit_diff_test.cpp +++ b/test/unit/math/prim/fun/log_inv_logit_diff_test.cpp @@ -10,8 +10,8 @@ TEST(MathFunctions, log_inv_logit_diff) { } TEST(MathFunctions, log_inv_logit_diff_nan) { - using stan::math::NOT_A_NUMBER; using stan::math::log_inv_logit_diff; + using stan::math::NOT_A_NUMBER; EXPECT_TRUE(std::isnan(log_inv_logit_diff(NOT_A_NUMBER, 2.16))); } diff --git a/test/unit/math/prim/meta/StdVectorBuilder_test.cpp b/test/unit/math/prim/meta/StdVectorBuilder_test.cpp index f1886099cd1..45707477ec7 100644 --- a/test/unit/math/prim/meta/StdVectorBuilder_test.cpp +++ b/test/unit/math/prim/meta/StdVectorBuilder_test.cpp @@ -30,8 +30,8 @@ TEST(MathMetaPrim, StdVectorBuilder_true_false_scalar) { } TEST(MathMetaPrim, StdVectorBuilder_type_check_scalar) { - using stan::StdVectorBuilder; using stan::contains_std_vector; + using stan::StdVectorBuilder; bool r = contains_std_vector::type>::value; @@ -69,8 +69,8 @@ TEST(MathMetaPrim, StdVectorBuilder_true_false_vector) { } TEST(MathMetaPrim, StdVectorBuilder_type_check_vector) { - using stan::StdVectorBuilder; using stan::contains_std_vector; + using stan::StdVectorBuilder; bool r = contains_std_vector< StdVectorBuilder>::type>::value; @@ -125,8 +125,8 @@ TEST(MathMetaPrim, StdVectorBuilder_true_false_matrix) { } TEST(MathMetaPrim, StdVectorBuilder_type_check_matrix) { - using stan::StdVectorBuilder; using stan::contains_std_vector; + using stan::StdVectorBuilder; bool r = contains_std_vector>::type>::value; diff --git a/test/unit/math/prim/meta/is_detected_test.cpp b/test/unit/math/prim/meta/is_detected_test.cpp index 30a824f902f..102e3851d77 100644 --- a/test/unit/math/prim/meta/is_detected_test.cpp +++ b/test/unit/math/prim/meta/is_detected_test.cpp @@ -20,9 +20,9 @@ struct Purr { TEST(MetaTraits, is_detected_checks) { using stan::is_detected; + using stan::test::internal::copy_assign_t; using stan::test::internal::Meow; using stan::test::internal::Purr; - using stan::test::internal::copy_assign_t; EXPECT_TRUE((is_detected::value)); EXPECT_TRUE((is_detected::value)); EXPECT_TRUE((is_detected::value)); diff --git a/test/unit/math/prim/meta/void_t_test.cpp b/test/unit/math/prim/meta/void_t_test.cpp index f8f77f26fe5..ed2cf711345 100644 --- a/test/unit/math/prim/meta/void_t_test.cpp +++ b/test/unit/math/prim/meta/void_t_test.cpp @@ -24,9 +24,9 @@ class dummy_iterable { } // namespace stan TEST(MetaTraits, void_t_checks) { + using stan::void_t; using stan::test::internal::dummy_iterable; using stan::test::internal::is_iterable; - using stan::void_t; EXPECT_FALSE(is_iterable::value); EXPECT_FALSE(is_iterable::value); EXPECT_FALSE(is_iterable::value); diff --git a/test/unit/math/prim/prob/inv_wishart_rng_test.cpp b/test/unit/math/prim/prob/inv_wishart_rng_test.cpp index c56ada5f426..c1f83030b17 100644 --- a/test/unit/math/prim/prob/inv_wishart_rng_test.cpp +++ b/test/unit/math/prim/prob/inv_wishart_rng_test.cpp @@ -33,9 +33,9 @@ TEST(probdistributionsInvWishartRng, symmetry) { } TEST(ProbDistributionsInvWishart, chiSquareGoodnessFitTest) { - using Eigen::MatrixXd; using boost::math::chi_squared; using boost::math::digamma; + using Eigen::MatrixXd; using stan::math::determinant; using stan::math::inv_wishart_rng; using std::log; diff --git a/test/unit/math/prim/prob/ordered_probit_test.cpp b/test/unit/math/prim/prob/ordered_probit_test.cpp index 0b5533a0c38..50350f224ad 100644 --- a/test/unit/math/prim/prob/ordered_probit_test.cpp +++ b/test/unit/math/prim/prob/ordered_probit_test.cpp @@ -25,8 +25,8 @@ TEST(ProbDistributions, ordered_probit_vals) { using Eigen::Dynamic; using Eigen::Matrix; - using stan::math::Phi; using stan::math::ordered_probit_log; + using stan::math::Phi; int K = 5; Matrix c(K - 1); @@ -51,8 +51,8 @@ TEST(ProbDistributions, ordered_probit_vals_2) { using Eigen::Dynamic; using Eigen::Matrix; - using stan::math::Phi; using stan::math::ordered_probit_log; + using stan::math::Phi; int K = 3; Matrix c(K - 1); diff --git a/test/unit/math/prim/prob/wishart_rng_test.cpp b/test/unit/math/prim/prob/wishart_rng_test.cpp index c14afcad877..4760d8ef9b8 100644 --- a/test/unit/math/prim/prob/wishart_rng_test.cpp +++ b/test/unit/math/prim/prob/wishart_rng_test.cpp @@ -36,9 +36,9 @@ TEST(probdistributionsWishartRng, symmetry) { } TEST(ProbDistributionsWishart, marginalTwoChiSquareGoodnessFitTest) { - using Eigen::MatrixXd; using boost::math::chi_squared; using boost::math::digamma; + using Eigen::MatrixXd; using stan::math::determinant; using stan::math::wishart_rng; using std::log; diff --git a/test/unit/math/rev/functor/algebra_solver_fp_test.cpp b/test/unit/math/rev/functor/algebra_solver_fp_test.cpp index e7100c91df0..f93fb3717aa 100644 --- a/test/unit/math/rev/functor/algebra_solver_fp_test.cpp +++ b/test/unit/math/rev/functor/algebra_solver_fp_test.cpp @@ -10,11 +10,11 @@ #include #include +using stan::math::algebra_solver_fp; +using stan::math::finite_diff_gradient_auto; using stan::math::FixedPointADJac; using stan::math::FixedPointSolver; using stan::math::KinsolFixedPointEnv; -using stan::math::algebra_solver_fp; -using stan::math::finite_diff_gradient_auto; using stan::math::to_array_1d; using stan::math::to_var; using stan::math::value_of; diff --git a/test/unit/math/rev/meta/partials_return_type_test.cpp b/test/unit/math/rev/meta/partials_return_type_test.cpp index c62bc58bd3b..8eed06a2e8f 100644 --- a/test/unit/math/rev/meta/partials_return_type_test.cpp +++ b/test/unit/math/rev/meta/partials_return_type_test.cpp @@ -3,8 +3,8 @@ #include #include -using stan::math::var; using stan::partials_return_type; +using stan::math::var; TEST(MetaTraitsRevScal, PartialsReturnTypeVar) { test::expect_same_type::type>(); @@ -17,8 +17,8 @@ TEST(MetaTraitsRevScal, PartialsReturnTypeVarTenParams) { } TEST(MetaTraitsRevArr, partials_return_type) { - using stan::math::var; using stan::partials_return_type; + using stan::math::var; partials_return_type >::type g(5.0); diff --git a/test/unit/math/rev/meta/partials_type_test.cpp b/test/unit/math/rev/meta/partials_type_test.cpp index 7c3079711fa..d3187a0ec47 100644 --- a/test/unit/math/rev/meta/partials_type_test.cpp +++ b/test/unit/math/rev/meta/partials_type_test.cpp @@ -2,8 +2,8 @@ #include TEST(MetaTraitsRevScal, partials_type) { - using stan::math::var; using stan::partials_type; + using stan::math::var; stan::partials_type::type f(2.0); EXPECT_EQ(2.0, f); From 8e944370dedbc0a4e3e4577c85a01220f2eb108d Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 2 Apr 2020 19:20:05 -0400 Subject: [PATCH 18/41] Handling review comments (Issue #1815, reduce_sum) --- stan/math/prim/functor/reduce_sum.hpp | 65 +++++++++------ stan/math/prim/functor/reduce_sum_static.hpp | 13 ++- stan/math/rev/functor.hpp | 1 + stan/math/rev/functor/reduce_sum.hpp | 82 +++++++++++-------- .../mix/functor/reduce_sum_part1_test.cpp | 10 +++ .../mix/functor/reduce_sum_part4_test.cpp | 2 +- .../math/prim/functor/reduce_sum_test.cpp | 6 +- .../unit/math/rev/functor/reduce_sum_test.cpp | 21 ++--- 8 files changed, 122 insertions(+), 78 deletions(-) diff --git a/stan/math/prim/functor/reduce_sum.hpp b/stan/math/prim/functor/reduce_sum.hpp index 5f2a16261a3..96d347cd4a1 100644 --- a/stan/math/prim/functor/reduce_sum.hpp +++ b/stan/math/prim/functor/reduce_sum.hpp @@ -53,7 +53,7 @@ struct reduce_sum_impl { * * grainsize must be greater than or equal to 1 * - * @param vmapped Sliced arguments used only in some sum terms + * @param vmapped Vector containing one element per term of sum * @param auto_partitioning Work partitioning style (ignored) * @param grainsize Suggested grainsize for tbb * @param[in, out] msgs The print stream for warning messages @@ -63,9 +63,9 @@ struct reduce_sum_impl { return_type_t operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, std::ostream* msgs, Args&&... args) const { - const std::size_t num_jobs = vmapped.size(); + const std::size_t num_terms = vmapped.size(); - if (num_jobs == 0) { + if (num_terms == 0) { return 0.0; } @@ -81,7 +81,7 @@ struct reduce_sum_impl { std::decay_t sub_slice; sub_slice.reserve(end - start + 1); - for (int i = start; i <= end; ++i) { + for (size_t i = start; i <= end; ++i) { sub_slice.emplace_back(vmapped[i]); } @@ -106,8 +106,11 @@ template , ReturnType, Vec, Args...> { /** - * Internal object meeting the Imperative form requirements of - * `tbb::parallel_reduce` + * This struct is used by the TBB to accumulate partial + * sums over consecutive ranges of the input. To distribute the workload, + * the TBB can split larger partial sums into smaller ones in which + * case the splitting copy constructor is used. It is designed to + * meet the Imperative form requirements of `tbb::parallel_reduce`. * * @note see link [here](https://tinyurl.com/vp7xw2t) for requirements. */ @@ -122,9 +125,11 @@ struct reduce_sum_impl, msgs_(msgs), args_tuple_(std::forward(args)...) {} - /* + /** * This is the copy operator as required for tbb::parallel_reduce - * Imperative form. This requires sum_ be reset to zero. + * Imperative form. This requires sum_ be reset to zero since + * the newly created reducer is used to accumulate an independent + * partial sum. */ recursive_reducer(recursive_reducer& other, tbb::split) : vmapped_(other.vmapped_), @@ -146,7 +151,7 @@ struct reduce_sum_impl, std::decay_t sub_slice; sub_slice.reserve(r.size()); - for (int i = r.begin(); i < r.end(); ++i) { + for (size_t i = r.begin(); i < r.end(); ++i) { sub_slice.emplace_back(vmapped_[i]); } @@ -155,7 +160,7 @@ struct reduce_sum_impl, return ReduceFunction()(r.begin(), r.end() - 1, sub_slice, msgs_, args...); }, - this->args_tuple_); + args_tuple_); } /** @@ -163,7 +168,7 @@ struct reduce_sum_impl, * * @param rhs Another partial sum */ - void join(const recursive_reducer& child) { this->sum_ += child.sum_; } + void join(const recursive_reducer& child) { sum_ += child.sum_; } }; /** @@ -173,7 +178,7 @@ struct reduce_sum_impl, * This specialization is parallelized using tbb and works only for * arithmetic types. * - * An instance, f, of `ReduceFunction` should have the signature: + * ReduceFunction must define an operator() with the same signature as: * double f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, * Args&&... args) * @@ -191,14 +196,17 @@ struct reduce_sum_impl, * instances. * * If auto partitioning is true, break work into pieces automatically, - * taking grainsize as a recommended work size (this process - * is not deterministic). If false, break work deterministically - * into pieces smaller than or equal to grainsize. The execution - * order is non-deterministic. + * taking grainsize as a recommended work size. The partitioning is + * not deterministic nor is the order guaranteed in which partial + * sums are accumulated. Due to floating point imprecisions this will likely + * lead to slight differences in the accumulated results between + * multiple runs. If false, break work deterministically into pieces smaller + * than or equal to grainsize and accumulate all the partial sums + * in the same order. This still may not achieve bitwise reproducibility. * * grainsize must be greater than or equal to 1 * - * @param vmapped Sliced arguments used only in some sum terms + * @param vmapped Vector containing one element per term of sum * @param auto_partitioning Work partitioning style * @param grainsize Suggested grainsize for tbb * @param[in, out] msgs The print stream for warning messages @@ -207,8 +215,8 @@ struct reduce_sum_impl, */ ReturnType operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, std::ostream* msgs, Args&&... args) const { - const std::size_t num_jobs = vmapped.size(); - if (num_jobs == 0) { + const std::size_t num_terms = vmapped.size(); + if (num_terms == 0) { return 0.0; } recursive_reducer worker(std::forward(vmapped), msgs, @@ -216,11 +224,11 @@ struct reduce_sum_impl, if (auto_partitioning) { tbb::parallel_reduce( - tbb::blocked_range(0, num_jobs, grainsize), worker); + tbb::blocked_range(0, num_terms, grainsize), worker); } else { tbb::simple_partitioner partitioner; tbb::parallel_deterministic_reduce( - tbb::blocked_range(0, num_jobs, grainsize), worker, + tbb::blocked_range(0, num_terms, grainsize), worker, partitioner); } @@ -236,7 +244,7 @@ struct reduce_sum_impl, * * This defers to reduce_sum_impl for the appropriate implementation * - * An instance, f, of `ReduceFunction` should have the signature: + * ReduceFunction must define an operator() with the same signature as: * T f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, Args&&... * args) * @@ -248,7 +256,7 @@ struct reduce_sum_impl, * @tparam ReturnType An arithmetic type * @tparam Vec Type of sliced argument * @tparam Args Types of shared arguments - * @param vmapped Sliced arguments used only in some sum terms + * @param vmapped Vector containing one element per term of sum * @param grainsize Suggested grainsize for tbb * @param[in, out] msgs The print stream for warning messages * @param args Shared arguments used in every sum term @@ -262,10 +270,21 @@ auto reduce_sum(Vec&& vmapped, int grainsize, std::ostream* msgs, check_positive("reduce_sum", "grainsize", grainsize); +#ifdef STAN_THREADS return internal::reduce_sum_impl()(std::forward(vmapped), true, grainsize, msgs, std::forward(args)...); +#else + const std::size_t num_terms = vmapped.size(); + + if (num_terms == 0) { + return return_type(0.0); + } + + return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), + msgs, std::forward(args)...); +#endif } } // namespace math diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp index 2dacdce55ba..e4a163f2a17 100644 --- a/stan/math/prim/functor/reduce_sum_static.hpp +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -20,7 +20,7 @@ namespace math { * * This defers to reduce_sum_impl for the appropriate implementation * - * An instance, f, of `ReduceFunction` should have the signature: + * ReduceFunction must define an operator() with the same signature as: * T f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, Args&&... * args) * @@ -46,10 +46,21 @@ auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs, check_positive("reduce_sum", "grainsize", grainsize); +#ifdef STAN_THREADS return internal::reduce_sum_impl()(std::forward(vmapped), false, grainsize, msgs, std::forward(args)...); +#else + const std::size_t num_terms = vmapped.size(); + + if (num_terms == 0) { + return return_type(0.0); + } + + return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), + msgs, std::forward(args)...); +#endif } } // namespace math diff --git a/stan/math/rev/functor.hpp b/stan/math/rev/functor.hpp index 0af65fe88e5..13566d8b8bc 100644 --- a/stan/math/rev/functor.hpp +++ b/stan/math/rev/functor.hpp @@ -20,5 +20,6 @@ #include #include #include +#include #endif diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index 988b9512b67..78c954c2411 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -28,13 +28,16 @@ template , ReturnType, Vec, Args...> { /** - * Internal object meeting the Imperative form requirements of - * `tbb::parallel_reduce` + * This struct is used by the TBB to accumulate partial + * sums over consecutive ranges of the input. To distribute the workload, + * the TBB can split larger partial sums into smaller ones in which + * case the splitting copy constructor is used. It is designed to + * meet the Imperative form requirements of `tbb::parallel_reduce`. * * @note see link [here](https://tinyurl.com/vp7xw2t) for requirements. */ struct recursive_reducer { - size_t per_job_sliced_terms_; + size_t vars_per_term_; size_t num_shared_terms_; // Number of terms shared across threads double* sliced_partials_; // Points to adjoints of the partial calculations Vec vmapped_; @@ -44,10 +47,10 @@ struct reduce_sum_impl, ReturnType, Eigen::VectorXd args_adjoints_{0}; template - recursive_reducer(size_t per_job_sliced_terms, size_t num_shared_terms, + recursive_reducer(size_t vars_per_term, size_t num_shared_terms, double* sliced_partials, VecT&& vmapped, std::ostream* msgs, ArgsT&&... args) - : per_job_sliced_terms_(per_job_sliced_terms), + : vars_per_term_(vars_per_term), num_shared_terms_(num_shared_terms), sliced_partials_(sliced_partials), vmapped_(std::forward(vmapped)), @@ -56,11 +59,12 @@ struct reduce_sum_impl, ReturnType, /* * This is the copy operator as required for tbb::parallel_reduce - * Imperative form. This requires the reduced values (sum_ and - * arg_adjoints_) be reset to zero. + * Imperative form. This requires sum_ and arg_adjoints_ be reset + * to zero since the newly created reducer is used to accumulate + * an independent partial sum. */ recursive_reducer(recursive_reducer& other, tbb::split) - : per_job_sliced_terms_(other.per_job_sliced_terms_), + : vars_per_term_(other.vars_per_term_), num_shared_terms_(other.num_shared_terms_), sliced_partials_(other.sliced_partials_), vmapped_(other.vmapped_), @@ -69,11 +73,14 @@ struct reduce_sum_impl, ReturnType, /** * Compute, using nested autodiff, the value and Jacobian of - * `ReduceFunction` called over the range defined by r and accumulate those - * in member variable sum_ (for the value) and args_adjoints_ (for the - * Jacobian). This function may be called multiple times per object - * instantiation (so the sum_ and args_adjoints_ must be accumulated, not - * just assigned). + * `ReduceFunction` called over the range defined by r and accumulate those + * in member variable sum_ (for the value) and args_adjoints_ (for the + * Jacobian). The nested autodiff uses deep copies of the involved operands + * ensuring that no side effects are implied to the adjoints of the input + * operands which reside potentially on a autodiff tape stored in a + * different thread other than the current thread of execution. This function + * may be called multiple times per object instantiation (so the sum_ and + * args_adjoints_ must be accumulated, notjust assigned). * * @param r Range over which to compute reduce_sum */ @@ -83,7 +90,7 @@ struct reduce_sum_impl, ReturnType, } if (args_adjoints_.size() == 0) { - args_adjoints_ = Eigen::VectorXd::Zero(this->num_shared_terms_); + args_adjoints_ = Eigen::VectorXd::Zero(num_shared_terms_); } // Initialize nested autodiff stack @@ -93,7 +100,7 @@ struct reduce_sum_impl, ReturnType, // back to main autodiff stack std::decay_t local_sub_slice; local_sub_slice.reserve(r.size()); - for (int i = r.begin(); i < r.end(); ++i) { + for (size_t i = r.begin(); i < r.end(); ++i) { local_sub_slice.emplace_back(deep_copy_vars(vmapped_[i])); } @@ -104,13 +111,13 @@ struct reduce_sum_impl, ReturnType, return std::tuple( deep_copy_vars(args)...); }, - this->args_tuple_); + args_tuple_); // Perform calculation var sub_sum_v = apply( [&](auto&&... args) { return ReduceFunction()(r.begin(), r.end() - 1, local_sub_slice, - this->msgs_, args...); + msgs_, args...); }, args_tuple_local_copy); @@ -122,7 +129,7 @@ struct reduce_sum_impl, ReturnType, // Accumulate adjoints of sliced_arguments accumulate_adjoints( - this->sliced_partials_ + r.begin() * per_job_sliced_terms_, + sliced_partials_ + r.begin() * vars_per_term_, local_sub_slice); // Accumulate adjoints of shared_arguments @@ -141,12 +148,12 @@ struct reduce_sum_impl, ReturnType, * @param rhs Another partial sum */ void join(const recursive_reducer& rhs) { - this->sum_ += rhs.sum_; - if (this->args_adjoints_.size() != 0 && rhs.args_adjoints_.size() != 0) { - this->args_adjoints_ += rhs.args_adjoints_; - } else if (this->args_adjoints_.size() == 0 + sum_ += rhs.sum_; + if (args_adjoints_.size() != 0 && rhs.args_adjoints_.size() != 0) { + args_adjoints_ += rhs.args_adjoints_; + } else if (args_adjoints_.size() == 0 && rhs.args_adjoints_.size() != 0) { - this->args_adjoints_ = rhs.args_adjoints_; + args_adjoints_ = rhs.args_adjoints_; } } }; @@ -158,7 +165,7 @@ struct reduce_sum_impl, ReturnType, * This specialization is parallelized using tbb and works for reverse * mode autodiff. * - * An instance, f, of `ReduceFunction` should have the signature: + * ReduceFunction must define an operator() with the same signature as: * var f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, * Args&&... args) * @@ -176,12 +183,15 @@ struct reduce_sum_impl, ReturnType, * instances. Results are stored as precomputed varis in the autodiff tree. * * If auto partitioning is true, break work into pieces automatically, - * taking grainsize as a recommended work size (this process - * is not deterministic). If false, break work deterministically - * into pieces smaller than or equal to grainsize. The execution - * order is non-deterministic. + * taking grainsize as a recommended work size. The partitioning is + * not deterministic nor is the order guaranteed in which partial + * sums are accumulated. Due to floating point imprecisions this will likely + * lead to slight differences in the accumulated results between + * multiple runs. If false, break work deterministically into pieces smaller + * than or equal to grainsize and accumulate all the partial sums + * in the same order. This still may not achieve bitwise reproducibility. * - * @param vmapped Sliced arguments used only in some sum terms + * @param vmapped Vector containing one element per term of sum * @param auto_partitioning Work partitioning style * @param grainsize Suggested grainsize for tbb * @param[in, out] msgs The print stream for warning messages @@ -190,14 +200,14 @@ struct reduce_sum_impl, ReturnType, */ inline var operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, std::ostream* msgs, Args&&... args) const { - const std::size_t num_jobs = vmapped.size(); + const std::size_t num_terms = vmapped.size(); - if (num_jobs == 0) { + if (num_terms == 0) { return var(0.0); } - const std::size_t per_job_sliced_terms = count_vars(vmapped[0]); - const std::size_t num_sliced_terms = num_jobs * per_job_sliced_terms; + const std::size_t vars_per_term = count_vars(vmapped[0]); + const std::size_t num_sliced_terms = num_terms * vars_per_term; const std::size_t num_shared_terms = count_vars(args...); vari** varis = ChainableStack::instance_->memalloc_.alloc_array( @@ -208,16 +218,16 @@ struct reduce_sum_impl, ReturnType, for (size_t i = 0; i < num_sliced_terms; ++i) { partials[i] = 0.0; } - recursive_reducer worker(per_job_sliced_terms, num_shared_terms, partials, + recursive_reducer worker(vars_per_term, num_shared_terms, partials, vmapped, msgs, args...); if (auto_partitioning) { tbb::parallel_reduce( - tbb::blocked_range(0, num_jobs, grainsize), worker); + tbb::blocked_range(0, num_terms, grainsize), worker); } else { tbb::simple_partitioner partitioner; tbb::parallel_deterministic_reduce( - tbb::blocked_range(0, num_jobs, grainsize), worker, + tbb::blocked_range(0, num_terms, grainsize), worker, partitioner); } diff --git a/test/unit/math/mix/functor/reduce_sum_part1_test.cpp b/test/unit/math/mix/functor/reduce_sum_part1_test.cpp index 113808ce070..15f18be2f4e 100644 --- a/test/unit/math/mix/functor/reduce_sum_part1_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part1_test.cpp @@ -57,6 +57,16 @@ TEST(MathMix_reduce_sum, grainsize) { stan::test::expect_ad(f4, data); } +TEST(MathMix_reduce_sum, std_vector_zero_length) { + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + + std::vector data(0); + + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data); + stan::test::expect_ad(reduce_sum_sum_lpdf, data); +} + TEST(MathMix_reduce_sum, std_vector_double_slice) { using stan::math::test::reduce_sum_static_sum_lpdf; using stan::math::test::reduce_sum_sum_lpdf; diff --git a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp index 6bb47541687..2bfd95e702a 100644 --- a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp @@ -102,7 +102,7 @@ TEST(MathMix_reduce_sum, eigen_three_args_with_doubles3) { } TEST(MathMix_reduce_sum, static_check) { - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::static_check_lpdf; diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index 27779236784..1c044d69b26 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -5,7 +5,7 @@ #include TEST(StanMathPrim_reduce_sum, value) { - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; double lambda_d = 10.0; @@ -32,7 +32,7 @@ TEST(StanMathPrim_reduce_sum, value) { } TEST(StanMathPrim_reduce_sum, grainsize) { - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; @@ -62,7 +62,7 @@ TEST(StanMathPrim_reduce_sum, grainsize) { } TEST(StanMathPrim_reduce_sum, start_end_slice) { - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::start_end_lpdf; diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 2748025eeb0..0262575082b 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -9,10 +9,9 @@ TEST(StanMathRev_reduce_sum, value) { using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t elems = 10000; - const std::size_t num_iter = 1000; std::vector data(elems); for (std::size_t i = 0; i != elems; ++i) @@ -40,11 +39,10 @@ TEST(StanMathRev_reduce_sum, value) { TEST(StanMathRev_reduce_sum, gradient) { using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t elems = 10000; - const std::size_t num_iter = 1000; std::vector data(elems); for (std::size_t i = 0; i != elems; ++i) @@ -91,11 +89,10 @@ TEST(StanMathRev_reduce_sum, gradient) { TEST(StanMathRev_reduce_sum, grainsize) { using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t elems = 10000; - const std::size_t num_iter = 1000; std::vector data(elems); for (std::size_t i = 0; i != elems; ++i) @@ -142,11 +139,10 @@ TEST(StanMathRev_reduce_sum, grainsize) { TEST(StanMathRev_reduce_sum, nesting_gradient) { using stan::math::test::get_new_msg; using stan::math::test::nesting_count_lpdf; - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t elems = 10000; - const std::size_t num_iter = 1000; std::vector data(elems); for (std::size_t i = 0; i != elems; ++i) @@ -195,13 +191,12 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { TEST(StanMathRev_reduce_sum, grouped_gradient) { using stan::math::test::get_new_msg; using stan::math::test::grouped_count_lpdf; - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t groups = 10; const std::size_t elems_per_group = 1000; const std::size_t elems = groups * elems_per_group; - const std::size_t num_iter = 1000; std::vector data(elems); std::vector gidx(elems); @@ -259,13 +254,12 @@ TEST(StanMathRev_reduce_sum, grouped_gradient) { TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { using stan::math::test::get_new_msg; using stan::math::test::grouped_count_lpdf; - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t groups = 10; const std::size_t elems_per_group = 1000; const std::size_t elems = groups * elems_per_group; - const std::size_t num_iter = 1000; std::vector data(elems); std::vector gidx(elems); @@ -324,13 +318,12 @@ TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { TEST(StanMathRev_reduce_sum, slice_group_gradient) { using stan::math::test::get_new_msg; using stan::math::test::slice_group_count_lpdf; - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t groups = 10; const std::size_t elems_per_group = 1000; const std::size_t elems = groups * elems_per_group; - const std::size_t num_iter = 1000; std::vector data(elems); std::vector gidx(elems); From faf49142508b64952797c7d806d0ac643a34ed42 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 2 Apr 2020 19:22:37 -0400 Subject: [PATCH 19/41] [Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/stable/2017-11-14) --- stan/math/prim/functor/reduce_sum.hpp | 4 ++-- stan/math/prim/functor/reduce_sum_static.hpp | 12 +++++------ .../prim/prob/bernoulli_logit_glm_lpmf.hpp | 2 +- stan/math/prim/prob/beta_rng.hpp | 2 +- stan/math/prim/prob/cauchy_rng.hpp | 2 +- stan/math/prim/prob/chi_square_rng.hpp | 2 +- stan/math/prim/prob/dirichlet_rng.hpp | 4 ++-- stan/math/prim/prob/discrete_range_rng.hpp | 2 +- .../math/prim/prob/double_exponential_rng.hpp | 2 +- stan/math/prim/prob/frechet_rng.hpp | 2 +- stan/math/prim/prob/hypergeometric_rng.hpp | 2 +- stan/math/prim/prob/inv_chi_square_rng.hpp | 2 +- stan/math/prim/prob/inv_gamma_rng.hpp | 2 +- stan/math/prim/prob/logistic_rng.hpp | 2 +- stan/math/prim/prob/lognormal_rng.hpp | 2 +- stan/math/prim/prob/multi_student_t_rng.hpp | 2 +- .../prim/prob/neg_binomial_2_log_glm_lpmf.hpp | 2 +- .../math/prim/prob/neg_binomial_2_log_rng.hpp | 2 +- stan/math/prim/prob/neg_binomial_2_rng.hpp | 2 +- stan/math/prim/prob/neg_binomial_rng.hpp | 2 +- stan/math/prim/prob/pareto_type_2_rng.hpp | 2 +- stan/math/prim/prob/poisson_log_rng.hpp | 2 +- stan/math/prim/prob/poisson_rng.hpp | 2 +- stan/math/prim/prob/rayleigh_rng.hpp | 2 +- .../prim/prob/scaled_inv_chi_square_rng.hpp | 2 +- stan/math/prim/prob/skew_normal_rng.hpp | 2 +- stan/math/prim/prob/student_t_rng.hpp | 2 +- stan/math/prim/prob/uniform_rng.hpp | 2 +- stan/math/prim/prob/von_mises_rng.hpp | 2 +- stan/math/prim/prob/weibull_rng.hpp | 2 +- stan/math/rev/functor/reduce_sum.hpp | 20 +++++++++---------- test/prob/student_t/student_t_test.hpp | 2 +- test/prob/test_fixture_ccdf_log.hpp | 2 +- test/prob/test_fixture_cdf.hpp | 2 +- test/prob/test_fixture_cdf_log.hpp | 2 +- test/prob/test_fixture_distr.hpp | 2 +- test/prob/uniform/uniform_ccdf_log_test.hpp | 2 +- test/prob/uniform/uniform_cdf_log_test.hpp | 2 +- test/prob/uniform/uniform_cdf_test.hpp | 2 +- test/prob/utility.hpp | 2 +- .../math/fwd/core/std_numeric_limits_test.cpp | 2 +- .../fwd/meta/partials_return_type_test.cpp | 2 +- .../unit/math/fwd/meta/partials_type_test.cpp | 2 +- .../math/mix/core/std_numeric_limits_test.cpp | 2 +- .../math/mix/meta/broadcast_array_test.cpp | 2 +- .../mix/meta/partials_return_type_test.cpp | 2 +- .../unit/math/mix/meta/partials_type_test.cpp | 2 +- test/unit/math/mix/meta/return_type_test.cpp | 2 +- test/unit/math/opencl/assign_event_test.cpp | 2 +- .../opencl/rev/triangular_transpose_test.cpp | 4 ++-- test/unit/math/prim/fun/beta_test.cpp | 2 +- .../fun/binomial_coefficient_log_test.cpp | 2 +- test/unit/math/prim/fun/inv_Phi_test.cpp | 2 +- .../math/prim/fun/log_inv_logit_diff_test.cpp | 2 +- .../math/prim/meta/StdVectorBuilder_test.cpp | 6 +++--- test/unit/math/prim/meta/is_detected_test.cpp | 2 +- test/unit/math/prim/meta/void_t_test.cpp | 2 +- .../math/prim/prob/inv_wishart_rng_test.cpp | 2 +- .../math/prim/prob/ordered_probit_test.cpp | 4 ++-- test/unit/math/prim/prob/wishart_rng_test.cpp | 2 +- .../rev/functor/algebra_solver_fp_test.cpp | 4 ++-- .../rev/meta/partials_return_type_test.cpp | 4 ++-- .../unit/math/rev/meta/partials_type_test.cpp | 2 +- 63 files changed, 84 insertions(+), 86 deletions(-) diff --git a/stan/math/prim/functor/reduce_sum.hpp b/stan/math/prim/functor/reduce_sum.hpp index 96d347cd4a1..1b84d5e64f7 100644 --- a/stan/math/prim/functor/reduce_sum.hpp +++ b/stan/math/prim/functor/reduce_sum.hpp @@ -110,7 +110,7 @@ struct reduce_sum_impl, * sums over consecutive ranges of the input. To distribute the workload, * the TBB can split larger partial sums into smaller ones in which * case the splitting copy constructor is used. It is designed to - * meet the Imperative form requirements of `tbb::parallel_reduce`. + * meet the Imperative form requirements of `tbb::parallel_reduce`. * * @note see link [here](https://tinyurl.com/vp7xw2t) for requirements. */ @@ -283,7 +283,7 @@ auto reduce_sum(Vec&& vmapped, int grainsize, std::ostream* msgs, } return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), - msgs, std::forward(args)...); + msgs, std::forward(args)...); #endif } diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp index e4a163f2a17..6b2435d33bc 100644 --- a/stan/math/prim/functor/reduce_sum_static.hpp +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -52,14 +52,14 @@ auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs, grainsize, msgs, std::forward(args)...); #else - const std::size_t num_terms = vmapped.size(); + const std::size_t num_terms = vmapped.size(); - if (num_terms == 0) { - return return_type(0.0); - } + if (num_terms == 0) { + return return_type(0.0); + } - return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), - msgs, std::forward(args)...); + return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), + msgs, std::forward(args)...); #endif } diff --git a/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp b/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp index 86b1af1c5be..d8ed7024ac1 100644 --- a/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp +++ b/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp @@ -51,8 +51,8 @@ return_type_t bernoulli_logit_glm_lpmf( const T_alpha &alpha, const T_beta &beta) { using Eigen::Array; using Eigen::Dynamic; - using Eigen::log1p; using Eigen::Matrix; + using Eigen::log1p; using std::exp; using T_partials_return = partials_return_t; using T_y_val = diff --git a/stan/math/prim/prob/beta_rng.hpp b/stan/math/prim/prob/beta_rng.hpp index e516bbed030..e3188349730 100644 --- a/stan/math/prim/prob/beta_rng.hpp +++ b/stan/math/prim/prob/beta_rng.hpp @@ -34,9 +34,9 @@ namespace math { template inline typename VectorBuilder::type beta_rng( const T_shape1 &alpha, const T_shape2 &beta, RNG &rng) { - using boost::variate_generator; using boost::random::gamma_distribution; using boost::random::uniform_real_distribution; + using boost::variate_generator; static const char *function = "beta_rng"; check_positive_finite(function, "First shape parameter", alpha); check_positive_finite(function, "Second shape parameter", beta); diff --git a/stan/math/prim/prob/cauchy_rng.hpp b/stan/math/prim/prob/cauchy_rng.hpp index d7ad6a1137a..57fa4a4adfb 100644 --- a/stan/math/prim/prob/cauchy_rng.hpp +++ b/stan/math/prim/prob/cauchy_rng.hpp @@ -31,8 +31,8 @@ namespace math { template inline typename VectorBuilder::type cauchy_rng( const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::variate_generator; using boost::random::cauchy_distribution; + using boost::variate_generator; static const char* function = "cauchy_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/chi_square_rng.hpp b/stan/math/prim/prob/chi_square_rng.hpp index bfb1e8436e3..9e113cdf150 100644 --- a/stan/math/prim/prob/chi_square_rng.hpp +++ b/stan/math/prim/prob/chi_square_rng.hpp @@ -26,8 +26,8 @@ namespace math { template inline typename VectorBuilder::type chi_square_rng( const T_deg& nu, RNG& rng) { - using boost::variate_generator; using boost::random::chi_squared_distribution; + using boost::variate_generator; static const char* function = "chi_square_rng"; check_positive_finite(function, "Degrees of freedom parameter", nu); diff --git a/stan/math/prim/prob/dirichlet_rng.hpp b/stan/math/prim/prob/dirichlet_rng.hpp index bd4cd0c6232..88116ec1040 100644 --- a/stan/math/prim/prob/dirichlet_rng.hpp +++ b/stan/math/prim/prob/dirichlet_rng.hpp @@ -38,10 +38,10 @@ namespace math { template inline Eigen::VectorXd dirichlet_rng( const Eigen::Matrix& alpha, RNG& rng) { + using Eigen::VectorXd; using boost::gamma_distribution; - using boost::variate_generator; using boost::random::uniform_real_distribution; - using Eigen::VectorXd; + using boost::variate_generator; using std::exp; using std::log; diff --git a/stan/math/prim/prob/discrete_range_rng.hpp b/stan/math/prim/prob/discrete_range_rng.hpp index adb976e5451..4348694ece9 100644 --- a/stan/math/prim/prob/discrete_range_rng.hpp +++ b/stan/math/prim/prob/discrete_range_rng.hpp @@ -34,8 +34,8 @@ template inline typename VectorBuilder::type discrete_range_rng(const T_lower& lower, const T_upper& upper, RNG& rng) { static const char* function = "discrete_range_rng"; - using boost::variate_generator; using boost::random::uniform_int_distribution; + using boost::variate_generator; check_consistent_sizes(function, "Lower bound parameter", lower, "Upper bound parameter", upper); check_greater_or_equal(function, "Upper bound parameter", upper, lower); diff --git a/stan/math/prim/prob/double_exponential_rng.hpp b/stan/math/prim/prob/double_exponential_rng.hpp index b5dd6cf6b3c..f0fe42dbd91 100644 --- a/stan/math/prim/prob/double_exponential_rng.hpp +++ b/stan/math/prim/prob/double_exponential_rng.hpp @@ -33,8 +33,8 @@ namespace math { template inline typename VectorBuilder::type double_exponential_rng(const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::variate_generator; using boost::random::uniform_real_distribution; + using boost::variate_generator; static const char* function = "double_exponential_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/frechet_rng.hpp b/stan/math/prim/prob/frechet_rng.hpp index 2b38832143c..66792cc3567 100644 --- a/stan/math/prim/prob/frechet_rng.hpp +++ b/stan/math/prim/prob/frechet_rng.hpp @@ -30,8 +30,8 @@ namespace math { template inline typename VectorBuilder::type frechet_rng( const T_shape& alpha, const T_scale& sigma, RNG& rng) { - using boost::variate_generator; using boost::random::weibull_distribution; + using boost::variate_generator; static const char* function = "frechet_rng"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/hypergeometric_rng.hpp b/stan/math/prim/prob/hypergeometric_rng.hpp index 2d4234d6c8f..7f8b91032c4 100644 --- a/stan/math/prim/prob/hypergeometric_rng.hpp +++ b/stan/math/prim/prob/hypergeometric_rng.hpp @@ -12,8 +12,8 @@ namespace math { template inline int hypergeometric_rng(int N, int a, int b, RNG& rng) { - using boost::variate_generator; using boost::math::hypergeometric_distribution; + using boost::variate_generator; static const char* function = "hypergeometric_rng"; check_bounded(function, "Draws parameter", N, 0, a + b); check_positive(function, "Draws parameter", N); diff --git a/stan/math/prim/prob/inv_chi_square_rng.hpp b/stan/math/prim/prob/inv_chi_square_rng.hpp index b886bb958b4..93160aad6ff 100644 --- a/stan/math/prim/prob/inv_chi_square_rng.hpp +++ b/stan/math/prim/prob/inv_chi_square_rng.hpp @@ -26,8 +26,8 @@ namespace math { template inline typename VectorBuilder::type inv_chi_square_rng( const T_deg& nu, RNG& rng) { - using boost::variate_generator; using boost::random::chi_squared_distribution; + using boost::variate_generator; static const char* function = "inv_chi_square_rng"; check_positive_finite(function, "Degrees of freedom parameter", nu); diff --git a/stan/math/prim/prob/inv_gamma_rng.hpp b/stan/math/prim/prob/inv_gamma_rng.hpp index 9adf2a3932f..b0e809a6298 100644 --- a/stan/math/prim/prob/inv_gamma_rng.hpp +++ b/stan/math/prim/prob/inv_gamma_rng.hpp @@ -31,8 +31,8 @@ namespace math { template inline typename VectorBuilder::type inv_gamma_rng(const T_shape& alpha, const T_scale& beta, RNG& rng) { - using boost::variate_generator; using boost::random::gamma_distribution; + using boost::variate_generator; static const char* function = "inv_gamma_rng"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", beta); diff --git a/stan/math/prim/prob/logistic_rng.hpp b/stan/math/prim/prob/logistic_rng.hpp index e8b27e63e3c..c87fec1ccf7 100644 --- a/stan/math/prim/prob/logistic_rng.hpp +++ b/stan/math/prim/prob/logistic_rng.hpp @@ -32,8 +32,8 @@ namespace math { template inline typename VectorBuilder::type logistic_rng( const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::variate_generator; using boost::random::exponential_distribution; + using boost::variate_generator; static const char* function = "logistic_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/lognormal_rng.hpp b/stan/math/prim/prob/lognormal_rng.hpp index a84e26491b9..8985de27936 100644 --- a/stan/math/prim/prob/lognormal_rng.hpp +++ b/stan/math/prim/prob/lognormal_rng.hpp @@ -31,8 +31,8 @@ namespace math { template inline typename VectorBuilder::type lognormal_rng( const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::variate_generator; using boost::random::lognormal_distribution; + using boost::variate_generator; static const char* function = "lognormal_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/multi_student_t_rng.hpp b/stan/math/prim/prob/multi_student_t_rng.hpp index e532b11bbcd..e27e89c4cdb 100644 --- a/stan/math/prim/prob/multi_student_t_rng.hpp +++ b/stan/math/prim/prob/multi_student_t_rng.hpp @@ -36,8 +36,8 @@ multi_student_t_rng( double nu, const T_loc& mu, const Eigen::Matrix& S, RNG& rng) { using boost::normal_distribution; - using boost::variate_generator; using boost::random::gamma_distribution; + using boost::variate_generator; static const char* function = "multi_student_t_rng"; check_not_nan(function, "Degrees of freedom parameter", nu); diff --git a/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp b/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp index 713a24c0217..e155b8935bf 100644 --- a/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp @@ -63,9 +63,9 @@ neg_binomial_2_log_glm_lpmf( const T_alpha& alpha, const T_beta& beta, const T_precision& phi) { using Eigen::Array; using Eigen::Dynamic; + using Eigen::Matrix; using Eigen::exp; using Eigen::log1p; - using Eigen::Matrix; using T_partials_return = partials_return_t; using T_precision_val = typename std::conditional_t< diff --git a/stan/math/prim/prob/neg_binomial_2_log_rng.hpp b/stan/math/prim/prob/neg_binomial_2_log_rng.hpp index 3a5228132e5..1a7d85e95bb 100644 --- a/stan/math/prim/prob/neg_binomial_2_log_rng.hpp +++ b/stan/math/prim/prob/neg_binomial_2_log_rng.hpp @@ -35,8 +35,8 @@ template inline typename VectorBuilder::type neg_binomial_2_log_rng(const T_loc& eta, const T_inv& phi, RNG& rng) { using boost::gamma_distribution; - using boost::variate_generator; using boost::random::poisson_distribution; + using boost::variate_generator; static const char* function = "neg_binomial_2_log_rng"; check_finite(function, "Log-location parameter", eta); check_positive_finite(function, "Inverse dispersion parameter", phi); diff --git a/stan/math/prim/prob/neg_binomial_2_rng.hpp b/stan/math/prim/prob/neg_binomial_2_rng.hpp index 4f93387bdfc..9a9cdef03cb 100644 --- a/stan/math/prim/prob/neg_binomial_2_rng.hpp +++ b/stan/math/prim/prob/neg_binomial_2_rng.hpp @@ -34,8 +34,8 @@ template inline typename VectorBuilder::type neg_binomial_2_rng(const T_loc& mu, const T_prec& phi, RNG& rng) { using boost::gamma_distribution; - using boost::variate_generator; using boost::random::poisson_distribution; + using boost::variate_generator; static const char* function = "neg_binomial_2_rng"; check_positive_finite(function, "Location parameter", mu); check_positive_finite(function, "Precision parameter", phi); diff --git a/stan/math/prim/prob/neg_binomial_rng.hpp b/stan/math/prim/prob/neg_binomial_rng.hpp index 95baf77902d..9557772f750 100644 --- a/stan/math/prim/prob/neg_binomial_rng.hpp +++ b/stan/math/prim/prob/neg_binomial_rng.hpp @@ -34,8 +34,8 @@ template inline typename VectorBuilder::type neg_binomial_rng( const T_shape& alpha, const T_inv& beta, RNG& rng) { using boost::gamma_distribution; - using boost::variate_generator; using boost::random::poisson_distribution; + using boost::variate_generator; static const char* function = "neg_binomial_rng"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Inverse scale parameter", beta); diff --git a/stan/math/prim/prob/pareto_type_2_rng.hpp b/stan/math/prim/prob/pareto_type_2_rng.hpp index 6b27f646352..f6108671821 100644 --- a/stan/math/prim/prob/pareto_type_2_rng.hpp +++ b/stan/math/prim/prob/pareto_type_2_rng.hpp @@ -39,8 +39,8 @@ template inline typename VectorBuilder::type pareto_type_2_rng(const T_loc& mu, const T_scale& lambda, const T_shape& alpha, RNG& rng) { - using boost::variate_generator; using boost::random::uniform_real_distribution; + using boost::variate_generator; static const char* function = "pareto_type_2_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", lambda); diff --git a/stan/math/prim/prob/poisson_log_rng.hpp b/stan/math/prim/prob/poisson_log_rng.hpp index 22c42a4345c..9d1364791f8 100644 --- a/stan/math/prim/prob/poisson_log_rng.hpp +++ b/stan/math/prim/prob/poisson_log_rng.hpp @@ -28,8 +28,8 @@ namespace math { template inline typename VectorBuilder::type poisson_log_rng( const T_rate& alpha, RNG& rng) { - using boost::variate_generator; using boost::random::poisson_distribution; + using boost::variate_generator; static const char* function = "poisson_log_rng"; static const double POISSON_MAX_LOG_RATE = 30 * LOG_TWO; check_finite(function, "Log rate parameter", alpha); diff --git a/stan/math/prim/prob/poisson_rng.hpp b/stan/math/prim/prob/poisson_rng.hpp index e2150d952e8..c1a0532ff5e 100644 --- a/stan/math/prim/prob/poisson_rng.hpp +++ b/stan/math/prim/prob/poisson_rng.hpp @@ -27,8 +27,8 @@ namespace math { template inline typename VectorBuilder::type poisson_rng( const T_rate& lambda, RNG& rng) { - using boost::variate_generator; using boost::random::poisson_distribution; + using boost::variate_generator; static const char* function = "poisson_rng"; check_not_nan(function, "Rate parameter", lambda); check_positive(function, "Rate parameter", lambda); diff --git a/stan/math/prim/prob/rayleigh_rng.hpp b/stan/math/prim/prob/rayleigh_rng.hpp index 6793e6cc0ab..a767d0b705d 100644 --- a/stan/math/prim/prob/rayleigh_rng.hpp +++ b/stan/math/prim/prob/rayleigh_rng.hpp @@ -27,8 +27,8 @@ namespace math { template inline typename VectorBuilder::type rayleigh_rng( const T_scale& sigma, RNG& rng) { - using boost::variate_generator; using boost::random::uniform_real_distribution; + using boost::variate_generator; static const char* function = "rayleigh_rng"; check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp b/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp index 444e26e24a7..f9648899f94 100644 --- a/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp +++ b/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp @@ -32,8 +32,8 @@ namespace math { template inline typename VectorBuilder::type scaled_inv_chi_square_rng(const T_deg& nu, const T_scale& s, RNG& rng) { - using boost::variate_generator; using boost::random::chi_squared_distribution; + using boost::variate_generator; static const char* function = "scaled_inv_chi_square_rng"; check_positive_finite(function, "Degrees of freedom parameter", nu); check_positive_finite(function, "Scale parameter", s); diff --git a/stan/math/prim/prob/skew_normal_rng.hpp b/stan/math/prim/prob/skew_normal_rng.hpp index 347cf175e74..926b5db2466 100644 --- a/stan/math/prim/prob/skew_normal_rng.hpp +++ b/stan/math/prim/prob/skew_normal_rng.hpp @@ -37,8 +37,8 @@ template inline typename VectorBuilder::type skew_normal_rng(const T_loc& mu, const T_scale& sigma, const T_shape& alpha, RNG& rng) { - using boost::variate_generator; using boost::random::normal_distribution; + using boost::variate_generator; static const char* function = "skew_normal_rng"; check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/student_t_rng.hpp b/stan/math/prim/prob/student_t_rng.hpp index 68149d1fc25..5edfa17f1d7 100644 --- a/stan/math/prim/prob/student_t_rng.hpp +++ b/stan/math/prim/prob/student_t_rng.hpp @@ -36,8 +36,8 @@ template inline typename VectorBuilder::type student_t_rng(const T_deg& nu, const T_loc& mu, const T_scale& sigma, RNG& rng) { - using boost::variate_generator; using boost::random::student_t_distribution; + using boost::variate_generator; static const char* function = "student_t_rng"; check_positive_finite(function, "Degrees of freedom parameter", nu); check_finite(function, "Location parameter", mu); diff --git a/stan/math/prim/prob/uniform_rng.hpp b/stan/math/prim/prob/uniform_rng.hpp index 05a8ec29e8b..84906bc9625 100644 --- a/stan/math/prim/prob/uniform_rng.hpp +++ b/stan/math/prim/prob/uniform_rng.hpp @@ -33,8 +33,8 @@ namespace math { template inline typename VectorBuilder::type uniform_rng( const T_alpha& alpha, const T_beta& beta, RNG& rng) { - using boost::variate_generator; using boost::random::uniform_real_distribution; + using boost::variate_generator; static const char* function = "uniform_rng"; check_finite(function, "Lower bound parameter", alpha); check_finite(function, "Upper bound parameter", beta); diff --git a/stan/math/prim/prob/von_mises_rng.hpp b/stan/math/prim/prob/von_mises_rng.hpp index 86d4a68df5a..d36ed93bbdb 100644 --- a/stan/math/prim/prob/von_mises_rng.hpp +++ b/stan/math/prim/prob/von_mises_rng.hpp @@ -44,8 +44,8 @@ namespace math { template inline typename VectorBuilder::type von_mises_rng( const T_loc& mu, const T_conc& kappa, RNG& rng) { - using boost::variate_generator; using boost::random::uniform_real_distribution; + using boost::variate_generator; static const char* function = "von_mises_rng"; check_finite(function, "Location parameter", mu); check_nonnegative(function, "Scale parameter", kappa); diff --git a/stan/math/prim/prob/weibull_rng.hpp b/stan/math/prim/prob/weibull_rng.hpp index 68e7c478e95..bf91178711f 100644 --- a/stan/math/prim/prob/weibull_rng.hpp +++ b/stan/math/prim/prob/weibull_rng.hpp @@ -31,8 +31,8 @@ namespace math { template inline typename VectorBuilder::type weibull_rng( const T_shape& alpha, const T_scale& sigma, RNG& rng) { - using boost::variate_generator; using boost::random::weibull_distribution; + using boost::variate_generator; static const char* function = "weibull_rng"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index 78c954c2411..f78ee50967c 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -32,7 +32,7 @@ struct reduce_sum_impl, ReturnType, * sums over consecutive ranges of the input. To distribute the workload, * the TBB can split larger partial sums into smaller ones in which * case the splitting copy constructor is used. It is designed to - * meet the Imperative form requirements of `tbb::parallel_reduce`. + * meet the Imperative form requirements of `tbb::parallel_reduce`. * * @note see link [here](https://tinyurl.com/vp7xw2t) for requirements. */ @@ -78,9 +78,9 @@ struct reduce_sum_impl, ReturnType, * Jacobian). The nested autodiff uses deep copies of the involved operands * ensuring that no side effects are implied to the adjoints of the input * operands which reside potentially on a autodiff tape stored in a - * different thread other than the current thread of execution. This function - * may be called multiple times per object instantiation (so the sum_ and - * args_adjoints_ must be accumulated, notjust assigned). + * different thread other than the current thread of execution. This + * function may be called multiple times per object instantiation (so the + * sum_ and args_adjoints_ must be accumulated, notjust assigned). * * @param r Range over which to compute reduce_sum */ @@ -128,9 +128,8 @@ struct reduce_sum_impl, ReturnType, sum_ += sub_sum_v.val(); // Accumulate adjoints of sliced_arguments - accumulate_adjoints( - sliced_partials_ + r.begin() * vars_per_term_, - local_sub_slice); + accumulate_adjoints(sliced_partials_ + r.begin() * vars_per_term_, + local_sub_slice); // Accumulate adjoints of shared_arguments apply( @@ -151,8 +150,7 @@ struct reduce_sum_impl, ReturnType, sum_ += rhs.sum_; if (args_adjoints_.size() != 0 && rhs.args_adjoints_.size() != 0) { args_adjoints_ += rhs.args_adjoints_; - } else if (args_adjoints_.size() == 0 - && rhs.args_adjoints_.size() != 0) { + } else if (args_adjoints_.size() == 0 && rhs.args_adjoints_.size() != 0) { args_adjoints_ = rhs.args_adjoints_; } } @@ -218,8 +216,8 @@ struct reduce_sum_impl, ReturnType, for (size_t i = 0; i < num_sliced_terms; ++i) { partials[i] = 0.0; } - recursive_reducer worker(vars_per_term, num_shared_terms, partials, - vmapped, msgs, args...); + recursive_reducer worker(vars_per_term, num_shared_terms, partials, vmapped, + msgs, args...); if (auto_partitioning) { tbb::parallel_reduce( diff --git a/test/prob/student_t/student_t_test.hpp b/test/prob/student_t/student_t_test.hpp index c3f4d7e42ab..5dd27477997 100644 --- a/test/prob/student_t/student_t_test.hpp +++ b/test/prob/student_t/student_t_test.hpp @@ -85,8 +85,8 @@ class AgradDistributionsStudentT : public AgradDistributionTest { const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& sigma, const T4&, const T5&) { using boost::math::lgamma; - using stan::math::log1p; using stan::math::LOG_SQRT_PI; + using stan::math::log1p; using stan::math::square; using std::log; diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index 468c5b7f139..3f3f0c5106d 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -9,9 +9,9 @@ using Eigen::Dynamic; using Eigen::Matrix; using stan::is_constant_all; using stan::is_vector; -using stan::scalar_type; using stan::math::value_of; using stan::math::var; +using stan::scalar_type; using std::vector; class AgradCcdfLogTest { diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index f4736bef637..9f80760e735 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -9,9 +9,9 @@ using Eigen::Dynamic; using Eigen::Matrix; using stan::is_constant_all; using stan::is_vector; -using stan::scalar_type; using stan::math::value_of; using stan::math::var; +using stan::scalar_type; using std::vector; class AgradCdfTest { diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index a0c133f9103..739e8a29e99 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -9,9 +9,9 @@ using Eigen::Dynamic; using Eigen::Matrix; using stan::is_constant_all; using stan::is_vector; -using stan::scalar_type; using stan::math::value_of; using stan::math::var; +using stan::scalar_type; using std::vector; class AgradCdfLogTest { diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index b4278c9d05d..cfcd73e6b4e 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -10,10 +10,10 @@ using Eigen::Dynamic; using Eigen::Matrix; using stan::is_constant_all; using stan::is_vector; -using stan::scalar_type; using stan::math::fvar; using stan::math::value_of; using stan::math::var; +using stan::scalar_type; using std::vector; /** diff --git a/test/prob/uniform/uniform_ccdf_log_test.hpp b/test/prob/uniform/uniform_ccdf_log_test.hpp index 3dd940a7ee9..74fcabb652d 100644 --- a/test/prob/uniform/uniform_ccdf_log_test.hpp +++ b/test/prob/uniform/uniform_ccdf_log_test.hpp @@ -60,8 +60,8 @@ class AgradCcdfLogUniform : public AgradCcdfLogTest { stan::return_type_t ccdf_log_function( const T_y& y, const T_low& alpha, const T_high& beta, const T3&, const T4&, const T5&) { - using stan::math::include_summand; using stan::math::LOG_ZERO; + using stan::math::include_summand; if (y < alpha || y > beta) return 0.0; diff --git a/test/prob/uniform/uniform_cdf_log_test.hpp b/test/prob/uniform/uniform_cdf_log_test.hpp index 27049d43c90..a5db47b0a12 100644 --- a/test/prob/uniform/uniform_cdf_log_test.hpp +++ b/test/prob/uniform/uniform_cdf_log_test.hpp @@ -61,8 +61,8 @@ class AgradCdfLogUniform : public AgradCdfLogTest { const T_high& beta, const T3&, const T4&, const T5&) { - using stan::math::include_summand; using stan::math::LOG_ZERO; + using stan::math::include_summand; if (y < alpha || y > beta) return LOG_ZERO; diff --git a/test/prob/uniform/uniform_cdf_test.hpp b/test/prob/uniform/uniform_cdf_test.hpp index db02726f657..38211532169 100644 --- a/test/prob/uniform/uniform_cdf_test.hpp +++ b/test/prob/uniform/uniform_cdf_test.hpp @@ -59,8 +59,8 @@ class AgradCdfUniform : public AgradCdfTest { const T_high& beta, const T3&, const T4&, const T5&) { - using stan::math::include_summand; using stan::math::LOG_ZERO; + using stan::math::include_summand; if (y < alpha || y > beta) return 0.0; diff --git a/test/prob/utility.hpp b/test/prob/utility.hpp index 0925b330b03..6d3a4fb42f2 100644 --- a/test/prob/utility.hpp +++ b/test/prob/utility.hpp @@ -5,9 +5,9 @@ using stan::is_constant_all; using stan::is_vector; -using stan::scalar_type; using stan::math::fvar; using stan::math::var; +using stan::scalar_type; using std::vector; using size_type = stan::math::index_type_t>; diff --git a/test/unit/math/fwd/core/std_numeric_limits_test.cpp b/test/unit/math/fwd/core/std_numeric_limits_test.cpp index 798736eb571..3b820ab876d 100644 --- a/test/unit/math/fwd/core/std_numeric_limits_test.cpp +++ b/test/unit/math/fwd/core/std_numeric_limits_test.cpp @@ -4,8 +4,8 @@ #include TEST(AgradFwdNumericLimits, All_Fvar) { - using stan::math::fvar; using stan::math::INFTY; + using stan::math::fvar; using std::isnan; EXPECT_TRUE(std::numeric_limits >::is_specialized); diff --git a/test/unit/math/fwd/meta/partials_return_type_test.cpp b/test/unit/math/fwd/meta/partials_return_type_test.cpp index 54b72a7b0c7..0783ad14d36 100644 --- a/test/unit/math/fwd/meta/partials_return_type_test.cpp +++ b/test/unit/math/fwd/meta/partials_return_type_test.cpp @@ -3,8 +3,8 @@ #include #include -using stan::partials_return_type; using stan::math::fvar; +using stan::partials_return_type; TEST(MathMetaFwd, PartialsReturnTypeFvarDouble) { test::expect_same_type >::type>(); diff --git a/test/unit/math/fwd/meta/partials_type_test.cpp b/test/unit/math/fwd/meta/partials_type_test.cpp index 442fe99dd0d..f49d091a24c 100644 --- a/test/unit/math/fwd/meta/partials_type_test.cpp +++ b/test/unit/math/fwd/meta/partials_type_test.cpp @@ -2,8 +2,8 @@ #include TEST(MathMetaFwd, partials_type) { - using stan::partials_type; using stan::math::fvar; + using stan::partials_type; stan::partials_type >::type a(2.0); EXPECT_EQ(2.0, a); diff --git a/test/unit/math/mix/core/std_numeric_limits_test.cpp b/test/unit/math/mix/core/std_numeric_limits_test.cpp index 9dde1d79f78..2b900ce58b9 100644 --- a/test/unit/math/mix/core/std_numeric_limits_test.cpp +++ b/test/unit/math/mix/core/std_numeric_limits_test.cpp @@ -3,8 +3,8 @@ #include TEST(AgradMixNumericLimits, All_Fvar) { - using stan::math::fvar; using stan::math::INFTY; + using stan::math::fvar; using stan::math::var; using std::isnan; diff --git a/test/unit/math/mix/meta/broadcast_array_test.cpp b/test/unit/math/mix/meta/broadcast_array_test.cpp index ef9697a2edb..e2bdbc257bf 100644 --- a/test/unit/math/mix/meta/broadcast_array_test.cpp +++ b/test/unit/math/mix/meta/broadcast_array_test.cpp @@ -5,8 +5,8 @@ TEST(MathMetaMix, broadcast_array) { using stan::math::fvar; - using stan::math::var; using stan::math::internal::broadcast_array; + using stan::math::var; fvar fv(1.0, 2.1); broadcast_array > ba(fv); diff --git a/test/unit/math/mix/meta/partials_return_type_test.cpp b/test/unit/math/mix/meta/partials_return_type_test.cpp index 53ed911e0a0..f19916b0ab6 100644 --- a/test/unit/math/mix/meta/partials_return_type_test.cpp +++ b/test/unit/math/mix/meta/partials_return_type_test.cpp @@ -3,9 +3,9 @@ #include #include -using stan::partials_return_type; using stan::math::fvar; using stan::math::var; +using stan::partials_return_type; TEST(MathMetaMix, PartialsReturnTypeFvarVar) { test::expect_same_type >::type>(); diff --git a/test/unit/math/mix/meta/partials_type_test.cpp b/test/unit/math/mix/meta/partials_type_test.cpp index 13c58504c7a..ec5d5af5e0b 100644 --- a/test/unit/math/mix/meta/partials_type_test.cpp +++ b/test/unit/math/mix/meta/partials_type_test.cpp @@ -2,9 +2,9 @@ #include TEST(MathMetaMix, partials_type) { - using stan::partials_type; using stan::math::fvar; using stan::math::var; + using stan::partials_type; stan::partials_type > >::type d(7.0, 1.0); EXPECT_EQ(7.0, d.val_.val()); diff --git a/test/unit/math/mix/meta/return_type_test.cpp b/test/unit/math/mix/meta/return_type_test.cpp index 0ded7225cc3..fd4013b3d2c 100644 --- a/test/unit/math/mix/meta/return_type_test.cpp +++ b/test/unit/math/mix/meta/return_type_test.cpp @@ -4,9 +4,9 @@ #include #include -using stan::return_type; using stan::math::fvar; using stan::math::var; +using stan::return_type; using d_t = double; using v_t = var; using fd_t = fvar; diff --git a/test/unit/math/opencl/assign_event_test.cpp b/test/unit/math/opencl/assign_event_test.cpp index cf156b4b3b5..84674753424 100644 --- a/test/unit/math/opencl/assign_event_test.cpp +++ b/test/unit/math/opencl/assign_event_test.cpp @@ -7,8 +7,8 @@ using stan::math::matrix_cl; using stan::math::opencl_kernels::in_buffer; using stan::math::opencl_kernels::in_out_buffer; -using stan::math::opencl_kernels::out_buffer; using stan::math::opencl_kernels::internal::assign_events; +using stan::math::opencl_kernels::out_buffer; TEST(assign_event, correct_vectors) { matrix_cl m; diff --git a/test/unit/math/opencl/rev/triangular_transpose_test.cpp b/test/unit/math/opencl/rev/triangular_transpose_test.cpp index 5ec8fe74bbd..0deb0634876 100644 --- a/test/unit/math/opencl/rev/triangular_transpose_test.cpp +++ b/test/unit/math/opencl/rev/triangular_transpose_test.cpp @@ -5,8 +5,8 @@ #include TEST(MathMatrixRevCL, triangular_transpose_m_exception_pass) { - using stan::math::matrix_cl; using stan::math::TriangularMapCL; + using stan::math::matrix_cl; using stan::math::var; matrix_cl m1(1, 1); matrix_cl m0; @@ -28,11 +28,11 @@ TEST(MathMatrixRevCL, triangular_transpose_m_exception_pass) { } TEST(MathMatrixRevCL, triangular_transpose_m_pass) { + using stan::math::TriangularMapCL; using stan::math::from_matrix_cl; using stan::math::matrix_cl; using stan::math::matrix_d; using stan::math::matrix_v; - using stan::math::TriangularMapCL; using stan::math::var; matrix_v m0(2, 2); matrix_d m0_dst(2, 2); diff --git a/test/unit/math/prim/fun/beta_test.cpp b/test/unit/math/prim/fun/beta_test.cpp index b8e4bb2e971..baf3a0300aa 100644 --- a/test/unit/math/prim/fun/beta_test.cpp +++ b/test/unit/math/prim/fun/beta_test.cpp @@ -10,9 +10,9 @@ TEST(MathFunctions, beta) { } TEST(MathFunctions, beta_nan) { - using stan::math::beta; using stan::math::INFTY; using stan::math::NOT_A_NUMBER; + using stan::math::beta; EXPECT_TRUE(std::isnan(beta(NOT_A_NUMBER, 2.16))); EXPECT_TRUE(std::isnan(beta(1.65, NOT_A_NUMBER))); diff --git a/test/unit/math/prim/fun/binomial_coefficient_log_test.cpp b/test/unit/math/prim/fun/binomial_coefficient_log_test.cpp index 91e7dd9ba04..e5df58947b9 100644 --- a/test/unit/math/prim/fun/binomial_coefficient_log_test.cpp +++ b/test/unit/math/prim/fun/binomial_coefficient_log_test.cpp @@ -48,8 +48,8 @@ TEST(MathFunctions, binomial_coefficient_log_nan) { } TEST(MathFunctions, binomial_coefficient_log_errors_edge_cases) { - using stan::math::binomial_coefficient_log; using stan::math::INFTY; + using stan::math::binomial_coefficient_log; EXPECT_NO_THROW(binomial_coefficient_log(10, 11)); EXPECT_THROW(binomial_coefficient_log(10, 11.01), std::domain_error); diff --git a/test/unit/math/prim/fun/inv_Phi_test.cpp b/test/unit/math/prim/fun/inv_Phi_test.cpp index be63208746d..9538f7151d6 100644 --- a/test/unit/math/prim/fun/inv_Phi_test.cpp +++ b/test/unit/math/prim/fun/inv_Phi_test.cpp @@ -3,8 +3,8 @@ #include TEST(MathFunctions, inv_Phi) { - using stan::math::inv_Phi; using stan::math::Phi; + using stan::math::inv_Phi; EXPECT_FLOAT_EQ(0.0, inv_Phi(0.5)); double p = 0.123456789; EXPECT_FLOAT_EQ(p, Phi(inv_Phi(p))); diff --git a/test/unit/math/prim/fun/log_inv_logit_diff_test.cpp b/test/unit/math/prim/fun/log_inv_logit_diff_test.cpp index 51eb4b2b1b4..c9287744fe1 100644 --- a/test/unit/math/prim/fun/log_inv_logit_diff_test.cpp +++ b/test/unit/math/prim/fun/log_inv_logit_diff_test.cpp @@ -10,8 +10,8 @@ TEST(MathFunctions, log_inv_logit_diff) { } TEST(MathFunctions, log_inv_logit_diff_nan) { - using stan::math::log_inv_logit_diff; using stan::math::NOT_A_NUMBER; + using stan::math::log_inv_logit_diff; EXPECT_TRUE(std::isnan(log_inv_logit_diff(NOT_A_NUMBER, 2.16))); } diff --git a/test/unit/math/prim/meta/StdVectorBuilder_test.cpp b/test/unit/math/prim/meta/StdVectorBuilder_test.cpp index 45707477ec7..f1886099cd1 100644 --- a/test/unit/math/prim/meta/StdVectorBuilder_test.cpp +++ b/test/unit/math/prim/meta/StdVectorBuilder_test.cpp @@ -30,8 +30,8 @@ TEST(MathMetaPrim, StdVectorBuilder_true_false_scalar) { } TEST(MathMetaPrim, StdVectorBuilder_type_check_scalar) { - using stan::contains_std_vector; using stan::StdVectorBuilder; + using stan::contains_std_vector; bool r = contains_std_vector::type>::value; @@ -69,8 +69,8 @@ TEST(MathMetaPrim, StdVectorBuilder_true_false_vector) { } TEST(MathMetaPrim, StdVectorBuilder_type_check_vector) { - using stan::contains_std_vector; using stan::StdVectorBuilder; + using stan::contains_std_vector; bool r = contains_std_vector< StdVectorBuilder>::type>::value; @@ -125,8 +125,8 @@ TEST(MathMetaPrim, StdVectorBuilder_true_false_matrix) { } TEST(MathMetaPrim, StdVectorBuilder_type_check_matrix) { - using stan::contains_std_vector; using stan::StdVectorBuilder; + using stan::contains_std_vector; bool r = contains_std_vector>::type>::value; diff --git a/test/unit/math/prim/meta/is_detected_test.cpp b/test/unit/math/prim/meta/is_detected_test.cpp index 102e3851d77..30a824f902f 100644 --- a/test/unit/math/prim/meta/is_detected_test.cpp +++ b/test/unit/math/prim/meta/is_detected_test.cpp @@ -20,9 +20,9 @@ struct Purr { TEST(MetaTraits, is_detected_checks) { using stan::is_detected; - using stan::test::internal::copy_assign_t; using stan::test::internal::Meow; using stan::test::internal::Purr; + using stan::test::internal::copy_assign_t; EXPECT_TRUE((is_detected::value)); EXPECT_TRUE((is_detected::value)); EXPECT_TRUE((is_detected::value)); diff --git a/test/unit/math/prim/meta/void_t_test.cpp b/test/unit/math/prim/meta/void_t_test.cpp index ed2cf711345..f8f77f26fe5 100644 --- a/test/unit/math/prim/meta/void_t_test.cpp +++ b/test/unit/math/prim/meta/void_t_test.cpp @@ -24,9 +24,9 @@ class dummy_iterable { } // namespace stan TEST(MetaTraits, void_t_checks) { - using stan::void_t; using stan::test::internal::dummy_iterable; using stan::test::internal::is_iterable; + using stan::void_t; EXPECT_FALSE(is_iterable::value); EXPECT_FALSE(is_iterable::value); EXPECT_FALSE(is_iterable::value); diff --git a/test/unit/math/prim/prob/inv_wishart_rng_test.cpp b/test/unit/math/prim/prob/inv_wishart_rng_test.cpp index c1f83030b17..c56ada5f426 100644 --- a/test/unit/math/prim/prob/inv_wishart_rng_test.cpp +++ b/test/unit/math/prim/prob/inv_wishart_rng_test.cpp @@ -33,9 +33,9 @@ TEST(probdistributionsInvWishartRng, symmetry) { } TEST(ProbDistributionsInvWishart, chiSquareGoodnessFitTest) { + using Eigen::MatrixXd; using boost::math::chi_squared; using boost::math::digamma; - using Eigen::MatrixXd; using stan::math::determinant; using stan::math::inv_wishart_rng; using std::log; diff --git a/test/unit/math/prim/prob/ordered_probit_test.cpp b/test/unit/math/prim/prob/ordered_probit_test.cpp index 50350f224ad..0b5533a0c38 100644 --- a/test/unit/math/prim/prob/ordered_probit_test.cpp +++ b/test/unit/math/prim/prob/ordered_probit_test.cpp @@ -25,8 +25,8 @@ TEST(ProbDistributions, ordered_probit_vals) { using Eigen::Dynamic; using Eigen::Matrix; - using stan::math::ordered_probit_log; using stan::math::Phi; + using stan::math::ordered_probit_log; int K = 5; Matrix c(K - 1); @@ -51,8 +51,8 @@ TEST(ProbDistributions, ordered_probit_vals_2) { using Eigen::Dynamic; using Eigen::Matrix; - using stan::math::ordered_probit_log; using stan::math::Phi; + using stan::math::ordered_probit_log; int K = 3; Matrix c(K - 1); diff --git a/test/unit/math/prim/prob/wishart_rng_test.cpp b/test/unit/math/prim/prob/wishart_rng_test.cpp index 4760d8ef9b8..c14afcad877 100644 --- a/test/unit/math/prim/prob/wishart_rng_test.cpp +++ b/test/unit/math/prim/prob/wishart_rng_test.cpp @@ -36,9 +36,9 @@ TEST(probdistributionsWishartRng, symmetry) { } TEST(ProbDistributionsWishart, marginalTwoChiSquareGoodnessFitTest) { + using Eigen::MatrixXd; using boost::math::chi_squared; using boost::math::digamma; - using Eigen::MatrixXd; using stan::math::determinant; using stan::math::wishart_rng; using std::log; diff --git a/test/unit/math/rev/functor/algebra_solver_fp_test.cpp b/test/unit/math/rev/functor/algebra_solver_fp_test.cpp index f93fb3717aa..e7100c91df0 100644 --- a/test/unit/math/rev/functor/algebra_solver_fp_test.cpp +++ b/test/unit/math/rev/functor/algebra_solver_fp_test.cpp @@ -10,11 +10,11 @@ #include #include -using stan::math::algebra_solver_fp; -using stan::math::finite_diff_gradient_auto; using stan::math::FixedPointADJac; using stan::math::FixedPointSolver; using stan::math::KinsolFixedPointEnv; +using stan::math::algebra_solver_fp; +using stan::math::finite_diff_gradient_auto; using stan::math::to_array_1d; using stan::math::to_var; using stan::math::value_of; diff --git a/test/unit/math/rev/meta/partials_return_type_test.cpp b/test/unit/math/rev/meta/partials_return_type_test.cpp index 8eed06a2e8f..c62bc58bd3b 100644 --- a/test/unit/math/rev/meta/partials_return_type_test.cpp +++ b/test/unit/math/rev/meta/partials_return_type_test.cpp @@ -3,8 +3,8 @@ #include #include -using stan::partials_return_type; using stan::math::var; +using stan::partials_return_type; TEST(MetaTraitsRevScal, PartialsReturnTypeVar) { test::expect_same_type::type>(); @@ -17,8 +17,8 @@ TEST(MetaTraitsRevScal, PartialsReturnTypeVarTenParams) { } TEST(MetaTraitsRevArr, partials_return_type) { - using stan::partials_return_type; using stan::math::var; + using stan::partials_return_type; partials_return_type >::type g(5.0); diff --git a/test/unit/math/rev/meta/partials_type_test.cpp b/test/unit/math/rev/meta/partials_type_test.cpp index d3187a0ec47..7c3079711fa 100644 --- a/test/unit/math/rev/meta/partials_type_test.cpp +++ b/test/unit/math/rev/meta/partials_type_test.cpp @@ -2,8 +2,8 @@ #include TEST(MetaTraitsRevScal, partials_type) { - using stan::partials_type; using stan::math::var; + using stan::partials_type; stan::partials_type::type f(2.0); EXPECT_EQ(2.0, f); From 065a61d2cee0cd6b1bde1ed5070512a1e68ae9ff Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 2 Apr 2020 19:32:17 -0400 Subject: [PATCH 20/41] Updated tests to include zero length sliced arrays (Issue #1815) --- test/unit/math/prim/functor/reduce_sum_test.cpp | 14 ++++++++++++++ test/unit/math/reduce_sum_util.hpp | 2 +- test/unit/math/rev/functor/reduce_sum_test.cpp | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index 1c044d69b26..371ab2030f5 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -128,6 +128,20 @@ TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_matrix_slice) { 40.0, std::vector(2, Eigen::MatrixXd::Ones(2, 2))); } +TEST(StanMathPrim_reduce_sum, no_args) { + tbb::task_scheduler_init default_scheduler; + using stan::math::test::get_new_msg; + using stan::math::test::sum_lpdf; + + std::vector data(0); + EXPECT_EQ(0.0, stan::math::reduce_sum_static( + data, 1, stan::math::test::get_new_msg())) + << "Failed for reduce_sum_static"; + EXPECT_EQ(0.0, + stan::math::reduce_sum(data, 1, stan::math::test::get_new_msg())) + << "Failed for reduce_sum"; +} + TEST(StanMathPrim_reduce_sum, int_arg) { stan::math::test::test_slices(5 * (10 + 5), 10, 5); } diff --git a/test/unit/math/reduce_sum_util.hpp b/test/unit/math/reduce_sum_util.hpp index c607b335cbf..0d68f541c89 100644 --- a/test/unit/math/reduce_sum_util.hpp +++ b/test/unit/math/reduce_sum_util.hpp @@ -137,7 +137,7 @@ struct grouped_count_lpdf { template void test_slices(T1 result, T2&& vec_value, Args&&... args) { - stan::math::init_threadpool_tbb(); + tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 0262575082b..cb087e1e7c0 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -6,6 +6,20 @@ #include #include +TEST(StanMathPrim_reduce_sum, no_args) { + tbb::task_scheduler_init default_scheduler; + using stan::math::test::get_new_msg; + using stan::math::test::sum_lpdf; + + std::vector data(0); + EXPECT_EQ(0.0, stan::math::reduce_sum_static( + data, 1, stan::math::test::get_new_msg()).val()) + << "Failed for reduce_sum_static"; + EXPECT_EQ(0.0, + stan::math::reduce_sum(data, 1, stan::math::test::get_new_msg()).val()) + << "Failed for reduce_sum"; +} + TEST(StanMathRev_reduce_sum, value) { using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; From 198f70f98fccf12a3560d316ac59ab4b633aa8d2 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 2 Apr 2020 19:34:02 -0400 Subject: [PATCH 21/41] [Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/stable/2017-11-14) --- test/unit/math/prim/functor/reduce_sum_test.cpp | 6 +++--- test/unit/math/rev/functor/reduce_sum_test.cpp | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index 371ab2030f5..95128ad7cc0 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -135,10 +135,10 @@ TEST(StanMathPrim_reduce_sum, no_args) { std::vector data(0); EXPECT_EQ(0.0, stan::math::reduce_sum_static( - data, 1, stan::math::test::get_new_msg())) + data, 1, stan::math::test::get_new_msg())) << "Failed for reduce_sum_static"; - EXPECT_EQ(0.0, - stan::math::reduce_sum(data, 1, stan::math::test::get_new_msg())) + EXPECT_EQ(0.0, stan::math::reduce_sum( + data, 1, stan::math::test::get_new_msg())) << "Failed for reduce_sum"; } diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index cb087e1e7c0..253395cedda 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -13,10 +13,12 @@ TEST(StanMathPrim_reduce_sum, no_args) { std::vector data(0); EXPECT_EQ(0.0, stan::math::reduce_sum_static( - data, 1, stan::math::test::get_new_msg()).val()) + data, 1, stan::math::test::get_new_msg()) + .val()) << "Failed for reduce_sum_static"; - EXPECT_EQ(0.0, - stan::math::reduce_sum(data, 1, stan::math::test::get_new_msg()).val()) + EXPECT_EQ(0.0, stan::math::reduce_sum( + data, 1, stan::math::test::get_new_msg()) + .val()) << "Failed for reduce_sum"; } From 27fb3d160cfe932ff2eb9fa558aa3a1c84348eef Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 3 Apr 2020 16:56:46 -0400 Subject: [PATCH 22/41] fix test name --- test/unit/math/rev/functor/reduce_sum_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 253395cedda..9d744d3cd44 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -6,7 +6,7 @@ #include #include -TEST(StanMathPrim_reduce_sum, no_args) { +TEST(StanMathRev_reduce_sum, no_args) { tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; From 88913c73355a54e9aeb434d78db5ad2ee060e61f Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 3 Apr 2020 17:40:48 -0400 Subject: [PATCH 23/41] fix reduce sum using statements for test --- test/unit/math/rev/functor/reduce_sum_test.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 9d744d3cd44..ef971768928 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -10,7 +10,7 @@ TEST(StanMathRev_reduce_sum, no_args) { tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; - + using stan::math::var; std::vector data(0); EXPECT_EQ(0.0, stan::math::reduce_sum_static( data, 1, stan::math::test::get_new_msg()) @@ -55,6 +55,7 @@ TEST(StanMathRev_reduce_sum, value) { TEST(StanMathRev_reduce_sum, gradient) { using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; + using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -64,7 +65,6 @@ TEST(StanMathRev_reduce_sum, gradient) { for (std::size_t i = 0; i != elems; ++i) data[i] = i; - using stan::math::var; var lambda_v = lambda_d; @@ -105,6 +105,7 @@ TEST(StanMathRev_reduce_sum, gradient) { TEST(StanMathRev_reduce_sum, grainsize) { using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; + using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -114,8 +115,6 @@ TEST(StanMathRev_reduce_sum, grainsize) { for (std::size_t i = 0; i != elems; ++i) data[i] = i; - using stan::math::var; - var lambda_v = lambda_d; std::vector idata; @@ -155,6 +154,7 @@ TEST(StanMathRev_reduce_sum, grainsize) { TEST(StanMathRev_reduce_sum, nesting_gradient) { using stan::math::test::get_new_msg; using stan::math::test::nesting_count_lpdf; + using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -164,8 +164,6 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { for (std::size_t i = 0; i != elems; ++i) data[i] = i; - using stan::math::var; - var lambda_v = lambda_d; std::vector idata; @@ -207,6 +205,7 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { TEST(StanMathRev_reduce_sum, grouped_gradient) { using stan::math::test::get_new_msg; using stan::math::test::grouped_count_lpdf; + using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -222,8 +221,6 @@ TEST(StanMathRev_reduce_sum, grouped_gradient) { gidx[i] = i / elems_per_group; } - using stan::math::var; - std::vector vlambda_v; for (std::size_t i = 0; i != groups; ++i) @@ -270,6 +267,7 @@ TEST(StanMathRev_reduce_sum, grouped_gradient) { TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { using stan::math::test::get_new_msg; using stan::math::test::grouped_count_lpdf; + using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -284,7 +282,6 @@ TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { data[i] = i; gidx[i] = i / elems_per_group; } - using stan::math::var; Eigen::Matrix vlambda_v(groups); @@ -334,6 +331,7 @@ TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { TEST(StanMathRev_reduce_sum, slice_group_gradient) { using stan::math::test::get_new_msg; using stan::math::test::slice_group_count_lpdf; + using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -354,8 +352,6 @@ TEST(StanMathRev_reduce_sum, slice_group_gradient) { gsidx[i + 1] = k; } - using stan::math::var; - std::vector vlambda_v; for (std::size_t i = 0; i != groups; ++i) From ace0bbb2a8e44144ad045e79be8042a09541785f Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 3 Apr 2020 17:41:00 -0400 Subject: [PATCH 24/41] Added tests for threading. Moved generic reduce_sum implementation to make it harder to accidentally remove rev/prim versions (Issue #1815) --- stan/math/fwd/functor.hpp | 1 + stan/math/prim/functor/reduce_sum.hpp | 74 +------------------ stan/math/prim/functor/reduce_sum_static.hpp | 2 +- .../math/prim/functor/reduce_sum_test.cpp | 31 ++++++++ test/unit/math/reduce_sum_util.hpp | 1 + .../unit/math/rev/functor/reduce_sum_test.cpp | 36 ++++++++- 6 files changed, 70 insertions(+), 75 deletions(-) diff --git a/stan/math/fwd/functor.hpp b/stan/math/fwd/functor.hpp index c65b467edf4..cb6b200f3a6 100644 --- a/stan/math/fwd/functor.hpp +++ b/stan/math/fwd/functor.hpp @@ -4,5 +4,6 @@ #include #include #include +#include #endif diff --git a/stan/math/prim/functor/reduce_sum.hpp b/stan/math/prim/functor/reduce_sum.hpp index 1b84d5e64f7..ab7ddd53632 100644 --- a/stan/math/prim/functor/reduce_sum.hpp +++ b/stan/math/prim/functor/reduce_sum.hpp @@ -17,81 +17,9 @@ namespace math { namespace internal { -/** - * reduce_sum_impl implementation for any autodiff type. - * - * @tparam ReduceFunction Type of reducer function - * @tparam ReturnType An arithmetic type - * @tparam Vec Type of sliced argument - * @tparam Args Types of shared arguments - */ template -struct reduce_sum_impl { - /** - * Call an instance of the function `ReduceFunction` on every element - * of an input sequence and sum these terms. - * - * This specialization is not parallelized and works for any autodiff types. - * - * An instance, f, of `ReduceFunction` should have the signature: - * T f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, - * Args&&... args) - * - * `ReduceFunction` must be default constructible without any arguments - * - * Each call to `ReduceFunction` is responsible for computing the - * start through end terms (inclusive) of the overall sum. All args are - * passed from this function through to the `ReduceFunction` instances. - * However, only the start through end (inclusive) elements of the vmapped - * argument are passed to the `ReduceFunction` instances (as the - * `vmapped_subset` argument). - * - * If auto partitioning is true, do the calculation with one - * ReduceFunction call. If false, break work into pieces strictly smaller - * than grainsize. - * - * grainsize must be greater than or equal to 1 - * - * @param vmapped Vector containing one element per term of sum - * @param auto_partitioning Work partitioning style (ignored) - * @param grainsize Suggested grainsize for tbb - * @param[in, out] msgs The print stream for warning messages - * @param args Shared arguments used in every sum term - * @return Summation of all terms - */ - return_type_t operator()(Vec&& vmapped, bool auto_partitioning, - int grainsize, std::ostream* msgs, - Args&&... args) const { - const std::size_t num_terms = vmapped.size(); - - if (num_terms == 0) { - return 0.0; - } - - if (auto_partitioning) { - return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), - msgs, std::forward(args)...); - } else { - return_type_t sum = 0.0; - for (size_t i = 0; i < (vmapped.size() + grainsize - 1) / grainsize; - ++i) { - size_t start = i * grainsize; - size_t end = std::min((i + 1) * grainsize, vmapped.size()) - 1; - - std::decay_t sub_slice; - sub_slice.reserve(end - start + 1); - for (size_t i = start; i <= end; ++i) { - sub_slice.emplace_back(vmapped[i]); - } - - sum += ReduceFunction()(start, end, std::forward(sub_slice), msgs, - std::forward(args)...); - } - return sum; - } - } -}; +struct reduce_sum_impl; /** * Specialization of reduce_sum_impl for arithmetic types diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp index 6b2435d33bc..591743be257 100644 --- a/stan/math/prim/functor/reduce_sum_static.hpp +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -55,7 +55,7 @@ auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs, const std::size_t num_terms = vmapped.size(); if (num_terms == 0) { - return return_type(0.0); + return return_type(0); } return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index 371ab2030f5..3e69a588535 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -235,3 +235,34 @@ TEST(StanMathPrim_reduce_sum, sum) { std::vector>(2, std::vector(5, 1.0)), std::vector(2, Eigen::VectorXd::Ones(5))); } + +#ifdef STAN_THREADS +std::vector threading_test_global; +struct threading_test_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, const std::vector&, + std::ostream* msgs) const { + threading_test_global[start] = tbb::this_task_arena::current_thread_index(); + + return stan::return_type_t(0); + } +}; + +TEST(StanMathPrim_reduce_sum, threading) { + tbb::task_scheduler_init default_scheduler; + threading_test_global = std::vector(10000, 0); + stan::math::reduce_sum_static(threading_test_global, 1, nullptr); + + auto uniques = std::set(threading_test_global.begin(), threading_test_global.end()); + + EXPECT_GT(uniques.size(), 1); + + threading_test_global = std::vector(10000, 0); + + stan::math::reduce_sum(threading_test_global, 1, nullptr); + + uniques = std::set(threading_test_global.begin(), threading_test_global.end()); + + EXPECT_GT(uniques.size(), 1); +} +#endif diff --git a/test/unit/math/reduce_sum_util.hpp b/test/unit/math/reduce_sum_util.hpp index 0d68f541c89..043b42d8310 100644 --- a/test/unit/math/reduce_sum_util.hpp +++ b/test/unit/math/reduce_sum_util.hpp @@ -76,6 +76,7 @@ struct sum_lpdf { return_type(sum_(std::forward(args)))...}); } }; + struct start_end_lpdf { template inline auto operator()(std::size_t start, std::size_t end, T1&&, diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index cb087e1e7c0..2e9282ebbd0 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -11,7 +11,7 @@ TEST(StanMathPrim_reduce_sum, no_args) { using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; - std::vector data(0); + std::vector data(0); EXPECT_EQ(0.0, stan::math::reduce_sum_static( data, 1, stan::math::test::get_new_msg()).val()) << "Failed for reduce_sum_static"; @@ -399,3 +399,37 @@ TEST(StanMathRev_reduce_sum, slice_group_gradient) { stan::math::recover_memory(); } + +#ifdef STAN_THREADS +std::vector threading_test_global; +struct threading_test_lpdf { + template + inline auto operator()(std::size_t start, std::size_t end, const std::vector&, + std::ostream* msgs) const { + threading_test_global[start] = tbb::this_task_arena::current_thread_index(); + + return stan::return_type_t(0); + } +}; + +TEST(StanMathRev_reduce_sum, threading) { + tbb::task_scheduler_init default_scheduler; + threading_test_global = std::vector(10000, 0); + std::vector data(threading_test_global.size(), 0); + stan::math::reduce_sum_static(data, 1, nullptr); + + auto uniques = std::set(threading_test_global.begin(), threading_test_global.end()); + + EXPECT_GT(uniques.size(), 1); + + threading_test_global = std::vector(10000, 0); + + stan::math::reduce_sum(data, 1, nullptr); + + uniques = std::set(threading_test_global.begin(), threading_test_global.end()); + + EXPECT_GT(uniques.size(), 1); + + stan::math::recover_memory(); +} +#endif From 560c293add3fb4fdb2edd286e023d902e5e05258 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 3 Apr 2020 21:41:48 +0000 Subject: [PATCH 25/41] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- test/unit/math/rev/functor/reduce_sum_test.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index ef971768928..5c66909a59c 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -8,9 +8,9 @@ TEST(StanMathRev_reduce_sum, no_args) { tbb::task_scheduler_init default_scheduler; + using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; - using stan::math::var; std::vector data(0); EXPECT_EQ(0.0, stan::math::reduce_sum_static( data, 1, stan::math::test::get_new_msg()) @@ -53,9 +53,9 @@ TEST(StanMathRev_reduce_sum, value) { } TEST(StanMathRev_reduce_sum, gradient) { + using stan::math::var; using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; - using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -65,7 +65,6 @@ TEST(StanMathRev_reduce_sum, gradient) { for (std::size_t i = 0; i != elems; ++i) data[i] = i; - var lambda_v = lambda_d; std::vector idata; @@ -103,9 +102,9 @@ TEST(StanMathRev_reduce_sum, gradient) { } TEST(StanMathRev_reduce_sum, grainsize) { + using stan::math::var; using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; - using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -152,9 +151,9 @@ TEST(StanMathRev_reduce_sum, grainsize) { } TEST(StanMathRev_reduce_sum, nesting_gradient) { + using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::nesting_count_lpdf; - using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -203,9 +202,9 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { } TEST(StanMathRev_reduce_sum, grouped_gradient) { + using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::grouped_count_lpdf; - using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -265,9 +264,9 @@ TEST(StanMathRev_reduce_sum, grouped_gradient) { } TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { + using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::grouped_count_lpdf; - using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; @@ -329,9 +328,9 @@ TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { } TEST(StanMathRev_reduce_sum, slice_group_gradient) { + using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::slice_group_count_lpdf; - using stan::math::var; tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; From 04e412be6d59c3db7e88028c06c3f5670bf7c1d4 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 3 Apr 2020 18:04:24 -0400 Subject: [PATCH 26/41] Shuffled reduce_sum test utils includes to avoid including mix into prim and rev tests. Added fwd reduce_sum header (Issue #1815) --- stan/math/fwd/functor/reduce_sum.hpp | 101 ++++++++++++++++++ .../mix/functor/reduce_sum_part1_test.cpp | 2 +- .../mix/functor/reduce_sum_part2_test.cpp | 2 +- .../mix/functor/reduce_sum_part3_test.cpp | 2 +- .../mix/functor/reduce_sum_part4_test.cpp | 2 +- .../unit/math/mix/functor/reduce_sum_util.hpp | 32 ++++++ .../math/prim/functor/reduce_sum_test.cpp | 2 +- .../{ => prim/functor}/reduce_sum_util.hpp | 17 +-- .../unit/math/rev/functor/reduce_sum_test.cpp | 4 +- 9 files changed, 142 insertions(+), 22 deletions(-) create mode 100644 stan/math/fwd/functor/reduce_sum.hpp create mode 100644 test/unit/math/mix/functor/reduce_sum_util.hpp rename test/unit/math/{ => prim/functor}/reduce_sum_util.hpp (90%) diff --git a/stan/math/fwd/functor/reduce_sum.hpp b/stan/math/fwd/functor/reduce_sum.hpp new file mode 100644 index 00000000000..559d7e52218 --- /dev/null +++ b/stan/math/fwd/functor/reduce_sum.hpp @@ -0,0 +1,101 @@ +#ifndef STAN_MATH_FWD_FUNCTOR_REDUCE_SUM_HPP +#define STAN_MATH_FWD_FUNCTOR_REDUCE_SUM_HPP + +#include +#include + +#include +#include +#include + +#include +#include +#include + +namespace stan { +namespace math { + +namespace internal { + +/** + * reduce_sum_impl implementation for any autodiff type. + * + * @tparam ReduceFunction Type of reducer function + * @tparam ReturnType An arithmetic type + * @tparam Vec Type of sliced argument + * @tparam Args Types of shared arguments + */ +template +struct reduce_sum_impl { + /** + * Call an instance of the function `ReduceFunction` on every element + * of an input sequence and sum these terms. + * + * This specialization is not parallelized and works for any autodiff types. + * + * An instance, f, of `ReduceFunction` should have the signature: + * T f(int start, int end, Vec&& vmapped_subset, std::ostream* msgs, + * Args&&... args) + * + * `ReduceFunction` must be default constructible without any arguments + * + * Each call to `ReduceFunction` is responsible for computing the + * start through end terms (inclusive) of the overall sum. All args are + * passed from this function through to the `ReduceFunction` instances. + * However, only the start through end (inclusive) elements of the vmapped + * argument are passed to the `ReduceFunction` instances (as the + * `vmapped_subset` argument). + * + * If auto partitioning is true, do the calculation with one + * ReduceFunction call. If false, break work into pieces strictly smaller + * than grainsize. + * + * grainsize must be greater than or equal to 1 + * + * @param vmapped Vector containing one element per term of sum + * @param auto_partitioning Work partitioning style (ignored) + * @param grainsize Suggested grainsize for tbb + * @param[in, out] msgs The print stream for warning messages + * @param args Shared arguments used in every sum term + * @return Summation of all terms + */ + return_type_t operator()(Vec&& vmapped, bool auto_partitioning, + int grainsize, std::ostream* msgs, + Args&&... args) const { + const std::size_t num_terms = vmapped.size(); + + if (num_terms == 0) { + return 0.0; + } + + if (auto_partitioning) { + return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), + msgs, std::forward(args)...); + } else { + return_type_t sum = 0.0; + for (size_t i = 0; i < (vmapped.size() + grainsize - 1) / grainsize; + ++i) { + size_t start = i * grainsize; + size_t end = std::min((i + 1) * grainsize, vmapped.size()) - 1; + + std::decay_t sub_slice; + sub_slice.reserve(end - start + 1); + for (size_t i = start; i <= end; ++i) { + sub_slice.emplace_back(vmapped[i]); + } + + sum += ReduceFunction()(start, end, std::forward(sub_slice), msgs, + std::forward(args)...); + } + return sum; + } + } +}; + +} // namespace internal + +} // namespace math +} // namespace stan + +#endif diff --git a/test/unit/math/mix/functor/reduce_sum_part1_test.cpp b/test/unit/math/mix/functor/reduce_sum_part1_test.cpp index 15f18be2f4e..abf7bc184f3 100644 --- a/test/unit/math/mix/functor/reduce_sum_part1_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part1_test.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include diff --git a/test/unit/math/mix/functor/reduce_sum_part2_test.cpp b/test/unit/math/mix/functor/reduce_sum_part2_test.cpp index 9eabefefab0..524671b3223 100644 --- a/test/unit/math/mix/functor/reduce_sum_part2_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part2_test.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include diff --git a/test/unit/math/mix/functor/reduce_sum_part3_test.cpp b/test/unit/math/mix/functor/reduce_sum_part3_test.cpp index df2d3c1e4be..52723cdeee3 100644 --- a/test/unit/math/mix/functor/reduce_sum_part3_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part3_test.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include diff --git a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp index 2bfd95e702a..a5392b52dec 100644 --- a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include diff --git a/test/unit/math/mix/functor/reduce_sum_util.hpp b/test/unit/math/mix/functor/reduce_sum_util.hpp new file mode 100644 index 00000000000..9d70fe1054c --- /dev/null +++ b/test/unit/math/mix/functor/reduce_sum_util.hpp @@ -0,0 +1,32 @@ +#ifndef TEST_UNIT_MATH_MIX_FUNCTOR_REDUCE_SUM_UTIL +#define TEST_UNIT_MATH_MIX_FUNCTOR_REDUCE_SUM_UTIL + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace stan { +namespace math { +namespace test { + +template +void expect_ad_reduce_sum_lpdf(T1&& data, T2&& arg) { + using stan::math::test::reduce_sum_int_sum_lpdf; + using stan::math::test::reduce_sum_static_int_sum_lpdf; + using stan::math::test::reduce_sum_static_sum_lpdf; + using stan::math::test::reduce_sum_sum_lpdf; + stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg); + stan::test::expect_ad(reduce_sum_static_sum_lpdf, data, arg); + stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg); + stan::test::expect_ad(reduce_sum_sum_lpdf, data, arg); +} + +} // namespace test +} // namespace math +} // namespace stan +#endif diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index 3e69a588535..87b1ea22b45 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/test/unit/math/reduce_sum_util.hpp b/test/unit/math/prim/functor/reduce_sum_util.hpp similarity index 90% rename from test/unit/math/reduce_sum_util.hpp rename to test/unit/math/prim/functor/reduce_sum_util.hpp index 043b42d8310..06fa0fa4f93 100644 --- a/test/unit/math/reduce_sum_util.hpp +++ b/test/unit/math/prim/functor/reduce_sum_util.hpp @@ -1,8 +1,7 @@ -#ifndef TEST_UNIT_MATH_REDUCE_SUM_UTIL -#define TEST_UNIT_MATH_REDUCE_SUM_UTIL +#ifndef TEST_UNIT_MATH_PRIM_FUNCTOR_REDUCE_SUM_UTIL +#define TEST_UNIT_MATH_PRIM_FUNCTOR_REDUCE_SUM_UTIL #include -#include #include #include #include @@ -170,18 +169,6 @@ auto reduce_sum_sum_lpdf = [](auto&& data, auto&&... args) { return stan::math::reduce_sum(data, 1, get_new_msg(), args...); }; -template -void expect_ad_reduce_sum_lpdf(T1&& data, T2&& arg) { - using stan::math::test::reduce_sum_int_sum_lpdf; - using stan::math::test::reduce_sum_static_int_sum_lpdf; - using stan::math::test::reduce_sum_static_sum_lpdf; - using stan::math::test::reduce_sum_sum_lpdf; - stan::test::expect_ad(reduce_sum_static_int_sum_lpdf, arg); - stan::test::expect_ad(reduce_sum_static_sum_lpdf, data, arg); - stan::test::expect_ad(reduce_sum_int_sum_lpdf, arg); - stan::test::expect_ad(reduce_sum_sum_lpdf, data, arg); -} - template struct static_check_lpdf { template diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 2e9282ebbd0..d330c90a9e3 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -1,12 +1,12 @@ #include -#include +#include #include #include #include #include #include -TEST(StanMathPrim_reduce_sum, no_args) { +TEST(StanMathRev_reduce_sum, no_args) { tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; From 63e7a353b34423a40d06c64ede1286f4f877e3eb Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 3 Apr 2020 22:07:35 +0000 Subject: [PATCH 27/41] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/fwd/functor/reduce_sum.hpp | 2 +- test/unit/math/prim/functor/reduce_sum_test.cpp | 16 ++++++++++------ test/unit/math/rev/functor/reduce_sum_test.cpp | 10 ++++++---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/stan/math/fwd/functor/reduce_sum.hpp b/stan/math/fwd/functor/reduce_sum.hpp index 559d7e52218..792a164c390 100644 --- a/stan/math/fwd/functor/reduce_sum.hpp +++ b/stan/math/fwd/functor/reduce_sum.hpp @@ -64,7 +64,7 @@ struct reduce_sum_impl { int grainsize, std::ostream* msgs, Args&&... args) const { const std::size_t num_terms = vmapped.size(); - + if (num_terms == 0) { return 0.0; } diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index c53c01e641f..4c5279856e4 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -240,8 +240,8 @@ TEST(StanMathPrim_reduce_sum, sum) { std::vector threading_test_global; struct threading_test_lpdf { template - inline auto operator()(std::size_t start, std::size_t end, const std::vector&, - std::ostream* msgs) const { + inline auto operator()(std::size_t start, std::size_t end, + const std::vector&, std::ostream* msgs) const { threading_test_global[start] = tbb::this_task_arena::current_thread_index(); return stan::return_type_t(0); @@ -251,17 +251,21 @@ struct threading_test_lpdf { TEST(StanMathPrim_reduce_sum, threading) { tbb::task_scheduler_init default_scheduler; threading_test_global = std::vector(10000, 0); - stan::math::reduce_sum_static(threading_test_global, 1, nullptr); + stan::math::reduce_sum_static(threading_test_global, 1, + nullptr); - auto uniques = std::set(threading_test_global.begin(), threading_test_global.end()); + auto uniques = std::set(threading_test_global.begin(), + threading_test_global.end()); EXPECT_GT(uniques.size(), 1); threading_test_global = std::vector(10000, 0); - stan::math::reduce_sum(threading_test_global, 1, nullptr); + stan::math::reduce_sum(threading_test_global, 1, + nullptr); - uniques = std::set(threading_test_global.begin(), threading_test_global.end()); + uniques = std::set(threading_test_global.begin(), + threading_test_global.end()); EXPECT_GT(uniques.size(), 1); } diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index fba9bc80ac1..bb91e49b9e5 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -401,8 +401,8 @@ TEST(StanMathRev_reduce_sum, slice_group_gradient) { std::vector threading_test_global; struct threading_test_lpdf { template - inline auto operator()(std::size_t start, std::size_t end, const std::vector&, - std::ostream* msgs) const { + inline auto operator()(std::size_t start, std::size_t end, + const std::vector&, std::ostream* msgs) const { threading_test_global[start] = tbb::this_task_arena::current_thread_index(); return stan::return_type_t(0); @@ -415,7 +415,8 @@ TEST(StanMathRev_reduce_sum, threading) { std::vector data(threading_test_global.size(), 0); stan::math::reduce_sum_static(data, 1, nullptr); - auto uniques = std::set(threading_test_global.begin(), threading_test_global.end()); + auto uniques = std::set(threading_test_global.begin(), + threading_test_global.end()); EXPECT_GT(uniques.size(), 1); @@ -423,7 +424,8 @@ TEST(StanMathRev_reduce_sum, threading) { stan::math::reduce_sum(data, 1, nullptr); - uniques = std::set(threading_test_global.begin(), threading_test_global.end()); + uniques = std::set(threading_test_global.begin(), + threading_test_global.end()); EXPECT_GT(uniques.size(), 1); From 128c10e425701daec4bf949ffdbc8717725fca43 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 3 Apr 2020 18:24:14 -0400 Subject: [PATCH 28/41] Added missing headers to tests (Issue #1815) --- test/unit/math/prim/functor/reduce_sum_test.cpp | 1 + test/unit/math/rev/functor/reduce_sum_test.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index c53c01e641f..ccb8b52bb23 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -3,6 +3,7 @@ #include #include +#include TEST(StanMathPrim_reduce_sum, value) { tbb::task_scheduler_init default_scheduler; diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index fba9bc80ac1..e1ee302aa68 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -5,6 +5,7 @@ #include #include #include +#include TEST(StanMathRev_reduce_sum, no_args) { tbb::task_scheduler_init default_scheduler; From 9301d9e3db4bd3022d6450e04c4ceabf92d99408 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 3 Apr 2020 19:17:02 -0400 Subject: [PATCH 29/41] Updated reduce_sum_static to perform as described even without threading (Issue #1815) --- stan/math/prim/functor/reduce_sum_static.hpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp index 591743be257..2e2a7fbd271 100644 --- a/stan/math/prim/functor/reduce_sum_static.hpp +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -58,8 +58,22 @@ auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs, return return_type(0); } - return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), - msgs, std::forward(args)...); + return_type_t sum = 0.0; + for (size_t i = 0; i < (vmapped.size() + grainsize - 1) / grainsize; + ++i) { + size_t start = i * grainsize; + size_t end = std::min((i + 1) * grainsize, vmapped.size()) - 1; + + std::decay_t sub_slice; + sub_slice.reserve(end - start + 1); + for (size_t i = start; i <= end; ++i) { + sub_slice.emplace_back(vmapped[i]); + } + + sum += ReduceFunction()(start, end, std::forward(sub_slice), msgs, + std::forward(args)...); + } + return sum; #endif } From 38f1141a1f8ecb6e93a70e294be8defbba67a69a Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 3 Apr 2020 23:18:28 +0000 Subject: [PATCH 30/41] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/prim/functor/reduce_sum_static.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp index 2e2a7fbd271..13eca0ccef3 100644 --- a/stan/math/prim/functor/reduce_sum_static.hpp +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -59,11 +59,10 @@ auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs, } return_type_t sum = 0.0; - for (size_t i = 0; i < (vmapped.size() + grainsize - 1) / grainsize; - ++i) { + for (size_t i = 0; i < (vmapped.size() + grainsize - 1) / grainsize; ++i) { size_t start = i * grainsize; size_t end = std::min((i + 1) * grainsize, vmapped.size()) - 1; - + std::decay_t sub_slice; sub_slice.reserve(end - start + 1); for (size_t i = start; i <= end; ++i) { @@ -71,7 +70,7 @@ auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs, } sum += ReduceFunction()(start, end, std::forward(sub_slice), msgs, - std::forward(args)...); + std::forward(args)...); } return sum; #endif From fe7007a8862df1f2bb609e9a1df21fcac900ab53 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 3 Apr 2020 19:35:44 -0400 Subject: [PATCH 31/41] Made it so that if reduce_sum_static is called without STAN_THREADS it just computes everything in one ReduceFunction call (Issue #1815) --- stan/math/prim/functor/reduce_sum_static.hpp | 20 ++++--------------- .../mix/functor/reduce_sum_part4_test.cpp | 2 ++ 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp index 2e2a7fbd271..69b52d1b309 100644 --- a/stan/math/prim/functor/reduce_sum_static.hpp +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -28,6 +28,8 @@ namespace math { * * grainsize must be greater than or equal to 1 * + * If STAN_THREADS is not defined, do all the work with one ReduceFunction call. + * * @tparam ReduceFunction Type of reducer function * @tparam ReturnType An arithmetic type * @tparam Vec Type of sliced argument @@ -58,22 +60,8 @@ auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs, return return_type(0); } - return_type_t sum = 0.0; - for (size_t i = 0; i < (vmapped.size() + grainsize - 1) / grainsize; - ++i) { - size_t start = i * grainsize; - size_t end = std::min((i + 1) * grainsize, vmapped.size()) - 1; - - std::decay_t sub_slice; - sub_slice.reserve(end - start + 1); - for (size_t i = start; i <= end; ++i) { - sub_slice.emplace_back(vmapped[i]); - } - - sum += ReduceFunction()(start, end, std::forward(sub_slice), msgs, - std::forward(args)...); - } - return sum; + return ReduceFunction()(0, vmapped.size() - 1, std::forward(vmapped), + msgs, std::forward(args)...); #endif } diff --git a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp index a5392b52dec..ef54cc99836 100644 --- a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp @@ -101,6 +101,7 @@ TEST(MathMix_reduce_sum, eigen_three_args_with_doubles3) { arg1, arg2, arg3); } +#ifdef STAN_THREADS TEST(MathMix_reduce_sum, static_check) { tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; @@ -130,3 +131,4 @@ TEST(MathMix_reduce_sum, static_check) { stan::test::expect_ad(fi3, arg); } } +#endif From 4c68c631cf5eda19f9f37cfcc4c4a14766402097 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 6 Apr 2020 16:15:41 -0400 Subject: [PATCH 32/41] Responding to code review (Issue #1815) --- stan/math/fwd/functor/reduce_sum.hpp | 4 +- stan/math/prim/functor/reduce_sum.hpp | 6 +-- stan/math/prim/functor/reduce_sum_static.hpp | 4 +- stan/math/rev/functor/reduce_sum.hpp | 44 +++++++++---------- .../mix/functor/reduce_sum_part4_test.cpp | 1 - .../math/prim/functor/reduce_sum_test.cpp | 5 --- .../math/prim/functor/reduce_sum_util.hpp | 8 +--- .../unit/math/rev/functor/reduce_sum_test.cpp | 9 ---- 8 files changed, 27 insertions(+), 54 deletions(-) diff --git a/stan/math/fwd/functor/reduce_sum.hpp b/stan/math/fwd/functor/reduce_sum.hpp index 792a164c390..1d1400acf1d 100644 --- a/stan/math/fwd/functor/reduce_sum.hpp +++ b/stan/math/fwd/functor/reduce_sum.hpp @@ -63,9 +63,7 @@ struct reduce_sum_impl { return_type_t operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, std::ostream* msgs, Args&&... args) const { - const std::size_t num_terms = vmapped.size(); - - if (num_terms == 0) { + if (vmapped.empty()) { return 0.0; } diff --git a/stan/math/prim/functor/reduce_sum.hpp b/stan/math/prim/functor/reduce_sum.hpp index ab7ddd53632..8342b65ce9d 100644 --- a/stan/math/prim/functor/reduce_sum.hpp +++ b/stan/math/prim/functor/reduce_sum.hpp @@ -144,7 +144,7 @@ struct reduce_sum_impl, ReturnType operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, std::ostream* msgs, Args&&... args) const { const std::size_t num_terms = vmapped.size(); - if (num_terms == 0) { + if (vmapped.empty()) { return 0.0; } recursive_reducer worker(std::forward(vmapped), msgs, @@ -204,9 +204,7 @@ auto reduce_sum(Vec&& vmapped, int grainsize, std::ostream* msgs, grainsize, msgs, std::forward(args)...); #else - const std::size_t num_terms = vmapped.size(); - - if (num_terms == 0) { + if (vmapped.empty()) { return return_type(0.0); } diff --git a/stan/math/prim/functor/reduce_sum_static.hpp b/stan/math/prim/functor/reduce_sum_static.hpp index 69b52d1b309..0411a998b86 100644 --- a/stan/math/prim/functor/reduce_sum_static.hpp +++ b/stan/math/prim/functor/reduce_sum_static.hpp @@ -54,9 +54,7 @@ auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs, grainsize, msgs, std::forward(args)...); #else - const std::size_t num_terms = vmapped.size(); - - if (num_terms == 0) { + if (vmapped.empty()) { return return_type(0); } diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index f78ee50967c..d573ec4fa4c 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -37,8 +37,8 @@ struct reduce_sum_impl, ReturnType, * @note see link [here](https://tinyurl.com/vp7xw2t) for requirements. */ struct recursive_reducer { - size_t vars_per_term_; - size_t num_shared_terms_; // Number of terms shared across threads + const size_t num_vars_per_term_; + const size_t num_vars_shared_terms_; // Number of vars in shared arguments double* sliced_partials_; // Points to adjoints of the partial calculations Vec vmapped_; std::ostream* msgs_; @@ -47,11 +47,11 @@ struct reduce_sum_impl, ReturnType, Eigen::VectorXd args_adjoints_{0}; template - recursive_reducer(size_t vars_per_term, size_t num_shared_terms, + recursive_reducer(size_t num_vars_per_term, size_t num_vars_shared_terms, double* sliced_partials, VecT&& vmapped, std::ostream* msgs, ArgsT&&... args) - : vars_per_term_(vars_per_term), - num_shared_terms_(num_shared_terms), + : num_vars_per_term_(num_vars_per_term), + num_vars_shared_terms_(num_vars_shared_terms), sliced_partials_(sliced_partials), vmapped_(std::forward(vmapped)), msgs_(msgs), @@ -64,8 +64,8 @@ struct reduce_sum_impl, ReturnType, * an independent partial sum. */ recursive_reducer(recursive_reducer& other, tbb::split) - : vars_per_term_(other.vars_per_term_), - num_shared_terms_(other.num_shared_terms_), + : num_vars_per_term_(other.num_vars_per_term_), + num_vars_shared_terms_(other.num_vars_shared_terms_), sliced_partials_(other.sliced_partials_), vmapped_(other.vmapped_), msgs_(other.msgs_), @@ -80,7 +80,7 @@ struct reduce_sum_impl, ReturnType, * operands which reside potentially on a autodiff tape stored in a * different thread other than the current thread of execution. This * function may be called multiple times per object instantiation (so the - * sum_ and args_adjoints_ must be accumulated, notjust assigned). + * sum_ and args_adjoints_ must be accumulated, not just assigned). * * @param r Range over which to compute reduce_sum */ @@ -90,7 +90,7 @@ struct reduce_sum_impl, ReturnType, } if (args_adjoints_.size() == 0) { - args_adjoints_ = Eigen::VectorXd::Zero(num_shared_terms_); + args_adjoints_ = Eigen::VectorXd::Zero(num_vars_shared_terms_); } // Initialize nested autodiff stack @@ -128,7 +128,7 @@ struct reduce_sum_impl, ReturnType, sum_ += sub_sum_v.val(); // Accumulate adjoints of sliced_arguments - accumulate_adjoints(sliced_partials_ + r.begin() * vars_per_term_, + accumulate_adjoints(sliced_partials_ + r.begin() * num_vars_per_term_, local_sub_slice); // Accumulate adjoints of shared_arguments @@ -200,23 +200,23 @@ struct reduce_sum_impl, ReturnType, std::ostream* msgs, Args&&... args) const { const std::size_t num_terms = vmapped.size(); - if (num_terms == 0) { + if (vmapped.empty()) { return var(0.0); } - const std::size_t vars_per_term = count_vars(vmapped[0]); - const std::size_t num_sliced_terms = num_terms * vars_per_term; - const std::size_t num_shared_terms = count_vars(args...); + const std::size_t num_vars_per_term = count_vars(vmapped[0]); + const std::size_t num_vars_sliced_terms = num_terms * num_vars_per_term; + const std::size_t num_vars_shared_terms = count_vars(args...); vari** varis = ChainableStack::instance_->memalloc_.alloc_array( - num_sliced_terms + num_shared_terms); + num_vars_sliced_terms + num_vars_shared_terms); double* partials = ChainableStack::instance_->memalloc_.alloc_array( - num_sliced_terms + num_shared_terms); + num_vars_sliced_terms + num_vars_shared_terms); - for (size_t i = 0; i < num_sliced_terms; ++i) { + for (size_t i = 0; i < num_vars_sliced_terms; ++i) { partials[i] = 0.0; } - recursive_reducer worker(vars_per_term, num_shared_terms, partials, vmapped, + recursive_reducer worker(num_vars_per_term, num_vars_shared_terms, partials, vmapped, msgs, args...); if (auto_partitioning) { @@ -230,14 +230,14 @@ struct reduce_sum_impl, ReturnType, } save_varis(varis, std::forward(vmapped)); - save_varis(varis + num_sliced_terms, std::forward(args)...); + save_varis(varis + num_vars_sliced_terms, std::forward(args)...); - for (size_t i = 0; i < num_shared_terms; ++i) { - partials[num_sliced_terms + i] = worker.args_adjoints_(i); + for (size_t i = 0; i < num_vars_shared_terms; ++i) { + partials[num_vars_sliced_terms + i] = worker.args_adjoints_(i); } return var(new precomputed_gradients_vari( - worker.sum_, num_sliced_terms + num_shared_terms, varis, partials)); + worker.sum_, num_vars_sliced_terms + num_vars_shared_terms, varis, partials)); } }; } // namespace internal diff --git a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp index ef54cc99836..e2cfead9893 100644 --- a/test/unit/math/mix/functor/reduce_sum_part4_test.cpp +++ b/test/unit/math/mix/functor/reduce_sum_part4_test.cpp @@ -103,7 +103,6 @@ TEST(MathMix_reduce_sum, eigen_three_args_with_doubles3) { #ifdef STAN_THREADS TEST(MathMix_reduce_sum, static_check) { - tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::static_check_lpdf; diff --git a/test/unit/math/prim/functor/reduce_sum_test.cpp b/test/unit/math/prim/functor/reduce_sum_test.cpp index 5d85ce9b02b..463f29a38a9 100644 --- a/test/unit/math/prim/functor/reduce_sum_test.cpp +++ b/test/unit/math/prim/functor/reduce_sum_test.cpp @@ -6,7 +6,6 @@ #include TEST(StanMathPrim_reduce_sum, value) { - tbb::task_scheduler_init default_scheduler; using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; double lambda_d = 10.0; @@ -33,7 +32,6 @@ TEST(StanMathPrim_reduce_sum, value) { } TEST(StanMathPrim_reduce_sum, grainsize) { - tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; @@ -63,7 +61,6 @@ TEST(StanMathPrim_reduce_sum, grainsize) { } TEST(StanMathPrim_reduce_sum, start_end_slice) { - tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::start_end_lpdf; @@ -130,7 +127,6 @@ TEST(StanMathPrim_reduce_sum, std_vector_std_vector_eigen_matrix_slice) { } TEST(StanMathPrim_reduce_sum, no_args) { - tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; @@ -250,7 +246,6 @@ struct threading_test_lpdf { }; TEST(StanMathPrim_reduce_sum, threading) { - tbb::task_scheduler_init default_scheduler; threading_test_global = std::vector(10000, 0); stan::math::reduce_sum_static(threading_test_global, 1, nullptr); diff --git a/test/unit/math/prim/functor/reduce_sum_util.hpp b/test/unit/math/prim/functor/reduce_sum_util.hpp index 06fa0fa4f93..231f7e32d8b 100644 --- a/test/unit/math/prim/functor/reduce_sum_util.hpp +++ b/test/unit/math/prim/functor/reduce_sum_util.hpp @@ -15,8 +15,7 @@ std::ostream* get_new_msg() { std::ostream* msgs = nullptr; return msgs; } -// reduce functor which is the BinaryFunction -// here we use iterators which represent integer indices + template struct count_lpdf { count_lpdf() {} @@ -125,8 +124,6 @@ struct grouped_count_lpdf { inline T operator()(std::size_t start, std::size_t end, VecInt1&& sub_slice, std::ostream* msgs, VecT&& lambda, VecInt2&& gidx) const { const std::size_t num_terms = end - start + 1; - // std::cout << "sub-slice " << start << " - " << end << "; num_terms = " << - // num_terms << "; size = " << sub_slice.size() << std::endl; std::decay_t lambda_slice(num_terms); for (std::size_t i = 0; i != num_terms; ++i) lambda_slice[i] = lambda[gidx[start + i]]; @@ -137,7 +134,6 @@ struct grouped_count_lpdf { template void test_slices(T1 result, T2&& vec_value, Args&&... args) { - tbb::task_scheduler_init default_scheduler; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; @@ -176,8 +172,6 @@ struct static_check_lpdf { const std::vector&, std::ostream* msgs, const std::vector& data) const { T sum = 0; - // std::cout << "start: " << start << ", end: " << end << ", grainsize: " << - // grainsize << std::endl; EXPECT_LE(end - start + 1, grainsize); for (size_t i = start; i <= end; i++) { sum += data[i]; diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 372cc302e73..b2f0212489e 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -8,7 +8,6 @@ #include TEST(StanMathRev_reduce_sum, no_args) { - tbb::task_scheduler_init default_scheduler; using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::sum_lpdf; @@ -26,7 +25,6 @@ TEST(StanMathRev_reduce_sum, no_args) { TEST(StanMathRev_reduce_sum, value) { using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; - tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t elems = 10000; std::vector data(elems); @@ -57,7 +55,6 @@ TEST(StanMathRev_reduce_sum, gradient) { using stan::math::var; using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; - tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t elems = 10000; @@ -106,7 +103,6 @@ TEST(StanMathRev_reduce_sum, grainsize) { using stan::math::var; using stan::math::test::count_lpdf; using stan::math::test::get_new_msg; - tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t elems = 10000; @@ -155,7 +151,6 @@ TEST(StanMathRev_reduce_sum, nesting_gradient) { using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::nesting_count_lpdf; - tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t elems = 10000; @@ -206,7 +201,6 @@ TEST(StanMathRev_reduce_sum, grouped_gradient) { using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::grouped_count_lpdf; - tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t groups = 10; @@ -268,7 +262,6 @@ TEST(StanMathRev_reduce_sum, grouped_gradient_eigen) { using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::grouped_count_lpdf; - tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t groups = 10; @@ -332,7 +325,6 @@ TEST(StanMathRev_reduce_sum, slice_group_gradient) { using stan::math::var; using stan::math::test::get_new_msg; using stan::math::test::slice_group_count_lpdf; - tbb::task_scheduler_init default_scheduler; double lambda_d = 10.0; const std::size_t groups = 10; @@ -411,7 +403,6 @@ struct threading_test_lpdf { }; TEST(StanMathRev_reduce_sum, threading) { - tbb::task_scheduler_init default_scheduler; threading_test_global = std::vector(10000, 0); std::vector data(threading_test_global.size(), 0); stan::math::reduce_sum_static(data, 1, nullptr); From e10b6262fcbc96c8039bfbcfe1057ab8a234d7f7 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 6 Apr 2020 16:17:08 -0400 Subject: [PATCH 33/41] [Jenkins] auto-formatting by clang-format version 6.0.0 --- stan/math/rev/functor/reduce_sum.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index d573ec4fa4c..01ba5f8d264 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -216,8 +216,8 @@ struct reduce_sum_impl, ReturnType, for (size_t i = 0; i < num_vars_sliced_terms; ++i) { partials[i] = 0.0; } - recursive_reducer worker(num_vars_per_term, num_vars_shared_terms, partials, vmapped, - msgs, args...); + recursive_reducer worker(num_vars_per_term, num_vars_shared_terms, partials, + vmapped, msgs, args...); if (auto_partitioning) { tbb::parallel_reduce( @@ -237,7 +237,8 @@ struct reduce_sum_impl, ReturnType, } return var(new precomputed_gradients_vari( - worker.sum_, num_vars_sliced_terms + num_vars_shared_terms, varis, partials)); + worker.sum_, num_vars_sliced_terms + num_vars_shared_terms, varis, + partials)); } }; } // namespace internal From 7f767600f6f2d6e52e9f159f03a5303f5d9d2ac8 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 7 Apr 2020 07:38:01 -0400 Subject: [PATCH 34/41] Added std::forward calls to rev version of reduce_sum (Issue #1815) --- stan/math/rev/functor/reduce_sum.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index 01ba5f8d264..c9b2dfcaed9 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -4,10 +4,12 @@ #include #include #include + #include #include #include +#include #include #include @@ -217,7 +219,7 @@ struct reduce_sum_impl, ReturnType, partials[i] = 0.0; } recursive_reducer worker(num_vars_per_term, num_vars_shared_terms, partials, - vmapped, msgs, args...); + std::forward(vmapped), msgs, std::forward(args)...); if (auto_partitioning) { tbb::parallel_reduce( From 2658e80e1891ed1a5800b1306bc681487b5477d0 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 7 Apr 2020 07:40:02 -0400 Subject: [PATCH 35/41] [Jenkins] auto-formatting by clang-format version 6.0.0 --- stan/math/rev/functor/reduce_sum.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index c9b2dfcaed9..fe38b54cb1c 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -219,7 +219,8 @@ struct reduce_sum_impl, ReturnType, partials[i] = 0.0; } recursive_reducer worker(num_vars_per_term, num_vars_shared_terms, partials, - std::forward(vmapped), msgs, std::forward(args)...); + std::forward(vmapped), msgs, + std::forward(args)...); if (auto_partitioning) { tbb::parallel_reduce( From 7f893d5da45f3cb45e65a4078ec50c04d43e0ea8 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 7 Apr 2020 07:41:23 -0400 Subject: [PATCH 36/41] Revert "Added std::forward calls to rev version of reduce_sum (Issue #1815)" This reverts commit 7f767600f6f2d6e52e9f159f03a5303f5d9d2ac8. --- stan/math/rev/functor/reduce_sum.hpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index c9b2dfcaed9..01ba5f8d264 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -4,12 +4,10 @@ #include #include #include - #include #include #include -#include #include #include @@ -219,7 +217,7 @@ struct reduce_sum_impl, ReturnType, partials[i] = 0.0; } recursive_reducer worker(num_vars_per_term, num_vars_shared_terms, partials, - std::forward(vmapped), msgs, std::forward(args)...); + vmapped, msgs, args...); if (auto_partitioning) { tbb::parallel_reduce( From ccee1f1faa58c5e0f503e243501839020ce75a04 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 7 Apr 2020 08:03:18 -0400 Subject: [PATCH 37/41] Added std::forward calls to rev version of reduce_sum again (Issue #1815) --- stan/math/rev/functor/reduce_sum.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index 01ba5f8d264..2d61d855bee 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -213,11 +213,16 @@ struct reduce_sum_impl, ReturnType, double* partials = ChainableStack::instance_->memalloc_.alloc_array( num_vars_sliced_terms + num_vars_shared_terms); + save_varis(varis, vmapped); + save_varis(varis + num_vars_sliced_terms, args...); + for (size_t i = 0; i < num_vars_sliced_terms; ++i) { partials[i] = 0.0; } + recursive_reducer worker(num_vars_per_term, num_vars_shared_terms, partials, - vmapped, msgs, args...); + std::forward(vmapped), msgs, + std::forward(args)...); if (auto_partitioning) { tbb::parallel_reduce( @@ -229,9 +234,6 @@ struct reduce_sum_impl, ReturnType, partitioner); } - save_varis(varis, std::forward(vmapped)); - save_varis(varis + num_vars_sliced_terms, std::forward(args)...); - for (size_t i = 0; i < num_vars_shared_terms; ++i) { partials[num_vars_sliced_terms + i] = worker.args_adjoints_(i); } From 5bd7f914fb69623b1a79399b7fdf23d101609c9d Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 7 Apr 2020 08:04:27 -0400 Subject: [PATCH 38/41] [Jenkins] auto-formatting by clang-format version 6.0.0 --- stan/math/rev/functor/reduce_sum.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index 2d61d855bee..352830301f1 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -222,7 +222,7 @@ struct reduce_sum_impl, ReturnType, recursive_reducer worker(num_vars_per_term, num_vars_shared_terms, partials, std::forward(vmapped), msgs, - std::forward(args)...); + std::forward(args)...); if (auto_partitioning) { tbb::parallel_reduce( From ad02fdf2ff2f9a6efba5867c055172ff1ba3d496 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 7 Apr 2020 18:56:51 -0400 Subject: [PATCH 39/41] Add STAN_NUM_THREADS to jenkinsfile, inline reduce_sum operators --- Jenkinsfile | 16 +++++++++------- stan/math/fwd/functor/reduce_sum.hpp | 5 ++++- stan/math/prim/functor/reduce_sum.hpp | 8 ++++---- stan/math/rev/functor/reduce_sum.hpp | 4 ++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index cb3217f4025..df08b5a98f1 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -150,7 +150,7 @@ pipeline { stage('Verify changes') { agent { label 'linux' } steps { - script { + script { retry(3) { checkout scm } sh 'git clean -xffd' @@ -181,7 +181,7 @@ pipeline { deleteDir() unstash 'MathSetup' sh "echo CXX=${MPICXX} >> make/local" - sh "echo CXX_TYPE=gcc >> make/local" + sh "echo CXX_TYPE=gcc >> make/local" sh "echo STAN_MPI=true >> make/local" runTests("test/unit") } @@ -242,6 +242,7 @@ pipeline { unstash 'MathSetup' sh "echo CXX=${env.CXX} -Werror > make/local" sh "echo CPPFLAGS+=-DSTAN_THREADS >> make/local" + sh "export STAN_NUM_THREADS=4" runTests("test/unit -f thread") sh "find . -name *_test.xml | xargs rm" runTests("test/unit -f map_rect") @@ -265,6 +266,7 @@ pipeline { unstash 'MathSetup' bat "echo CXX=${env.CXX} -Werror > make/local" bat "echo CXXFLAGS+=-DSTAN_THREADS >> make/local" + sh "export STAN_NUM_THREADS=4" runTestsWin("test/unit -f thread") runTestsWin("test/unit -f map_rect") } @@ -272,7 +274,7 @@ pipeline { } } stage('Additional merge tests') { - when { + when { allOf { anyOf { branch 'develop' @@ -309,12 +311,12 @@ pipeline { } } stage('Upstream tests') { - when { + when { allOf { - expression { - env.BRANCH_NAME ==~ /PR-\d+/ + expression { + env.BRANCH_NAME ==~ /PR-\d+/ } - expression { + expression { !skipRemainingStages } } diff --git a/stan/math/fwd/functor/reduce_sum.hpp b/stan/math/fwd/functor/reduce_sum.hpp index 1d1400acf1d..013f3bb9c1b 100644 --- a/stan/math/fwd/functor/reduce_sum.hpp +++ b/stan/math/fwd/functor/reduce_sum.hpp @@ -16,6 +16,9 @@ namespace stan { namespace math { namespace internal { + template + struct reduce_sum_impl; /** * reduce_sum_impl implementation for any autodiff type. @@ -60,7 +63,7 @@ struct reduce_sum_impl { * @param args Shared arguments used in every sum term * @return Summation of all terms */ - return_type_t operator()(Vec&& vmapped, bool auto_partitioning, + inline return_type_t operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, std::ostream* msgs, Args&&... args) const { if (vmapped.empty()) { diff --git a/stan/math/prim/functor/reduce_sum.hpp b/stan/math/prim/functor/reduce_sum.hpp index 8342b65ce9d..daf172452da 100644 --- a/stan/math/prim/functor/reduce_sum.hpp +++ b/stan/math/prim/functor/reduce_sum.hpp @@ -72,7 +72,7 @@ struct reduce_sum_impl, * * @param r Range over which to compute `ReduceFunction` */ - void operator()(const tbb::blocked_range& r) { + inline void operator()(const tbb::blocked_range& r) { if (r.empty()) { return; } @@ -96,7 +96,7 @@ struct reduce_sum_impl, * * @param rhs Another partial sum */ - void join(const recursive_reducer& child) { sum_ += child.sum_; } + inline void join(const recursive_reducer& child) { sum_ += child.sum_; } }; /** @@ -141,7 +141,7 @@ struct reduce_sum_impl, * @param args Shared arguments used in every sum term * @return Summation of all terms */ - ReturnType operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, + inline ReturnType operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, std::ostream* msgs, Args&&... args) const { const std::size_t num_terms = vmapped.size(); if (vmapped.empty()) { @@ -192,7 +192,7 @@ struct reduce_sum_impl, */ template , typename... Args> -auto reduce_sum(Vec&& vmapped, int grainsize, std::ostream* msgs, +inline auto reduce_sum(Vec&& vmapped, int grainsize, std::ostream* msgs, Args&&... args) { using return_type = return_type_t; diff --git a/stan/math/rev/functor/reduce_sum.hpp b/stan/math/rev/functor/reduce_sum.hpp index 352830301f1..4f0d3300fd2 100644 --- a/stan/math/rev/functor/reduce_sum.hpp +++ b/stan/math/rev/functor/reduce_sum.hpp @@ -129,7 +129,7 @@ struct reduce_sum_impl, ReturnType, // Accumulate adjoints of sliced_arguments accumulate_adjoints(sliced_partials_ + r.begin() * num_vars_per_term_, - local_sub_slice); + std::move(local_sub_slice)); // Accumulate adjoints of shared_arguments apply( @@ -146,7 +146,7 @@ struct reduce_sum_impl, ReturnType, * * @param rhs Another partial sum */ - void join(const recursive_reducer& rhs) { + inline void join(const recursive_reducer& rhs) { sum_ += rhs.sum_; if (args_adjoints_.size() != 0 && rhs.args_adjoints_.size() != 0) { args_adjoints_ += rhs.args_adjoints_; From dcfffc508ef190ec667185d734e9fa045a1c2914 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 7 Apr 2020 18:57:53 -0400 Subject: [PATCH 40/41] [Jenkins] auto-formatting by clang-format version 6.0.0 --- stan/math/fwd/functor/reduce_sum.hpp | 14 ++++++++------ stan/math/prim/functor/reduce_sum.hpp | 7 ++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/stan/math/fwd/functor/reduce_sum.hpp b/stan/math/fwd/functor/reduce_sum.hpp index 013f3bb9c1b..4ebf14fa245 100644 --- a/stan/math/fwd/functor/reduce_sum.hpp +++ b/stan/math/fwd/functor/reduce_sum.hpp @@ -16,9 +16,9 @@ namespace stan { namespace math { namespace internal { - template - struct reduce_sum_impl; +template +struct reduce_sum_impl; /** * reduce_sum_impl implementation for any autodiff type. @@ -63,9 +63,11 @@ struct reduce_sum_impl { * @param args Shared arguments used in every sum term * @return Summation of all terms */ - inline return_type_t operator()(Vec&& vmapped, bool auto_partitioning, - int grainsize, std::ostream* msgs, - Args&&... args) const { + inline return_type_t operator()(Vec&& vmapped, + bool auto_partitioning, + int grainsize, + std::ostream* msgs, + Args&&... args) const { if (vmapped.empty()) { return 0.0; } diff --git a/stan/math/prim/functor/reduce_sum.hpp b/stan/math/prim/functor/reduce_sum.hpp index daf172452da..3acd5ddd2d0 100644 --- a/stan/math/prim/functor/reduce_sum.hpp +++ b/stan/math/prim/functor/reduce_sum.hpp @@ -141,8 +141,9 @@ struct reduce_sum_impl, * @param args Shared arguments used in every sum term * @return Summation of all terms */ - inline ReturnType operator()(Vec&& vmapped, bool auto_partitioning, int grainsize, - std::ostream* msgs, Args&&... args) const { + inline ReturnType operator()(Vec&& vmapped, bool auto_partitioning, + int grainsize, std::ostream* msgs, + Args&&... args) const { const std::size_t num_terms = vmapped.size(); if (vmapped.empty()) { return 0.0; @@ -193,7 +194,7 @@ struct reduce_sum_impl, template , typename... Args> inline auto reduce_sum(Vec&& vmapped, int grainsize, std::ostream* msgs, - Args&&... args) { + Args&&... args) { using return_type = return_type_t; check_positive("reduce_sum", "grainsize", grainsize); From 2ea9a7bd97b16aed8c72a3d22bfee83a17d8c080 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 7 Apr 2020 18:58:04 -0400 Subject: [PATCH 41/41] remove export from windows threading test --- Jenkinsfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index df08b5a98f1..b039a14c314 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -266,7 +266,6 @@ pipeline { unstash 'MathSetup' bat "echo CXX=${env.CXX} -Werror > make/local" bat "echo CXXFLAGS+=-DSTAN_THREADS >> make/local" - sh "export STAN_NUM_THREADS=4" runTestsWin("test/unit -f thread") runTestsWin("test/unit -f map_rect") }