fix(reduce_sum_static): wrap Args with ref_type_t to materialize Eigen expressions#3305
fix(reduce_sum_static): wrap Args with ref_type_t to materialize Eigen expressions#3305tmchow wants to merge 1 commit intostan-dev:developfrom
Conversation
…n 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 stan-dev#3304
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
Summary
Closes #3304.
reduce_sumwraps its shared arguments withref_type_t<Args&&>...before handing them to TBB so that Eigen expression-template temporaries (e.g.M.row(0),matrix.col(i)) materialize instead of being held as references to a destroyed temporary.reduce_sum_static'sSTAN_THREADSbranch was not updated in the same pass and still forwarded plainArgs....A caller that passes a temporary Eigen expression as a shared argument to
reduce_sum_staticcan therefore end up with a dangling reference once the temporary falls out of scope at the call site.Fix
stan/math/prim/functor/reduce_sum_static.hpp:This exactly mirrors the wrapping already in place in
reduce_sum.hpp(see line 208).Notes
ref_type_tis already in scope viastan/math/prim/meta.hpp(already#included).ReduceFunction()(noreduce_sum_implinstantiation) or returns zero.Closes #3304