Skip to content

refactor: Range based pair algorithms - #2202

Merged
rprospero merged 7 commits into
develop2from
range_based_pair_algorithms
Jul 22, 2025
Merged

refactor: Range based pair algorithms#2202
rprospero merged 7 commits into
develop2from
range_based_pair_algorithms

Conversation

@rprospero

@rprospero rprospero commented Jul 11, 2025

Copy link
Copy Markdown
Contributor

This PR takes advantage of the range concept to simplify the calling procedure for for_each_pair and for_each_pair_early. They now just take the entire range as a single argument instead of requiring individual beginning and ending iterators. In other words, code that was

for_each_pair(vector.begin(), vector.end(), doSomething);

is now

for_each_pair(vector, doSomething);

Also, each function was originally overloaded with one version for iterators and another for flat integers. Now, instead, there is a single method which does the right thing for either instance. The one price for this is that the integer parameters now need to be wrapped in an iota view. For example, code that was

for_each_pair(0, count, doSomething)

is now

for_each_pair(std::views:iota(0, count), doSomething)

This should not be merged before #2201, as I think as that PR adds some calls to for_each_pair that I will need to convert to the range format

@github-actions github-actions Bot left a comment

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.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 3abf23c Previous: 9005f62 Ratio
BM_Box_MinimumImage<OrthorhombicBox> 14.007764158714778 ns/iter 5.911377415380199 ns/iter 2.37

This comment was automatically generated by workflow using github-action-benchmark.

CC: @disorderedmaterials/dissolve-devs

@trisyoungs trisyoungs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Excellent. One question and one suggestion for further clean-up of the calls to potentially consider before merging.

Comment thread src/classes/partialSet.cpp Outdated

dissolve::for_each_pair(
ParallelPolicies::par, 0, nTypes,
ParallelPolicies::par, std::views::iota(0, nTypes),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a thought - should we have an overload of for_each_pair specific to 0 -> N-1 ranges, which only takes the upper N value as a single argument, and which calls for_each_pair with the appropriate std::iota range?

Comment on lines +126 to +120
dissolve::for_each_pair(ParallelPolicies::seq, std::span(sp->atoms().begin(), sp->nAtoms()), pairwiseForceOperator);
else
dissolve::for_each_pair(ParallelPolicies::par, sp->atoms().begin(), sp->atoms().end(), pairwiseForceOperator);
dissolve::for_each_pair(ParallelPolicies::par, std::span(sp->atoms().begin(), sp->nAtoms()), pairwiseForceOperator);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Curious - why the use of std::span here and nowhere else?

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.

To zeroth order: I don't know.

To first order: if I pass just pass in sp->atoms(), I get a compiler error about SpeciesAtom missing a copy constructor. It seems that the specific std::vector implementation of ranges in GCC used the copy constructor for some optimizations. Thankfully, the standard does have std::span which just takes two iterators and doesn't make any assumptions, so we can use it as a wrapper.

@rprospero
rprospero force-pushed the range_based_pair_algorithms branch 2 times, most recently from 76e315a to c4ae8aa Compare July 21, 2025 18:17
@rprospero
rprospero force-pushed the range_based_pair_algorithms branch from c4ae8aa to 3abf23c Compare July 22, 2025 12:44
@rprospero
rprospero merged commit 4040c83 into develop2 Jul 22, 2025
11 checks passed
@rprospero
rprospero deleted the range_based_pair_algorithms branch July 22, 2025 14:33
rprospero added a commit that referenced this pull request Jul 24, 2025
rprospero added a commit that referenced this pull request Sep 5, 2025
rprospero added a commit that referenced this pull request Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants