Skip to content

Commit

Permalink
Fix the performance regression by source_location
Browse files Browse the repository at this point in the history
Signed-off-by: Shreyas Atre <[email protected]>
  • Loading branch information
SAtacker committed Sep 13, 2023
1 parent 0a5db92 commit 6872e6c
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 8 deletions.
19 changes: 11 additions & 8 deletions library/include/chplx/begin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@

namespace chplx {

#if defined(CHPLX_NO_SOURCE_LOCATION)
template <typename F, typename... Args>
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>(f), detail::generate_annotation(location)),
std::forward<F>(f),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...);
}

#else
template <typename F, typename... Args>
requires(!std::is_same_v<std::decay_t<F>, 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>(f),
std::forward<Args>(args)...);
hpx::parallel::execution::post(hpx::execution::par.executor(),
hpx::annotated_function(
std::forward<F>(f), detail::generate_annotation(location)),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...);
}
#endif
} // namespace chplx
8 changes: 8 additions & 0 deletions library/include/chplx/cobegin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ namespace chplx {
requires(!std::is_same_v<std::decay_t<F>, 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>(f));
(g.run(exec, std::forward<F>(fs)), ...);
g.wait();
#else
cobegin(HPX_CURRENT_SOURCE_LOCATION(), std::forward<F>(f),
std::forward<Fs>(fs)...);
#endif
}
} // namespace chplx
69 changes: 69 additions & 0 deletions library/include/chplx/coforall_loop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,28 @@ namespace chplx {
void coforall(hpx::source_location const& location, Tuple<Ts...>& t, F&& f,
Args&&... args)
{
#if defined(CHPLX_NO_SOURCE_LOCATION)
detail::coforall(t, std::forward<F>(f), std::forward<Args>(args)...);
#else
detail::coforall(t,
hpx::annotated_function(
std::forward<F>(f), detail::generate_annotation(location)),
std::forward<Args>(args)...);
#endif
}

template <typename... Ts, typename F, typename... Args>
void coforall(hpx::source_location const& location, Tuple<Ts...> const& t,
F&& f, Args&&... args)
{
#if defined(CHPLX_NO_SOURCE_LOCATION)
detail::coforall(t, std::forward<F>(f), std::forward<Args>(args)...);
#else
detail::coforall(t,
hpx::annotated_function(
std::forward<F>(f), detail::generate_annotation(location)),
std::forward<Args>(args)...);
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -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<decltype(args)>(fargs)...);
},
r.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -115,6 +134,7 @@ namespace chplx {
r.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -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<decltype(args)>(fargs)...);
},
d.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -138,6 +170,7 @@ namespace chplx {
d.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -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<decltype(args)>(fargs)...);
},
d.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -161,6 +205,7 @@ namespace chplx {
d.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -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<decltype(args)>(fargs)...);
},
zr.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -184,6 +241,7 @@ namespace chplx {
zr.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -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<decltype(args)>(fargs)...);
},
a.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -206,6 +274,7 @@ namespace chplx {
a.size(),
detail::task_intent<std::decay_t<Args>>::call(
std::forward<Args>(args))...));
#endif
}

template <typename Target, typename F, typename... Args>
Expand Down
70 changes: 70 additions & 0 deletions library/include/chplx/forall_loop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,29 @@ namespace chplx {
void forall(hpx::source_location const& location, Tuple<Ts...>& t, F&& f,
Args&&... args)
{
#if defined(CHPLX_NO_SOURCE_LOCATION)
detail::forall(t, std::forward<F>(f), std::forward<Args>(args)...);
#else
detail::forall(t,
hpx::annotated_function(
std::forward<F>(f), detail::generate_annotation(location)),
std::forward<Args>(args)...);
#endif
}

template <typename... Ts, typename F, typename... Args>
void forall(hpx::source_location const& location, Tuple<Ts...> const& t,
F&& f, Args&&... args)
{
#if defined(CHPLX_NO_SOURCE_LOCATION)
detail::forall(t, detail::generate_annotation(location),
std::forward<Args>(args)...);
#else
detail::forall(t,
hpx::annotated_function(
std::forward<F>(f), detail::generate_annotation(location)),
std::forward<Args>(args)...);
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -97,6 +106,17 @@ namespace chplx {
void forall(hpx::source_location const& location,
Range<T, BoundedType, Stridable> 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<std::decay_t<Args>>::call(
std::forward<Args>(args))]<typename Arg>(
Arg&& value) mutable {
f(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});
#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -109,6 +129,7 @@ namespace chplx {
wrapped(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -117,6 +138,17 @@ namespace chplx {
void forall(hpx::source_location const& location,
Domain<N, T, Stridable> 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<std::decay_t<Args>>::call(
std::forward<Args>(args))]<typename Arg>(
Arg&& value) mutable {
f(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});
#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -129,6 +161,7 @@ namespace chplx {
wrapped(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -137,6 +170,17 @@ namespace chplx {
void forall(hpx::source_location const& location, AssocDomain<T> 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<std::decay_t<Args>>::call(
std::forward<Args>(args))]<typename Arg>(
Arg&& value) mutable {
f(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});
#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -149,6 +193,7 @@ namespace chplx {
wrapped(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -157,6 +202,17 @@ namespace chplx {
void forall(hpx::source_location const& location,
detail::ZipRange<Rs...> 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<std::decay_t<Args>>::call(
std::forward<Args>(args))]<typename Arg>(
Arg&& value) mutable {
f(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});
#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -169,6 +225,7 @@ namespace chplx {
wrapped(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -177,6 +234,18 @@ namespace chplx {
void forall(hpx::source_location const& location, Array<T, Domain> 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<std::decay_t<Args>>::call(
std::forward<Args>(args))]<typename Arg>(
Arg&& value) mutable {
f(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});

#else
auto wrapped =
hpx::annotated_function(f, detail::generate_annotation(location));

Expand All @@ -189,6 +258,7 @@ namespace chplx {
wrapped(std::forward<Arg>(value),
hpx::util::decay_unwrap<decltype(fargs)>::call(fargs)...);
});
#endif
}

template <typename Target, typename F, typename... Args>
Expand Down
4 changes: 4 additions & 0 deletions library/src/detail/generate_annotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6872e6c

Please sign in to comment.