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

SpaceAwareAccessor #7074

Merged
merged 7 commits into from
Jun 19, 2024
Merged

SpaceAwareAccessor #7074

merged 7 commits into from
Jun 19, 2024

Conversation

crtrott
Copy link
Member

@crtrott crtrott commented Jun 17, 2024

This will be the default accessor for View adding memory access check.

@crtrott
Copy link
Member Author

crtrott commented Jun 17, 2024

Needs a rebase after merging the thing with the default mdspan on.

Comment on lines 52 to 75
{
int errors;
using acc_t =
Kokkos::Impl::SpaceAwareAccessor<memory_space_t,
Kokkos::default_accessor<const T>>;
acc_t acc{};
typename acc_t::data_handle_type ptr = v.data();

Kokkos::parallel_reduce(
Kokkos::RangePolicy<ExecutionSpace>(0, v.extent(0)),
KOKKOS_LAMBDA(int i, int& error) {
if (acc.access(ptr, i) != ptr[i]) error++;
static_assert(std::is_same_v<typename acc_t::element_type, const T>);
static_assert(std::is_same_v<typename acc_t::reference, const T&>);
static_assert(
std::is_same_v<typename acc_t::data_handle_type, const T*>);
static_assert(std::is_same_v<typename acc_t::offset_policy, acc_t>);
static_assert(
std::is_same_v<typename acc_t::memory_space, memory_space_t>);
static_assert(std::is_empty_v<acc_t>);
},
errors);
ASSERT_EQ(errors, 0);
}
Copy link
Member

Choose a reason for hiding this comment

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

Don't you get this for free if you instantiate your test for a const-qualified type?
Please get rid of that block and instantiate for some const T.

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 guess I can instantiate the View with remove_const

Copy link
Member Author

Choose a reason for hiding this comment

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

done


KOKKOS_FUNCTION
constexpr reference access(data_handle_type p, size_t i) const noexcept {
// Same error as Kokkos view in case anyone greps for this in their tools
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand your comment and unsure on what's best to do here (pretend it is a view error or say accessor)

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting question actually: originally I thought we just check error here, but that means no label. I guess we could internally use an mdspan without SpaceAwareAccessor and just use one with SpaceAwareAccessor in the public interfaces.

Copy link
Member Author

Choose a reason for hiding this comment

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

changed the message

template <class, class>
friend struct SpaceAwareAccessor;
};

Copy link
Member

Choose a reason for hiding this comment

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

Should we attempt to handle the case where NestedAccessor is a specialization of SpaceAwareAccessor in which case we may be able to "collapse" and not produce and double-nested accessor?

Copy link
Member Author

Choose a reason for hiding this comment

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

If and when we make this public maybe. For now I wouldn't say so since we are not gonna use it in generic contexts?

};

template <class NestedAccessor>
struct SpaceAwareAccessor<AnonymousSpace, NestedAccessor> {
Copy link
Member

Choose a reason for hiding this comment

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

I suppose since NestedAccessor can be some potentially weird user-defined type, it is not safe to just derive from it to save on the boiler plate code below?

Copy link
Member Author

@crtrott crtrott Jun 18, 2024

Choose a reason for hiding this comment

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

yeah, not sure I prefer inheritance even if it worked, i mean there isn't that much stuff defined.

Comment on lines 27 to 28
#include <Kokkos_Concepts.hpp>
#include <impl/Kokkos_Utilities.hpp>
Copy link
Member

Choose a reason for hiding this comment

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

What are you using from these?
I think all you need is an include for Impl::runtime_check_memory_access_violation()

Copy link
Member Author

Choose a reason for hiding this comment

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

Macros: KOKKOS_FUNCTION
Concepts: is_memory_space
Utilities: runtime_check_memory_access_violation

or?

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#include <Kokkos_Concepts.hpp>
#include <impl/Kokkos_Utilities.hpp>
#include <Kokkos_Core_fwd.hpp> // runtime_check_memory_access_violation

runtime_check_memory_access_violation (without ViewTracker and ViewMapping argument) is defined in Kokkos_Core_fwd.hpp (which also includes Kokkos_Macros.hpp).

@@ -76,7 +76,7 @@ KOKKOS_ENABLE_OPTION(IMPL_HIP_UNIFIED_MEMORY OFF "Whether to leverage unified me
KOKKOS_ENABLE_OPTION(DESUL_ATOMICS_EXTERNAL OFF "Whether to use an external desul installation")
KOKKOS_ENABLE_OPTION(ATOMICS_BYPASS OFF "**NOT RECOMMENDED** Whether to make atomics non-atomic for non-threaded MPI-only use cases")

KOKKOS_ENABLE_OPTION(IMPL_MDSPAN OFF "Whether to enable experimental mdspan support")
KOKKOS_ENABLE_OPTION(IMPL_MDSPAN ON "Whether to enable experimental mdspan support")
Copy link
Member

Choose a reason for hiding this comment

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

I suppose you refer to this change and the CI configuration ones when you talk about needing to rebase.
In any case commenting here to note this does not belong to this PR.

Copy link
Member

Choose a reason for hiding this comment

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

OK now I ran into #7069

Copy link
Member Author

Choose a reason for hiding this comment

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

rebase has happened

core/src/Kokkos_View.hpp Show resolved Hide resolved
typename Traits::memory_space,
Kokkos::default_accessor<typename Traits::value_type>>;
using mdspan_type = mdspan<typename Traits::value_type, extents_type,
mdspan_layout_type, accessor_type>;
Copy link
Member

Choose a reason for hiding this comment

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

Should we comment somewhere that the converting constructor from a mdspan strips out the accessor and just require convertibility to a space-aware default accessor?

Copy link
Member Author

Choose a reason for hiding this comment

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

not sure what you mean, the constructor takes this thing. So all the rules for mdspan conversion apply anyway. View will only do something with it after the user thing was converted to mdspan_type

}
#endif

TEST(TEST_CATEGORY_DEATH, memory_access_violations_from_device) {
Copy link
Member

Choose a reason for hiding this comment

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

We might want some sort of mdpan_ prefix to classify that test properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

its not mdspan. I guess I can prefix it with space_aware_accessor making it space_aware_accessor_memory_access_violations_from_device ...

Copy link
Member Author

Choose a reason for hiding this comment

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

changed the name

This mdspan accessor wraps otehr accessors, and adds memory space
accessibility checks.
That version is convertible into all other variants
@crtrott crtrott force-pushed the space-aware-accessor branch 2 times, most recently from fc4f684 to f29c746 Compare June 18, 2024 17:56
CI serviced these. Also turn off tests if mdspan is not enabled
Copy link
Contributor

@nmm0 nmm0 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, I think we just need to figure out the explicit conversion thing

std::enable_if_t<std::is_constructible_v<NestedAccessor,
OtherNestedAccessorType>,
int> = 0>
KOKKOS_FUNCTION constexpr SpaceAwareAccessor(
Copy link
Contributor

Choose a reason for hiding this comment

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

At the very least I think this constructor should be explicit

Copy link
Contributor

Choose a reason for hiding this comment

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

We will probably discuss this either during the team meeting or a separate meeting

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 want to have a full discussion on how to consistently deal with explicitness for View and related facilities.

: nested_acc(other.nested_acc) {}

KOKKOS_FUNCTION
SpaceAwareAccessor(const NestedAccessor& acc) : nested_acc(acc) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

This too

Kokkos::parallel_reduce(
Kokkos::RangePolicy<ExecutionSpace>(0, v.extent(0)),
KOKKOS_LAMBDA(int i, int& error) {
if (acc.access(ptr, i) != ptr[i]) error++;
Copy link
Contributor

Choose a reason for hiding this comment

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

very nitpicky but we may want to make an using acc_base_t = Kokkos::default_accessor<T> and make this check acc.access(ptr,i) != acc_base.access(ptr,i) just to make the requirements clearer

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 agree with that necessarily pretending this tests whether we really just call through seems at best misleading.

#else
[[no_unique_address]]
#endif
NestedAccessor nested_acc;
Copy link
Member Author

Choose a reason for hiding this comment

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

Because the question came up: yes you need no_unique_address recursively for it to work:
https://godbolt.org/z/T84E7Yb1f

Comment on lines 27 to 28
#include <Kokkos_Concepts.hpp>
#include <impl/Kokkos_Utilities.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#include <Kokkos_Concepts.hpp>
#include <impl/Kokkos_Utilities.hpp>
#include <Kokkos_Core_fwd.hpp> // runtime_check_memory_access_violation

runtime_check_memory_access_violation (without ViewTracker and ViewMapping argument) is defined in Kokkos_Core_fwd.hpp (which also includes Kokkos_Macros.hpp).


namespace Kokkos {

// For now use the accessors in IMPL namespace, as an
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// For now use the accessors in IMPL namespace, as an
// For now use the accessors in Impl namespace, as an

Copy link
Member Author

Choose a reason for hiding this comment

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

Kokkos_Core_fwd.hpp straight includes impl/Kokkos_Utilities.hpp but it doesn't include Kokkos_Concept.hpp so no reason to change that.


using memory_space = MemorySpace;

static_assert(is_memory_space<memory_space>::value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
static_assert(is_memory_space<memory_space>::value);
static_assert(is_memory_space_v<memory_space>);

@crtrott crtrott merged commit 362c9d7 into kokkos:develop Jun 19, 2024
28 of 29 checks passed
@crtrott crtrott deleted the space-aware-accessor branch June 19, 2024 21:25
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.

None yet

5 participants