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

dual view: update template types + Kokkos::is_dual_view (#6085) #6120

Merged
merged 1 commit into from
May 16, 2023

Conversation

romintomasetti
Copy link
Contributor

@romintomasetti romintomasetti commented May 10, 2023

@dalg24-jenkins
Copy link
Collaborator

Can one of the admins verify this patch?

@romintomasetti romintomasetti force-pushed the kokkos-dual-view-template-types branch 2 times, most recently from 14f3e57 to c628a01 Compare May 10, 2023 14:51
@romintomasetti
Copy link
Contributor Author

@ajpowelsnl There were a little more changes required...

@ajpowelsnl
Copy link
Contributor

@ajpowelsnl There were a little more changes required...

Please let us know if there's anything we can do to help ...

@romintomasetti romintomasetti force-pushed the kokkos-dual-view-template-types branch from c628a01 to 2e79a95 Compare May 10, 2023 15:55
@romintomasetti
Copy link
Contributor Author

@ajpowelsnl I think this is ready for your approval to test 😄

Copy link
Member

@dalg24 dalg24 left a comment

Choose a reason for hiding this comment

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

(peer reviewed with Daniel)

containers/src/Kokkos_DualView.hpp Outdated Show resolved Hide resolved

using type = Kokkos::DualView<
typename dst_traits::data_type, typename dst_traits::array_layout,
typename dst_traits::device_type, typename dst_traits::memory_traits>;
typename dst_traits::device_type, typename dst_traits::hooks_policy,
Copy link
Member

Choose a reason for hiding this comment

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

Comment about that change.

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'm not sure if you mean commenting that in the code or in the PR...

I'll comment it here first.

The whole point of this PR is to allow a Kokkos::DualView to accept any Properties... that a Kokkos::View can accept. Before this PR, it was hardcoded that DualView only accepts 4 types:

  1. DataType
  2. Arg1Type
  3. Arg2Type
  4. Arg3Type

but there was no check enforcing the type of the last 3 (Arg1Type, Arg2Type, Arg3Type). So they could be anything that Kokkos::ViewTraits would accept. However, DualViewSubview::type would then "hardcode" that when taking a subview of a dual view, only data type, array layout, device type and memory traits would be set. If my understanding is correct, that could have lead to issues if for instance I had created my original dual view with some hooks policy and then took a subview of it because my subview would have the default hooks policy (please correct me if this isn't correct! 😉).

However, I must admit that I'm not satisfied of my solution because I only added hooks_policy while Kokkos::ViewTraits may also accept specialize and others.

So my question for you @dalg24 is how I can ensure that all traits are passed to DualViewSubviewType::type? I guess ideally one should be able to do:

template <class DV, class... Args>
struct DualViewSubviewType {
  static_assert(Kokkos::is_dual_view_v<DV>);

  using dst_traits = typename Kokkos::Impl::ViewMapping<
      void, typename DV::traits, Args...>::traits_type;

  using type = Kokkos::DualView<dst_traits>;
};

where dst_traits is a Kokkos::ViewTraits that would then embed all the traits given by the user and this would ensure that all types are passed. But I agree this would not work so I wonder how one can ensure that DualViewSubviewType::type will always "forward" all types, even in the future when ViewTraits would accept some more types...

Copy link
Member

@dalg24 dalg24 May 11, 2023

Choose a reason for hiding this comment

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

I would have tried something like

namespace Impl {

template <class V>
struct V2DV;

template <class D, class... P>
struct V2DV<View<D, P...>> {
  using type = DualView<D, P...>;
};
} /* namespace Impl */

template <class DataType, class... Properties, class... Args>
auto subview(const DualView<DataType, Properties...>& src, Args... args) {
  // leverage Kokkos::View facilities to deduce the properties of the subview
  using deduce_subview_type =
      decltype(subview(std::declval<View<DataType, Properties...>>(), args...));
  // map it back to dual view
  return typename Impl::V2DV<deduce_subview_type>::type(src, args...);
}

to defer the deduction to Kokkos Core.
Ping @nmm0 who introduced the view hooks and @masterleinad who recently refactored subview(View<...>, ...)

I grepped for "subview" in the containers unit test and the test coverage looks pretty light.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dalg24 This is indeed a much nicer solution!

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'm still wondering why these args... are never perfect-forwarded though... (usually they are integer values but they could be e.g. pairs).

Copy link
Member

Choose a reason for hiding this comment

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

You can use perfect forwarding if you like.

containers/src/Kokkos_DualView.hpp Outdated Show resolved Hide resolved
void deep_copy(
const ExecutionSpace& exec,
DualView<DT, DL, DD, DM> dst, // trust me, this must not be a reference
Copy link
Member

Choose a reason for hiding this comment

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

Comment about why this is ok to delete that comment and not trust whoever wrote that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, my first feeling was that this code was wrong because it was calling modify_host/modify_device on a (shallow) copy of dst. But as the modified_flags member is a (shared) view, it seems this is still OK.

Still I was not "trusting" such a comment like trust me, this must not be a reference (reminds me of "Trust me I'm an engineer") because to me this should work with a reference otherwise there must be other cases where taking a dual view by reference is dangerous.

So I wanted to put my hypothesis that in fact it works with a reference to the test.

On my side, it worked for OpenMP and Cuda (without UVM).

Copy link
Member

Choose a reason for hiding this comment

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

I am leaning towards saying your change is fine.
My point was this needs to be discussed and not swept under the rug. It looks like the comment has been there since the initial commit in this project when the code was extracted from Sandia's Trilinos repository.
cc @crtrott

Copy link
Contributor

Choose a reason for hiding this comment

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

It was introduced in trilinos/Trilinos@0806efc by @mhoemmen.

@romintomasetti romintomasetti changed the title dual view: update template types (#6085) dual view: update template types + Kokkos::is_dual_view (#6085) May 11, 2023
@romintomasetti romintomasetti force-pushed the kokkos-dual-view-template-types branch 3 times, most recently from 65abe36 to a034700 Compare May 11, 2023 14:37
Copy link
Contributor

@masterleinad masterleinad 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 to me given that we discuss changing the copy in deep_copy to a reference some.

Copy link
Member

@dalg24 dalg24 left a comment

Choose a reason for hiding this comment

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

I am ok with changing to a reference in deep_copy

@dalg24
Copy link
Member

dalg24 commented May 15, 2023

OK to test

void deep_copy(
const ExecutionSpace& exec,
DualView<DT, DL, DD, DM> dst, // trust me, this must not be a reference
const DualView<ST, SL, SD, SM>& src) {
DualView<DT, DP...>& dst,
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer

Suggested change
DualView<DT, DP...>& dst,
const DualView<DT, DP...>& dst,

to mirror the overloads for Kokkos::View more closely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 😉

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'm reverting this because these functions are calling modify_host on dst that mustn't be const:

In file included from /__w/kokkos/kokkos/containers/unit_tests/TestDualView.hpp:25:
/__w/kokkos/kokkos/containers/unit_tests/../src/Kokkos_DualView.hpp:1194:9: error: no matching member function for call to 'modify_host'
    dst.modify_host();
    ~~~~^~~~~~~~~~~
/__w/kokkos/kokkos/containers/unit_tests/TestDualView.hpp:185:13: note: in instantiation of function template specialization 'Kokkos::deep_copy<int **, Kokkos::LayoutLeft, Kokkos::OpenMP, int **, Kokkos::LayoutLeft, Kokkos::OpenMP>' requested here
    Kokkos::deep_copy(b, a);

@romintomasetti romintomasetti force-pushed the kokkos-dual-view-template-types branch 2 times, most recently from 95556cf to 7b3859e Compare May 15, 2023 16:51
@romintomasetti romintomasetti force-pushed the kokkos-dual-view-template-types branch from 7b3859e to 1767bfe Compare May 16, 2023 08:07
@romintomasetti
Copy link
Contributor Author

Ready to go 🚀

@dalg24 dalg24 merged commit 4197fa8 into kokkos:develop May 16, 2023
28 checks passed
@romintomasetti romintomasetti deleted the kokkos-dual-view-template-types branch May 17, 2023 13:52
ndellingwood added a commit to trilinos/Trilinos that referenced this pull request May 22, 2023
Compatibility update for kokkos/kokkos#6120
Solution provided by @dalg24 in kokkos/kokkos#6141

Co-authored-by: Damien L-G <dalg24@gmail.com@>
ndellingwood added a commit to ndellingwood/Trilinos that referenced this pull request May 22, 2023
Compatibility update for kokkos/kokkos#6120
Solution provided by @dalg24 in kokkos/kokkos#6141

Co-authored-by: Damien L-G <dalg24@gmail.com@>
ndellingwood added a commit to ndellingwood/Trilinos that referenced this pull request May 22, 2023
Compatibility update for kokkos/kokkos#6120
Solution provided by @dalg24 in kokkos/kokkos#6141

Co-authored-by: Damien L-G <dalg24@gmail.com>
ndellingwood added a commit to trilinos/Trilinos that referenced this pull request May 22, 2023
Compatibility update for kokkos/kokkos#6120
Solution provided by @dalg24 in kokkos/kokkos#6141

Co-authored-by: Damien L-G <dalg24@gmail.com>
ndellingwood added a commit to trilinos/Trilinos that referenced this pull request May 22, 2023
Compatibility update for kokkos/kokkos#6120
Solution provided by @dalg24 in kokkos/kokkos#6141

Co-authored-by: Damien L-G <dalg24@gmail.com>
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

6 participants