Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/linux_release_modules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
-DHPX_WITH_VERIFY_LOCKS_BACKTRACE=ON \
-DHPX_WITH_CHECK_MODULE_DEPENDENCIES=ON \
-DHPX_WITH_CXX_MODULES=ON \
-DHPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT=ON \
-DHPX_WITH_DATAPAR=ON \
-DHPX_WITH_DATAPAR_BACKEND=EMULATE
base-targets: hpx_core core components
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/macos_debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
-DHPX_WITH_CONTRACTS=ON \
-DHPX_WITH_CONTRACTS_MODE=ENFORCE \
-DHPX_WITH_TASK_SIZE_DEFAULT_PARAMETERS=ON \
-DHPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT=ON \
-DHPX_WITH_TESTS_COMMAND_LINE=--hpx:queuing=local-priority-fifo-double
base-targets: hpx_core core components
# macOS runners are few, keep the number of parallel jobs low
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/windows_release_vs2022_modules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
-DHPX_WITH_VERIFY_LOCKS_BACKTRACE=ON \
-DHPX_WITH_CHECK_MODULE_DEPENDENCIES=ON \
-DHPX_WITH_CXX_MODULES=ON \
-DHPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT=ON \
-DHPX_WITH_DATAPAR=ON \
-DHPX_WITH_DATAPAR_BACKEND=EMULATE
base-targets: hpx_core core components
Expand Down
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,19 @@ if(HPX_WITH_TASK_SIZE_DEFAULT_PARAMETERS)
hpx_add_config_define(HPX_HAVE_TASK_SIZE_DEFAULT_PARAMETERS)
endif()

hpx_option(
HPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT
BOOL
"Use a shared_suspending_fork_join_executor as the executor of the parallel execution policies par and par_unseq (default: OFF)"
OFF
CATEGORY "Thread Manager"
ADVANCED
)

if(HPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT)
hpx_add_config_define(HPX_HAVE_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT)
endif()

hpx_option(
HPX_WITH_COROUTINE_COUNTERS BOOL
"Enable keeping track of coroutine creation and rebind counts (default: OFF)"
Expand Down
90 changes: 89 additions & 1 deletion docs/sphinx/manual/executors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ API.
Main Executors in |hpx|
=======================

|hpx| provides multiple executors. Below are three of the most commonly used:
|hpx| provides multiple executors. Below are some of the most commonly used:

Parallel Executor
-----------------
Expand Down Expand Up @@ -92,6 +92,94 @@ Fork-Join Executor
}
);

Suspending Fork-Join Executor
-----------------------------

* Does the same job as the fork-join executor, but its waiting threads spin
only for a short time (the spin duration, 20 microseconds by default) and
then suspend until the next parallel region starts or the current one has
finished.
* Other work can run on the cores of an idle executor, and idle cores can go
to sleep.
* Constexpr default constructible; the worker threads are created on first
use. Copies made on an HPX thread share the worker threads of the original.
* The calling thread takes part in the parallel regions if it runs on one of
the executor's PUs; otherwise it suspends until the region has finished.

.. code-block:: c++

#include <hpx/execution.hpp>
#include <hpx/algorithm.hpp>

std::vector<int> data(100, 1);

hpx::execution::experimental::suspending_fork_join_executor exec;

hpx::for_each(
hpx::execution::par.on(exec),
data.begin(), data.end(),
[](int &x) {
x += 1;
}
);

A ``suspending_fork_join_executor`` can't be used from several threads at the
same time or from inside one of its own parallel regions (nested
parallelism). ``shared_suspending_fork_join_executor`` can:

* All ``shared_suspending_fork_join_executor`` objects with the same
configuration (priority, stack size, loop schedule and spin duration) share
one set of worker threads per thread pool. It is created when an |hpx|
thread of the pool uses such an executor first, and stopped when the
runtime starts to shut down.
* A parallel region runs on these worker threads if they are free. If they are
busy, because they run a region for another thread or because the region is
started from inside one of their regions, the region runs through a
``parallel_executor`` instead: one task for each worker thread of the pool,
each with a contiguous range of the indices. The same happens if the calling
thread is not an |hpx| thread, and once the worker threads were stopped.
* Exceptions are reported by whichever executor ran the region: the first
exception from the worker threads, or an ``hpx::exception_list`` from the
``parallel_executor``.
* As with ``suspending_fork_join_executor``, asynchronous calls
(``bulk_async_execute``, ``async_invoke``) run the region before they
return a ready future.
* Single tasks (``sync_execute``, ``async_execute``, ``then_execute``,
``post``) and continuations (``bulk_then_execute``) run as a
``parallel_executor`` runs them.
* Copies never own worker threads, so the executor can be a ``constexpr``
variable.

