Skip to content

Add OpenMP segmented prefix sum #1837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Add OpenMP segmented prefix sum #1837

wants to merge 1 commit into from

Conversation

upsj
Copy link
Member

@upsj upsj commented Apr 30, 2025

Again part of #1758, this provides the equivalent of thrust::exclusive_scan_by_key

@upsj upsj added the 1:ST:ready-for-review This PR is ready for review label Apr 30, 2025
@upsj upsj requested a review from a team April 30, 2025 09:29
@upsj upsj self-assigned this Apr 30, 2025
@ginkgo-bot ginkgo-bot added reg:build This is related to the build system. reg:testing This is related to testing. mod:openmp This is related to the OpenMP module. labels Apr 30, 2025
{
for (int i = 0; i < nthreads - 1; i++) {
// the next block carries over the previous partial sum
// if it starts and ends with the same key as the next one
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
// if it starts and ends with the same key as the next one
// if it starts and ends with the same key as the previous one

Comment on lines +44 to +51
if (num_entries < 2) {
if (num_entries == 0) {
return;
} else {
*it = init;
return;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (num_entries < 2) {
if (num_entries == 0) {
return;
} else {
*it = init;
return;
}
}
if (num_entries == 0) {
return;
} else if (num_entries == 1) {
*it = init;
return;
}

void segmented_prefix_sum(
std::shared_ptr<const OmpExecutor> exec, KeyIterator key, Iterator it,
const size_type num_entries,
typename std::iterator_traits<KeyIterator>::value_type key_init = {},
Copy link
Member

Choose a reason for hiding this comment

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

does exclusive_scan_by_key have key_init?

Copy link
Member

Choose a reason for hiding this comment

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

Is it for the situation when KeyIterator is float or double?
I think we only need to consider it is integer (or bool/char) type because float and double as the key having the same value sounds unusual to me

const size_type num_entries,
typename std::iterator_traits<KeyIterator>::value_type key_init = {},
typename std::iterator_traits<Iterator>::value_type init = {},
ScanOp op = {})
Copy link
Member

Choose a reason for hiding this comment

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

As it call prefix_sum, we do not need the ScanOp.

std::min(num_entries, (thread_id + 1) * def_num_witems);

auto partial_sum = init;
auto cur_key = startidx < num_entries ? key[startidx] : key_init;
Copy link
Member

Choose a reason for hiding this comment

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

I think key_init can be the last key.
It may introduce one or two operation in proc_sums[i + 1] = op(proc_sums[i], proc_sums[i + 1]);, but I think it should be okay.

Comment on lines +26 to +29
* Similar to prefix_sum, only reduces within runs of the same key value (each
* key run must only occur once, otherwise the scan operation is not necessarily
* associaive). It also doesn't ignore the last value!
* Similar to thrust::exclusive_scan_by_key
Copy link
Member

Choose a reason for hiding this comment

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

I do not get it. if it does not ignore the last value, should it be the inclusive one?

Comment on lines +67 to +68
gko::kernels::omp::components::segmented_prefix_sum(
this->exec, keys.cbegin(), input.begin(), keys.size());
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
gko::kernels::omp::components::segmented_prefix_sum(
this->exec, keys.cbegin(), input.begin(), keys.size());
gko::kernels::omp::components::segmented_prefix_sum(
this->exec, keys.begin(), input.begin(), keys.size());

TYPED_TEST_SUITE(PrefixSum, gko::test::IndexTypes, TypenameNameGenerator);


TYPED_TEST(PrefixSum, SegmentedPrefixSumWorks)
Copy link
Member

Choose a reason for hiding this comment

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

another test for checking the init is not 0

template <typename KeyIterator, typename Iterator,
typename ScanOp =
std::plus<typename std::iterator_traits<Iterator>::value_type>>
void segmented_prefix_sum(
Copy link
Member

Choose a reason for hiding this comment

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

segmented_scan_by_key?

@MarcelKoch MarcelKoch added this to the Ginkgo 1.10.0 milestone May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1:ST:ready-for-review This PR is ready for review mod:openmp This is related to the OpenMP module. reg:build This is related to the build system. reg:testing This is related to testing.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants