Skip to content

[Mirror] View with mdspan Arguments - #1

Open
csiefer2 wants to merge 8 commits into
developfrom
pr-mirror-8852
Open

[Mirror] View with mdspan Arguments#1
csiefer2 wants to merge 8 commits into
developfrom
pr-mirror-8852

Conversation

@csiefer2

Copy link
Copy Markdown
Owner

Automated mirror of upstream PR kokkos#8852 Initial support for View with mdspan template arguments.

This supports instantiating View as

View<ElementType, ExtentsType, LayoutType, AccessorType>

Why we are doing this

  • General principle of greater alignment with the ISO C++ standard i.e. std::mdspan
  • This will enable specifying index type other than size_t leading to smaller size of the View object, and potentially higher performance in particular for high rank Views when using 32bit indexing
  • Opens the path to having arbitrarily mixed static and dynamic dimensions (not in this PR though)
  • Opens natural path to arbitrary rank View (i.e. > 8) (not in this PR though)

Current Limitation

All four arguments must be provided and they must be compatible with BasicView, e.g. AccessorType must be one of Kokkos's accessor types for Views such as SpaceAwareAccessor.

Status of this PR

This is a draft to gather implementation strategy feedback.

The changes to the Develop test just illustrate that this produces the intended behavior.

Where I want to end up is the following

  • T is an element type

  • E some instance of extents

  • L an mdspan compatible layout

  • Space is a execution or memory space or Kokkos::Device

  • MemTraits is Kokkos::MemoryTraits<...> with some args

  • A is a potentially user provided accessor

  • Must have: Kokkos::View<T, E, L, A>

  • Must have: Kokkos::View<T, E, L, Kokkos::Accessor<T, Space, MemTraits>>

  • Very Likely want: Kokkos::View<T, E, L> and Kokkos::View<T, E, Kokkos::Accessor<T, Space, MemTraits>>

  • Maybe want: Kokkos::View<T, E, L, Kokkos::AccessorPolicy<Space, MemTraits>> and View<T, E, Kokkos::AccessorPolicy<Space, MemTraits>>

The AccessorPolicy works like layouts in mdspan i.e. AccessorPolicy<Space, MemTraits>::accessor<T> is Accessor<T, Space, MemTraits>.

I would start out both in Experimental.

Issues

  • MemoryTraits is not preserving: std::is_same_v<View<T, E, L, M>::memory_traits, M> might be false. Only values in memory traits that are implemented by the accessors actually are preserved right now.
  • The nested typedefs for uniform view types are using old style arguments
    • but making them use the new ones means it could break stuff like KokkosKernels where the implementation converts everything to the uniform types and then expects that the second argument for example is a layout.
    • When mixing static and dynamic dimensions arbitrarily we wouldn't be able to give old style unified types etc.

This supports instantiating View as
View<ElementType, ExtentsType, LayoutType, AccessorType>

Limitation: all four arguments must be provided and they must be
compatible with BasicView, e.g. AccessorType must be one of Kokkos's
accessortype for Views such as SpaceAwareAccessor, and you can't
arbitrarily mix dynamic and static extents.

Also: the memory_traits typedef will come out wrong for now.

Signed-off-by: Christian Trott <crtrott@sandia.gov>
Signed-off-by: Christian Trott <crtrott@sandia.gov>
Signed-off-by: Christian Trott <crtrott@sandia.gov>
Signed-off-by: Christian Trott <crtrott@sandia.gov>
Signed-off-by: Christian Trott <crtrott@sandia.gov>
Signed-off-by: Christian Trott <crtrott@sandia.gov>
Signed-off-by: Christian Trott <crtrott@sandia.gov>
Signed-off-by: Christian Trott <crtrott@sandia.gov>
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @csiefer2, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant update to Kokkos::View by integrating std::mdspan concepts into its template arguments. The primary goal is to modernize the View interface, offering greater flexibility in defining data structures, enabling custom index types for potential performance gains, and paving the way for future enhancements like mixed static/dynamic dimensions and arbitrary-rank views. The changes involve substantial refactoring of View's internal template machinery and the introduction of new accessor deduction mechanisms.