If |hpx| is configured with
``HPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT=ON`` (default: ``OFF``),
``shared_suspending_fork_join_executor`` is the executor of the parallel
execution policies ``par`` and ``par_unseq``, instead of
``parallel_executor``. The task policies ``par(task)`` and
``par_unseq(task)`` keep using ``parallel_executor``: they run an algorithm
asynchronously, which the worker threads of a fork-join executor can't do.
For the same reason, mapping a policy with a
``shared_suspending_fork_join_executor`` to a task policy (``to_task``, as in
``par(task)`` or ``par.on(exec)(task)``) gives it a ``parallel_executor``.

.. code-block:: c++

#include <hpx/execution.hpp>
#include <hpx/algorithm.hpp>

std::vector<int> data(100, 1);

// can be used by any number of threads, also from inside the loop body
constexpr hpx::execution::experimental::shared_suspending_fork_join_executor
exec;

hpx::for_each(
hpx::execution::par.on(exec),
data.begin(), data.end(),
[](int &x) {
x += 1;
}
);

Sequential Executor
-------------------

Expand Down
9 changes: 7 additions & 2 deletions libs/core/executors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set(executors_headers
hpx/executors/scheduler_executor.hpp
hpx/executors/sequenced_executor.hpp
hpx/executors/service_executors.hpp
hpx/executors/suspending_fork_join_executor.hpp
hpx/executors/std_execution_policy.hpp
hpx/executors/sync.hpp
hpx/executors/task.hpp
Expand Down Expand Up @@ -105,8 +106,12 @@ endif()
# cmake-format: on

set(executors_sources
current_executor.cpp exception_list_callbacks.cpp fork_join_executor.cpp
parallel_scheduler.cpp service_executors.cpp
current_executor.cpp
exception_list_callbacks.cpp
fork_join_executor.cpp
parallel_scheduler.cpp
service_executors.cpp
suspending_fork_join_executor.cpp
)

include(HPX_AddModule)
Expand Down
23 changes: 19 additions & 4 deletions libs/core/executors/include/hpx/executors/execution_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <hpx/executors/execution_policy_mappings.hpp>
#include <hpx/executors/parallel_executor.hpp>
#include <hpx/executors/sequenced_executor.hpp>
#if defined(HPX_HAVE_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT)
#include <hpx/executors/suspending_fork_join_executor.hpp>
#endif
#include <hpx/modules/async_base.hpp>
#include <hpx/modules/execution.hpp>
#include <hpx/modules/execution_base.hpp>
Expand All @@ -40,6 +43,15 @@ namespace hpx::execution {
typename Executor, typename Parameters = void,
typename Category = void>
struct execution_policy;

// The executor of the parallel policy and the parallel unsequenced
// policy. The task policies always use parallel_executor.
#if defined(HPX_HAVE_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT)
using default_parallel_executor =
hpx::execution::experimental::shared_suspending_fork_join_executor;
#else
using default_parallel_executor = parallel_executor;
#endif
} // namespace detail

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -737,8 +749,9 @@ namespace hpx::execution {
/// type to disambiguate parallel algorithm overloading and indicate that a
/// parallel algorithm's execution may be parallelized.
HPX_CXX_CORE_EXPORT using parallel_policy =
detail::parallel_policy_shim<parallel_executor,
hpx::traits::executor_parameters_type_t<parallel_executor>>;
detail::parallel_policy_shim<detail::default_parallel_executor,
hpx::traits::executor_parameters_type_t<
detail::default_parallel_executor>>;

/// Default parallel execution policy object.
HPX_CXX_CORE_EXPORT inline constexpr parallel_policy par{};
Expand Down Expand Up @@ -903,8 +916,10 @@ namespace hpx::execution {
/// indicate that a parallel algorithm's execution may be parallelized and
/// vectorized.
HPX_CXX_CORE_EXPORT using parallel_unsequenced_policy =
detail::parallel_unsequenced_policy_shim<parallel_executor,
hpx::traits::executor_parameters_type_t<parallel_executor>>;
detail::parallel_unsequenced_policy_shim<
detail::default_parallel_executor,
hpx::traits::executor_parameters_type_t<
detail::default_parallel_executor>>;

/// Default vector execution policy object.
HPX_CXX_CORE_EXPORT inline constexpr parallel_unsequenced_policy
Expand Down
Loading
Loading