Skip to content

[parallel] Fix element_type deduction in minmax algorithms#7208

Open
aneek22112007-tech wants to merge 29 commits intoTheHPXProject:masterfrom
aneek22112007-tech:fix/minmax-type-deduction
Open

[parallel] Fix element_type deduction in minmax algorithms#7208
aneek22112007-tech wants to merge 29 commits intoTheHPXProject:masterfrom
aneek22112007-tech:fix/minmax-type-deduction

Conversation

@aneek22112007-tech
Copy link
Copy Markdown
Contributor

Proposed Changes

  • Type-Safe Deduction: Replaced the use of std::iterator_traits<FwdIter>::value_type with hpx::util::invoke_result_t<Proj, typename std::iterator_traits<FwdIter>::reference>. This ensures that the internal storage type correctly matches the result of the user-provided projection.

  • Proxy Awareness: Wrapped the deduced type in hpx::traits::proxy_value_t. This allows the algorithms to correctly handle value proxies returned by segmented container iterators (like partitioned_vector) while maintaining standard behavior for regular iterators.

  • Comprehensive Fix: Applied these changes across all 9 locations in minmax.hpp, covering min_element, max_element, and minmax_element (including their internal _ind helper variants used by segmented drivers).

  • C++ Standard Compliance: Fixed missing typename keywords for dependent type lookups, ensuring the code compiles across all project-supported compilers (Clang, GCC, and MSVC).

Any background context you want to provide?

This fix addresses a bug where nth_element (and other algorithms calling min_element internally) would fail to compile when used with projections that transform the data type (e.g., projecting a struct to an int).

By deducing the type from the projection result rather than the iterator's value type, we prevent type mismatches. Additionally, the inclusion of proxy_value_t is essential for segmented algorithms, as it allows the parallel driver to correctly unwrap local proxies into a global value type that can be transferred across localities.

Checklist

Not all points below apply to all pull requests.

  • I have added a new feature and have added tests to go along with it.
  • I have fixed a bug and have added a regression test.
  • I have added a test using random numbers; I have made sure it uses a seed, and that random numbers generated are valid inputs for the tests.

@StellarBot
Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented Apr 20, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Comment thread libs/core/algorithms/include/hpx/parallel/algorithms/minmax.hpp Outdated
@aneek22112007-tech aneek22112007-tech force-pushed the fix/minmax-type-deduction branch from 41803ad to 2f805eb Compare April 20, 2026 20:28
Comment thread libs/core/algorithms/include/hpx/parallel/algorithms/minmax.hpp Outdated
@aneek22112007-tech aneek22112007-tech force-pushed the fix/minmax-type-deduction branch from 85adccc to 52be457 Compare April 20, 2026 20:59
Copy link
Copy Markdown
Contributor

@hkaiser hkaiser left a comment

Choose a reason for hiding this comment

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

Do we have tests that exercise these changes (i.e., that would fail without them)?

@aneek22112007-tech
Copy link
Copy Markdown
Contributor Author

Do we have tests that exercise these changes (i.e., that would fail without them)?

Yes, I added a regression test for this: libs/core/algorithms/tests/unit/algorithms/minmax_element_parallel_projection.cpp.

It specifically covers the parallel reduction paths with projections. Without these fixes, the parallel dispatches fail to compile whenever a projection returns a different type than the iterator's value type (due to the incorrect element_type deduction). I also took the opportunity to fix the copyright and formatting issues that were triggering the CI inspect failures.

@hkaiser
Copy link
Copy Markdown
Contributor

hkaiser commented Apr 23, 2026

Do we have tests that exercise these changes (i.e., that would fail without them)?

Yes, I added a regression test for this: libs/core/algorithms/tests/unit/algorithms/minmax_element_parallel_projection.cpp.

It specifically covers the parallel reduction paths with projections. Without these fixes, the parallel dispatches fail to compile whenever a projection returns a different type than the iterator's value type (due to the incorrect element_type deduction). I also took the opportunity to fix the copyright and formatting issues that were triggering the CI inspect failures.

Could you move the new tests into the directory tests/utnit/container_algorithms, please?

@hkaiser
Copy link
Copy Markdown
Contributor

hkaiser commented Apr 24, 2026

FWIW< inspect now reports:

/libs/core/algorithms/tests/unit/container_algorithms/minmax_element_projection.cpp: *I* missing #include (string) for symbol std::string on line 95

#include <hpx/execution.hpp>
#include <hpx/init.hpp>
#include <hpx/modules/testing.hpp>
#include <hpx/parallel/container_algorithms/minmax.hpp>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This causes compilation issues when C++ modules are enabled. Simply remove this #include.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @hkaiser,

Good catch! I've removed the redundant #include <hpx/init.hpp> from the test file to ensure it's compatible with C++ modules. I've just pushed the update.

Thanks!

@aneek22112007-tech
Copy link
Copy Markdown
Contributor Author

FWIW< inspect now reports:

/libs/core/algorithms/tests/unit/container_algorithms/minmax_element_projection.cpp: *I* missing #include (string) for symbol std::string on line 95

Hi @hkaiser,

I’ve finished the refactoring for is_sorted and is_partitioned and I believe the implementation is now stable.

I’ve moved everything over to the standard hpx::traits for iterator deduction and added full projection support across all algorithm overloads. This not only cleans up the code but should handle edge cases like proxy iterators much more reliably.

The bulk of the work went into resolving the dispatch failures in the segmented algorithms. I tracked the issue down to the CRTP base class initialization—it was missing the template parameters for the derived type, which was breaking the parallel dispatch in segmented contexts. I also synchronized the partitioner logic in is_sorted_until to ensure the intermediate results and return types are handled correctly.

I've verified the changes by running the partitioned_vector unit tests, and they are now passing across all execution policies without any warnings.

Ready for your review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants