Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Details_temporaryViewUtils.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TPETRA_DETAILS_TEMPVIEWUTILS_HPP
11 #define TPETRA_DETAILS_TEMPVIEWUTILS_HPP
12 
13 #include "Kokkos_Core.hpp"
15 
16 namespace Tpetra
17 {
18 namespace Details
19 {
20 namespace TempView
21 {
22 
23 template<typename MemorySpace>
24 struct AlwaysMPISafe
25 {
26  enum : bool {value = false};
27 };
28 
29 template<>
30 struct AlwaysMPISafe<Kokkos::HostSpace>
31 {
32  enum : bool {value = true};
33 };
34 
35 #ifdef KOKKOS_ENABLE_CUDA
36 template<>
37 struct AlwaysMPISafe<Kokkos::CudaHostPinnedSpace>
38 {
39  enum : bool {value = true};
40 };
41 #endif
42 
43 #ifdef KOKKOS_ENABLE_HIP
44 template<>
45 struct AlwaysMPISafe<Kokkos::HIPHostPinnedSpace>
46 {
47  enum : bool {value = true};
48 };
49 #endif
50 
52 template<typename View1, typename View2>
54 {
55  using L1 = typename View1::array_layout;
56  using L2 = typename View2::array_layout;
57  enum : bool {EitherLeft = std::is_same<L1, Kokkos::LayoutLeft>::value || std::is_same<L2, Kokkos::LayoutLeft>::value};
58 enum : bool {BothStride = std::is_same<L1, Kokkos::LayoutStride>::value && std::is_same<L2, Kokkos::LayoutStride>::value};
59  using type = typename std::conditional<EitherLeft || BothStride, Kokkos::LayoutLeft, Kokkos::LayoutRight>::type;
60 };
61 
63 template<typename SrcView, typename Layout, typename std::enable_if<!std::is_same<typename SrcView::array_layout, Layout>::value>::type* = nullptr>
64 Kokkos::View<typename SrcView::data_type, Layout, typename SrcView::device_type>
65 toLayout(const SrcView& src)
66 {
67  static_assert(!std::is_same<Kokkos::LayoutStride, Layout>::value,
68  "TempView::toLayout: Layout must be contiguous (not LayoutStride)");
69  Layout layout(src.extent(0), src.extent(1));
70  Kokkos::View<typename SrcView::non_const_data_type, Layout, typename SrcView::device_type> dst(Kokkos::ViewAllocateWithoutInitializing(src.label()), layout);
71  Kokkos::deep_copy(dst, src);
72  return dst;
73 }
74 
75 template<typename SrcView, typename Layout, typename std::enable_if<std::is_same<typename SrcView::array_layout, Layout>::value>::type* = nullptr>
76 Kokkos::View<typename SrcView::data_type, Layout, typename SrcView::device_type>
77 toLayout(const SrcView& src)
78 {
79  if(src.span_is_contiguous())
80  {
81  return src;
82  }
83  else
84  {
85  //Even though the layout is already correct, it's not contiguous.
86  Layout layout(src.extent(0), src.extent(1));
87  Kokkos::View<typename SrcView::non_const_data_type, Layout, typename SrcView::device_type>
88  result(Kokkos::ViewAllocateWithoutInitializing(src.label()), layout);
89  Kokkos::deep_copy(result, src);
90  return result;
91  }
92 }
93 
97 template<typename SrcView, bool AssumeGPUAware, typename = typename std::enable_if<AssumeGPUAware || AlwaysMPISafe<typename SrcView::memory_space>::value>::type>
98 SrcView
99 toMPISafe(const SrcView& src)
100 {
101  using SrcLayout = typename SrcView::array_layout;
102  static_assert(!std::is_same<SrcLayout, Kokkos::LayoutStride>::value, "toMPISafe requires that SrcView is contiguous");
103  return toLayout<SrcView, SrcLayout>(src);
104 }
105 
106 template<typename SrcView, bool AssumeGPUAware, typename = typename std::enable_if<!(AssumeGPUAware || AlwaysMPISafe<typename SrcView::memory_space>::value)>::type>
107 decltype(Kokkos::create_mirror_view_and_copy(std::declval<Kokkos::HostSpace>(), std::declval<SrcView>()))
108 toMPISafe(const SrcView& src)
109 {
110  using SrcLayout = typename SrcView::array_layout;
111  static_assert(!std::is_same<SrcLayout, Kokkos::LayoutStride>::value, "toMPISafe requires that SrcView is contiguous");
112  auto srcContig = toLayout<SrcView, SrcLayout>(src);
113  return Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), srcContig);
114 }
115 
116 }}} //namespace Tpetra::Details::TempView
117 
118 #endif
119 
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
Declaration of Tpetra::Details::isInterComm.
Get the contiguous layout that matches as many of the given views as possible. If neither or both arg...