Skip to content

Commit

Permalink
Merge #6099
Browse files Browse the repository at this point in the history
6099: Futures attempt to execute threads directly if those have not started executing r=hkaiser a=hkaiser

- adding new API function hpx::threads::get_outer_self_id


Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>
Co-authored-by: Panos <pansysk75@gmail.com>
Co-authored-by: Panos Syskakis <pansysk75@gmail.com>
  • Loading branch information
3 people committed Jul 31, 2023
2 parents dd17010 + fe7a35d commit c16ae10
Show file tree
Hide file tree
Showing 78 changed files with 1,635 additions and 484 deletions.
1 change: 1 addition & 0 deletions .jenkins/cscs/env-clang-13.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ configure_extra_options+=" -DHPX_WITH_COMPILER_WARNINGS=ON"
configure_extra_options+=" -DHPX_WITH_COMPILER_WARNINGS_AS_ERRORS=ON"
configure_extra_options+=" -DHPX_WITH_SPINLOCK_DEADLOCK_DETECTION=ON"
configure_extra_options+=" -DHPX_WITH_UNITY_BUILD=ON"
configure_extra_options+=" -DHPX_COROUTINES_WITH_THREAD_SCHEDULE_HINT_RUNS_AS_CHILD=ON"

# enable extra counters to verify everything compiles
configure_extra_options+=" -DHPX_WITH_BACKGROUND_THREAD_COUNTERS=ON"
Expand Down
1 change: 1 addition & 0 deletions .jenkins/lsu/batch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ulimit -l unlimited
set +e
ctest \
${ctest_extra_args} \
--output-on-failure \
-S ${src_dir}/.jenkins/lsu/ctest.cmake \
-DCTEST_CONFIGURE_EXTRA_OPTIONS="${configure_extra_options}" \
-DCTEST_BUILD_CONFIGURATION_NAME="${configuration_name_with_build_type}" \
Expand Down
2 changes: 2 additions & 0 deletions .jenkins/lsu/env-clang-11.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ configure_extra_options+=" -DHPX_WITH_LOGGING=OFF"
# Make sure HWLOC does not report 'cores'. This is purely an option to enable
# testing the topology code under conditions close to those on FreeBSD.
configure_extra_options+=" -DHPX_TOPOLOGY_WITH_ADDITIONAL_HWLOC_TESTING=ON"

configure_extra_options+=" -DHPX_COROUTINES_WITH_THREAD_SCHEDULE_HINT_RUNS_AS_CHILD=ON"
2 changes: 2 additions & 0 deletions .jenkins/lsu/env-clang-12.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ configure_extra_options+=" -DHPX_WITH_LOGGING=OFF"
# Make sure HWLOC does not report 'cores'. This is purely an option to enable
# testing the topology code under conditions close to those on FreeBSD.
configure_extra_options+=" -DHPX_TOPOLOGY_WITH_ADDITIONAL_HWLOC_TESTING=ON"

configure_extra_options+=" -DHPX_COROUTINES_WITH_THREAD_SCHEDULE_HINT_RUNS_AS_CHILD=ON"
2 changes: 2 additions & 0 deletions .jenkins/lsu/env-gcc-10.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ configure_extra_options+=" -DHPX_WITH_FETCH_LCI=ON"

# The pwrapi library still needs to be set up properly on rostam
# configure_extra_options+=" -DHPX_WITH_POWER_COUNTER=ON"

configure_extra_options+=" -DHPX_COROUTINES_WITH_THREAD_SCHEDULE_HINT_RUNS_AS_CHILD=ON"
2 changes: 2 additions & 0 deletions .jenkins/lsu/env-gcc-9.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ configure_extra_options+=" -DHPX_WITH_FETCH_LCI=ON"

# The pwrapi library still needs to be set up properly on rostam
# configure_extra_options+=" -DHPX_WITH_POWER_COUNTER=ON"

configure_extra_options+=" -DHPX_COROUTINES_WITH_THREAD_SCHEDULE_HINT_RUNS_AS_CHILD=ON"
23 changes: 13 additions & 10 deletions components/iostreams/src/server/output_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <hpx/serialization/serialize.hpp>
#include <hpx/serialization/shared_ptr.hpp>
#include <hpx/serialization/vector.hpp>
#include <hpx/threading_base/thread_data.hpp>
#include <hpx/threading_base/thread_helpers.hpp>

