Skip to content

Commit

Permalink
Merge pull request #3086 from stan-dev/fix/3085-use-long-int-for-cont…
Browse files Browse the repository at this point in the history
…ainer-sizes
  • Loading branch information
WardBrian authored Jun 18, 2024
2 parents 44c91c9 + 7a0038a commit 4c626fd
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 31 deletions.
3 changes: 2 additions & 1 deletion stan/math/opencl/prim/binomial_logit_glm_lpmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <stan/math/prim/fun/value_of_rec.hpp>

#include <cmath>
#include <cstdint>

namespace stan {
namespace math {
Expand All @@ -37,7 +38,7 @@ return_type_t<T_x_cl, T_alpha_cl, T_beta_cl> binomial_logit_glm_lpmf(
constexpr bool is_alpha_vector = !is_stan_scalar<T_alpha_cl>::value;

const size_t N_instances
= max(max_size(n, N, alpha), static_cast<size_t>(x.rows()));
= max(max_size(n, N, alpha), static_cast<int64_t>(x.rows()));
const size_t N_attributes = x.cols();

check_consistent_sizes(function, "Successes variable", n,
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/prim/cols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifdef STAN_OPENCL

#include <stan/math/opencl/matrix_cl.hpp>
#include <cstdint>

namespace stan {
namespace math {
Expand All @@ -17,7 +18,7 @@ namespace math {
*/
template <typename T_x,
require_nonscalar_prim_or_rev_kernel_expression_t<T_x>* = nullptr>
inline int cols(const T_x& x) {
inline int64_t cols(const T_x& x) {
return x.cols();
}
} // namespace math
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/prim/num_elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <stan/math/prim/meta.hpp>
#include <stan/math/opencl/prim/size.hpp>
#include <cstdint>

namespace stan {
namespace math {
Expand All @@ -16,7 +17,7 @@ namespace math {
*/
template <typename T,
require_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr>
size_t num_elements(const T& m) {
int64_t num_elements(const T& m) {
return math::size(m);
}

Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/prim/rows.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifdef STAN_OPENCL

#include <stan/math/opencl/matrix_cl.hpp>
#include <cstdint>

namespace stan {
namespace math {
Expand All @@ -18,7 +19,7 @@ namespace math {

template <typename T_x,
require_nonscalar_prim_or_rev_kernel_expression_t<T_x>* = nullptr>
inline int rows(const T_x& x) {
inline int64_t rows(const T_x& x) {
return x.rows();
}

Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/prim/size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifdef STAN_OPENCL

#include <stan/math/prim/meta.hpp>
#include <cstdint>

namespace stan {
namespace math {
Expand All @@ -15,7 +16,7 @@ namespace math {
*/
template <typename T,
require_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr>
size_t size(const T& m) {
int64_t size(const T& m) {
return m.rows() * m.cols();
}

Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/fun/cols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/meta.hpp>
#include <cstdint>

namespace stan {
namespace math {
Expand All @@ -16,7 +17,7 @@ namespace math {
* @return Number of columns.
*/
template <typename T, require_matrix_t<T>* = nullptr>
inline Eigen::Index cols(const T& m) {
inline int64_t cols(const T& m) {
return m.cols();
}

Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/fun/max_size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define STAN_MATH_PRIM_FUN_MAX_SIZE_HPP

#include <stan/math/prim/fun/size.hpp>
#include <cstdint>
#include <algorithm>

namespace stan {
Expand All @@ -16,7 +17,7 @@ namespace math {
* @return the size of the largest input
*/
template <typename T1, typename... Ts>
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)...});
}

Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/fun/max_size_mvt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stan/math/prim/fun/size_mvt.hpp>
#include <algorithm>
#include <cstdint>

namespace stan {
namespace math {
Expand All @@ -21,7 +22,7 @@ namespace math {
* matrix or std::vector of Eigen matrices
*/
template <typename T1, typename... Ts>
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)...});
}

Expand Down
7 changes: 4 additions & 3 deletions stan/math/prim/fun/num_elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/meta.hpp>
#include <cstdint>
#include <vector>

namespace stan {
Expand All @@ -16,7 +17,7 @@ namespace math {
* @return 1
*/
template <typename T, require_stan_scalar_t<T>* = nullptr>
inline int num_elements(const T& x) {
inline int64_t num_elements(const T& x) {
return 1;
}

Expand All @@ -29,7 +30,7 @@ inline int num_elements(const T& x) {
* @return size of matrix
*/
template <typename T, require_matrix_t<T>* = nullptr>
inline int num_elements(const T& m) {
inline int64_t num_elements(const T& m) {
return m.size();
}

Expand All @@ -43,7 +44,7 @@ inline int num_elements(const T& m) {
* @return number of contained arguments
*/
template <typename T>
inline int num_elements(const std::vector<T>& v) {
inline int64_t num_elements(const std::vector<T>& v) {
if (v.size() == 0) {
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/fun/rows.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/meta.hpp>
#include <cstdint>

namespace stan {
namespace math {
Expand All @@ -16,7 +17,7 @@ namespace math {
* @return Number of rows.
*/
template <typename T, require_matrix_t<T>* = nullptr>
inline int rows(const T& m) {
inline int64_t rows(const T& m) {
return m.rows();
}

Expand Down
10 changes: 5 additions & 5 deletions stan/math/prim/fun/size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/fun/Eigen.hpp>
#include <cstdlib>
#include <cstdint>
#include <vector>

namespace stan {
Expand All @@ -14,8 +14,8 @@ namespace math {
* that are always of length 1.
*/
template <typename T, require_stan_scalar_t<T>* = nullptr>
inline size_t size(const T& /*x*/) {
return 1U;
inline int64_t size(const T& /*x*/) {
return 1;
}

/** \ingroup type_trait
Expand All @@ -25,12 +25,12 @@ inline size_t size(const T& /*x*/) {
* @tparam T type of m
*/
template <typename T, require_container_t<T>* = nullptr>
inline size_t size(const T& m) {
inline int64_t size(const T& m) {
return m.size();
}

template <typename T, require_var_matrix_t<T>* = nullptr>
inline size_t size(const T& m) {
inline int64_t size(const T& m) {
return m.size();
}

Expand Down
9 changes: 5 additions & 4 deletions stan/math/prim/fun/size_mvt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/fun/Eigen.hpp>
#include <cstdint>
#include <stdexcept>
#include <vector>

Expand All @@ -21,17 +22,17 @@ namespace math {
* @throw std::invalid_argument since the type is a scalar.
*/
template <typename ScalarT, require_stan_scalar_t<ScalarT>* = 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 <typename MatrixT, require_matrix_t<MatrixT>* = nullptr>
size_t size_mvt(const MatrixT& /* unused */) {
return 1U;
int64_t size_mvt(const MatrixT& /* unused */) {
return 1;
}

template <typename MatrixT, require_matrix_t<MatrixT>* = nullptr>
size_t size_mvt(const std::vector<MatrixT>& x) {
int64_t size_mvt(const std::vector<MatrixT>& x) {
return x.size();
}

Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/prob/poisson_binomial_cdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ return_type_t<T_theta> 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<T_y> y_vec(y);
vector_seq_view<T_theta> theta_vec(theta);

Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/prob/poisson_binomial_lccdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ return_type_t<T_theta> 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<T_y> y_vec(y);
vector_seq_view<T_theta> theta_vec(theta);

Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/prob/poisson_binomial_lcdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ return_type_t<T_theta> 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<T_y> y_vec(y);
vector_seq_view<T_theta> theta_vec(theta);

Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/prob/poisson_binomial_lpmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ return_type_t<T_theta> 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<T_y> y_vec(y);
vector_seq_view<T_theta> theta_vec(theta);

Expand Down
4 changes: 2 additions & 2 deletions test/unit/math/opencl/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ T to_vector_if(const T& x, std::size_t N) {

using stan::math::rows;
template <typename T, require_not_container_t<T>* = nullptr>
int rows(const T&) {
int64_t rows(const T&) {
return 1;
}
template <typename T, require_std_vector_t<T>* = nullptr>
int rows(const T& x) {
int64_t rows(const T& x) {
return x.size();
}

Expand Down

0 comments on commit 4c626fd

Please sign in to comment.