Skip to content

Commit 8ffc778

Browse files
authored
Merge pull request STEllAR-GROUP#6650 from STEllAR-GROUP/partitioned_vector_constructors
Adding constructor overloads to partitioned_vector
2 parents c88c392 + 34ac869 commit 8ffc778

File tree

10 files changed

+178
-23
lines changed

10 files changed

+178
-23
lines changed

components/containers/partitioned_vector/include/hpx/components/containers/partitioned_vector/partitioned_vector_component_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2014 Anuj R. Sharma
2-
// Copyright (c) 2014-2024 Hartmut Kaiser
2+
// Copyright (c) 2014-2025 Hartmut Kaiser
33
//
44
// SPDX-License-Identifier: BSL-1.0
55
// Distributed under the Boost Software License, Version 1.0. (See accompanying

components/containers/partitioned_vector/include/hpx/components/containers/partitioned_vector/partitioned_vector_decl.hpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2014 Anuj R. Sharma
2-
// Copyright (c) 2014-2024 Hartmut Kaiser
2+
// Copyright (c) 2014-2025 Hartmut Kaiser
33
//
44
// SPDX-License-Identifier: BSL-1.0
55
// Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -410,17 +410,26 @@ namespace hpx {
410410
///
411411
/// \param size The overall size of the vector
412412
/// \param val Default value for the elements in vector
413-
/// \param symbolic_name The (optional) name to register the newly
414-
/// created vector
415413
///
416414
partitioned_vector(size_type size, T const& val);
417415

416+
/// Constructor which creates and initializes vector of a size as given
417+
/// by the range, where all elements are initialized with the values
418+
/// from the given range [begin, end) and using the given distribution
419+
/// policy.
420+
///
421+
/// \param begin Start of range to use for initializing the
422+
/// new instance.
423+
/// \param end End of range to use for initializing the
424+
/// new instance.
425+
///
426+
partitioned_vector(typename Data::const_iterator begin,
427+
typename Data::const_iterator end);
428+
418429
/// Constructor which creates vector of \a size using the given
419430
/// distribution policy.
420431
///
421432
/// \param policy The distribution policy to use
422-
/// \param symbolic_name The (optional) name to register the newly
423-
/// created vector
424433
///
425434
template <typename DistPolicy>
426435
explicit partitioned_vector(DistPolicy const& policy,
@@ -432,8 +441,6 @@ namespace hpx {
432441
///
433442
/// \param size The overall size of the vector
434443
/// \param policy The distribution policy to use
435-
/// \param symbolic_name The (optional) name to register the newly
436-
/// created vector
437444
///
438445
template <typename DistPolicy>
439446
partitioned_vector(size_type size, DistPolicy const& policy,
@@ -447,15 +454,30 @@ namespace hpx {
447454
/// \param size The overall size of the vector
448455
/// \param val Default value for the elements in vector
449456
/// \param policy The distribution policy to use
450-
/// \param symbolic_name The (optional) name to register the newly
451-
/// created vector
452457
///
453458
template <typename DistPolicy>
454459
partitioned_vector(size_type size, T const& val,
455460
DistPolicy const& policy,
456461
std::enable_if_t<traits::is_distribution_policy_v<DistPolicy>>* =
457462
nullptr);
458463

464+
/// Constructor which creates and initializes vector of a size as given
465+
/// by the range, where all elements are initialized with the values
466+
/// from the given range [begin, end) and using the given distribution
467+
/// policy.
468+
///
469+
/// \param begin Start of range to use for initializing the
470+
/// new instance.
471+
/// \param end End of range to use for initializing the
472+
/// new instance.
473+
/// \param policy The distribution policy to use
474+
///
475+
template <typename DistPolicy>
476+
partitioned_vector(typename Data::const_iterator begin,
477+
typename Data::const_iterator end, DistPolicy const& policy,
478+
std::enable_if_t<traits::is_distribution_policy_v<DistPolicy>>* =
479+
nullptr);
480+
459481
/// Copy construction performs a deep copy of the right hand side
460482
/// vector.
461483
partitioned_vector(partitioned_vector const& rhs)
@@ -467,14 +489,13 @@ namespace hpx {
467489
}
468490

469491
partitioned_vector(partitioned_vector&& rhs) noexcept
470-
: base_type(HPX_MOVE(rhs))
492+
: base_type(HPX_MOVE(static_cast<base_type&&>(rhs)))
471493
, size_(rhs.size_)
472494
, partitions_(HPX_MOVE(rhs.partitions_))
473495
{
474496
rhs.size_ = 0;
475497
}
476498

477-
public:
478499
/// \brief Array subscript operator. This does not throw any exception.
479500
///
480501
/// \param pos Position of the element in the vector [Note the first

components/containers/partitioned_vector/include/hpx/components/containers/partitioned_vector/partitioned_vector_impl.hpp

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2014 Anuj R. Sharma
2-
// Copyright (c) 2014-2024 Hartmut Kaiser
2+
// Copyright (c) 2014-2025 Hartmut Kaiser
33
//
44
// SPDX-License-Identifier: BSL-1.0
55
// Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -534,6 +534,34 @@ namespace hpx {
534534
create(val, hpx::container_layout);
535535
}
536536

537+
template <typename T, typename Data /*= std::vector<T> */>
538+
HPX_PARTITIONED_VECTOR_SPECIALIZATION_EXPORT
539+
partitioned_vector<T, Data>::partitioned_vector(
540+
typename Data::const_iterator begin, typename Data::const_iterator end)
541+
: size_(std::distance(begin, end))
542+
{
543+
if (size_ != 0)
544+
{
545+
// create all partitions
546+
create(hpx::container_layout);
547+
548+
// fill partitions with their part of the data
549+
std::vector<std::size_t> const empty;
550+
std::vector<hpx::future<void>> futures;
551+
futures.reserve(partitions_.size());
552+
for (std::size_t i = 0; i != partitions_.size(); ++i)
553+
{
554+
HPX_ASSERT(static_cast<std::size_t>(std::distance(
555+
begin, end)) >= partitions_[i].size_);
556+
auto const end_part = std::next(begin, partitions_[i].size_);
557+
futures.push_back(set_values(i, empty, Data(begin, end_part)));
558+
begin = end_part;
559+
}
560+
HPX_ASSERT(begin == end);
561+
hpx::wait_all(HPX_MOVE(futures));
562+
}
563+
}
564+
537565
template <typename T, typename Data /*= std::vector<T> */>
538566
template <typename DistPolicy>
539567
HPX_PARTITIONED_VECTOR_SPECIALIZATION_EXPORT
@@ -549,11 +577,10 @@ namespace hpx {
549577
template <typename T, typename Data /*= std::vector<T> */>
550578
template <typename DistPolicy>
551579
HPX_PARTITIONED_VECTOR_SPECIALIZATION_EXPORT
552-
partitioned_vector<T, Data>::partitioned_vector(DistPolicy const& policy,
580+
partitioned_vector<T, Data>::partitioned_vector(DistPolicy const&,
553581
std::enable_if_t<traits::is_distribution_policy_v<DistPolicy>>*)
554582
: size_(0)
555583
{
556-
reserve(policy);
557584
}
558585

559586
template <typename T, typename Data /*= std::vector<T> */>
@@ -568,4 +595,35 @@ namespace hpx {
568595
if (size != 0)
569596
create(val, policy);
570597
}
598+
599+
template <typename T, typename Data /*= std::vector<T> */>
600+
template <typename DistPolicy>
601+
partitioned_vector<T, Data>::partitioned_vector(
602+
typename Data::const_iterator begin, typename Data::const_iterator end,
603+
DistPolicy const& policy,
604+
std::enable_if_t<
605+
traits::is_distribution_policy_v<DistPolicy>>* /*= nullptr*/)
606+
: size_(std::distance(begin, end))
607+
{
608+
if (size_ != 0)
609+
{
610+
// create all partitions
611+
create(policy);
612+
613+
// fill partitions with their part of the data
614+
std::vector<std::size_t> const empty;
615+
std::vector<hpx::future<void>> futures;
616+
futures.reserve(partitions_.size());
617+
for (std::size_t i = 0; i != partitions_.size(); ++i)
618+
{
619+
HPX_ASSERT(static_cast<std::size_t>(std::distance(
620+
begin, end)) >= partitions_[i].size_);
621+
auto const end_part = std::next(begin, partitions_[i].size_);
622+
futures.push_back(set_values(i, empty, Data(begin, end_part)));
623+
begin = end_part;
624+
}
625+
HPX_ASSERT(begin == end);
626+
hpx::wait_all(HPX_MOVE(futures));
627+
}
628+
}
571629
} // namespace hpx

components/containers/partitioned_vector/include/hpx/components/containers/partitioned_vector/partitioned_vector_predef.hpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2014 Anuj R. Sharma
2-
// Copyright (c) 2014-2024 Hartmut Kaiser
2+
// Copyright (c) 2014-2025 Hartmut Kaiser
33
//
44
// SPDX-License-Identifier: BSL-1.0
55
// Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -43,6 +43,10 @@ extern template hpx::partitioned_vector<double,
4343
extern template hpx::partitioned_vector<double,
4444
std::vector<double>>::partitioned_vector(size_type, double const&,
4545
hpx::explicit_container_distribution_policy const&, void*);
46+
extern template hpx::partitioned_vector<double, std::vector<double>>::
47+
partitioned_vector(std::vector<double>::const_iterator,
48+
std::vector<double>::const_iterator,
49+
hpx::explicit_container_distribution_policy const&, void*);
4650

4751
// partitioned_vector<int>
4852
HPX_REGISTER_PARTITIONED_VECTOR_DECLARATION(int)
@@ -62,6 +66,10 @@ extern template hpx::partitioned_vector<int,
6266
extern template hpx::partitioned_vector<int,
6367
std::vector<int>>::partitioned_vector(size_type, int const&,
6468
hpx::explicit_container_distribution_policy const&, void*);
69+
extern template hpx::partitioned_vector<int,
70+
std::vector<int>>::partitioned_vector(std::vector<int>::const_iterator,
71+
std::vector<int>::const_iterator,
72+
hpx::explicit_container_distribution_policy const&, void*);
6573

6674
// partitioned_vector<long long>
6775
using long_long = long long;
@@ -85,6 +93,10 @@ extern template hpx::partitioned_vector<long long,
8593
extern template hpx::partitioned_vector<long long,
8694
std::vector<long long>>::partitioned_vector(size_type, long long const&,
8795
hpx::explicit_container_distribution_policy const&, void*);
96+
extern template hpx::partitioned_vector<long long, std::vector<long long>>::
97+
partitioned_vector(std::vector<long long>::const_iterator,
98+
std::vector<long long>::const_iterator,
99+
hpx::explicit_container_distribution_policy const&, void*);
88100

89101
// partitioned_vector<std::string>
90102
using partitioned_vector_std_string_argument = std::string;
@@ -110,5 +122,9 @@ extern template hpx::partitioned_vector<std::string,
110122
extern template hpx::partitioned_vector<std::string,
111123
std::vector<std::string>>::partitioned_vector(size_type, std::string const&,
112124
hpx::explicit_container_distribution_policy const&, void*);
125+
extern template hpx::partitioned_vector<std::string, std::vector<std::string>>::
126+
partitioned_vector(std::vector<std::string>::const_iterator,
127+
std::vector<std::string>::const_iterator,
128+
hpx::explicit_container_distribution_policy const&, void*);
113129

114130
#endif

components/containers/partitioned_vector/src/partitioned_vector_component_double.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Hartmut Kaiser
1+
// Copyright (c) 2017-2025 Hartmut Kaiser
22
//
33
// SPDX-License-Identifier: BSL-1.0
44
// Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -43,6 +43,10 @@ hpx::partitioned_vector<double, std::vector<double>>::partitioned_vector(
4343
template HPX_PARTITIONED_VECTOR_EXPORT hpx::partitioned_vector<double,
4444
std::vector<double>>::partitioned_vector(size_type, double const&,
4545
hpx::explicit_container_distribution_policy const&, void*);
46+
template HPX_PARTITIONED_VECTOR_EXPORT
47+
hpx::partitioned_vector<double, std::vector<double>>::partitioned_vector(
48+
std::vector<double>::const_iterator, std::vector<double>::const_iterator,
49+
hpx::explicit_container_distribution_policy const&, void*);
4650

4751
#if defined(HPX_MSVC)
4852
#pragma warning(pop)

components/containers/partitioned_vector/src/partitioned_vector_component_int.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Hartmut Kaiser
1+
// Copyright (c) 2017-2025 Hartmut Kaiser
22
//
33
// SPDX-License-Identifier: BSL-1.0
44
// Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -43,8 +43,12 @@ template HPX_PARTITIONED_VECTOR_EXPORT
4343
hpx::partitioned_vector<int, std::vector<int>>::partitioned_vector(
4444
size_type, hpx::explicit_container_distribution_policy const&, void*);
4545
template HPX_PARTITIONED_VECTOR_EXPORT
46+
hpx::partitioned_vector<int, std::vector<int>>::partitioned_vector(size_type,
47+
int const&, hpx::explicit_container_distribution_policy const&, void*);
48+
template HPX_PARTITIONED_VECTOR_EXPORT
4649
hpx::partitioned_vector<int, std::vector<int>>::partitioned_vector(
47-
size_type, int const&, hpx::explicit_container_distribution_policy const&, void*);
50+
std::vector<int>::const_iterator, std::vector<int>::const_iterator,
51+
hpx::explicit_container_distribution_policy const&, void*);
4852

4953
template class HPX_PARTITIONED_VECTOR_EXPORT
5054
hpx::server::partitioned_vector<long long, std::vector<long long>>;
@@ -64,6 +68,11 @@ hpx::partitioned_vector<long long, std::vector<long long>>::partitioned_vector(
6468
template HPX_PARTITIONED_VECTOR_EXPORT hpx::partitioned_vector<long long,
6569
std::vector<long long>>::partitioned_vector(size_type, long long const&,
6670
hpx::explicit_container_distribution_policy const&, void*);
71+
template HPX_PARTITIONED_VECTOR_EXPORT
72+
hpx::partitioned_vector<long long, std::vector<long long>>::partitioned_vector(
73+
std::vector<long long>::const_iterator,
74+
std::vector<long long>::const_iterator,
75+
hpx::explicit_container_distribution_policy const&, void*);
6776

6877
#if defined(HPX_MSVC)
6978
#pragma warning(pop)

components/containers/partitioned_vector/src/partitioned_vector_component_std_string.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Hartmut Kaiser
1+
// Copyright (c) 2017-2025 Hartmut Kaiser
22
//
33
// SPDX-License-Identifier: BSL-1.0
44
// Distributed under the Boost Software License, Version 1.0. (See accompanying
@@ -46,6 +46,11 @@ template HPX_PARTITIONED_VECTOR_EXPORT hpx::partitioned_vector<std::string,
4646
template HPX_PARTITIONED_VECTOR_EXPORT hpx::partitioned_vector<std::string,
4747
std::vector<std::string>>::partitioned_vector(size_type, std::string const&,
4848
hpx::explicit_container_distribution_policy const&, void*);
49+
template HPX_PARTITIONED_VECTOR_EXPORT
50+
hpx::partitioned_vector<std::string, std::vector<std::string>>::
51+
partitioned_vector(std::vector<std::string>::const_iterator,
52+
std::vector<std::string>::const_iterator,
53+
hpx::explicit_container_distribution_policy const&, void*);
4954

5055
#if defined(HPX_MSVC)
5156
#pragma warning(pop)

components/containers/partitioned_vector/tests/regressions/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
# Copyright (c) 2007-2017 Hartmut Kaiser
1+
# Copyright (c) 2007-2025 Hartmut Kaiser
22
#
33
# SPDX-License-Identifier: BSL-1.0
44
# Distributed under the Boost Software License, Version 1.0. (See accompanying
55
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
66

7-
set(tests partitioned_vector_2201)
7+
set(tests partitioned_vector_2201 partitioned_vector_constructor_6650)
88

99
set(partitioned_vector_2201_FLAGS COMPONENT_DEPENDENCIES partitioned_vector)
10+
set(partitioned_vector_constructor_6650_FLAGS COMPONENT_DEPENDENCIES
11+
partitioned_vector
12+
)
1013

1114
foreach(test ${tests})
1215
set(sources ${test}.cpp)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2025 Hartmut Kaiser
2+
//
3+
// SPDX-License-Identifier: BSL-1.0
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#include <hpx/config.hpp>
8+
9+
#if !defined(HPX_COMPUTE_DEVICE_CODE)
10+
#include <hpx/hpx_main.hpp>
11+
#include <hpx/include/partitioned_vector.hpp>
12+
#include <hpx/modules/runtime_distributed.hpp>
13+
14+
///////////////////////////////////////////////////////////////////////////////
15+
int main()
16+
{
17+
// create as many partitions as we have localities
18+
[[maybe_unused]] hpx::partitioned_vector<int> data(
19+
hpx::container_layout(hpx::find_all_localities()));
20+
21+
return 0;
22+
}
23+
24+
#else
25+
26+
int main()
27+
{
28+
return 0;
29+
}
30+
31+
#endif

libs/full/segmented_algorithms/tests/unit/partitioned_vector_copy.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (c) 2014-2017 Hartmut Kaiser
1+
// Copyright (c) 2014-2025 Hartmut Kaiser
22
//
33
// SPDX-License-Identifier: BSL-1.0
44
// Distributed under the Boost Software License, Version 1.0. (See accompanying
55
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
66

77
#include <hpx/config.hpp>
8+
89
#if !defined(HPX_COMPUTE_DEVICE_CODE)
910
#include <hpx/hpx_main.hpp>
1011
#include <hpx/include/parallel_copy.hpp>
@@ -153,6 +154,12 @@ void copy_tests()
153154
copy_tests(v);
154155
}
155156

157+
{
158+
std::vector<T> data(length, T(42));
159+
hpx::partitioned_vector<T> v(data.begin(), data.end());
160+
copy_tests(v);
161+
}
162+
156163
copy_tests_with_policy<T>(length, 1, hpx::container_layout);
157164
copy_tests_with_policy<T>(length, 3, hpx::container_layout(3));
158165
copy_tests_with_policy<T>(length, 3, hpx::container_layout(3, localities));
@@ -168,4 +175,5 @@ int main()
168175

169176
return hpx::util::report_errors();
170177
}
178+
171179
#endif

0 commit comments

Comments
 (0)