Skip to content

Commit

Permalink
Fixing parallel::fill to make partitioned_vector serialization work
Browse files Browse the repository at this point in the history
- flyby: fixing partitioned vector serialization test
- flyby: simplify actual serialization code
  • Loading branch information
hkaiser committed Aug 1, 2017
1 parent 5845fa0 commit a5b25d0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 90 deletions.
Expand Up @@ -248,8 +248,6 @@ namespace hpx
friend class const_segment_vector_iterator<
T, Data, typename partitions_vector_type::const_iterator>;

friend struct hpx::serialization::detail::partitioned_vector_segmented_serializer;

std::size_t get_partition_size() const
{
std::size_t num_parts = partitions_.size();
Expand Down
3 changes: 2 additions & 1 deletion hpx/parallel/segmented_algorithms/fill.hpp
Expand Up @@ -61,10 +61,11 @@ namespace hpx { namespace parallel { inline namespace v1
typedef typename util::detail::algorithm_result<
ExPolicy
>::type result_type;
typedef typename std::iterator_traits<InIter>::value_type value_type;

return hpx::util::void_guard<result_type>(),
hpx::parallel::for_each(std::forward<ExPolicy>(policy),
first, last, fill_function<T>(value));
first, last, fill_function<value_type>(value));
}

// forward declare the non-segmented version of this algorithm
Expand Down
6 changes: 6 additions & 0 deletions hpx/runtime/components/client_base.hpp
Expand Up @@ -573,6 +573,12 @@ namespace hpx { namespace components
registered_name_.clear();
}

// Access registered name for the component
std::string const& registered_name() const
{
return registered_name_;
}

protected:
// will be set for created (non-attached) objects
std::string registered_name_;
Expand Down
94 changes: 18 additions & 76 deletions hpx/runtime/serialization/partitioned_vector.hpp
Expand Up @@ -7,94 +7,36 @@
#define HPX_SERIALIZATION_PARTITIONED_VECTOR_HPP

#include <hpx/config.hpp>
#include <hpx/components/containers/partitioned_vector/partitioned_vector.hpp>
#include <hpx/runtime/serialization/serialize.hpp>
#include <hpx/traits/is_bitwise_serializable.hpp>

#include <hpx/include/partitioned_vector.hpp>

#include <hpx/parallel/algorithms/is_partitioned.hpp>
#include <hpx/include/traits.hpp>
#include <hpx/traits/segmented_iterator_traits.hpp>

#include <hpx/throw_exception.hpp>

#include <string>

namespace hpx { namespace serialization
{
namespace detail
{
struct partitioned_vector_segmented_serializer
{
template<typename T>
static void serialize(input_archive & ar,
hpx::partitioned_vector<T> & v, std::false_type)
{
HPX_THROW_EXCEPTION(
hpx::invalid_status,
"hpx::serialization::serialize",
"requires segmented partitioned_vector");
}

template<typename T>
static void serialize(input_archive & ar,
hpx::partitioned_vector<T> & v, std::true_type)
{
std::string pvec_registered_name;
ar >> pvec_registered_name;
hpx::future<void> fconnect = v.connect_to(pvec_registered_name);
fconnect.wait();
}

template<typename T>
static void serialize(output_archive & ar,
const hpx::partitioned_vector<T> & v, std::false_type)
{
if(v.registered_name_.size() < 1) {

HPX_THROW_EXCEPTION(
hpx::invalid_status,
"hpx::serialization::serialize",
"partitioned_vector is not registered");

}

HPX_THROW_EXCEPTION(
hpx::invalid_status,
"hpx::serialization::serialize",
"requires segmented partitioned_vector");

}

template<typename T>
static void serialize(output_archive & ar,
const hpx::partitioned_vector<T> & v, std::true_type)
{
ar << v.registered_name_;
}
};
}

template <typename T>
void serialize(input_archive & ar, hpx::partitioned_vector<T> & v,
unsigned)
void serialize(input_archive& ar, hpx::partitioned_vector<T>& v, unsigned)
{
typedef typename hpx::partitioned_vector<T>::segment_iterator segment_iterator;
typedef hpx::traits::is_segmented_iterator<segment_iterator> is_segmented;

hpx::serialization::detail::partitioned_vector_segmented_serializer::serialize<T>(
ar, v, is_segmented());
std::string pvec_registered_name;
ar >> pvec_registered_name;
v.connect_to(pvec_registered_name).get();
}

template <typename T>
void serialize(output_archive & ar, const hpx::partitioned_vector<T> & v,
unsigned)
void serialize(
output_archive& ar, const hpx::partitioned_vector<T>& v, unsigned)
{
typedef typename hpx::partitioned_vector<T>::segment_iterator segment_iterator;
typedef hpx::traits::is_segmented_iterator<segment_iterator> is_segmented;

hpx::serialization::detail::partitioned_vector_segmented_serializer::serialize<T>(
ar, v, is_segmented());
std::string pvec_registered_name = v.registered_name();
if (pvec_registered_name.empty())
{
HPX_THROW_EXCEPTION(
hpx::invalid_status,
"hpx::serialization::serialize",
"partitioned_vector is not registered");
}
ar << pvec_registered_name;
}

}}

#endif
2 changes: 1 addition & 1 deletion tests/unit/serialization/CMakeLists.txt
Expand Up @@ -25,7 +25,7 @@ set(tests

add_subdirectory(polymorphic)

set(serialization_partitioned_vector_FLAGS DEPENDENCIES partitioned_vector)
set(serialization_partitioned_vector_FLAGS DEPENDENCIES partitioned_vector_component)
set(serialization_partitioned_vector_PARAMETERS THREADS_PER_LOCALITY 4)

foreach(test ${tests})
Expand Down
19 changes: 9 additions & 10 deletions tests/unit/serialization/serialization_partitioned_vector.cpp
Expand Up @@ -5,21 +5,18 @@

#include <hpx/config.hpp>

#include <hpx/parallel/algorithms/fill.hpp>
#include <hpx/include/parallel_fill.hpp>
#include <hpx/include/partitioned_vector.hpp>
#include <hpx/runtime/serialization/serialize.hpp>

#include <hpx/runtime/serialization/partitioned_vector.hpp>
#include <hpx/runtime/serialization/input_archive.hpp>
#include <hpx/runtime/serialization/output_archive.hpp>

#include <hpx/include/traits.hpp>
#include <hpx/traits/segmented_iterator_traits.hpp>
#include <hpx/include/partitioned_vector.hpp>

#include <hpx/util/lightweight_test.hpp>

#include <cstddef>
#include <numeric>
#include <vector>

HPX_REGISTER_PARTITIONED_VECTOR(int);
HPX_REGISTER_PARTITIONED_VECTOR(unsigned);
Expand All @@ -42,18 +39,20 @@ void test(T minval, T maxval)
std::size_t sz = static_cast<std::size_t>(maxval-minval);

hpx::partitioned_vector<T> os(sz);
hpx::parallel::fill(hpx::parallel::par, std::begin(os), std::end(os), 0);
hpx::parallel::fill(
hpx::parallel::execution::par, std::begin(os), std::end(os), 0);

oarchive << os;

hpx::serialization::input_archive iarchive(buffer);

hpx::partitioned_vector<T> is(os.size());
hpx::parallel::fill(hpx::parallel::par, std::begin(is), std::end(is), 0);
hpx::parallel::fill(
hpx::parallel::execution::par, std::begin(is), std::end(is), 0);

iarchive >> is;
HPX_TEST_EQ(os.size(), is.size());
for(std::size_t i = 0; i < os.size(); ++i)
for (std::size_t i = 0; i < os.size(); ++i)
{
HPX_TEST_EQ(os[i], is[i]);
}
Expand All @@ -79,7 +78,7 @@ int main()
test<unsigned long>((std::numeric_limits<unsigned long>::min)(),
(std::numeric_limits<unsigned long>::min)() + 100);
test<unsigned long>((std::numeric_limits<unsigned long>::max)() - 100,
(std::numeric_limits<unsigned long>::max)());
(std::numeric_limits<unsigned long>::max)());
test<double>((std::numeric_limits<double>::min)(),
(std::numeric_limits<double>::min)() + 100);
test<double>(-100, 100);
Expand Down

0 comments on commit a5b25d0

Please sign in to comment.