Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,40 @@
#pragma once

#include <hpx/config.hpp>
#include <hpx/assert.hpp>
#include <hpx/init_runtime_local/detail/init_logging.hpp>
#include <hpx/init_runtime_local/macros.hpp>
#include <hpx/modules/functional.hpp>
#include <hpx/modules/prefix.hpp>
#include <hpx/modules/preprocessor.hpp>
#include <hpx/modules/program_options.hpp>
#include <hpx/modules/resource_partitioner.hpp>
#include <hpx/modules/runtime_local.hpp>

namespace hpx { namespace program_options {
class variables_map;
class options_description;
}} // namespace hpx::program_options

#include <csignal>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <functional>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#include <hpx/modules/resource_partitioner_mode.hpp>
#include <hpx/modules/runtime_mode.hpp>

// Forward declaration of hpx::runtime to avoid pulling in the full runtime
// header -- the full type is only needed in the .cpp implementations.
namespace hpx {

class runtime;

using startup_function_type = hpx::move_only_function<void()>;
using shutdown_function_type = hpx::move_only_function<void()>;

namespace resource {

class partitioner;
} // namespace resource
} // namespace hpx

#if defined(__FreeBSD__)
HPX_CXX_CORE_EXPORT extern HPX_CORE_EXPORT char** freebsd_environ;
HPX_CXX_CORE_EXPORT extern char** environ;
Expand All @@ -44,6 +59,10 @@ namespace hpx {
HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT int init_helper(
hpx::program_options::variables_map&,
hpx::function<int(int, char**)> const&);

HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT void on_exit() noexcept;
HPX_CXX_CORE_EXPORT [[noreturn]] HPX_CORE_EXPORT void on_abort(
int signal) noexcept;
} // namespace detail