#include <hpx/components/iostreams/server/buffer.hpp>
Expand All @@ -25,11 +26,12 @@
#include <memory>
#include <utility>

namespace hpx { namespace iostreams { namespace detail {
namespace hpx::iostreams::detail {

void buffer::save(serialization::output_archive& ar, unsigned) const
{
bool valid = (data_.get() && !data_->empty());
ar& valid;
bool const valid = (data_.get() && !data_->empty());
ar << valid;
if (valid)
{
ar& data_;
Expand All @@ -39,15 +41,15 @@ namespace hpx { namespace iostreams { namespace detail {
void buffer::load(serialization::input_archive& ar, unsigned)
{
bool valid = false;
ar& valid;
ar >> valid;
if (valid)
{
ar& data_;
}
}
}}} // namespace hpx::iostreams::detail
} // namespace hpx::iostreams::detail

namespace hpx { namespace iostreams { namespace server {
namespace hpx::iostreams::server {
///////////////////////////////////////////////////////////////////////////
void output_stream::call_write_async(std::uint32_t locality_id,
std::uint64_t count, detail::buffer const& in, hpx::id_type /*this_id*/)
Expand Down Expand Up @@ -87,12 +89,13 @@ namespace hpx { namespace iostreams { namespace server {
{ // {{{
// Perform the IO in another OS thread.
detail::buffer in(buf_in);
hpx::get_thread_pool("io_pool")->get_io_service().post(hpx::bind_front(
&output_stream::call_write_sync, this, locality_id, count,
std::ref(in), threads::thread_id_ref_type(threads::get_self_id())));
hpx::get_thread_pool("io_pool")->get_io_service().post(
hpx::bind_front(&output_stream::call_write_sync, this, locality_id,
count, std::ref(in),
threads::thread_id_ref_type(threads::get_outer_self_id())));

// Sleep until the worker thread wakes us up.
this_thread::suspend(threads::thread_schedule_state::suspended,
"output_stream::write_sync");
} // }}}
}}} // namespace hpx::iostreams::server
} // namespace hpx::iostreams::server
15 changes: 7 additions & 8 deletions docs/sphinx/manual/hpx_runtime_and_resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ turned on, work stealing is done from queues associated with the same NUMA domai
first, only after that work is stolen from other NUMA domains.

This scheduler is enabled at build time by default using the FIFO
(first-in-first-out) queing policy. This policy can be invoked using
(first-in-first-out) queueing policy. This policy can be invoked using
:option:`--hpx:queuing`\ ``local-priority-fifo``. The scheduler can also be
enabled using the LIFO (last-in-first-out) policy. This is not the default
policy and must be invoked using the command line option
:option:`--hpx:queuing`\
``=local-priority-lifo``.
:option:`--hpx:queuing`\ ``local-priority-lifo``.

Static priority scheduling policy
---------------------------------
Expand All @@ -63,7 +62,7 @@ robin fashion. There is no thread stealing in this policy.
Local scheduling policy
-----------------------

* invoke using: :option:`--hpx:queuing`\ ``=local`` (or ``-ql``)
* invoke using: :option:`--hpx:queuing`\ ``local`` (or ``-ql``)
* flag to turn on for build: ``HPX_THREAD_SCHEDULERS=all`` or
``HPX_THREAD_SCHEDULERS=local``

Expand All @@ -73,7 +72,7 @@ thread pulls its tasks (user threads).
Static scheduling policy
------------------------

* invoke using: :option:`--hpx:queuing`\ ``=static``
* invoke using: :option:`--hpx:queuing`\ ``static``
* flag to turn on for build: ``HPX_THREAD_SCHEDULERS=all`` or
``HPX_THREAD_SCHEDULERS=static``

Expand All @@ -84,7 +83,7 @@ robin fashion. There is no thread stealing in this policy.
Priority ABP scheduling policy
------------------------------

* invoke using: :option:`--hpx:queuing`\ ``=abp-priority-fifo``
* invoke using: :option:`--hpx:queuing`\ ``abp-priority-fifo``
* flag to turn on for build: ``HPX_THREAD_SCHEDULERS=all`` or
``HPX_THREAD_SCHEDULERS=abp-priority``

Expand All @@ -102,8 +101,8 @@ domain first, only after that work is stolen from other NUMA domains.

This scheduler can be used with two underlying queuing policies (FIFO:
first-in-first-out, and LIFO: last-in-first-out). In order to use the LIFO
policy use the command line option :option:`--hpx:queuing`\
``=abp-priority-lifo``.
policy use the command line option
:option:`--hpx:queuing`\ ``=abp-priority-lifo``.

..
Questions, concerns and notes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ namespace hpx::execution::experimental {
{
} with_priority{};

template <>
struct is_scheduling_property<with_priority_t> : std::true_type
{
};

inline constexpr struct get_priority_t final
: hpx::functional::detail::tag_fallback<get_priority_t>
{
Expand All @@ -88,6 +93,11 @@ namespace hpx::execution::experimental {
{
} with_stacksize{};

template <>
struct is_scheduling_property<with_stacksize_t> : std::true_type
{
};

inline constexpr struct get_stacksize_t final
: hpx::functional::detail::tag_fallback<get_stacksize_t>
{
Expand All @@ -112,6 +122,11 @@ namespace hpx::execution::experimental {
{
} with_hint{};

template <>
struct is_scheduling_property<with_hint_t> : std::true_type
{
};

inline constexpr struct get_hint_t final
: hpx::functional::detail::tag_fallback<get_hint_t>
{
Expand All @@ -136,6 +151,11 @@ namespace hpx::execution::experimental {
{
} with_annotation{};

template <>
struct is_scheduling_property<with_annotation_t> : std::true_type
{
};

inline constexpr struct get_annotation_t final
: hpx::functional::detail::tag_fallback<get_annotation_t>
{
Expand Down
7 changes: 4 additions & 3 deletions libs/core/config/cmake/templates/config_version.hpp.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2020 Hartmut Kaiser
// Copyright (c) 2007-2023 Hartmut Kaiser
// Copyright (c) 2011 Bryce Lelbach
// Copyright (c) 2013 Adrian Serio
//
Expand Down Expand Up @@ -30,8 +30,9 @@
/// ``HPX_VERSION_FULL & 0x00FF00 == HPX_VERSION_MINOR``
/// ``HPX_VERSION_FULL & 0x0000FF == HPX_VERSION_SUBMINOR``
#define HPX_VERSION_FULL \
((HPX_VERSION_MAJOR << 16) | (HPX_VERSION_MINOR << 8) | \
HPX_VERSION_SUBMINOR)
((HPX_PP_CAT(0x, HPX_VERSION_MAJOR) << 16) | \
(HPX_PP_CAT(0x, HPX_VERSION_MINOR) << 8) | \
HPX_PP_CAT(0x, HPX_VERSION_SUBMINOR))

/// Evaluates to the release date of this HPX version in the format YYYYMMDD.
#define HPX_VERSION_DATE @HPX_VERSION_DATE@
Expand Down
20 changes: 19 additions & 1 deletion libs/core/coroutines/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2020 The STE||AR-Group
# Copyright (c) 2019-2023 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -14,6 +14,23 @@ hpx_option(
MODULE COROUTINES
)

hpx_option(
HPX_COROUTINES_WITH_THREAD_SCHEDULE_HINT_RUNS_AS_CHILD
BOOL
"Futures attempt to run associated threads directly if those have not been started (default: OFF)"
OFF
CATEGORY "Thread Manager"
ADVANCED
MODULE COROUTINES
)

if(HPX_COROUTINES_WITH_THREAD_SCHEDULE_HINT_RUNS_AS_CHILD)
hpx_add_config_define_namespace(
DEFINE HPX_COROUTINES_HAVE_THREAD_SCHEDULE_HINT_RUNS_AS_CHILD
NAMESPACE COROUTINES
)
endif()

set(coroutines_headers
hpx/coroutines/coroutine.hpp
hpx/coroutines/coroutine_fwd.hpp
Expand All @@ -29,6 +46,7 @@ set(coroutines_headers
hpx/coroutines/detail/coroutine_impl.hpp
hpx/coroutines/detail/coroutine_self.hpp
hpx/coroutines/detail/coroutine_stackful_self.hpp
hpx/coroutines/detail/coroutine_stackful_self_direct.hpp
hpx/coroutines/detail/coroutine_stackless_self.hpp
hpx/coroutines/detail/get_stack_pointer.hpp
hpx/coroutines/detail/posix_utility.hpp
Expand Down
8 changes: 7 additions & 1 deletion libs/core/coroutines/include/hpx/coroutines/coroutine.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2006, Giovanni P. Deretta
// Copyright (c) 2007-2021 Hartmut Kaiser
// Copyright (c) 2007-2023 Hartmut Kaiser
//
// This code may be used under either of the following two licences:
//
Expand Down Expand Up @@ -151,6 +151,12 @@ namespace hpx::threads::coroutines {
return impl_.result();
}

HPX_FORCEINLINE result_type invoke_directly(arg_type arg = arg_type())
{
HPX_ASSERT(impl_.is_ready());
return impl_.invoke_directly(arg);
}

bool is_ready() const noexcept
{
return impl_.is_ready();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,11 @@ namespace hpx::threads::coroutines {
return (std::numeric_limits<std::ptrdiff_t>::max)();
}
#endif
void reset_stack() const
void reset_stack(bool direct_execution)
{
if (direct_execution)
return;

if (ctx_)
{
#if defined(HPX_USE_POSIX_STACK_UTILITIES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ namespace hpx::threads::coroutines::detail::lx {

using context_impl_base = x86_linux_context_impl_base;

// Create a context that on restore invokes Functor on
// a new stack. The stack size can be optionally specified.
// Create a context that on restore invokes Functor on a new stack. The
// stack size can be optionally specified.
explicit x86_linux_context_impl(std::ptrdiff_t stack_size = -1)
: m_stack_size(stack_size == -1 ?
static_cast<std::ptrdiff_t>(default_stack_size) :
Expand Down Expand Up @@ -365,8 +365,11 @@ namespace hpx::threads::coroutines::detail::lx {
return m_stack_size;
}

void reset_stack()
void reset_stack(bool direct_execution)
{
if (direct_execution)
return;

HPX_ASSERT(m_stack);
if (posix::reset_stack(
m_stack, static_cast<std::size_t>(m_stack_size)))
Expand All @@ -379,7 +382,10 @@ namespace hpx::threads::coroutines::detail::lx {

void rebind_stack()
{
HPX_ASSERT(m_stack);
// directly executed coroutine, no need to allocate a stack
if (m_stack == nullptr)
return;

#if defined(HPX_HAVE_COROUTINE_COUNTERS)
increment_stack_recycle_count();
#endif
Expand Down Expand Up @@ -527,8 +533,8 @@ namespace hpx::threads::coroutines::detail::lx {
#endif
};

// Free function. Saves the current context in @p from
// and restores the context in @p to.
// Free function. Saves the current context in @p from and restores the
// context in @p to.
// @note This function is found by ADL.
inline void swap_context(x86_linux_context_impl_base& from,
x86_linux_context_impl_base const& to, default_hint) noexcept
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,11 @@ namespace hpx::threads::coroutines {
return (std::numeric_limits<std::ptrdiff_t>::max)();
}
#endif
void reset_stack()
void reset_stack(bool direct_execution)
{
if (direct_execution)
return;

if (m_stack)
{
if (posix::reset_stack(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
#include <hpx/config.hpp>
#include <hpx/coroutines/config/defines.hpp>
#include <hpx/assert.hpp>
#include <hpx/coroutines/detail/get_stack_pointer.hpp>
#include <hpx/coroutines/detail/swap_context.hpp>

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <system_error>

#if defined(HPX_HAVE_COROUTINE_COUNTERS)
Expand Down Expand Up @@ -279,7 +281,7 @@ namespace hpx::threads::coroutines {
return stacksize_;
}

static constexpr void reset_stack() noexcept {}
static constexpr void reset_stack(bool) noexcept {}

#if defined(HPX_HAVE_COROUTINE_COUNTERS)
void rebind_stack() noexcept
Expand All @@ -294,7 +296,8 @@ namespace hpx::threads::coroutines {
// https://stackoverflow.com/a/20930496/269943
static std::ptrdiff_t get_available_stack_space() noexcept
{
MEMORY_BASIC_INFORMATION mbi = {}; // page range
MEMORY_BASIC_INFORMATION mbi = {}; // page range
std::memset(&mbi, '\0', sizeof(mbi));
VirtualQuery(&mbi, &mbi, sizeof(mbi)); // get range
return reinterpret_cast<std::ptrdiff_t>(&mbi) -
reinterpret_cast<std::ptrdiff_t>(mbi.AllocationBase);
Expand Down
Loading

0 comments on commit c16ae10

Please sign in to comment.