From 6872e6cdff496f3f6dd5f8251ec1a4ae799c6bd0 Mon Sep 17 00:00:00 2001 From: Shreyas Atre Date: Wed, 13 Sep 2023 15:08:45 -0500 Subject: [PATCH] Fix the performance regression by source_location Signed-off-by: Shreyas Atre --- library/include/chplx/begin.hpp | 19 +++--- library/include/chplx/cobegin.hpp | 8 +++ library/include/chplx/coforall_loop.hpp | 69 +++++++++++++++++++++ library/include/chplx/forall_loop.hpp | 70 ++++++++++++++++++++++ library/src/detail/generate_annotation.cpp | 4 ++ 5 files changed, 162 insertions(+), 8 deletions(-) diff --git a/library/include/chplx/begin.hpp b/library/include/chplx/begin.hpp index 95ec4a5a..44b59005 100644 --- a/library/include/chplx/begin.hpp +++ b/library/include/chplx/begin.hpp @@ -19,21 +19,24 @@ namespace chplx { +#if defined(CHPLX_NO_SOURCE_LOCATION) template - void begin(hpx::source_location const& location, F&& f, Args&&... args) + void begin(F&& f, Args&&... args) { hpx::parallel::execution::post(hpx::execution::par.executor(), - hpx::annotated_function( - std::forward(f), detail::generate_annotation(location)), + std::forward(f), detail::task_intent>::call( std::forward(args))...); } - +#else template - requires(!std::is_same_v, hpx::source_location>) - void begin(F&& f, Args&&... args) + void begin(hpx::source_location const& location, F&& f, Args&&... args) { - begin(HPX_CURRENT_SOURCE_LOCATION(), std::forward(f), - std::forward(args)...); + hpx::parallel::execution::post(hpx::execution::par.executor(), + hpx::annotated_function( + std::forward(f), detail::generate_annotation(location)), + detail::task_intent>::call( + std::forward(args))...); } +#endif } // namespace chplx diff --git a/library/include/chplx/cobegin.hpp b/library/include/chplx/cobegin.hpp index 82a6d1f9..e393fdfc 100644 --- a/library/include/chplx/cobegin.hpp +++ b/library/include/chplx/cobegin.hpp @@ -34,7 +34,15 @@ namespace chplx { requires(!std::is_same_v, hpx::source_location>) void cobegin(F&& f, Fs&&... fs) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + auto exec = hpx::execution::par.executor(); + hpx::experimental::task_group g; + g.run(exec, std::forward(f)); + (g.run(exec, std::forward(fs)), ...); + g.wait(); +#else cobegin(HPX_CURRENT_SOURCE_LOCATION(), std::forward(f), std::forward(fs)...); +#endif } } // namespace chplx diff --git a/library/include/chplx/coforall_loop.hpp b/library/include/chplx/coforall_loop.hpp index a07130b0..2ca238f7 100644 --- a/library/include/chplx/coforall_loop.hpp +++ b/library/include/chplx/coforall_loop.hpp @@ -77,20 +77,28 @@ namespace chplx { void coforall(hpx::source_location const& location, Tuple& t, F&& f, Args&&... args) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + detail::coforall(t, std::forward(f), std::forward(args)...); +#else detail::coforall(t, hpx::annotated_function( std::forward(f), detail::generate_annotation(location)), std::forward(args)...); +#endif } template void coforall(hpx::source_location const& location, Tuple const& t, F&& f, Args&&... args) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + detail::coforall(t, std::forward(f), std::forward(args)...); +#else detail::coforall(t, hpx::annotated_function( std::forward(f), detail::generate_annotation(location)), std::forward(args)...); +#endif } //----------------------------------------------------------------------------- @@ -103,6 +111,17 @@ namespace chplx { auto policy = hpx::parallel::util::adapt_sharing_mode(hpx::execution::par, hpx::threads::thread_sharing_hint::do_not_combine_tasks); +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::wait_all(hpx::parallel::execution::bulk_async_execute( + policy.executor(), + [&](std::size_t idx, auto&&... fargs) { + return f(r.orderToIndex(idx), + std::forward(fargs)...); + }, + r.size(), + detail::task_intent>::call( + std::forward(args))...)); +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -115,6 +134,7 @@ namespace chplx { r.size(), detail::task_intent>::call( std::forward(args))...)); +#endif } //----------------------------------------------------------------------------- @@ -126,6 +146,18 @@ namespace chplx { auto policy = hpx::parallel::util::adapt_sharing_mode(hpx::execution::par, hpx::threads::thread_sharing_hint::do_not_combine_tasks); + +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::wait_all(hpx::parallel::execution::bulk_async_execute( + policy.executor(), + [&](std::size_t idx, auto&&... fargs) { + return f(d.orderToIndex(idx), + std::forward(fargs)...); + }, + d.size(), + detail::task_intent>::call( + std::forward(args))...)); +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -138,6 +170,7 @@ namespace chplx { d.size(), detail::task_intent>::call( std::forward(args))...)); +#endif } //----------------------------------------------------------------------------- @@ -149,6 +182,17 @@ namespace chplx { auto policy = hpx::parallel::util::adapt_sharing_mode(hpx::execution::par, hpx::threads::thread_sharing_hint::do_not_combine_tasks); +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::wait_all(hpx::parallel::execution::bulk_async_execute( + policy.executor(), + [&](std::size_t idx, auto&&... fargs) { + return f(d.orderToIndex(idx), + std::forward(fargs)...); + }, + d.size(), + detail::task_intent>::call( + std::forward(args))...)); +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -161,6 +205,7 @@ namespace chplx { d.size(), detail::task_intent>::call( std::forward(args))...)); +#endif } //----------------------------------------------------------------------------- @@ -172,6 +217,18 @@ namespace chplx { auto policy = hpx::parallel::util::adapt_sharing_mode(hpx::execution::par, hpx::threads::thread_sharing_hint::do_not_combine_tasks); + +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::wait_all(hpx::parallel::execution::bulk_async_execute( + policy.executor(), + [&](std::size_t idx, auto&&... fargs) { + return f(zr.orderToIndex(idx), + std::forward(fargs)...); + }, + zr.size(), + detail::task_intent>::call( + std::forward(args))...)); +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -184,6 +241,7 @@ namespace chplx { zr.size(), detail::task_intent>::call( std::forward(args))...)); +#endif } //----------------------------------------------------------------------------- @@ -195,6 +253,16 @@ namespace chplx { auto policy = hpx::parallel::util::adapt_sharing_mode(hpx::execution::par, hpx::threads::thread_sharing_hint::do_not_combine_tasks); +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::wait_all(hpx::parallel::execution::bulk_async_execute( + policy.executor(), + [&](std::size_t idx, auto&&... fargs) { + return f(a[idx], std::forward(fargs)...); + }, + a.size(), + detail::task_intent>::call( + std::forward(args))...)); +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -206,6 +274,7 @@ namespace chplx { a.size(), detail::task_intent>::call( std::forward(args))...)); +#endif } template diff --git a/library/include/chplx/forall_loop.hpp b/library/include/chplx/forall_loop.hpp index beac4300..f5dcd558 100644 --- a/library/include/chplx/forall_loop.hpp +++ b/library/include/chplx/forall_loop.hpp @@ -74,20 +74,29 @@ namespace chplx { void forall(hpx::source_location const& location, Tuple& t, F&& f, Args&&... args) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + detail::forall(t, std::forward(f), std::forward(args)...); +#else detail::forall(t, hpx::annotated_function( std::forward(f), detail::generate_annotation(location)), std::forward(args)...); +#endif } template void forall(hpx::source_location const& location, Tuple const& t, F&& f, Args&&... args) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + detail::forall(t, detail::generate_annotation(location), + std::forward(args)...); +#else detail::forall(t, hpx::annotated_function( std::forward(f), detail::generate_annotation(location)), std::forward(args)...); +#endif } //----------------------------------------------------------------------------- @@ -97,6 +106,17 @@ namespace chplx { void forall(hpx::source_location const& location, Range const& r, F&& f, Args&&... args) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::ranges::experimental::for_loop(hpx::execution::par, + detail::IteratorGenerator(r), + [&, + ... fargs = detail::task_intent>::call( + std::forward(args))]( + Arg&& value) mutable { + f(std::forward(value), + hpx::util::decay_unwrap::call(fargs)...); + }); +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -109,6 +129,7 @@ namespace chplx { wrapped(std::forward(value), hpx::util::decay_unwrap::call(fargs)...); }); +#endif } //----------------------------------------------------------------------------- @@ -117,6 +138,17 @@ namespace chplx { void forall(hpx::source_location const& location, Domain const& d, F&& f, Args&&... args) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::ranges::experimental::for_loop(hpx::execution::par, + detail::IteratorGenerator(d), + [&, + ... fargs = detail::task_intent>::call( + std::forward(args))]( + Arg&& value) mutable { + f(std::forward(value), + hpx::util::decay_unwrap::call(fargs)...); + }); +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -129,6 +161,7 @@ namespace chplx { wrapped(std::forward(value), hpx::util::decay_unwrap::call(fargs)...); }); +#endif } //----------------------------------------------------------------------------- @@ -137,6 +170,17 @@ namespace chplx { void forall(hpx::source_location const& location, AssocDomain const& d, F&& f, Args&&... args) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::ranges::experimental::for_loop(hpx::execution::par, + detail::IteratorGenerator(d, 0, d.size()), + [&, + ... fargs = detail::task_intent>::call( + std::forward(args))]( + Arg&& value) mutable { + f(std::forward(value), + hpx::util::decay_unwrap::call(fargs)...); + }); +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -149,6 +193,7 @@ namespace chplx { wrapped(std::forward(value), hpx::util::decay_unwrap::call(fargs)...); }); +#endif } //----------------------------------------------------------------------------- @@ -157,6 +202,17 @@ namespace chplx { void forall(hpx::source_location const& location, detail::ZipRange const& zr, F&& f, Args&&... args) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::ranges::experimental::for_loop(hpx::execution::par, + detail::IteratorGenerator(zr), + [&, + ... fargs = detail::task_intent>::call( + std::forward(args))]( + Arg&& value) mutable { + f(std::forward(value), + hpx::util::decay_unwrap::call(fargs)...); + }); +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -169,6 +225,7 @@ namespace chplx { wrapped(std::forward(value), hpx::util::decay_unwrap::call(fargs)...); }); +#endif } //----------------------------------------------------------------------------- @@ -177,6 +234,18 @@ namespace chplx { void forall(hpx::source_location const& location, Array const& a, F&& f, Args&&... args) { +#if defined(CHPLX_NO_SOURCE_LOCATION) + hpx::ranges::experimental::for_loop(hpx::execution::par, + detail::IteratorGenerator(a), + [&, + ... fargs = detail::task_intent>::call( + std::forward(args))]( + Arg&& value) mutable { + f(std::forward(value), + hpx::util::decay_unwrap::call(fargs)...); + }); + +#else auto wrapped = hpx::annotated_function(f, detail::generate_annotation(location)); @@ -189,6 +258,7 @@ namespace chplx { wrapped(std::forward(value), hpx::util::decay_unwrap::call(fargs)...); }); +#endif } template diff --git a/library/src/detail/generate_annotation.cpp b/library/src/detail/generate_annotation.cpp index e07ac7a1..de44db84 100644 --- a/library/src/detail/generate_annotation.cpp +++ b/library/src/detail/generate_annotation.cpp @@ -16,9 +16,13 @@ namespace chplx::detail { std::string generate_annotation(hpx::source_location const& location) { #if defined(HPX_HAVE_THREAD_DESCRIPTION) +#if defined(CHPLX_NO_SOURCE_LOCATION) + return {}; +#else std::filesystem::path p(location.file_name()); return hpx::util::format("%s(%d): %s", p.filename(), location.line(), location.function_name()); +#endif #else return {}; #endif