Skip to content

Commit a5be9df

Browse files
committed
fix(reduce_sum_static): wrap Args with ref_type_t to materialize Eigen expressions
reduce_sum has been wrapping shared Args with ref_type_t<Args&&>... since v4.8+ so that Eigen expression-template temporaries (e.g. M.row(0), matrix.col(i)) materialize before being handed off to TBB's parallel reducer. reduce_sum_static's STAN_THREADS branch was not updated in the same pass and still forwarded plain Args..., so callers passing a temporary Eigen expression as a shared argument could get a dangling reference once the temporary went out of scope. Mirror the reduce_sum wrapping exactly: internal::reduce_sum_impl<..., ref_type_t<Args&&>...>()(...) No other changes. ref_type_t is already in scope via stan/math/prim/meta.hpp (already included). Closes #3304
1 parent ac8c21a commit a5be9df

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

stan/math/prim/functor/reduce_sum_static.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ inline auto reduce_sum_static(Vec&& vmapped, int grainsize, std::ostream* msgs,
5050

5151
#ifdef STAN_THREADS
5252
return internal::reduce_sum_impl<ReduceFunction, void, return_type, Vec,
53-
Args...>()(std::forward<Vec>(vmapped), false,
54-
grainsize, msgs,
55-
std::forward<Args>(args)...);
53+
ref_type_t<Args&&>...>()(
54+
std::forward<Vec>(vmapped), false, grainsize, msgs,
55+
std::forward<Args>(args)...);
5656
#else
5757
if (vmapped.empty()) {
5858
return return_type(0);

0 commit comments

Comments
 (0)