namespace local {
Expand All @@ -57,26 +76,20 @@ namespace hpx {
{
}

void operator()() const
{
std::cout << "Configuration after runtime start:\n";
std::cout << "----------------------------------\n";
rt_.get().get_config().dump(0, std::cout);
std::cout << "----------------------------------\n";
}
HPX_CORE_EXPORT void operator()() const;

std::reference_wrapper<hpx::runtime const> rt_;
};

// Default params to initialize the init_params struct
HPX_CXX_CORE_EXPORT [[maybe_unused]] inline int dummy_argc = 1;
HPX_CXX_CORE_EXPORT [[maybe_unused]] inline char app_name[256] =
HPX_APPLICATION_STRING;
"unknown HPX application";
inline char* default_argv[2] = {app_name, nullptr};
HPX_CXX_CORE_EXPORT [[maybe_unused]] inline char** dummy_argv =
default_argv;

// HPX_APPLICATION_STRING is specific to an application and therefore
// "unknown HPX application" is specific to an application and therefore
// cannot be in the source file
HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT
hpx::program_options::options_description const&
Expand All @@ -92,13 +105,13 @@ namespace hpx {
{
init_params()
{
std::strncpy(detail::app_name, HPX_APPLICATION_STRING,
std::strncpy(detail::app_name, "unknown HPX application",
sizeof(detail::app_name) - 1);
}

std::reference_wrapper<
hpx::program_options::options_description const>
desc_cmdline = detail::default_desc(HPX_APPLICATION_STRING);
desc_cmdline = detail::default_desc("unknown HPX application");
std::vector<std::string> cfg;
mutable startup_function_type startup;
mutable shutdown_function_type shutdown;
Expand All @@ -115,35 +128,11 @@ namespace hpx {
int argc, char** argv, init_params const& params,
bool blocking);

HPX_CXX_CORE_EXPORT inline int init_start_impl(
HPX_CXX_CORE_EXPORT int init_start_impl(
hpx::function<int(hpx::program_options::variables_map&)> const&
f,
int argc, char** argv, init_params const& params, bool blocking)
{
if (argc == 0 || argv == nullptr)
{
argc = dummy_argc;
argv = dummy_argv;
}

util::set_hpx_prefix(HPX_PREFIX);
#if defined(__FreeBSD__)
freebsd_environ = environ;
#endif
// set a handler for std::abort
[[maybe_unused]] auto prev_sh =
std::signal(SIGABRT, hpx::detail::on_abort);

[[maybe_unused]] auto ret = std::atexit(hpx::detail::on_exit);
HPX_ASSERT_MSG(ret == 0, "std::atexit returned error code");

#if defined(HPX_HAVE_CXX11_STD_QUICK_EXIT)
ret = std::at_quick_exit(hpx::detail::on_exit);
HPX_ASSERT_MSG(
ret == 0, "std::at_quick_exit returned error code");
#endif
return run_or_start(f, argc, argv, params, blocking);
}
int argc, char** argv, init_params const& params,
bool blocking);
} // namespace detail

HPX_CXX_CORE_EXPORT inline int init(
Expand Down
42 changes: 42 additions & 0 deletions libs/core/init_runtime_local/src/init_runtime_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <hpx/assert.hpp>
#include <hpx/init_runtime_local/detail/init_logging.hpp>
#include <hpx/init_runtime_local/init_runtime_local.hpp>
#include <hpx/init_runtime_local/macros.hpp>
#include <hpx/modules/algorithms.hpp>
#include <hpx/modules/command_line_handling_local.hpp>
#include <hpx/modules/errors.hpp>
Expand All @@ -21,6 +22,7 @@
#include <hpx/modules/futures.hpp>
#include <hpx/modules/lock_registration.hpp>
#include <hpx/modules/logging.hpp>
#include <hpx/modules/prefix.hpp>
#include <hpx/modules/program_options.hpp>
#include <hpx/modules/resource_partitioner.hpp>
#include <hpx/modules/runtime_local.hpp>
Expand Down Expand Up @@ -100,6 +102,16 @@ namespace hpx {

namespace local {

namespace detail {
void dump_config::operator()() const
{
std::cout << "Configuration after runtime start:\n";
std::cout << "----------------------------------\n";
rt_.get().get_config().dump(0, std::cout);
std::cout << "----------------------------------\n";
}
} // namespace detail

// Print stack trace and exit.
#if defined(HPX_WINDOWS)
extern BOOL WINAPI termination_handler(DWORD ctrl_type);
Expand Down Expand Up @@ -540,3 +552,33 @@ namespace hpx {
} // namespace detail
} // namespace local
} // namespace hpx

namespace hpx::local::detail {
int init_start_impl(
hpx::function<int(hpx::program_options::variables_map&)> const& f,
int argc, char** argv, init_params const& params, bool blocking)
{
if (argc == 0 || argv == nullptr)
{
argc = dummy_argc;
argv = dummy_argv;
}

util::set_hpx_prefix(HPX_PREFIX);
#if defined(__FreeBSD__)
freebsd_environ = environ;
#endif
// set a handler for std::abort
[[maybe_unused]] auto prev_sh =
std::signal(SIGABRT, hpx::detail::on_abort);

[[maybe_unused]] auto ret = std::atexit(hpx::detail::on_exit);
HPX_ASSERT_MSG(ret == 0, "std::atexit returned error code");

#if defined(HPX_HAVE_CXX11_STD_QUICK_EXIT)
ret = std::at_quick_exit(hpx::detail::on_exit);
HPX_ASSERT_MSG(ret == 0, "std::at_quick_exit returned error code");
#endif
return run_or_start(f, argc, argv, params, blocking);
}
} // namespace hpx::local::detail
2 changes: 2 additions & 0 deletions libs/core/resource_partitioner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

set(resource_partitioner_headers
hpx/modules/resource_partitioner_mode.hpp
hpx/resource_partitioner/detail/create_partitioner.hpp
hpx/resource_partitioner/detail/partitioner.hpp
hpx/resource_partitioner/partitioner.hpp
hpx/resource_partitioner/partitioner_fwd.hpp
hpx/resource_partitioner/partitioner_mode.hpp
)

# cmake-format: off
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2022-2026 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#pragma once

#include <hpx/resource_partitioner/partitioner_mode.hpp>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <hpx/config.hpp>
#include <hpx/modules/functional.hpp>
#include <hpx/modules/threading_base.hpp>
#include <hpx/resource_partitioner/partitioner_mode.hpp>

#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -36,19 +37,9 @@ namespace hpx::resource {
/// Returns false otherwise.
HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT bool is_partitioner_valid();

/// This enumeration describes the modes available when creating a
/// resource partitioner.
HPX_CXX_CORE_EXPORT enum class partitioner_mode : std::int8_t {
/// Default mode.
default_ = 0,

/// Allow processing units to be oversubscribed, i.e. multiple
/// worker threads to share a single processing unit.
allow_oversubscription = 1,

/// Allow worker threads to be added and removed from thread pools.
allow_dynamic_pools = 2
};
// partitioner_mode enum is defined in partitioner_mode.hpp
// (included above) to allow lightweight inclusion without the
// functional/threading_base transitive dependencies of this header.

#define HPX_PARTITIONER_MODE_UNSCOPED_ENUM_DEPRECATION_MSG \
"The unscoped partitioner_mode names are deprecated. Please use " \
Expand All @@ -65,18 +56,6 @@ namespace hpx::resource {

#undef HPX_PARTITIONER_MODE_UNSCOPED_ENUM_DEPRECATION_MSG

HPX_CXX_CORE_EXPORT constexpr partitioner_mode operator&(
partitioner_mode lhs, partitioner_mode rhs) noexcept
{
return static_cast<partitioner_mode>(
static_cast<int>(lhs) & static_cast<int>(rhs));
}

HPX_CXX_CORE_EXPORT constexpr bool as_bool(partitioner_mode val) noexcept
{
return static_cast<int>(val) != 0;
}

HPX_CXX_CORE_EXPORT using scheduler_function =
hpx::function<std::unique_ptr<hpx::threads::thread_pool_base>(
hpx::threads::thread_pool_init_parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2017 Shoshana Jakobovits
// Copyright (c) 2022-2026 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

/// \file partitioner_mode.hpp
/// \page hpx::resource::partitioner_mode
/// \headerfile hpx/resource_partitioner/partitioner_mode.hpp

#pragma once

#include <hpx/config.hpp>
#include <cstdint>

namespace hpx::resource {

/// This enumeration describes the modes available when creating a
/// resource partitioner.
HPX_CXX_CORE_EXPORT enum class partitioner_mode : std::int8_t {
/// Default mode.
default_ = 0,

/// Allow processing units to be oversubscribed, i.e. multiple
/// worker threads to share a single processing unit.
allow_oversubscription = 1,

/// Allow worker threads to be added and removed from thread pools.
allow_dynamic_pools = 2
};

HPX_CXX_CORE_EXPORT constexpr partitioner_mode operator&(
partitioner_mode lhs, partitioner_mode rhs) noexcept
{
return static_cast<partitioner_mode>(
static_cast<int>(lhs) & static_cast<int>(rhs));
}

HPX_CXX_CORE_EXPORT constexpr partitioner_mode operator|(
partitioner_mode lhs, partitioner_mode rhs) noexcept
{
return static_cast<partitioner_mode>(
static_cast<int>(lhs) | static_cast<int>(rhs));
}

HPX_CXX_CORE_EXPORT constexpr partitioner_mode operator^(
partitioner_mode lhs, partitioner_mode rhs) noexcept
{
return static_cast<partitioner_mode>(
static_cast<int>(lhs) ^ static_cast<int>(rhs));
}

HPX_CXX_CORE_EXPORT constexpr partitioner_mode operator~(
partitioner_mode mode) noexcept
{
return static_cast<partitioner_mode>(~static_cast<int>(mode));
}

HPX_CXX_CORE_EXPORT constexpr bool as_bool(partitioner_mode val) noexcept
{
return static_cast<int>(val) != 0;
}

} // namespace hpx::resource
1 change: 1 addition & 0 deletions libs/core/runtime_configuration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

set(runtime_configuration_headers
hpx/modules/runtime_mode.hpp
hpx/runtime_configuration/agas_service_mode.hpp
hpx/runtime_configuration/component_commandline_base.hpp
hpx/runtime_configuration/component_factory_base.hpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2007-2026 Hartmut Kaiser
// Copyright (c) 2011 Bryce Lelbach
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#pragma once

#include <hpx/runtime_configuration/runtime_mode.hpp>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2025 Hartmut Kaiser
// Copyright (c) 2007-2026 Hartmut Kaiser
// Copyright (c) 2011 Bryce Lelbach
//
// SPDX-License-Identifier: BSL-1.0
Expand All @@ -12,7 +12,6 @@
#pragma once

#include <hpx/config.hpp>

#include <cstdint>
#include <string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <hpx/modules/testing.hpp>

#include <cstddef>
#include <iostream>

using namespace std;

Expand Down
Loading
Loading