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 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_DETAILS_TEMPVIEWUTILS_HPP
43 #define TPETRA_DETAILS_TEMPVIEWUTILS_HPP
44 
45 #include "Kokkos_Core.hpp"
47 
48 namespace Tpetra
49 {
50 namespace Details
51 {
52 namespace TempView
53 {
54 
55 template<typename MemorySpace>
56 struct AlwaysMPISafe
57 {
58  enum : bool {value = false};
59 };
60 
61 template<>
62 struct AlwaysMPISafe<Kokkos::HostSpace>
63 {
64  enum : bool {value = true};
65 };
66 
67 #ifdef KOKKOS_ENABLE_CUDA
68 template<>
69 struct AlwaysMPISafe<Kokkos::CudaHostPinnedSpace>
70 {
71  enum : bool {value = true};
72 };
73 #endif
74 
75 #ifdef KOKKOS_ENABLE_HIP
76 template<>
77 struct AlwaysMPISafe<Kokkos::HIPHostPinnedSpace>
78 {
79  enum : bool {value = true};
80 };
81 #endif
82 
84 template<typename View1, typename View2>
86 {
87  using L1 = typename View1::array_layout;
88  using L2 = typename View2::array_layout;
89  enum : bool {EitherLeft = std::is_same<L1, Kokkos::LayoutLeft>::value || std::is_same<L2, Kokkos::LayoutLeft>::value};
90 enum : bool {BothStride = std::is_same<L1, Kokkos::LayoutStride>::value && std::is_same<L2, Kokkos::LayoutStride>::value};
91  using type = typename std::conditional<EitherLeft || BothStride, Kokkos::LayoutLeft, Kokkos::LayoutRight>::type;
92 };
93 
95 template<typename SrcView, typename Layout, typename std::enable_if<!std::is_same<typename SrcView::array_layout, Layout>::value>::type* = nullptr>
96 Kokkos::View<typename SrcView::data_type, Layout, typename SrcView::device_type>
97 toLayout(const SrcView& src)
98 {
99  static_assert(!std::is_same<Kokkos::LayoutStride, Layout>::value,
100  "TempView::toLayout: Layout must be contiguous (not LayoutStride)");
101  Layout layout(src.extent(0), src.extent(1));
102  Kokkos::View<typename SrcView::non_const_data_type, Layout, typename SrcView::device_type> dst(Kokkos::ViewAllocateWithoutInitializing(src.label()), layout);
103  Kokkos::deep_copy(dst, src);
104  return dst;
105 }
106 
107 template<typename SrcView, typename Layout, typename std::enable_if<std::is_same<typename SrcView::array_layout, Layout>::value>::type* = nullptr>
108 Kokkos::View<typename SrcView::data_type, Layout, typename SrcView::device_type>
109 toLayout(const SrcView& src)
110 {
111  if(src.span_is_contiguous())
112  {
113  return src;
114  }
115  else
116  {
117  //Even though the layout is already correct, it's not contiguous.
118  Layout layout(src.extent(0), src.extent(1));
119  Kokkos::View<typename SrcView::non_const_data_type, Layout, typename SrcView::device_type>
120  result(Kokkos::ViewAllocateWithoutInitializing(src.label()), layout);
121  Kokkos::deep_copy(result, src);
122  return result;
123  }
124 }
125 
129 template<typename SrcView, bool AssumeGPUAware, typename = typename std::enable_if<AssumeGPUAware || AlwaysMPISafe<typename SrcView::memory_space>::value>::type>
130 SrcView
131 toMPISafe(const SrcView& src)
132 {
133  using SrcLayout = typename SrcView::array_layout;
134  static_assert(!std::is_same<SrcLayout, Kokkos::LayoutStride>::value, "toMPISafe requires that SrcView is contiguous");
135  return toLayout<SrcView, SrcLayout>(src);
136 }
137 
138 template<typename SrcView, bool AssumeGPUAware, typename = typename std::enable_if<!(AssumeGPUAware || AlwaysMPISafe<typename SrcView::memory_space>::value)>::type>
139 decltype(Kokkos::create_mirror_view_and_copy(std::declval<Kokkos::HostSpace>(), std::declval<SrcView>()))
140 toMPISafe(const SrcView& src)
141 {
142  using SrcLayout = typename SrcView::array_layout;
143  static_assert(!std::is_same<SrcLayout, Kokkos::LayoutStride>::value, "toMPISafe requires that SrcView is contiguous");
144  auto srcContig = toLayout<SrcView, SrcLayout>(src);
145  return Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), srcContig);
146 }
147 
148 }}} //namespace Tpetra::Details::TempView
149 
150 #endif
151 
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...