Skip to content

Commit

Permalink
Enable using for_loop with range generators
Browse files Browse the repository at this point in the history
- flyby: adding C++23 generator<>
- flyby: moving coroutine facilities to namespace hpx
- flyby: simplify sequential implementation of for_loop
- flyby: adding C++20 default_sentinel
- flyby: adding flat_set/flat_map
  • Loading branch information
hkaiser committed Mar 17, 2023
1 parent f0d9721 commit 61cfb64
Show file tree
Hide file tree
Showing 60 changed files with 6,026 additions and 427 deletions.
18 changes: 18 additions & 0 deletions cmake/HPX_AddConfigTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,24 @@ function(hpx_check_for_cxx20_std_construct_at)
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx20_std_default_sentinel)
add_hpx_config_test(
HPX_WITH_CXX20_STD_DEFAULT_SENTINEL
SOURCE cmake/tests/cxx20_std_default_sentinel.cpp
FILE ${ARGN}
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx23_std_generator)
add_hpx_config_test(
HPX_WITH_CXX23_STD_GENERATOR
SOURCE cmake/tests/cxx23_std_generator.cpp
FILE ${ARGN}
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx_lambda_capture_decltype)
add_hpx_config_test(
Expand Down
22 changes: 15 additions & 7 deletions cmake/HPX_AddDefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,29 @@ function(write_config_defines_file)

set(hpx_config_defines "\n")
foreach(def ${DEFINITIONS_VAR})
# C++20 specific variable
string(FIND ${def} "HAVE_CXX20" _pos)
# C++23 specific variable
string(FIND ${def} "HAVE_CXX23" _pos)
if(NOT ${_pos} EQUAL -1)
set(hpx_config_defines
"${hpx_config_defines}#if __cplusplus >= 202002\n#define ${def}\n#endif\n"
"${hpx_config_defines}#if __cplusplus >= 202300\n#define ${def}\n#endif\n"
)
else()
# C++17 specific variable
string(FIND ${def} "HAVE_CXX17" _pos)
# C++20 specific variable
string(FIND ${def} "HAVE_CXX20" _pos)
if(NOT ${_pos} EQUAL -1)
set(hpx_config_defines
"${hpx_config_defines}#if __cplusplus >= 201500\n#define ${def}\n#endif\n"
"${hpx_config_defines}#if __cplusplus >= 202002\n#define ${def}\n#endif\n"
)
else()
set(hpx_config_defines "${hpx_config_defines}#define ${def}\n")
# C++17 specific variable
string(FIND ${def} "HAVE_CXX17" _pos)
if(NOT ${_pos} EQUAL -1)
set(hpx_config_defines
"${hpx_config_defines}#if __cplusplus >= 201500\n#define ${def}\n#endif\n"
)
else()
set(hpx_config_defines "${hpx_config_defines}#define ${def}\n")
endif()
endif()
endif()
endforeach()
Expand Down
5 changes: 5 additions & 0 deletions cmake/HPX_CheckCXXStandard.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ elseif(HPX_WITH_CXX20)
"HPX_WITH_CXX20 is deprecated. Use HPX_WITH_CXX_STANDARD=20 instead."
)
set(HPX_CXX_STANDARD 20)
elseif(HPX_WITH_CXX23)
hpx_warn(
"HPX_WITH_CXX23 is deprecated. Use HPX_WITH_CXX_STANDARD=23 instead."
)
set(HPX_CXX_STANDARD 23)
endif()

if(CMAKE_CXX_STANDARD)
Expand Down
8 changes: 8 additions & 0 deletions cmake/HPX_PerformCxxFeatureTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ function(hpx_perform_cxx_feature_tests)
hpx_check_for_cxx20_std_construct_at(
DEFINITIONS HPX_HAVE_CXX20_STD_CONSTRUCT_AT
)

hpx_check_for_cxx20_std_default_sentinel(
DEFINITIONS HPX_HAVE_CXX20_STD_DEFAULT_SENTINEL
)
endif()

if(HPX_WITH_CXX_STANDARD GREATER_EQUAL 23)
hpx_check_for_cxx23_std_generator(DEFINITIONS HPX_HAVE_CXX23_STD_GENERATOR)
endif()

hpx_check_for_cxx_lambda_capture_decltype(
Expand Down
14 changes: 7 additions & 7 deletions cmake/tests/cxx20_coroutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
#if defined(__has_include)
#if __has_include(<coroutine>)
#include <coroutine>
namespace hpx { namespace coro {
namespace hpx {
using std::coroutine_handle;
using std::suspend_always;
using std::suspend_never;
}} // namespace hpx::coro
} // namespace hpx
#else
#include <experimental/coroutine>
namespace hpx { namespace coro {
namespace hpx {
using std::experimental::coroutine_handle;
using std::experimental::suspend_always;
using std::experimental::suspend_never;
}} // namespace hpx::coro
} // namespace hpx
#endif
#endif

Expand All @@ -36,12 +36,12 @@ struct resumable
return {};
}

hpx::coro::suspend_never initial_suspend() const noexcept
hpx::suspend_never initial_suspend() const noexcept
{
return {};
}

hpx::coro::suspend_never final_suspend() const noexcept
hpx::suspend_never final_suspend() const noexcept
{
return {};
}
Expand All @@ -58,7 +58,7 @@ struct resumable
{
return 42;
}
void await_suspend(hpx::coro::coroutine_handle<promise_type>) const noexcept
void await_suspend(hpx::coroutine_handle<promise_type>) const noexcept
{
}
};
Expand Down
14 changes: 14 additions & 0 deletions cmake/tests/cxx20_std_default_sentinel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2023 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)

#include <iterator>

int main()
{
[[maybe_unused]] auto sent1 = std::default_sentinel;
[[maybe_unused]] auto sent2 = std::default_sentinel_t{};
return 0;
}
23 changes: 23 additions & 0 deletions cmake/tests/cxx23_std_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2023 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)

// test for availability of std::generator (C++ 23)

#include <generator>

std::generator<char> letters(char first)
{
for (;; co_yield first++)
;
}

int main()
{
for (const char ch : letters('a'))
{
}
return 0;
}
7 changes: 3 additions & 4 deletions examples/quickstart/barrier_docs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
// This example is meant for inclusion in the documentation.

//[barrier_docs

#include <hpx/barrier.hpp>
#include <hpx/future.hpp>
#include <hpx/local/barrier.hpp>
#include <hpx/local/future.hpp>
#include <hpx/local/init.hpp>

#include <iostream>

int hpx_main()
Expand Down Expand Up @@ -43,5 +43,4 @@ int main(int argc, char* argv[])
{
return hpx::local::init(hpx_main, argc, argv);
}

//]
9 changes: 4 additions & 5 deletions examples/quickstart/condition_variable_docs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// This example is meant for inclusion in the documentation.

//[condition_variable_docs

#include <hpx/condition_variable.hpp>
#include <hpx/local/condition_variable.hpp>
#include <hpx/local/init.hpp>
#include <hpx/mutex.hpp>
#include <hpx/thread.hpp>
#include <hpx/local/mutex.hpp>
#include <hpx/local/thread.hpp>

#include <iostream>
#include <string>

Expand Down Expand Up @@ -74,5 +74,4 @@ int main(int argc, char* argv[])
{
return hpx::local::init(hpx_main, argc, argv);
}

//]
6 changes: 3 additions & 3 deletions examples/quickstart/counting_semaphore_docs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
// This example is meant for inclusion in the documentation.

//[counting_semaphore_docs

#include <hpx/local/init.hpp>
#include <hpx/synchronization/counting_semaphore.hpp>
#include <hpx/thread.hpp>
#include <hpx/local/semaphore.hpp>
#include <hpx/local/thread.hpp>

#include <iostream>

// initialize the semaphore with a count of 3
Expand Down
7 changes: 3 additions & 4 deletions examples/quickstart/mutex_docs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
// This example is meant for inclusion in the documentation.

//[mutex_docs

#include <hpx/future.hpp>
#include <hpx/local/future.hpp>
#include <hpx/local/init.hpp>
#include <hpx/mutex.hpp>
#include <hpx/local/mutex.hpp>

#include <iostream>

int hpx_main()
Expand Down Expand Up @@ -38,5 +38,4 @@ int main(int argc, char* argv[])
{
return hpx::local::init(hpx_main, argc, argv);
}

//]
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace hpx::parallel::detail {
template <typename Iter, typename Sent, typename F>
constexpr Iter sequential_generate_helper(Iter first, Sent last, F&& f)
{
return util::loop_ind(hpx::execution::seq, first, last,
[f = HPX_FORWARD(F, f)](auto& v) mutable { v = f(); });
return util::loop_ind<hpx::execution::sequenced_policy>(
first, last, [f = HPX_FORWARD(F, f)](auto& v) mutable { v = f(); });
}

struct sequential_generate_t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ namespace hpx::parallel::detail {
private:
template <typename InIterB, typename InIterE, typename T,
typename Reduce>
friend constexpr T tag_fallback_invoke(sequential_reduce_t,
ExPolicy&& policy, InIterB first, InIterE last, T init, Reduce&& r)
friend constexpr T tag_fallback_invoke(sequential_reduce_t, ExPolicy&&,
InIterB first, InIterE last, T init, Reduce&& r)
{
util::loop_ind(HPX_FORWARD(ExPolicy, policy), first, last,
[&init, &r](
auto const& v) mutable { init = HPX_INVOKE(r, init, v); });
util::loop_ind<ExPolicy>(
first, last, [&init, &r](auto const& v) mutable {
init = HPX_INVOKE(r, init, v);
});
return init;
}

Expand All @@ -47,11 +48,11 @@ namespace hpx::parallel::detail {
template <typename Iter, typename Sent, typename T, typename Reduce,
typename Convert>
friend constexpr auto tag_fallback_invoke(sequential_reduce_t,
ExPolicy&& policy, Iter first, Sent last, T init, Reduce&& r,
ExPolicy&&, Iter first, Sent last, T init, Reduce&& r,
Convert&& conv)
{
util::loop_ind(HPX_FORWARD(ExPolicy, policy), first, last,
[&init, &r, &conv](auto const& v) mutable {
util::loop_ind<std::decay_t<ExPolicy>>(
first, last, [&init, &r, &conv](auto const& v) mutable {
init = HPX_INVOKE(r, init, HPX_INVOKE(conv, v));
});
return init;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,7 @@ namespace hpx::parallel {
template <typename ExPolicy, typename InIterB, typename InIterE,
typename F>
static constexpr InIterB sequential(
[[maybe_unused]] ExPolicy&& policy, InIterB first, InIterE last,
F&& f, hpx::identity)
ExPolicy&&, InIterB first, InIterE last, F&& f, hpx::identity)
{
if constexpr (hpx::traits::is_random_access_iterator_v<InIterB>)
{
Expand All @@ -493,8 +492,8 @@ namespace hpx::parallel {
}
else
{
return util::loop_ind(HPX_FORWARD(ExPolicy, policy), first,
last, HPX_FORWARD(F, f));
return util::loop_ind<std::decay_t<ExPolicy>>(
first, last, HPX_FORWARD(F, f));
}
}

Expand Down

0 comments on commit 61cfb64

Please sign in to comment.