Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue regarding "Clean up code: .str().length() #2577" #1

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
eec9ef5
update assign and rvalue functions to handle negative min-max indices
SteveBronder Jan 20, 2022
af980a6
update tests for checking rvalue sizes when returning negative indices
SteveBronder Jan 20, 2022
3eb1ccc
fix min_max index for var mat types
SteveBronder Jan 21, 2022
ecfb825
Merge commit '5621ebde258fe9926c6a36160f12e998e028670e' into HEAD
yashikno Jan 21, 2022
1cbee84
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 21, 2022
7c552a4
update
SteveBronder Jan 21, 2022
8cb5990
Merge branch 'fix/negative-indexing' of github.com:stan-dev/stan into…
SteveBronder Jan 21, 2022
65db926
update test cases and make error messages nicer
SteveBronder Jan 22, 2022
3b716ef
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 22, 2022
14f5dff
fix ending ) for serializer error
SteveBronder Jan 22, 2022
55b3b9e
remove size zero checks for assigned to value
SteveBronder Jan 22, 2022
ace0af5
cleanup extra error messages for matrix assign
SteveBronder Jan 22, 2022
00d1368
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 22, 2022
d17b844
fix line length
SteveBronder Jan 22, 2022
a602734
Merge branch 'fix/negative-indexing' of github.com:stan-dev/stan into…
SteveBronder Jan 22, 2022
99c1117
cleanup template
SteveBronder Jan 22, 2022
685a133
check sizes for arrays in assignment
SteveBronder Jan 24, 2022
9744ceb
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 24, 2022
cfe80e8
rvalue reference catch
SteveBronder Jan 24, 2022
a58ae32
remove check on lhs from assign to size 0
SteveBronder Jan 27, 2022
a34588e
Merge pull request #3095 from stan-dev/fix/negative-indexing
rok-cesnovar Jan 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/stan/io/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ class serializer {
*/
void check_r_capacity(size_t m) const {
if (pos_r_ + m > r_size_) {
[]() STAN_COLD_PATH {
throw std::runtime_error("no more storage available to write");
}();
[](auto r_size_, auto pos_r_, auto m)
STAN_COLD_PATH {
throw std::runtime_error(
std::string("In serializer: Storage capacity [") +
std::to_string(r_size_) +
"] exceeded while writing value of size [" +
std::to_string(m) + "] from position [" +
std::to_string(pos_r_) +
"]. This is an internal error, if you see it please report it as" +
" an issue on the Stan github repository.");
}(r_size_, pos_r_, m);
}
}

Expand Down
59 changes: 56 additions & 3 deletions src/stan/model/indexing/access_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,51 @@ inline auto colwise_reverse(const T& x) {
* @param y The value to assign from.
*/
template <typename T1, typename T2,
require_any_not_t<is_var_matrix<T1>, is_eigen<T2>>* = nullptr>
void assign_impl(T1&& x, T2&& y) {
require_all_t<is_stan_scalar<T1>, is_stan_scalar<T2>>* = nullptr>
void assign_impl(T1&& x, T2&& y, const char* name) {
x = std::forward<T2>(y);
}

/**
* Base case of assignment
* @tparam T1 Any type that's not a var matrix.
* @tparam T2 Any type that's not a var matrix.
* @param x The value to assign to
* @param y The value to assign from.
*/
template <typename T1, typename T2,
require_any_not_t<is_var_matrix<T1>, is_eigen<T2>>* = nullptr,
require_all_t<is_matrix<T1>, is_matrix<T2>>* = nullptr>
void assign_impl(T1&& x, T2&& y, const char* name) {
// We are allowed to assign to fully uninitialized matrix
if (x.size() != 0) {
static constexpr const char* obj_type
= is_vector<T1>::value ? "vector" : "matrix";
stan::math::check_size_match(
(std::string(obj_type) + " assign columns").c_str(), name, x.cols(),
"right hand side columns", y.cols());
stan::math::check_size_match(
(std::string(obj_type) + " assign rows").c_str(), name, x.rows(),
"right hand side rows", y.rows());
}
x = std::forward<T2>(y);
}

/**
* Base case of assignment
* @tparam T1 Any type that's not a var matrix.
* @tparam T2 Any type that's not a var matrix.
* @param x The value to assign to
* @param y The value to assign from.
*/
template <typename T1, typename T2,
require_all_t<is_std_vector<T1>, is_std_vector<T2>>* = nullptr>
void assign_impl(T1&& x, T2&& y, const char* name) {
// We are allowed to assign to fully uninitialized matrix
if (unlikely(x.size() != 0)) {
stan::math::check_size_match("assign array size", name, x.size(),
"right hand side", y.size());
}
x = std::forward<T2>(y);
}

Expand All @@ -60,7 +103,17 @@ void assign_impl(T1&& x, T2&& y) {
*/
template <typename Mat1, typename Mat2, require_var_matrix_t<Mat1>* = nullptr,
require_eigen_st<std::is_arithmetic, Mat2>* = nullptr>
void assign_impl(Mat1&& x, Mat2&& y) {
void assign_impl(Mat1&& x, Mat2&& y, const char* name) {
if (x.size() != 0) {
static constexpr const char* obj_type
= is_vector<Mat1>::value ? "vector" : "matrix";
stan::math::check_size_match(
(std::string(obj_type) + " assign columns").c_str(), name, x.cols(),
"right hand side columns", y.cols());
stan::math::check_size_match(
(std::string(obj_type) + " assign rows").c_str(), name, x.rows(),
"right hand side rows", y.rows());
}
auto prev_vals = stan::math::to_arena(x.val());
x.vi_->val_ = std::forward<Mat2>(y);
stan::math::reverse_pass_callback([x, prev_vals]() mutable {
Expand Down
331 changes: 170 additions & 161 deletions src/stan/model/indexing/assign.hpp

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions src/stan/model/indexing/assign_varmat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ template <typename Vec1, typename Vec2, require_var_vector_t<Vec1>* = nullptr,
internal::require_var_vector_or_arithmetic_eigen<Vec2>* = nullptr>
inline void assign(Vec1&& x, const Vec2& y, const char* name,
const index_multi& idx) {
stan::math::check_size_match("vector[multi] assign", "left hand side",
idx.ns_.size(), name, y.size());
stan::math::check_size_match("vector[multi] assign", name, idx.ns_.size(),
"right hand side", y.size());
const auto x_size = x.size();
const auto assign_size = idx.ns_.size();
arena_t<std::vector<int>> x_idx(assign_size);
Expand Down Expand Up @@ -229,8 +229,8 @@ inline void assign(Mat1&& x, const Vec& y, const char* name, index_uni row_idx,
stan::math::check_range("matrix[uni, multi] assign", name, x.rows(),
row_idx.n_);
const auto assign_cols = col_idx.ns_.size();
stan::math::check_size_match("matrix[uni, multi] assign", "left hand side",
assign_cols, name, y.size());
stan::math::check_size_match("matrix[uni, multi] assign columns", name,
assign_cols, "right hand side", y.size());
const int row_idx_val = row_idx.n_ - 1;
arena_t<std::vector<int>> x_idx(assign_cols);
arena_t<Eigen::Matrix<double, -1, 1>> prev_val(assign_cols);
Expand Down Expand Up @@ -310,10 +310,10 @@ template <typename Mat1, typename Mat2,
inline void assign(Mat1&& x, const Mat2& y, const char* name,
const index_multi& idx) {
const auto assign_rows = idx.ns_.size();
stan::math::check_size_match("matrix[multi] assign", "left hand side rows",
assign_rows, name, y.rows());
stan::math::check_size_match("matrix[multi] assign", "left hand side columns",
x.cols(), name, y.cols());
stan::math::check_size_match("matrix[multi] assign rows", name, assign_rows,
"right hand side rows", y.rows());
stan::math::check_size_match("matrix[multi] assign columns", name, x.cols(),
"right hand side rows", y.cols());
arena_t<std::vector<int>> x_idx(assign_rows);
arena_t<Eigen::Matrix<double, -1, -1>> prev_vals(assign_rows, x.cols());
Eigen::Matrix<double, -1, -1> y_val_idx(assign_rows, x.cols());
Expand Down Expand Up @@ -392,10 +392,11 @@ inline void assign(Mat1&& x, const Mat2& y, const char* name,
const index_multi& row_idx, const index_multi& col_idx) {
const auto assign_rows = row_idx.ns_.size();
const auto assign_cols = col_idx.ns_.size();
stan::math::check_size_match("matrix[multi,multi] assign", "left hand side",
assign_rows, name, y.rows());
stan::math::check_size_match("matrix[multi,multi] assign", "left hand side",
assign_cols, name, y.cols());
stan::math::check_size_match("matrix[multi,multi] assign rows", name,
assign_rows, "right hand side rows", y.rows());
stan::math::check_size_match("matrix[multi,multi] assign columns", name,
assign_cols, "right hand side columns",
y.cols());
using arena_vec = std::vector<int, stan::math::arena_allocator<int>>;
using pair_type = std::pair<int, arena_vec>;
arena_vec x_col_idx(assign_cols);
Expand Down Expand Up @@ -514,8 +515,9 @@ template <typename Mat1, typename Mat2, typename Idx,
inline void assign(Mat1&& x, const Mat2& y, const char* name,
const Idx& row_idx, const index_multi& col_idx) {
const auto assign_cols = col_idx.ns_.size();
stan::math::check_size_match("matrix[..., multi] assign", "left hand side",
assign_cols, name, y.cols());
stan::math::check_size_match("matrix[..., multi] assign columns", name,
assign_cols, "right hand side columns",
y.cols());
std::unordered_set<int> x_set;
const auto& y_eval = y.eval();
x_set.reserve(assign_cols);
Expand Down
Loading