Skip to content
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

[k2] make sort functions implementation runtime specific #1208

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

astrophysik
Copy link
Contributor

No description provided.

@astrophysik astrophysik added k2 k2 related runtime Feature related to runtime labels Jan 15, 2025
@astrophysik astrophysik self-assigned this Jan 15, 2025
@astrophysik astrophysik added this to the next milestone Jan 16, 2025
@astrophysik astrophysik requested a review from apolyakov January 16, 2025 10:09
Comment on lines +498 to +506
task_t<void> f$asort(array<T> &a, int64_t flag = SORT_REGULAR) {
switch (flag) {
case SORT_REGULAR:
co_return co_await array_functions_impl_::sort<task_t<void>>(a, array_functions_impl_::sort_compare<T>(), false);
case SORT_NUMERIC:
co_return co_await array_functions_impl_::sort<task_t<void>>(a, array_functions_impl_::sort_compare_numeric<T>(), false);
case SORT_STRING:
co_return co_await array_functions_impl_::sort<task_t<void>>(a, array_functions_impl_::sort_compare_string<T>(), false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it interruptible?
I thought that all sort function that are not receiving callable as argument should not be interruptible.
Am I wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right
I did it interruptible for easy implementation. But maybe it would be better to make it a right way in this pr

Copy link
Contributor

Choose a reason for hiding this comment

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

If it's a big deal then it's OK to leave as is.

@astrophysik astrophysik force-pushed the vsadokhov/support-interruptible-sort branch from fdbc659 to 01a0899 Compare January 24, 2025 12:43

template<typename T, typename Comparator>
requires(std::invocable<Comparator, T, T>) task_t<void> sort(T *begin_init, T *end_init, Comparator compare) noexcept {
auto compare_call = [compare]<typename U>(U lhs, U rhs) -> task_t<int64_t> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Lambda coroutine should not have captured values as they're not saved in a coroutine frame

T *j = end;

while (true) {
while (i < j && (co_await compare_call(*begin, *i)) > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can get rid of co_await completely unless Comparator is async function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can, but it require to create copy paste function without co_await. Is it good decision - I don't think so. In any case, we should make base sort functions not interruptible, it's part of a larger task

namespace array_functions_impl_ {

template<typename Result, typename U, typename Comparator>
Result sort(array<U> &arr, Comparator comparator, bool renumber) noexcept {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's unsafe to pass an array to a coroutine by reference. In this function you access internal pointers of the array. Having such an array passed by reference, we can face with a case of the array being changed while we were blocked after co_await call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sort functions works inplace

@astrophysik astrophysik removed this from the next milestone Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
k2 k2 related runtime Feature related to runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants