diff --git a/examples/dynamic-forall.cpp b/examples/dynamic-forall.cpp index 5131010bd6..3c120cb0ea 100644 --- a/examples/dynamic-forall.cpp +++ b/examples/dynamic-forall.cpp @@ -77,28 +77,25 @@ int main(int argc, char *argv[]) //----------------------------------------------------------------------------// - std::cout << "\n Running C-style vector addition...\n"; - - // _cstyle_vector_add_start - for (int i = 0; i < N; ++i) { - c[i] = a[i] + b[i]; - } - // _cstyle_vector_add_end - - checkResult(c, N); -//printResult(c, N); - - -//----------------------------------------------------------------------------// -// Example of dynamic policy selection for forall -//----------------------------------------------------------------------------// + std::cout << "\n Running dynamic forall vector addition and reductions...\n"; + int sum = 0; + using VAL_INT_SUM = RAJA::expt::ValOp; + + RAJA::RangeSegment range(0, N); + //policy is chosen from the list - RAJA::expt::dynamic_forall(pol, RAJA::RangeSegment(0, N), [=] RAJA_HOST_DEVICE (int i) { + RAJA::expt::dynamic_forall(pol, range, + RAJA::expt::Reduce(&sum), + RAJA::expt::KernelName("RAJA dynamic forall"), + [=] RAJA_HOST_DEVICE (int i, VAL_INT_SUM &_sum) { + c[i] = a[i] + b[i]; + _sum += 1; }); // _rajaseq_vector_add_end + std::cout<<"Sum = "< struct dynamic_helper { - template - static void invoke_forall(const int pol, SEGMENT const &seg, BODY const &body) + template + static void invoke_forall(const int pol, SEGMENT const &seg, PARAMS&&... params) { if(IDX==pol){ using t_pol = typename camp::at>::type; - RAJA::forall(seg, body); + RAJA::forall(seg, params...); return; } - dynamic_helper::invoke_forall(pol, seg, body); + dynamic_helper::invoke_forall(pol, seg, params...); } - template + template static resources::EventProxy - invoke_forall(RAJA::resources::Resource r, const int pol, SEGMENT const &seg, BODY const &body) + invoke_forall(RAJA::resources::Resource r, const int pol, SEGMENT const &seg, PARAMS&&... params) { using t_pol = typename camp::at>::type; using resource_type = typename resources::get_resource::type; if(IDX==pol){ - RAJA::forall(r.get(), seg, body); + RAJA::forall(r.get(), seg, params...); //Return a generic event proxy from r, //because forall returns a typed event proxy return {r}; } - return dynamic_helper::invoke_forall(r, pol, seg, body); + return dynamic_helper::invoke_forall(r, pol, seg, params...); } }; @@ -688,28 +688,28 @@ namespace expt template struct dynamic_helper<0, POLICY_LIST> { - template + template static void - invoke_forall(const int pol, SEGMENT const &seg, BODY const &body) + invoke_forall(const int pol, SEGMENT const &seg, PARAMS&&... params) { if(0==pol){ using t_pol = typename camp::at>::type; - RAJA::forall(seg, body); + RAJA::forall(seg, params...); return; } RAJA_ABORT_OR_THROW("Policy enum not supported "); } - template + template static resources::EventProxy - invoke_forall(RAJA::resources::Resource r, const int pol, SEGMENT const &seg, BODY const &body) + invoke_forall(RAJA::resources::Resource r, const int pol, SEGMENT const &seg, PARAMS&&... params) { if(pol != 0) RAJA_ABORT_OR_THROW("Policy value out of range "); using t_pol = typename camp::at>::type; using resource_type = typename resources::get_resource::type; - RAJA::forall(r.get(), seg, body); + RAJA::forall(r.get(), seg, params...); //Return a generic event proxy from r, //because forall returns a typed event proxy @@ -718,8 +718,8 @@ namespace expt }; - template - void dynamic_forall(const int pol, SEGMENT const &seg, BODY const &body) + template + void dynamic_forall(const int pol, SEGMENT const &seg, PARAMS&&... params) { constexpr int N = camp::size::value; static_assert(N > 0, "RAJA policy list must not be empty"); @@ -727,12 +727,12 @@ namespace expt if(pol > N-1) { RAJA_ABORT_OR_THROW("Policy enum not supported"); } - dynamic_helper::invoke_forall(pol, seg, body); + dynamic_helper::invoke_forall(pol, seg, params...); } - template + template resources::EventProxy - dynamic_forall(RAJA::resources::Resource r, const int pol, SEGMENT const &seg, BODY const &body) + dynamic_forall(RAJA::resources::Resource r, const int pol, SEGMENT const &seg, PARAMS&&... params) { constexpr int N = camp::size::value; static_assert(N > 0, "RAJA policy list must not be empty"); @@ -741,7 +741,7 @@ namespace expt RAJA_ABORT_OR_THROW("Policy value out of range"); } - return dynamic_helper::invoke_forall(r, pol, seg, body); + return dynamic_helper::invoke_forall(r, pol, seg, params...); } } // namespace expt