From 69a53c48c8476e7d89cd95e67d27241707d8c9bd Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Mon, 17 Jun 2024 12:00:27 -0400 Subject: [PATCH 1/7] Consistently use 'long int' for return of size functions --- stan/math/prim/fun/cols.hpp | 2 +- stan/math/prim/fun/num_elements.hpp | 6 +++--- stan/math/prim/fun/rows.hpp | 2 +- stan/math/prim/fun/size.hpp | 9 ++++----- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/stan/math/prim/fun/cols.hpp b/stan/math/prim/fun/cols.hpp index 60cde07830e..20bd3f3f3f2 100644 --- a/stan/math/prim/fun/cols.hpp +++ b/stan/math/prim/fun/cols.hpp @@ -16,7 +16,7 @@ namespace math { * @return Number of columns. */ template * = nullptr> -inline Eigen::Index cols(const T& m) { +inline long int cols(const T& m) { return m.cols(); } diff --git a/stan/math/prim/fun/num_elements.hpp b/stan/math/prim/fun/num_elements.hpp index 27bcadd1b62..ba9b2c7a01e 100644 --- a/stan/math/prim/fun/num_elements.hpp +++ b/stan/math/prim/fun/num_elements.hpp @@ -16,7 +16,7 @@ namespace math { * @return 1 */ template * = nullptr> -inline int num_elements(const T& x) { +inline long int num_elements(const T& x) { return 1; } @@ -29,7 +29,7 @@ inline int num_elements(const T& x) { * @return size of matrix */ template * = nullptr> -inline int num_elements(const T& m) { +inline long int num_elements(const T& m) { return m.size(); } @@ -43,7 +43,7 @@ inline int num_elements(const T& m) { * @return number of contained arguments */ template -inline int num_elements(const std::vector& v) { +inline long int num_elements(const std::vector& v) { if (v.size() == 0) { return 0; } diff --git a/stan/math/prim/fun/rows.hpp b/stan/math/prim/fun/rows.hpp index b42d3fc6ae0..9d0a1366991 100644 --- a/stan/math/prim/fun/rows.hpp +++ b/stan/math/prim/fun/rows.hpp @@ -16,7 +16,7 @@ namespace math { * @return Number of rows. */ template * = nullptr> -inline int rows(const T& m) { +inline long int rows(const T& m) { return m.rows(); } diff --git a/stan/math/prim/fun/size.hpp b/stan/math/prim/fun/size.hpp index b5540638290..5e6f70dc116 100644 --- a/stan/math/prim/fun/size.hpp +++ b/stan/math/prim/fun/size.hpp @@ -3,7 +3,6 @@ #include #include -#include #include namespace stan { @@ -14,8 +13,8 @@ namespace math { * that are always of length 1. */ template * = nullptr> -inline size_t size(const T& /*x*/) { - return 1U; +inline long int size(const T& /*x*/) { + return 1; } /** \ingroup type_trait @@ -25,12 +24,12 @@ inline size_t size(const T& /*x*/) { * @tparam T type of m */ template * = nullptr> -inline size_t size(const T& m) { +inline long int size(const T& m) { return m.size(); } template * = nullptr> -inline size_t size(const T& m) { +inline long int size(const T& m) { return m.size(); } From bd85f663ff1a0e256e1ca359f557622ac8c906b9 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Mon, 17 Jun 2024 18:05:29 -0400 Subject: [PATCH 2/7] Use int64_t --- stan/math/prim/fun/cols.hpp | 3 ++- stan/math/prim/fun/num_elements.hpp | 7 ++++--- stan/math/prim/fun/rows.hpp | 3 ++- stan/math/prim/fun/size.hpp | 7 ++++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/stan/math/prim/fun/cols.hpp b/stan/math/prim/fun/cols.hpp index 20bd3f3f3f2..07a24d5867a 100644 --- a/stan/math/prim/fun/cols.hpp +++ b/stan/math/prim/fun/cols.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace stan { namespace math { @@ -16,7 +17,7 @@ namespace math { * @return Number of columns. */ template * = nullptr> -inline long int cols(const T& m) { +inline int64_t cols(const T& m) { return m.cols(); } diff --git a/stan/math/prim/fun/num_elements.hpp b/stan/math/prim/fun/num_elements.hpp index ba9b2c7a01e..574d2739fa1 100644 --- a/stan/math/prim/fun/num_elements.hpp +++ b/stan/math/prim/fun/num_elements.hpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace stan { @@ -16,7 +17,7 @@ namespace math { * @return 1 */ template * = nullptr> -inline long int num_elements(const T& x) { +inline int64_t num_elements(const T& x) { return 1; } @@ -29,7 +30,7 @@ inline long int num_elements(const T& x) { * @return size of matrix */ template * = nullptr> -inline long int num_elements(const T& m) { +inline int64_t num_elements(const T& m) { return m.size(); } @@ -43,7 +44,7 @@ inline long int num_elements(const T& m) { * @return number of contained arguments */ template -inline long int num_elements(const std::vector& v) { +inline int64_t num_elements(const std::vector& v) { if (v.size() == 0) { return 0; } diff --git a/stan/math/prim/fun/rows.hpp b/stan/math/prim/fun/rows.hpp index 9d0a1366991..b6e5f9925fe 100644 --- a/stan/math/prim/fun/rows.hpp +++ b/stan/math/prim/fun/rows.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace stan { namespace math { @@ -16,7 +17,7 @@ namespace math { * @return Number of rows. */ template * = nullptr> -inline long int rows(const T& m) { +inline int64_t rows(const T& m) { return m.rows(); } diff --git a/stan/math/prim/fun/size.hpp b/stan/math/prim/fun/size.hpp index 5e6f70dc116..a9005d97b39 100644 --- a/stan/math/prim/fun/size.hpp +++ b/stan/math/prim/fun/size.hpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace stan { @@ -13,7 +14,7 @@ namespace math { * that are always of length 1. */ template * = nullptr> -inline long int size(const T& /*x*/) { +inline int64_t size(const T& /*x*/) { return 1; } @@ -24,12 +25,12 @@ inline long int size(const T& /*x*/) { * @tparam T type of m */ template * = nullptr> -inline long int size(const T& m) { +inline int64_t size(const T& m) { return m.size(); } template * = nullptr> -inline long int size(const T& m) { +inline int64_t size(const T& m) { return m.size(); } From 6fbb3a450d1b62688568fd782262e5af43d5c7bb Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Tue, 18 Jun 2024 09:23:42 -0400 Subject: [PATCH 3/7] Missed signatures --- stan/math/opencl/prim/num_elements.hpp | 3 ++- stan/math/opencl/prim/size.hpp | 3 ++- stan/math/prim/fun/max_size.hpp | 3 ++- stan/math/prim/fun/max_size_mvt.hpp | 3 ++- stan/math/prim/fun/size_mvt.hpp | 9 +++++---- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/stan/math/opencl/prim/num_elements.hpp b/stan/math/opencl/prim/num_elements.hpp index aedae05f616..71f73d84d29 100644 --- a/stan/math/opencl/prim/num_elements.hpp +++ b/stan/math/opencl/prim/num_elements.hpp @@ -4,6 +4,7 @@ #include #include +#include namespace stan { namespace math { @@ -16,7 +17,7 @@ namespace math { */ template * = nullptr> -size_t num_elements(const T& m) { +int64_t num_elements(const T& m) { return math::size(m); } diff --git a/stan/math/opencl/prim/size.hpp b/stan/math/opencl/prim/size.hpp index 37d037a2943..f7b78a128b1 100644 --- a/stan/math/opencl/prim/size.hpp +++ b/stan/math/opencl/prim/size.hpp @@ -3,6 +3,7 @@ #ifdef STAN_OPENCL #include +#include namespace stan { namespace math { @@ -15,7 +16,7 @@ namespace math { */ template * = nullptr> -size_t size(const T& m) { +int64_t size(const T& m) { return m.rows() * m.cols(); } diff --git a/stan/math/prim/fun/max_size.hpp b/stan/math/prim/fun/max_size.hpp index 9b0c8f2af8f..bbbcfefc7b3 100644 --- a/stan/math/prim/fun/max_size.hpp +++ b/stan/math/prim/fun/max_size.hpp @@ -2,6 +2,7 @@ #define STAN_MATH_PRIM_FUN_MAX_SIZE_HPP #include +#include #include namespace stan { @@ -16,7 +17,7 @@ namespace math { * @return the size of the largest input */ template -inline size_t max_size(const T1& x1, const Ts&... xs) { +inline int64_t max_size(const T1& x1, const Ts&... xs) { return std::max({stan::math::size(x1), stan::math::size(xs)...}); } diff --git a/stan/math/prim/fun/max_size_mvt.hpp b/stan/math/prim/fun/max_size_mvt.hpp index 72e45f76ea8..3cd661a9a9e 100644 --- a/stan/math/prim/fun/max_size_mvt.hpp +++ b/stan/math/prim/fun/max_size_mvt.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace stan { namespace math { @@ -21,7 +22,7 @@ namespace math { * matrix or std::vector of Eigen matrices */ template -inline size_t max_size_mvt(const T1& x1, const Ts&... xs) { +inline int64_t max_size_mvt(const T1& x1, const Ts&... xs) { return std::max({stan::math::size_mvt(x1), stan::math::size_mvt(xs)...}); } diff --git a/stan/math/prim/fun/size_mvt.hpp b/stan/math/prim/fun/size_mvt.hpp index 6543412964f..b413388c711 100644 --- a/stan/math/prim/fun/size_mvt.hpp +++ b/stan/math/prim/fun/size_mvt.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -21,17 +22,17 @@ namespace math { * @throw std::invalid_argument since the type is a scalar. */ template * = nullptr> -size_t size_mvt(const ScalarT& /* unused */) { +int64_t size_mvt(const ScalarT& /* unused */) { throw std::invalid_argument("size_mvt passed to an unrecognized type."); } template * = nullptr> -size_t size_mvt(const MatrixT& /* unused */) { - return 1U; +int64_t size_mvt(const MatrixT& /* unused */) { + return 1; } template * = nullptr> -size_t size_mvt(const std::vector& x) { +int64_t size_mvt(const std::vector& x) { return x.size(); } From 66ab23f3cb0c4ad83fc8a45b5d268b6712445b8e Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Tue, 18 Jun 2024 09:43:01 -0400 Subject: [PATCH 4/7] Missed opencl overloads --- stan/math/opencl/prim/cols.hpp | 3 ++- stan/math/opencl/prim/rows.hpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/stan/math/opencl/prim/cols.hpp b/stan/math/opencl/prim/cols.hpp index d52e31b8e2c..1e4c934b4bd 100644 --- a/stan/math/opencl/prim/cols.hpp +++ b/stan/math/opencl/prim/cols.hpp @@ -3,6 +3,7 @@ #ifdef STAN_OPENCL #include +#include namespace stan { namespace math { @@ -17,7 +18,7 @@ namespace math { */ template * = nullptr> -inline int cols(const T_x& x) { +inline int64_t cols(const T_x& x) { return x.cols(); } } // namespace math diff --git a/stan/math/opencl/prim/rows.hpp b/stan/math/opencl/prim/rows.hpp index 23e7fabe133..a9984051d59 100644 --- a/stan/math/opencl/prim/rows.hpp +++ b/stan/math/opencl/prim/rows.hpp @@ -3,6 +3,7 @@ #ifdef STAN_OPENCL #include +#include namespace stan { namespace math { @@ -18,7 +19,7 @@ namespace math { template * = nullptr> -inline int rows(const T_x& x) { +inline int64_t rows(const T_x& x) { return x.rows(); } From c72e175fb99aaecdfda2a1c740c03d8ae6a3713d Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Tue, 18 Jun 2024 10:38:10 -0400 Subject: [PATCH 5/7] Fix test --- test/unit/math/opencl/util.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/math/opencl/util.hpp b/test/unit/math/opencl/util.hpp index 484185c4831..9170a8d848c 100644 --- a/test/unit/math/opencl/util.hpp +++ b/test/unit/math/opencl/util.hpp @@ -233,11 +233,11 @@ T to_vector_if(const T& x, std::size_t N) { using stan::math::rows; template * = nullptr> -int rows(const T&) { +int64_t rows(const T&) { return 1; } template * = nullptr> -int rows(const T& x) { +int64_t rows(const T& x) { return x.size(); } From 09b96d786b9eb17f307d060bcd92b1578185c38a Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Tue, 18 Jun 2024 11:08:51 -0400 Subject: [PATCH 6/7] Test fixes --- stan/math/prim/prob/poisson_binomial_cdf.hpp | 4 ++-- stan/math/prim/prob/poisson_binomial_lccdf.hpp | 4 ++-- stan/math/prim/prob/poisson_binomial_lcdf.hpp | 4 ++-- stan/math/prim/prob/poisson_binomial_lpmf.hpp | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/stan/math/prim/prob/poisson_binomial_cdf.hpp b/stan/math/prim/prob/poisson_binomial_cdf.hpp index 0c4f647e835..f47abf7472d 100644 --- a/stan/math/prim/prob/poisson_binomial_cdf.hpp +++ b/stan/math/prim/prob/poisson_binomial_cdf.hpp @@ -37,13 +37,13 @@ return_type_t poisson_binomial_cdf(const T_y& y, const T_theta& theta) { static constexpr const char* function = "poisson_binomial_cdf"; - size_t size_theta = size_mvt(theta); + auto size_theta = size_mvt(theta); if (size_theta > 1) { check_consistent_sizes(function, "Successes variables", y, "Probability parameters", theta); } - size_t max_sz = std::max(stan::math::size(y), size_theta); + auto max_sz = std::max(stan::math::size(y), size_theta); scalar_seq_view y_vec(y); vector_seq_view theta_vec(theta); diff --git a/stan/math/prim/prob/poisson_binomial_lccdf.hpp b/stan/math/prim/prob/poisson_binomial_lccdf.hpp index 2f07bdb60e6..8d946f1a4c6 100644 --- a/stan/math/prim/prob/poisson_binomial_lccdf.hpp +++ b/stan/math/prim/prob/poisson_binomial_lccdf.hpp @@ -38,13 +38,13 @@ return_type_t poisson_binomial_lccdf(const T_y& y, const T_theta& theta) { static constexpr const char* function = "poisson_binomial_lccdf"; - size_t size_theta = size_mvt(theta); + auto size_theta = size_mvt(theta); if (size_theta > 1) { check_consistent_sizes(function, "Successes variables", y, "Probability parameters", theta); } - size_t max_sz = std::max(stan::math::size(y), size_mvt(theta)); + auto max_sz = std::max(stan::math::size(y), size_mvt(theta)); scalar_seq_view y_vec(y); vector_seq_view theta_vec(theta); diff --git a/stan/math/prim/prob/poisson_binomial_lcdf.hpp b/stan/math/prim/prob/poisson_binomial_lcdf.hpp index de5e91a32ce..7d449aa0caa 100644 --- a/stan/math/prim/prob/poisson_binomial_lcdf.hpp +++ b/stan/math/prim/prob/poisson_binomial_lcdf.hpp @@ -37,13 +37,13 @@ return_type_t poisson_binomial_lcdf(const T_y& y, const T_theta& theta) { static constexpr const char* function = "poisson_binomial_lcdf"; - size_t size_theta = size_mvt(theta); + auto size_theta = size_mvt(theta); if (size_theta > 1) { check_consistent_sizes(function, "Successes variables", y, "Probability parameters", theta); } - size_t max_sz = std::max(stan::math::size(y), size_theta); + auto max_sz = std::max(stan::math::size(y), size_theta); scalar_seq_view y_vec(y); vector_seq_view theta_vec(theta); diff --git a/stan/math/prim/prob/poisson_binomial_lpmf.hpp b/stan/math/prim/prob/poisson_binomial_lpmf.hpp index ac8b8a80c46..709a709c756 100644 --- a/stan/math/prim/prob/poisson_binomial_lpmf.hpp +++ b/stan/math/prim/prob/poisson_binomial_lpmf.hpp @@ -29,13 +29,13 @@ return_type_t poisson_binomial_lpmf(const T_y& y, const T_theta& theta) { static constexpr const char* function = "poisson_binomial_lpmf"; - size_t size_theta = size_mvt(theta); + auto size_theta = size_mvt(theta); if (size_theta > 1) { check_consistent_sizes(function, "Successes variables", y, "Probability parameters", theta); } - size_t max_sz = std::max(stan::math::size(y), size_theta); + auto max_sz = std::max(stan::math::size(y), size_theta); scalar_seq_view y_vec(y); vector_seq_view theta_vec(theta); From 7a0038aa122c74ff315ff87f384d66cd9829d38a Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Tue, 18 Jun 2024 12:19:53 -0400 Subject: [PATCH 7/7] Missed cast --- stan/math/opencl/prim/binomial_logit_glm_lpmf.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stan/math/opencl/prim/binomial_logit_glm_lpmf.hpp b/stan/math/opencl/prim/binomial_logit_glm_lpmf.hpp index a94052b860d..db189db081c 100644 --- a/stan/math/opencl/prim/binomial_logit_glm_lpmf.hpp +++ b/stan/math/opencl/prim/binomial_logit_glm_lpmf.hpp @@ -20,6 +20,7 @@ #include #include +#include namespace stan { namespace math { @@ -37,7 +38,7 @@ return_type_t binomial_logit_glm_lpmf( constexpr bool is_alpha_vector = !is_stan_scalar::value; const size_t N_instances - = max(max_size(n, N, alpha), static_cast(x.rows())); + = max(max_size(n, N, alpha), static_cast(x.rows())); const size_t N_attributes = x.cols(); check_consistent_sizes(function, "Successes variable", n,