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

Generalized domains #698

Merged
merged 12 commits into from
Jan 30, 2025
Merged

Generalized domains #698

merged 12 commits into from
Jan 30, 2025

Conversation

tpadioleau
Copy link
Member

@tpadioleau tpadioleau commented Dec 5, 2024

PoC for #438

Steps:

  • remove internal_mdspan
  • implement Chunk and ChunkSpan with SupportType being a customization point
  • implement a StridedDiscreteDomain
  • implement a StorageDiscreteDomain
  • update algorithms

Not clear if we should define an equivalent of the slice operator taking a DiscreteDomain

cc @science-enthusiast

@tpadioleau tpadioleau self-assigned this Dec 5, 2024
@tpadioleau tpadioleau changed the title Remove optimization trick with mdspan Sliced domains Dec 5, 2024
@tpadioleau tpadioleau force-pushed the draft-sliced-domains branch from d42ad0e to b18c085 Compare December 5, 2024 21:48
@tpadioleau tpadioleau changed the title Sliced domains Generalized domains Dec 10, 2024
@tpadioleau tpadioleau force-pushed the draft-sliced-domains branch 3 times, most recently from 9196b14 to 5d8ac57 Compare December 13, 2024 19:40
@tpadioleau tpadioleau mentioned this pull request Dec 13, 2024
@tpadioleau tpadioleau force-pushed the draft-sliced-domains branch 2 times, most recently from f17f86f to bc58e5c Compare December 16, 2024 10:47
@tpadioleau tpadioleau force-pushed the draft-sliced-domains branch 4 times, most recently from b734484 to e2b02c6 Compare December 26, 2024 17:15
@tpadioleau tpadioleau force-pushed the draft-sliced-domains branch 2 times, most recently from 6b779a1 to adaa00b Compare January 2, 2025 13:14
@tpadioleau tpadioleau force-pushed the draft-sliced-domains branch 7 times, most recently from 64ca9f7 to f297957 Compare January 16, 2025 17:46
@tpadioleau tpadioleau marked this pull request as ready for review January 16, 2025 17:46
@tpadioleau
Copy link
Member Author

tpadioleau commented Jan 16, 2025

The feature is almost ready but the integration could be further improved:

  • duplicated code due to specialization,
  • the name of some variables do not reflect well what they represent,
  • the new discrete domains StridedDiscreteDomain and StorageDiscreteDomain do not yet support slicing with DiscreteElement whereas it should not be difficult to extend

A slight overhead might be observed in algorithms in order to support the generalized discrete domains.

It is out of scope of the current PR but a future work should allow to slice a ChunkSpan<T, DiscreteDomain<...>> with a strided range of DiscreteVector to end up with a ChunkSpan<T, StridedDiscreteDomain<...>>:

ChunkSpan<T, DiscreteDomain<DDim1, DDim2>> chk_span1;
ChunkSpan<T, StridedDiscreteDomain<DDim1>> chk_span2 = chk_span1[strided_slice(DiscreteVector<DDim1>(2), DiscreteVector<DDim1>(10), DiscreteVector<DDim1>(2)][DiscreteVector<DDim2>(2)];

similar to https://en.cppreference.com/w/cpp/container/mdspan/strided_slice

@tpadioleau tpadioleau force-pushed the draft-sliced-domains branch 3 times, most recently from a7b6d26 to 360ed44 Compare January 25, 2025 09:06
@tpadioleau tpadioleau force-pushed the draft-sliced-domains branch from 360ed44 to b9eecc3 Compare January 25, 2025 09:15
@tpadioleau tpadioleau force-pushed the draft-sliced-domains branch from b9eecc3 to ce3cda4 Compare January 25, 2025 09:16

template <class InputIt1, class InputIt2>
KOKKOS_FUNCTION bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
{

Choose a reason for hiding this comment

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

Could a is_pointer_v based concept help with better usage of this free function?

Copy link
Member Author

Choose a reason for hiding this comment

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

Discussed in person, I don't think so because it would require to assume an implementation detail on Kokkos iterators.

count -= step + 1;
} else {
count = step;
}
Copy link

@science-enthusiast science-enthusiast Jan 27, 2025

Choose a reason for hiding this comment

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

Could count be updated as count = last - first just after this if-then-else construct? first could be updated as it is being updated right now and last could be updated inside the else block as last = it. In this way, count is updated in the same manner it was initialized.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't, to be honest I copy-pasted the version found on cppref

KOKKOS_FUNCTION ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{
ForwardIt it;
typename std::iterator_traits<ForwardIt>::difference_type count = last - first;

Choose a reason for hiding this comment

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

This type is referenced for defining step also. A type alias could be created.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I will update

bool binary_search(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{
first = ::ddc::detail::lower_bound(first, last, value, comp);
return (!(first == last) && !(comp(value, *first)));

Choose a reason for hiding this comment

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

If we are sufficiently careful with how lower_bound and comp are implemented, could just !(first == last) be enough?

Copy link
Member Author

Choose a reason for hiding this comment

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

Same as lower_bound, this is a copy paste of the version found on cppref

Choose a reason for hiding this comment

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

I had a closer look at std::binary_search and std::lower_bound and understand better now. I was wrong in my previous comment. Both !(first == last) and !(comp(value, *first)) are needed. lower_bound searches for the first element in the partitioned range [first, last) which is not ordered before value. lower_bound can return last only if bool(comp(*iter, value)) is true for all iters. Both lower_bound and binary_search assume that [first, last) is partitioned. Also comp behaves like < and not <=. If for any element elem of [first, last), if bool(comp(elem, value)) does not imply !bool(comp(value, elem)) then the behavior is undefined. Hence the first element for which bool(comp(*iter, value)) is false should be either equal or greater than value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok thanks a lot for the explanation!

@tpadioleau
Copy link
Member Author

@science-enthusiast do you think it can be merged as is ?

@science-enthusiast
Copy link

@science-enthusiast do you think it can be merged as is ?

Yes, I think it can be merged. So that people can start using it. I have a few doubts that I will ask you. For me, this has been a good way to go deeper into the DDC code base. In the future, I will be able to make deeper comments.

Copy link

@science-enthusiast science-enthusiast left a comment

Choose a reason for hiding this comment

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

The DDC code base is new to me and I continue to understand it better by reviewing this PR. It is better to merge these changes and let people start using it. Issues can be addressed in the future.

@tpadioleau tpadioleau merged commit 18e3e55 into main Jan 30, 2025
57 checks passed
@tpadioleau tpadioleau deleted the draft-sliced-domains branch January 30, 2025 16:47
@tpadioleau
Copy link
Member Author

The DDC code base is new to me and I continue to understand it better by reviewing this PR. It is better to merge these changes and let people start using it. Issues can be addressed in the future.

Yes thank you for your time.

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