Highlights

  • mdspan-style View Instantiation: Introduced initial support for instantiating Kokkos::View using mdspan-like template arguments: View<ElementType, ExtentsType, LayoutType, AccessorType>. This aligns Kokkos::View more closely with the std::mdspan standard.
  • Flexible Indexing and Performance: Enabled the specification of index types other than size_t through mdspan extents, which can lead to smaller View objects and potential performance improvements, especially for high-rank Views using 32-bit indexing.
  • Accessor Deduction and Memory Traits: Implemented a new Kokkos::Experimental::Accessor alias that deduces appropriate accessor types based on ElementType, Space, and MemoryTraits. This includes adding impl_memory_traits to various accessors to correctly propagate memory traits.
  • Template Metaprogramming Refinements: Refactored Impl::BasicViewFromTraits and the main Kokkos::View class template to accommodate the new mdspan-style arguments, ensuring compatibility and proper trait deduction. A new ViewTraits specialization was added for mdspan arguments.
  • Layout Conversion Utilities: Added Impl::ArrayLayoutFromLayout to convert mdspan layout types (e.g., layout_left, layout_right) to their corresponding Kokkos array layout types (e.g., LayoutLeft, LayoutRight).
  • New Unit Tests: Added two new unit tests: TestAccessorFromMemoryTraits.cpp to verify the behavior of the new Experimental::Accessor and TestViewMDSpanTemplateArgumentEquivalence.cpp to confirm the equivalence between mdspan-style View instantiations and traditional View instantiations.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • core/src/Kokkos_Core.cppm
    • Added using ::Kokkos::Experimental::Accessor; to the Experimental namespace export list.
  • core/src/Kokkos_Core_Impl.cppm
    • Added using ::Kokkos::Impl::ArrayLayoutFromLayout; to the export list.
    • Added using ::Kokkos::Impl::LayoutFromArrayLayout; to the export list.
  • core/src/Kokkos_View.hpp
    • Modified Impl::BasicViewFromTraits to accept DataType as its first template argument and introduced a new specialization for extents types.
    • Changed the View class template from DataType, Properties... to FirstArg, Properties... to support mdspan-style arguments.
    • Updated the base_t and traits type aliases within View to correctly use the new BasicViewFromTraits structure.
    • Modified the hooks_policy alias to derive from traits::hooks_policy.
    • Added a new ViewTraits specialization for mdspan-style arguments (T, extents<IndexType, Extents...>, Prop...) to inherit from the corresponding View's traits.
    • Corrected the conditional compilation #endif directive.
  • core/src/View/MDSpan/Kokkos_MDSpan_Accessor.hpp
    • Added impl_memory_traits() static constexpr member function to SpaceAwareAccessor to deduce memory traits.
    • Added impl_memory_traits() static constexpr member function to SpaceAwareAccessor specialization for AnonymousSpace.
    • Added impl_memory_traits() static constexpr member function to AtomicAccessorRelaxed to return MemoryTraits<Unmanaged | Atomic>().
    • Added impl_memory_traits() static constexpr member function to ReferenceCountedAccessor to deduce and adjust memory traits, ensuring Managed is implied.
    • Introduced Impl::ImplAccessor to deduce accessor types based on ElementType, Space, and MemTraits, and added Kokkos::Experimental::Accessor as a public alias.
    • Added Impl::memory_traits_from_accessor and Impl::MemoryTraitsFromAccessor to uniformly retrieve memory traits from accessors.
  • core/src/View/MDSpan/Kokkos_MDSpan_Layout.hpp
    • Added Impl::ArrayLayoutFromLayout struct and its specializations to convert mdspan layout types (e.g., layout_left, layout_right, layout_stride) to Kokkos array layout types (e.g., LayoutLeft, LayoutRight, LayoutStride).
  • core/unit_test/CMakeLists.txt
    • Added view/TestAccessorFromMemoryTraits.cpp and view/TestViewMDSpanTemplateArgumentEquivalence.cpp to the COMPILE_ONLY_SOURCES list.
    • Added conditional removal of view/TestAccessorFromMemoryTraits.cpp and view/TestViewMDSpanTemplateArgumentEquivalence.cpp if Kokkos_ENABLE_IMPL_MDSPAN is not enabled or Kokkos_ENABLE_IMPL_VIEW_LEGACY is enabled.
  • core/unit_test/view/TestAccessorFromMemoryTraits.cpp
    • Added a new unit test file to verify the correct deduction and behavior of Kokkos::Experimental::Accessor with various memory traits combinations.
  • core/unit_test/view/TestViewMDSpanTemplateArgumentEquivalence.cpp
    • Added a new unit test file to assert the type equivalence between Kokkos::View instantiated with mdspan-style arguments and Kokkos::View instantiated with traditional Kokkos arguments.
Activity
  • The pull request is currently a draft, intended to gather implementation strategy feedback from reviewers.
  • Initial changes include tests to illustrate the intended behavior of the new View instantiation methods.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces initial support for Kokkos::View with mdspan-style template arguments, which is a significant step towards aligning with the C++ standard and enabling more flexible view configurations. The implementation strategy of using BasicViewFromTraits as a dispatcher to handle both old and new argument styles is well-executed. The changes to accessors to correctly propagate memory traits and the addition of reverse layout mappings are necessary and correctly implemented. The new compile-time tests are a great addition to ensure the equivalence between the two ways of instantiating views. I have one suggestion for improving code clarity and robustness in Kokkos_MDSpan_Accessor.hpp.

Comment on lines +426 to +429
return MemoryTraits<(mt::is_random_access ? RandomAccess : 0) |
(mt::is_atomic ? Atomic : 0) |
(mt::is_restrict ? Restrict : 0) |
(mt::is_aligned ? Kokkos::Aligned : 0)>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This implementation for deriving memory traits is correct, but it can be simplified and made more robust. Instead of reconstructing the memory traits from individual flags, you can use bitwise operations on the impl_value of the nested memory traits to remove the Unmanaged flag. This is more concise and will automatically handle any new memory trait flags that might be added in the future.

    return MemoryTraits<mt::impl_value & ~Unmanaged>();

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