Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions libs/core/algorithms/include/hpx/parallel/algorithms/minmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,33 +706,41 @@ namespace hpx::parallel {
minmax_element_result<FwdIter> sequential_minmax_element(ExPolicy&&,
FwdIter it, std::size_t count, F const& f, Proj const& proj)
{
minmax_element_result<FwdIter> result = {it, it};
HPX_ASSERT(count != 0);

if (count == 0 || count == 1)
return result;
if (count == 1)
return minmax_element_result<FwdIter>{it, it};

using element_type = hpx::traits::proxy_value_t<
typename std::iterator_traits<FwdIter>::value_type>;

element_type min_value = HPX_INVOKE(proj, *it);
element_type max_value = min_value;
util::loop_n<std::decay_t<ExPolicy>>(
++it, count - 1, [&](FwdIter const& curr) -> void {
element_type curr_value = HPX_INVOKE(proj, *curr);
if (HPX_INVOKE(f, curr_value, min_value))
{
result.min = curr;
min_value = curr_value;
}
auto min = it;
auto max = it;
++it;

if (!HPX_INVOKE(f, curr_value, max_value))
{
result.max = curr;
max_value = HPX_MOVE(curr_value);
}
});
element_type min_value = HPX_INVOKE(proj, *min);
element_type max_value = HPX_INVOKE(proj, *max);

for (std::size_t i = 1; i < count; ++i, ++it)
{
element_type curr_value = HPX_INVOKE(proj, *it);

// Update min if curr_value < min_value
if (HPX_INVOKE(f, curr_value, min_value))
{
min = it;
min_value = curr_value;
}

// Update max if min_value <= curr_value (not curr_value < min_value)
if (!HPX_INVOKE(f, curr_value, max_value))
{
max = it;
max_value = curr_value;
}
}

return result;
return minmax_element_result<FwdIter>{min, max};
}

template <typename Iter>
Expand Down Expand Up @@ -771,7 +779,7 @@ namespace hpx::parallel {

element_type curr_max_value =
HPX_INVOKE(proj, *curr->max);
if (!HPX_INVOKE(f, curr_max_value, max_value))
if (HPX_INVOKE(f, curr_max_value, max_value))
Copy link
Member

Choose a reason for hiding this comment

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

This is now inconsistent with your sequential implementation above (same for the change below). Which version is correct?

{
result.max = curr->max;
max_value = HPX_MOVE(curr_max_value);
Expand Down Expand Up @@ -813,7 +821,7 @@ namespace hpx::parallel {
min_value = curr_value;
}

if (!HPX_INVOKE(f, curr_value, max_value))
if (HPX_INVOKE(f, curr_value, max_value))
{
max = curr;
max_value = HPX_MOVE(curr_value);
Expand Down
3 changes: 3 additions & 0 deletions libs/core/algorithms/tests/unit/algorithms/max_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <hpx/algorithm.hpp>
#include <hpx/init.hpp>
#include <hpx/modules/testing.hpp>
#include <hpx/parallel/algorithms/minmax.hpp>

#include <cstddef>
#include <ctime>
Expand Down Expand Up @@ -125,6 +126,8 @@ void max_element_test()
{
test_max_element<std::random_access_iterator_tag>();
test_max_element<std::forward_iterator_tag>();
test_max_element_semantics<std::random_access_iterator_tag>();
test_max_element_semantics<std::forward_iterator_tag>();
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions libs/core/algorithms/tests/unit/algorithms/min_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <hpx/algorithm.hpp>
#include <hpx/init.hpp>
#include <hpx/modules/testing.hpp>
#include <hpx/parallel/algorithms/minmax.hpp>

#include <cstddef>
#include <ctime>
Expand Down Expand Up @@ -126,6 +127,8 @@ void min_element_test()
{
test_min_element<std::random_access_iterator_tag>();
test_min_element<std::forward_iterator_tag>();
test_min_element_semantics<std::random_access_iterator_tag>();
test_min_element_semantics<std::forward_iterator_tag>();
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <hpx/algorithm.hpp>
#include <hpx/init.hpp>
#include <hpx/modules/testing.hpp>
#include <hpx/parallel/algorithms/minmax.hpp>

#include <cstddef>
#include <ctime>
Expand Down Expand Up @@ -139,6 +140,8 @@ void minmax_element_test()
{
test_minmax_element<std::random_access_iterator_tag>();
test_minmax_element<std::forward_iterator_tag>();
test_minmax_element_semantics<std::random_access_iterator_tag>();
test_minmax_element_semantics<std::forward_iterator_tag>();
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
Loading