Skip to content

Commit

Permalink
Merge pull request #6410 from STEllAR-GROUP/scope
Browse files Browse the repository at this point in the history
Adding scope_xxx from library fundamentals TS v3
  • Loading branch information
hkaiser authored Jan 13, 2024
2 parents e8b7598 + b30d08c commit 6f6f287
Show file tree
Hide file tree
Showing 39 changed files with 1,015 additions and 502 deletions.
18 changes: 18 additions & 0 deletions cmake/HPX_AddConfigTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,15 @@ function(hpx_check_for_cxx20_std_bit_cast)
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx20_constexpr_destructor)
add_hpx_config_test(
HPX_WITH_CXX20_CONSTEXPR_DESTRUCTOR
SOURCE cmake/tests/cxx20_constexpr_destructor.cpp
FILE ${ARGN}
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx23_std_generator)
add_hpx_config_test(
Expand All @@ -642,6 +651,15 @@ function(hpx_check_for_cxx23_std_generator)
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx26_experimental_scope)
add_hpx_config_test(
HPX_WITH_CXX26_EXPERIMENTAL_SCOPE
SOURCE cmake/tests/cxx26_experimental_scope.cpp
FILE ${ARGN}
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx_lambda_capture_decltype)
add_hpx_config_test(
Expand Down
8 changes: 8 additions & 0 deletions cmake/HPX_PerformCxxFeatureTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,20 @@ function(hpx_perform_cxx_feature_tests)
)

hpx_check_for_cxx20_std_bit_cast(DEFINITIONS HPX_HAVE_CXX20_STD_BIT_CAST)

hpx_check_for_cxx20_constexpr_destructor(
DEFINITIONS HPX_HAVE_CXX20_CONSTEXPR_DESTRUCTOR
)
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_cxx26_experimental_scope(
DEFINITIONS HPX_HAVE_CXX26_EXPERIMENTAL_SCOPE
)

hpx_check_for_cxx_lambda_capture_decltype(
DEFINITIONS HPX_HAVE_CXX_LAMBDA_CAPTURE_DECLTYPE
)
Expand Down
16 changes: 11 additions & 5 deletions cmake/templates/conf.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
# serve to show the default.

import os
import sys
import re
import sys
import traceback
from datetime import datetime

# If extensions (or modules to document with autodoc) are in another directory,
Expand Down Expand Up @@ -102,10 +103,15 @@ for lib_dir in os.listdir(hpx_libs_dir):
for f in files:
if not file_regex.match(f) is None:
skip = True
for line in open(subdir_full + '/' + f):
if '/// \\' in line:
skip = False
break
try:
for line in open(subdir_full + '/' + f):
if '/// \\' in line:
skip = False
break
except:
print('Exception while processing %s' % f)
print(traceback.format_exc())

if skip:
continue
breathe_projects_source[module][1].append(subdir + '/' + f)
Expand Down
19 changes: 19 additions & 0 deletions cmake/tests/cxx20_constexpr_destructor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024 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>

struct A
{
constexpr A() noexcept {}
constexpr ~A() {}
};

int main()
{
[[maybe_unused]] A a;
return 0;
}
22 changes: 22 additions & 0 deletions cmake/tests/cxx26_experimental_scope.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2024 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::experimental::scope_xxx

#include <experimental/scope>

#if !defined(__cpp_lib_experimental_scope)
# error "__cpp_lib_experimental_scope not defined, assume scope_exit etc. is not supported"
#endif

