Skip to content

Conversation

@bartgol
Copy link
Contributor

@bartgol bartgol commented Aug 28, 2025

Motivation

This is motivated by some work in EAMxx. Namely, I have an array (nx, ny, nz), which I need to mask with another array, which could have shape (nx) or (nx,nz). To avoid lots of conditionals, I would like to do something similar to numpy's array broadcasting.

This PR allows to wrap a view into a higher dim array, in such a way that it is "replicated" along the new dimensions. E.g., one can take a 1d view x representing a column, and replicated it in a 2d matrix array A, in such a way that A(i,j) = x(j) for all i. This, of course, does not increase storage requirement, and does not copy any data. It simply "hides" the slicing of "extruded" dimensions.

E3SM Stakeholder Feedback

May be needed in EAMxx.

Testing

Added a unit test.

KOKKOS_FORCEINLINE_FUNCTION
reference_type operator()(Is... indices) const {
int i=0;
((indices *= coeff[i++]),...);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we do some sort of assertion on i that it didn't exceed the rank of m_view_impl? Or check the size of ...indices at the beginning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, good idea. Maybe I should just add 8 overloads for ranks 1-8...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I switched to 8 separate methods

- Use int[8] rather than another view to store outfacing layout
- Implement 8 separate method for operator(), like Kokkos::View does
- Improve docs for the constructor
@bartgol bartgol requested a review from tcclevenger September 9, 2025 00:32
Copy link
Contributor

@tcclevenger tcclevenger left a comment

Choose a reason for hiding this comment

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

Looks good!

@bartgol bartgol merged commit 7c82fc7 into master Sep 9, 2025
4 checks passed
@bartgol bartgol deleted the bartgol/view-broadcast branch September 9, 2025 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request kokkos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants