Skip to content

Commit

Permalink
Fixing integer overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Heller committed Nov 2, 2017
1 parent d96af68 commit afa254d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 17 deletions.
Expand Up @@ -47,6 +47,20 @@ extern template hpx::partitioned_vector<int,
std::vector<int>>::partitioned_vector(size_type, int const&,
hpx::container_distribution_policy const&, void*);

// partitioned_vector<long long>
typedef long long long_long;
HPX_REGISTER_PARTITIONED_VECTOR_DECLARATION(long_long);

extern template class hpx::server::partitioned_vector<long long, std::vector<long long>>;
extern template class hpx::partitioned_vector_partition<long long, std::vector<long long>>;
extern template class hpx::partitioned_vector<long long, std::vector<long long>>;
extern template hpx::partitioned_vector<long long,
std::vector<long long>>::partitioned_vector(size_type,
hpx::container_distribution_policy const&, void*);
extern template hpx::partitioned_vector<long long,
std::vector<long long>>::partitioned_vector(size_type, long long const&,
hpx::container_distribution_policy const&, void*);

// partitioned_vector<std::string>
using partitioned_vector_std_string_argument = std::string;

Expand Down
Expand Up @@ -13,6 +13,8 @@
#include <vector>

HPX_REGISTER_PARTITIONED_VECTOR(int);
typedef long long long_long;
HPX_REGISTER_PARTITIONED_VECTOR(long_long);

// an out-of-line definition of a member of a class template cannot have default
// arguments
Expand All @@ -35,6 +37,20 @@ template HPX_PARTITIONED_VECTOR_EXPORT
size_type, int const&, hpx::container_distribution_policy const&,
void*);

template class HPX_PARTITIONED_VECTOR_EXPORT
hpx::server::partitioned_vector<long long, std::vector<long long>>;
template class HPX_PARTITIONED_VECTOR_EXPORT
hpx::partitioned_vector_partition<long long, std::vector<long long>>;
template class HPX_PARTITIONED_VECTOR_EXPORT
hpx::partitioned_vector<long long, std::vector<long long>>;
template HPX_PARTITIONED_VECTOR_EXPORT
hpx::partitioned_vector<long long, std::vector<long long>>::partitioned_vector(
size_type, hpx::container_distribution_policy const&, void*);
template HPX_PARTITIONED_VECTOR_EXPORT
hpx::partitioned_vector<long long, std::vector<long long>>::partitioned_vector(
size_type, long long const&, hpx::container_distribution_policy const&,
void*);

#if defined(HPX_MSVC)
#pragma warning(pop)
#endif
Expand Down
12 changes: 9 additions & 3 deletions tests/unit/parallel/algorithms/sort_by_key.cpp
Expand Up @@ -80,7 +80,9 @@ void sort_by_key_benchmark()
std::iota(keys.begin(), keys.end(), 0);

// shuffle the keys up,
std::random_shuffle(keys.begin(), keys.end());
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(keys.begin(), keys.end(), g);

// make copies of initial states
o_keys = keys;
Expand Down Expand Up @@ -128,7 +130,9 @@ void test_sort_by_key1(ExPolicy &&policy, Tkey, Tval, const Op &op, const Helper
std::iota(keys.begin(), keys.end(), 0);

// shuffle the keys up,
std::random_shuffle(keys.begin(), keys.end());
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(keys.begin(), keys.end(), g);

// make copies of initial states
o_keys = keys;
Expand Down Expand Up @@ -177,7 +181,9 @@ void test_sort_by_key_async(ExPolicy &&policy, Tkey, Tval, const Op &op,
std::iota(keys.begin(), keys.end(), 0);

// shuffle the keys up,
std::random_shuffle(keys.begin(), keys.end());
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(keys.begin(), keys.end(), g);

// make copies of initial states
o_keys = keys;
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/parallel/algorithms/test_utils.hpp
Expand Up @@ -9,6 +9,7 @@
#include <hpx/include/parallel_execution_policy.hpp>
#include <hpx/include/util.hpp>

#include <algorithm>
#include <atomic>
#include <cstddef>
#include <iterator>
Expand Down Expand Up @@ -220,7 +221,9 @@ namespace test
{
std::vector<std::size_t> c(size);
std::iota(std::begin(c), std::end(c), 0);
std::random_shuffle(std::begin(c), std::end(c));
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(std::begin(c), std::end(c), g);
return c;
}

Expand All @@ -229,7 +232,9 @@ namespace test
{
std::vector<T> c(size);
std::iota(std::begin(c), std::end(c), 0);
std::random_shuffle(std::begin(c), std::end(c));
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(std::begin(c), std::end(c), g);
return c;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/parallel/algorithms/transform_binary2_tests.hpp
Expand Up @@ -59,8 +59,8 @@ void test_transform_binary2(ExPolicy policy, IteratorTag)
std::vector<int> c1(10007);
std::vector<int> c2(c1.size());
std::vector<int> d1(c1.size()); //-V656
std::iota(std::begin(c1), std::end(c1), std::rand());
std::iota(std::begin(c2), std::end(c2), std::rand());
std::iota(std::begin(c1), std::end(c1), std::rand() % (std::numeric_limits<int>::max()/2));
std::iota(std::begin(c2), std::end(c2), std::rand() % (std::numeric_limits<int>::max()/2));

auto result =
hpx::parallel::transform(policy,
Expand Down Expand Up @@ -95,8 +95,8 @@ void test_transform_binary2_async(ExPolicy p, IteratorTag)
std::vector<int> c1(10007);
std::vector<int> c2(c1.size());
std::vector<int> d1(c1.size()); //-V656
std::iota(std::begin(c1), std::end(c1), std::rand());
std::iota(std::begin(c2), std::end(c2), std::rand());
std::iota(std::begin(c1), std::end(c1), std::rand() % (std::numeric_limits<int>::max()/2));
std::iota(std::begin(c2), std::end(c2), std::rand() % (std::numeric_limits<int>::max()/2));

auto f =
hpx::parallel::transform(p,
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/parallel/algorithms/transform_binary_tests.hpp
Expand Up @@ -59,8 +59,8 @@ void test_transform_binary(ExPolicy policy, IteratorTag)
std::vector<int> c1(10007);
std::vector<int> c2(c1.size());
std::vector<int> d1(c1.size()); //-V656
std::iota(std::begin(c1), std::end(c1), std::rand());
std::iota(std::begin(c2), std::end(c2), std::rand());
std::iota(std::begin(c1), std::end(c1), std::rand() % (std::numeric_limits<int>::max()/2));
std::iota(std::begin(c2), std::end(c2), std::rand() % (std::numeric_limits<int>::max()/2));

auto result =
hpx::parallel::transform(policy,
Expand Down Expand Up @@ -95,8 +95,8 @@ void test_transform_binary_async(ExPolicy p, IteratorTag)
std::vector<int> c1(10007);
std::vector<int> c2(c1.size());
std::vector<int> d1(c1.size()); //-V656
std::iota(std::begin(c1), std::end(c1), std::rand());
std::iota(std::begin(c2), std::end(c2), std::rand());
std::iota(std::begin(c1), std::end(c1), std::rand() % (std::numeric_limits<int>::max()/2));
std::iota(std::begin(c2), std::end(c2), std::rand() % (std::numeric_limits<int>::max()/2));

auto f =
hpx::parallel::transform(p,
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/parallel/container_algorithms/test_utils.hpp
Expand Up @@ -247,7 +247,9 @@ namespace test
{
std::vector<std::size_t> c(size);
std::iota(std::begin(c), std::end(c), 0);
std::random_shuffle(std::begin(c), std::end(c));
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(std::begin(c), std::end(c), g);
return c;
}

Expand All @@ -256,7 +258,9 @@ namespace test
{
Vector c(size);
std::iota(std::begin(c.base()), std::end(c.base()), 0);
std::random_shuffle(std::begin(c.base()), std::end(c.base()));
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(std::begin(c.base()), std::end(c.base()), g);
return c;
}

Expand Down
Expand Up @@ -350,7 +350,7 @@ void exclusive_scan_tests(std::vector<hpx::id_type>& localities)
int main()
{
std::vector<hpx::id_type> localities = hpx::find_all_localities();
exclusive_scan_tests<int>(localities);
exclusive_scan_tests<long long>(localities);
exclusive_scan_tests<double>(localities);

return hpx::util::report_errors();
Expand Down
Expand Up @@ -348,7 +348,7 @@ void inclusive_scan_tests(std::vector<hpx::id_type>& localities)
int main()
{
std::vector<hpx::id_type> localities = hpx::find_all_localities();
inclusive_scan_tests<int>(localities);
inclusive_scan_tests<long long>(localities);
inclusive_scan_tests<double>(localities);

return hpx::util::report_errors();
Expand Down

5 comments on commit afa254d

@hkaiser
Copy link
Member

@hkaiser hkaiser commented on afa254d Nov 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do all of our target compilers support long long? Wouldn't it be better to replace long long with std::int64_t?

Also, inspect will complain about use of max() without proper protection against unneeded macro expansion.

@sithhell
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. I'll change it to std::int64_t and protect the call to max().

@sithhell
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to http://en.cppreference.com/w/cpp/language/types long long is supported since c++11 and has at least 64 bits.

@hkaiser
Copy link
Member

@hkaiser hkaiser commented on afa254d Nov 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to http://en.cppreference.com/w/cpp/language/types long long is supported since c++11 and has at least 64 bits.

Sure, nobody was questioning that. I asked whether it's supported on all platforms we care about.

@sithhell
Copy link
Member

@sithhell sithhell commented on afa254d Nov 2, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.