Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
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
16namespace Tpetra
17{
18namespace Details
19{
20namespace TempView
21{
22
23template<typename MemorySpace>
24struct AlwaysMPISafe
25{
26 enum : bool {value = false};
27};
28
29template<>
30struct AlwaysMPISafe<Kokkos::HostSpace>
31{
32 enum : bool {value = true};
33};
34
35#ifdef KOKKOS_ENABLE_CUDA
36template<>
37struct AlwaysMPISafe<Kokkos::CudaHostPinnedSpace>
38{
39 enum : bool {value = true};
40};
41#endif
42
43#ifdef KOKKOS_ENABLE_HIP
44template<>
45struct AlwaysMPISafe<Kokkos::HIPHostPinnedSpace>
46{
47 enum : bool {value = true};
48};
49#endif
50
52template<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};
58enum : 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
64Kokkos::View<typename SrcView::data_type, Layout, typename SrcView::device_type>
65toLayout(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
76Kokkos::View<typename SrcView::data_type, Layout, typename SrcView::device_type>
77toLayout(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
97template<typename SrcView, bool AssumeGPUAware, typename = typename std::enable_if<AssumeGPUAware || AlwaysMPISafe<typename SrcView::memory_space>::value>::type>
98SrcView
99toMPISafe(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
106template<typename SrcView, bool AssumeGPUAware, typename = typename std::enable_if<!(AssumeGPUAware || AlwaysMPISafe<typename SrcView::memory_space>::value)>::type>
107decltype(Kokkos::create_mirror_view_and_copy(std::declval<Kokkos::HostSpace>(), std::declval<SrcView>()))
108toMPISafe(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
Declaration of Tpetra::Details::isInterComm.
Struct that holds views of the contents of a CrsMatrix.
Implementation details of Tpetra.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Get the contiguous layout that matches as many of the given views as possible. If neither or both arg...