int main()
{
std::experimental::scope_exit se([] {});
std::experimental::scope_failure sf([] {});
std::experimental::scope_success ss([] {});

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <hpx/components_base/server/locking_hook.hpp>
#include <hpx/modules/collectives.hpp>
#include <hpx/modules/errors.hpp>
#include <hpx/modules/functional.hpp>
#include <hpx/preprocessor/cat.hpp>
#include <hpx/preprocessor/expand.hpp>
#include <hpx/preprocessor/nargs.hpp>
Expand Down Expand Up @@ -203,22 +204,6 @@ namespace hpx { namespace server {
/// \return Return the value of the element at position represented
/// by \a pos.
///
struct erase_on_exit
{
erase_on_exit(data_type& m, typename data_type::iterator& it)
: m_(m)
, it_(it)
{
}
~erase_on_exit()
{
m_.erase(it_);
}

data_type& m_;
typename data_type::iterator& it_;
};

T get_value(Key const& key, bool erase)
{
typename data_type::iterator it =
Expand All @@ -234,7 +219,9 @@ namespace hpx { namespace server {
if (!erase)
return it->second;

erase_on_exit t(partition_unordered_map_, it);
auto on_exit = hpx::experimental::scope_exit(
[this, &it] { partition_unordered_map_.erase(it); });

return it->second;
}

Expand Down
30 changes: 11 additions & 19 deletions libs/core/algorithms/include/hpx/parallel/task_group.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2023 Hartmut Kaiser
// Copyright (c) 2021-2024 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -16,6 +16,7 @@
#include <hpx/execution_base/execution.hpp>
#include <hpx/execution_base/traits/is_executor.hpp>
#include <hpx/executors/parallel_executor.hpp>
#include <hpx/functional/experimental/scope_exit.hpp>
#include <hpx/functional/invoke_fused.hpp>
#include <hpx/futures/detail/future_data.hpp>
#include <hpx/modules/memory.hpp>
Expand Down Expand Up @@ -44,21 +45,6 @@ namespace hpx::experimental {
task_group& operator=(task_group const&) = delete;
task_group& operator=(task_group&&) = delete;

private:
struct on_exit
{
HPX_CORE_EXPORT explicit on_exit(task_group& tg);
HPX_CORE_EXPORT ~on_exit();

on_exit(on_exit const& rhs) = delete;
on_exit& operator=(on_exit const& rhs) = delete;

HPX_CORE_EXPORT on_exit(on_exit&& rhs) noexcept;
HPX_CORE_EXPORT on_exit& operator=(on_exit&& rhs) noexcept;

hpx::lcos::local::latch* latch_;
};

public:
/// \brief Adds a task to compute \c f() and returns immediately.
///
Expand All @@ -82,13 +68,19 @@ namespace hpx::experimental {
void run(Executor&& exec, F&& f, Ts&&... ts)
{
// make sure exceptions don't leave the latch in the wrong state
on_exit l(*this);
if (latch_.reset_if_needed_and_count_up(1, 1))
{
has_arrived_.store(false, std::memory_order_release);
}

auto on_exit =
hpx::experimental::scope_exit([this] { latch_.count_down(1); });

hpx::parallel::execution::post(HPX_FORWARD(Executor, exec),
[this, l = HPX_MOVE(l), f = HPX_FORWARD(F, f),
[this, on_exit = HPX_MOVE(on_exit), f = HPX_FORWARD(F, f),
t = hpx::make_tuple(HPX_FORWARD(Ts, ts)...)]() mutable {
// latch needs to be released before the lambda exits
on_exit _(HPX_MOVE(l));
auto _(HPX_MOVE(on_exit));

hpx::detail::try_catch_exception_ptr(
[&]() { hpx::invoke_fused(HPX_MOVE(f), HPX_MOVE(t)); },
Expand Down
33 changes: 1 addition & 32 deletions libs/core/algorithms/src/task_group.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2023 Hartmut Kaiser
// Copyright (c) 2021-2024 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -16,37 +16,6 @@

namespace hpx::experimental {

///////////////////////////////////////////////////////////////////////////
task_group::on_exit::on_exit(task_group& tg)
: latch_(&tg.latch_)
{
if (latch_->reset_if_needed_and_count_up(1, 1))
{
tg.has_arrived_.store(false, std::memory_order_release);
}
}

task_group::on_exit::~on_exit()
{
if (latch_ != nullptr)
{
latch_->count_down(1);
}
}

task_group::on_exit::on_exit(on_exit&& rhs) noexcept
: latch_(rhs.latch_)
{
rhs.latch_ = nullptr;
}

task_group::on_exit& task_group::on_exit::operator=(on_exit&& rhs) noexcept
{
latch_ = rhs.latch_;
rhs.latch_ = nullptr;
return *this;
}

///////////////////////////////////////////////////////////////////////////
task_group::task_group()
: latch_(1)
Expand Down
1 change: 1 addition & 0 deletions libs/core/algorithms/tests/unit/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ set(tests
is_heap_until
includes
inclusive_scan
inclusive_scan_exception
inplace_merge
is_partitioned
is_sorted
Expand Down
89 changes: 41 additions & 48 deletions libs/core/algorithms/tests/unit/algorithms/inclusive_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,50 +75,6 @@ void inclusive_scan_test3()
test_inclusive_scan3<std::forward_iterator_tag>();
}

///////////////////////////////////////////////////////////////////////////////
template <typename IteratorTag>
void test_inclusive_scan_exception()
{
using namespace hpx::execution;

// If the execution policy object is of type vector_execution_policy,
// std::terminate shall be called. therefore we do not test exceptions
// with a vector execution policy
test_inclusive_scan_exception(seq, IteratorTag());
test_inclusive_scan_exception(par, IteratorTag());

test_inclusive_scan_exception_async(seq(task), IteratorTag());
test_inclusive_scan_exception_async(par(task), IteratorTag());
}

void inclusive_scan_exception_test()
{
test_inclusive_scan_exception<std::random_access_iterator_tag>();
test_inclusive_scan_exception<std::forward_iterator_tag>();
}

///////////////////////////////////////////////////////////////////////////////
template <typename IteratorTag>
void test_inclusive_scan_bad_alloc()
{
using namespace hpx::execution;

// If the execution policy object is of type vector_execution_policy,
// std::terminate shall be called. therefore we do not test exceptions
// with a vector execution policy
test_inclusive_scan_bad_alloc(seq, IteratorTag());
test_inclusive_scan_bad_alloc(par, IteratorTag());

test_inclusive_scan_bad_alloc_async(seq(task), IteratorTag());
test_inclusive_scan_bad_alloc_async(par(task), IteratorTag());
}

void inclusive_scan_bad_alloc_test()
{
test_inclusive_scan_bad_alloc<std::random_access_iterator_tag>();
test_inclusive_scan_bad_alloc<std::forward_iterator_tag>();
}

////////////////////////////////////////////////////////////////////////////////
void inclusive_scan_validate()
{
Expand All @@ -133,6 +89,46 @@ void inclusive_scan_validate()
test_inclusive_scan_validate(hpx::execution::par, a, a);
}

///////////////////////////////////////////////////////////////////////////////
void inclusive_scan_benchmark()
{
try
{
#if defined(HPX_DEBUG)
std::vector<double> c(1000000);
#else
std::vector<double> c(100000000);
#endif
std::vector<double> d(c.size());
std::fill(std::begin(c), std::end(c), 1.0);

double const val(0);
auto op = [](double v1, double v2) { return v1 + v2; };

hpx::chrono::high_resolution_timer t;
hpx::inclusive_scan(hpx::execution::par, std::begin(c), std::end(c),
std::begin(d), op, val);
double elapsed = t.elapsed();

// verify values
std::vector<double> e(c.size());
hpx::parallel::detail::sequential_inclusive_scan(
std::begin(c), std::end(c), std::begin(e), val, op);

bool ok = std::equal(std::begin(d), std::end(d), std::begin(e));
HPX_TEST(ok);
if (ok)
{
// CDash graph plotting
hpx::util::print_cdash_timing("InclusiveScanTime", elapsed);
}
}
catch (...)
{
HPX_TEST(false);
}
}

///////////////////////////////////////////////////////////////////////////////
int hpx_main(hpx::program_options::variables_map& vm)
{
Expand All @@ -147,9 +143,6 @@ int hpx_main(hpx::program_options::variables_map& vm)
inclusive_scan_test2();
inclusive_scan_test3();

inclusive_scan_exception_test();
inclusive_scan_bad_alloc_test();

inclusive_scan_validate();
inclusive_scan_benchmark();

Expand All @@ -166,7 +159,7 @@ int main(int argc, char* argv[])
desc_commandline.add_options()("seed,s", value<unsigned int>(),
"the random number generator seed to use for this run");

// By default this test should run on all available cores
// By default, this test should run on all available cores
std::vector<std::string> const cfg = {"hpx.os_threads=all"};

// Initialize and run HPX
Expand Down
Loading

0 comments on commit 6f6f287

Please sign in to comment.