Skip to content

Commit

Permalink
Merge pull request #5371 from Jedi18/adapt_uninitialized_copy
Browse files Browse the repository at this point in the history
Adapt uninitialized_copy and uninitialized_copy_n to C++ 20
  • Loading branch information
msimberg authored Jul 8, 2021
2 parents 3caa624 + c453ed8 commit fc37a2f
Show file tree
Hide file tree
Showing 12 changed files with 1,649 additions and 174 deletions.
9 changes: 7 additions & 2 deletions docs/sphinx/api/public_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,8 @@ more information about the parallel algorithms.
Functions
---------

- :cpp:func:`hpx::parallel::v1::uninitialized_copy`
- :cpp:func:`hpx::parallel::v1::uninitialized_copy_n`
- :cpp:func:`hpx::uninitialized_copy`
- :cpp:func:`hpx::uninitialized_copy_n`
- :cpp:func:`hpx::uninitialized_default_construct`
- :cpp:func:`hpx::uninitialized_default_construct_n`
- :cpp:func:`hpx::parallel::v1::uninitialized_fill`
Expand All @@ -576,13 +576,18 @@ Functions
- :cpp:func:`hpx::uninitialized_value_construct`
- :cpp:func:`hpx::uninitialized_value_construct_n`

- :cpp:func:`hpx::ranges::uninitialized_copy`
- :cpp:func:`hpx::ranges::uninitialized_copy_n`
- :cpp:func:`hpx::ranges::uninitialized_default_construct`
- :cpp:func:`hpx::ranges::uninitialized_default_construct_n`
- :cpp:func:`hpx::ranges::uninitialized_move`
- :cpp:func:`hpx::ranges::uninitialized_move_n`
- :cpp:func:`hpx::ranges::uninitialized_value_construct`
- :cpp:func:`hpx::ranges::uninitialized_value_construct_n`

- :cpp:func:`hpx::ranges::uninitialized_copy`
- :cpp:func:`hpx::ranges::uninitialized_copy_n`

Header ``hpx/numeric.hpp``
==========================

Expand Down
4 changes: 2 additions & 2 deletions docs/sphinx/manual/writing_single_node_hpx_applications.rst
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,11 @@ Parallel algorithms
* Destroys a range of objects.
* ``<hpx/memory.hpp>``
* :cppreference-memory:`destroy_n`
* * :cpp:func:`hpx::parallel::v1::uninitialized_copy`
* * :cpp:func:`hpx::uninitialized_copy`
* Copies a range of objects to an uninitialized area of memory.
* ``<hpx/memory.hpp>``
* :cppreference-memory:`uninitialized_copy`
* * :cpp:func:`hpx::parallel::v1::uninitialized_copy_n`
* * :cpp:func:`hpx::uninitialized_copy_n`
* Copies a number of objects to an uninitialized area of memory.
* ``<hpx/memory.hpp>``
* :cppreference-memory:`uninitialized_copy_n`
Expand Down
2 changes: 0 additions & 2 deletions libs/full/include_local/include/hpx/local/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <hpx/parallel/memory.hpp>

namespace hpx {
using hpx::parallel::uninitialized_copy;
using hpx::parallel::uninitialized_copy_n;
using hpx::parallel::uninitialized_fill;
using hpx::parallel::uninitialized_fill_n;
} // namespace hpx
1 change: 1 addition & 0 deletions libs/parallelism/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ set(algorithms_headers
hpx/parallel/container_algorithms/stable_sort.hpp
hpx/parallel/container_algorithms/transform.hpp
hpx/parallel/container_algorithms/transform_reduce.hpp
hpx/parallel/container_algorithms/uninitialized_copy.hpp
hpx/parallel/container_algorithms/uninitialized_default_construct.hpp
hpx/parallel/container_algorithms/uninitialized_move.hpp
hpx/parallel/container_algorithms/uninitialized_value_construct.hpp
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
#include <hpx/parallel/memory.hpp>

#include <hpx/parallel/container_algorithms/destroy.hpp>
#include <hpx/parallel/container_algorithms/uninitialized_copy.hpp>
#include <hpx/parallel/container_algorithms/uninitialized_default_construct.hpp>
#include <hpx/parallel/container_algorithms/uninitialized_move.hpp>
#include <hpx/parallel/container_algorithms/uninitialized_value_construct.hpp>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void test_uninitialized_copy(ExPolicy&& policy, IteratorTag)
std::vector<std::size_t> c(10007);
std::vector<std::size_t> d(c.size());
std::iota(std::begin(c), std::end(c), std::rand());
hpx::parallel::uninitialized_copy(std::forward<ExPolicy>(policy),
hpx::uninitialized_copy(std::forward<ExPolicy>(policy),
iterator(std::begin(c)), iterator(std::end(c)), std::begin(d));

std::size_t count = 0;
Expand All @@ -57,7 +57,7 @@ void test_uninitialized_copy_async(ExPolicy&& p, IteratorTag)
std::iota(std::begin(c), std::end(c), std::rand());

hpx::future<base_iterator> f =
hpx::parallel::uninitialized_copy(std::forward<ExPolicy>(p),
hpx::uninitialized_copy(std::forward<ExPolicy>(p),
iterator(std::begin(c)), iterator(std::end(c)), std::begin(d));
f.wait();

Expand Down Expand Up @@ -92,7 +92,7 @@ void test_uninitialized_copy_exception(ExPolicy policy, IteratorTag)
bool caught_exception = false;
try
{
hpx::parallel::uninitialized_copy(policy,
hpx::uninitialized_copy(policy,
decorated_iterator(std::begin(c),
[&throw_after]() {
if (throw_after-- == 0)
Expand Down Expand Up @@ -133,7 +133,7 @@ void test_uninitialized_copy_exception_async(ExPolicy p, IteratorTag)
bool returned_from_algorithm = false;
try
{
hpx::future<base_iterator> f = hpx::parallel::uninitialized_copy(p,
hpx::future<base_iterator> f = hpx::uninitialized_copy(p,
decorated_iterator(std::begin(c),
[&throw_after]() {
if (throw_after-- == 0)
Expand Down Expand Up @@ -182,7 +182,7 @@ void test_uninitialized_copy_bad_alloc(ExPolicy policy, IteratorTag)
bool caught_bad_alloc = false;
try
{
hpx::parallel::uninitialized_copy(policy,
hpx::uninitialized_copy(policy,
decorated_iterator(std::begin(c),
[&throw_after]() {
if (throw_after-- == 0)
Expand Down Expand Up @@ -223,7 +223,7 @@ void test_uninitialized_copy_bad_alloc_async(ExPolicy p, IteratorTag)
bool returned_from_algorithm = false;
try
{
hpx::future<base_iterator> f = hpx::parallel::uninitialized_copy(p,
hpx::future<base_iterator> f = hpx::uninitialized_copy(p,
decorated_iterator(std::begin(c),
[&throw_after]() {
if (throw_after-- == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void test_uninitialized_copy_n(ExPolicy policy, IteratorTag)
std::vector<std::size_t> d(c.size());
std::iota(std::begin(c), std::end(c), std::rand());

hpx::parallel::uninitialized_copy_n(
hpx::uninitialized_copy_n(
policy, iterator(std::begin(c)), c.size(), std::begin(d));

std::size_t count = 0;
Expand All @@ -55,7 +55,7 @@ void test_uninitialized_copy_n_async(ExPolicy p, IteratorTag)
std::vector<std::size_t> d(c.size());
std::iota(std::begin(c), std::end(c), std::rand());

hpx::future<base_iterator> f = hpx::parallel::uninitialized_copy_n(
hpx::future<base_iterator> f = hpx::uninitialized_copy_n(
p, iterator(std::begin(c)), c.size(), std::begin(d));
f.wait();

Expand Down Expand Up @@ -109,7 +109,7 @@ void test_uninitialized_copy_n_exception(ExPolicy policy, IteratorTag)
bool caught_exception = false;
try
{
hpx::parallel::uninitialized_copy_n(policy,
hpx::uninitialized_copy_n(policy,
decorated_iterator(std::begin(c),
[&throw_after]() {
if (throw_after-- == 0)
Expand Down Expand Up @@ -150,7 +150,7 @@ void test_uninitialized_copy_n_exception_async(ExPolicy p, IteratorTag)
bool returned_from_algorithm = false;
try
{
hpx::future<base_iterator> f = hpx::parallel::uninitialized_copy_n(p,
hpx::future<base_iterator> f = hpx::uninitialized_copy_n(p,
decorated_iterator(std::begin(c),
[&throw_after]() {
if (throw_after-- == 0)
Expand Down Expand Up @@ -220,7 +220,7 @@ void test_uninitialized_copy_n_bad_alloc(ExPolicy policy, IteratorTag)
bool caught_bad_alloc = false;
try
{
hpx::parallel::uninitialized_copy_n(policy,
hpx::uninitialized_copy_n(policy,
decorated_iterator(std::begin(c),
[&throw_after]() {
if (throw_after-- == 0)
Expand Down Expand Up @@ -261,7 +261,7 @@ void test_uninitialized_copy_n_bad_alloc_async(ExPolicy p, IteratorTag)
bool returned_from_algorithm = false;
try
{
hpx::future<base_iterator> f = hpx::parallel::uninitialized_copy_n(p,
hpx::future<base_iterator> f = hpx::uninitialized_copy_n(p,
decorated_iterator(std::begin(c),
[&throw_after]() {
if (throw_after-- == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ set(tests
transform_reduce_binary_exception_range
transform_reduce_binary_range
transform_reduce_range
uninitialized_copy_range
uninitialized_copy_n_range
uninitialized_default_construct_range
uninitialized_default_constructn_range
uninitialized_move_range
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright (c) 2014 Grant Mercer
// Copyright (c) 2015 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 <hpx/iterator_support/iterator_range.hpp>
#include <hpx/local/init.hpp>
#include <hpx/modules/testing.hpp>
#include <hpx/parallel/container_algorithms/uninitialized_copy.hpp>

#include <cstddef>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector>

#include <hpx/iterator_support/tests/iter_sent.hpp>
#include "test_utils.hpp"

////////////////////////////////////////////////////////////////////////////
template <typename IteratorTag>
void test_uninitialized_copy_n_sent(IteratorTag)
{
std::vector<std::size_t> c(10007);
std::vector<std::size_t> d(c.size());
std::iota(std::begin(c), std::end(c), std::rand());
std::copy(std::begin(c), std::end(c), std::rbegin(d));
std::size_t sent_len = (std::rand() % 10007) + 1;
hpx::ranges::uninitialized_copy_n(std::begin(c), sent_len, std::begin(d),
sentinel<std::size_t>{*(std::begin(d) + sent_len)});

std::size_t count = 0;
// loop till for sent_len since either the sentinel for the input or output iterator
// will be reached by then
HPX_TEST(std::equal(std::begin(c), std::begin(c) + sent_len, std::begin(d),
[&count](std::size_t v1, std::size_t v2) -> bool {
HPX_TEST_EQ(v1, v2);
++count;
return v1 == v2;
}));

HPX_TEST_EQ(count, sent_len);
}

template <typename ExPolicy, typename IteratorTag>
void test_uninitialized_copy_n_sent(ExPolicy&& policy, IteratorTag)
{
static_assert(hpx::is_execution_policy<ExPolicy>::value,
"hpx::is_execution_policy<ExPolicy>::value");

std::vector<std::size_t> c(10007);
std::vector<std::size_t> d(c.size());
std::iota(std::begin(c), std::end(c), std::rand());
std::copy(std::begin(c), std::end(c), std::rbegin(d));
std::size_t sent_len = (std::rand() % 10007) + 1;
hpx::ranges::uninitialized_copy_n(policy, std::begin(c), sent_len,
std::begin(d), sentinel<std::size_t>{*(std::begin(d) + sent_len)});

std::size_t count = 0;
// loop till for sent_len since either the sentinel for the input or output iterator
// will be reached by then
HPX_TEST(std::equal(std::begin(c), std::begin(c) + sent_len, std::begin(d),
[&count](std::size_t v1, std::size_t v2) -> bool {
HPX_TEST_EQ(v1, v2);
++count;
return v1 == v2;
}));

HPX_TEST_EQ(count, sent_len);
}

template <typename ExPolicy, typename IteratorTag>
void test_uninitialized_copy_n_sent_async(ExPolicy&& p, IteratorTag)
{
std::vector<std::size_t> c(10007);
std::vector<std::size_t> d(c.size());
std::iota(std::begin(c), std::end(c), std::rand());
std::copy(std::begin(c), std::end(c), std::rbegin(d));
std::size_t sent_len = (std::rand() % 10007) + 1;
auto f = hpx::ranges::uninitialized_copy_n(p, std::begin(c), sent_len,
std::begin(d), sentinel<std::size_t>{*(std::begin(d) + sent_len)});
f.wait();

std::size_t count = 0;
HPX_TEST(std::equal(std::begin(c), std::begin(c) + sent_len, std::begin(d),
[&count](std::size_t v1, std::size_t v2) -> bool {
HPX_TEST_EQ(v1, v2);
++count;
return v1 == v2;
}));
HPX_TEST_EQ(count, sent_len);
}

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

test_uninitialized_copy_n_sent(IteratorTag());

test_uninitialized_copy_n_sent(seq, IteratorTag());
test_uninitialized_copy_n_sent(par, IteratorTag());
test_uninitialized_copy_n_sent(par_unseq, IteratorTag());

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

void uninitialized_copy_n_sent_test()
{
test_uninitialized_copy_n_sent<std::random_access_iterator_tag>();
test_uninitialized_copy_n_sent<std::forward_iterator_tag>();
}

int hpx_main(hpx::program_options::variables_map& vm)
{
unsigned int seed = (unsigned int) std::time(nullptr);
if (vm.count("seed"))
seed = vm["seed"].as<unsigned int>();

std::cout << "using seed: " << seed << std::endl;
std::srand(seed);

uninitialized_copy_n_sent_test();
return hpx::local::finalize();
}

int main(int argc, char* argv[])
{
// add command line option which controls the random number generator seed
using namespace hpx::program_options;
options_description desc_commandline(
"Usage: " HPX_APPLICATION_STRING " [options]");

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
std::vector<std::string> const cfg = {"hpx.os_threads=all"};

// Initialize and run HPX
hpx::local::init_params init_args;
init_args.desc_cmdline = desc_commandline;
init_args.cfg = cfg;

HPX_TEST_EQ_MSG(hpx::local::init(hpx_main, argc, argv, init_args), 0,
"HPX main exited with non-zero status");

return hpx::util::report_errors();
}
Loading

0 comments on commit fc37a2f

Please sign in to comment.