Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Details_StaticView.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_STATICVIEW_HPP
11 #define TPETRA_DETAILS_STATICVIEW_HPP
12 
13 #include "TpetraCore_config.h"
14 //#include "Tpetra_Details_Behavior.hpp"
15 #include "Kokkos_DualView.hpp"
16 
17 namespace Tpetra {
18 namespace Details {
19 namespace Impl {
20 
21 template <class MemorySpace>
22 class StaticKokkosAllocation {
23  public:
24  StaticKokkosAllocation() = delete;
25  ~StaticKokkosAllocation() = delete;
26  StaticKokkosAllocation(const StaticKokkosAllocation&) = delete;
27  StaticKokkosAllocation& operator=(const StaticKokkosAllocation&) = delete;
28  StaticKokkosAllocation(StaticKokkosAllocation&&) = delete;
29  StaticKokkosAllocation& operator=(StaticKokkosAllocation&&) = delete;
30 
31  // Allocation automatically registers deallocation to happen at
32  // Kokkos::finalize. Reallocation only happens if needed.
33  static void* resize(MemorySpace space, const size_t size);
34 };
35 
36 #ifdef KOKKOS_ENABLE_CUDA
37 template <>
38 class StaticKokkosAllocation<Kokkos::CudaSpace> {
39  public:
40  StaticKokkosAllocation() = delete;
41  ~StaticKokkosAllocation() = delete;
42  StaticKokkosAllocation(const StaticKokkosAllocation&) = delete;
43  StaticKokkosAllocation& operator=(const StaticKokkosAllocation&) = delete;
44  StaticKokkosAllocation(StaticKokkosAllocation&&) = delete;
45  StaticKokkosAllocation& operator=(StaticKokkosAllocation&&) = delete;
46 
47  static void* resize(Kokkos::CudaSpace space, const size_t size);
48 };
49 
50 template <>
51 class StaticKokkosAllocation<Kokkos::CudaUVMSpace> {
52  public:
53  StaticKokkosAllocation() = delete;
54  ~StaticKokkosAllocation() = delete;
55  StaticKokkosAllocation(const StaticKokkosAllocation&) = delete;
56  StaticKokkosAllocation& operator=(const StaticKokkosAllocation&) = delete;
57  StaticKokkosAllocation(StaticKokkosAllocation&&) = delete;
58  StaticKokkosAllocation& operator=(StaticKokkosAllocation&&) = delete;
59 
60  static void* resize(Kokkos::CudaUVMSpace space, const size_t size);
61 };
62 
63 template <>
64 class StaticKokkosAllocation<Kokkos::CudaHostPinnedSpace> {
65  public:
66  StaticKokkosAllocation() = delete;
67  ~StaticKokkosAllocation() = delete;
68  StaticKokkosAllocation(const StaticKokkosAllocation&) = delete;
69  StaticKokkosAllocation& operator=(const StaticKokkosAllocation&) = delete;
70  StaticKokkosAllocation(StaticKokkosAllocation&&) = delete;
71  StaticKokkosAllocation& operator=(StaticKokkosAllocation&&) = delete;
72 
73  static void* resize(Kokkos::CudaHostPinnedSpace space, const size_t size);
74 };
75 #endif // KOKKOS_ENABLE_CUDA
76 
77 #ifdef KOKKOS_ENABLE_HIP
78 template <>
79 class StaticKokkosAllocation<Kokkos::HIPSpace> {
80  public:
81  StaticKokkosAllocation() = delete;
82  ~StaticKokkosAllocation() = delete;
83  StaticKokkosAllocation(const StaticKokkosAllocation&) = delete;
84  StaticKokkosAllocation& operator=(const StaticKokkosAllocation&) = delete;
85  StaticKokkosAllocation(StaticKokkosAllocation&&) = delete;
86  StaticKokkosAllocation& operator=(StaticKokkosAllocation&&) = delete;
87 
88  static void* resize(Kokkos::HIPSpace space, const size_t size);
89 };
90 
91 template <>
92 class StaticKokkosAllocation<Kokkos::HIPHostPinnedSpace> {
93  public:
94  StaticKokkosAllocation() = delete;
95  ~StaticKokkosAllocation() = delete;
96  StaticKokkosAllocation(const StaticKokkosAllocation&) = delete;
97  StaticKokkosAllocation& operator=(const StaticKokkosAllocation&) = delete;
98  StaticKokkosAllocation(StaticKokkosAllocation&&) = delete;
99  StaticKokkosAllocation& operator=(StaticKokkosAllocation&&) = delete;
100 
101  static void* resize(Kokkos::HIPHostPinnedSpace space, const size_t size);
102 };
103 #endif // KOKKOS_ENABLE_HIP
104 
105 template <>
106 class StaticKokkosAllocation<Kokkos::HostSpace> {
107  public:
108  StaticKokkosAllocation() = delete;
109  ~StaticKokkosAllocation() = delete;
110  StaticKokkosAllocation(const StaticKokkosAllocation&) = delete;
111  StaticKokkosAllocation& operator=(const StaticKokkosAllocation&) = delete;
112  StaticKokkosAllocation(StaticKokkosAllocation&&) = delete;
113  StaticKokkosAllocation& operator=(StaticKokkosAllocation&&) = delete;
114 
115  static void* resize(Kokkos::HostSpace space, const size_t size);
116 };
117 
118 template <class ValueType, class MemorySpace>
119 ValueType*
120 getStaticKokkosMemory(MemorySpace space,
121  const size_t num_entries,
122  const size_t value_size = sizeof(ValueType)) {
123  void* ptr = StaticKokkosAllocation<MemorySpace>::resize(space, num_entries * value_size);
124  return reinterpret_cast<ValueType*>(ptr);
125 }
126 
127 } // namespace Impl
128 
129 template <class ValueType, class DeviceType>
130 Kokkos::View<ValueType*, DeviceType>
131 getStatic1dView(const size_t size) {
132  using Impl::getStaticKokkosMemory;
133  using mem_space = typename DeviceType::memory_space;
134  using view_type = Kokkos::View<ValueType*, DeviceType>;
135 
136  ValueType* ptr = getStaticKokkosMemory<ValueType>(mem_space(), size);
137  return view_type(ptr, size);
138 }
139 
140 template <class ValueType, class DeviceType>
141 Kokkos::View<ValueType**, Kokkos::LayoutLeft, DeviceType>
142 getStatic2dView(const size_t num_rows, const size_t num_cols) {
143  using Impl::getStaticKokkosMemory;
144  using mem_space = typename DeviceType::memory_space;
145  using view_type = Kokkos::View<ValueType**, Kokkos::LayoutLeft, DeviceType>;
146 
147  const size_t size = num_rows * num_cols;
148  ValueType* ptr = getStaticKokkosMemory<ValueType>(mem_space(), size);
149  return view_type(ptr, num_rows, num_cols);
150 }
151 
152 template <class ValueType, class DeviceType>
153 Kokkos::DualView<ValueType**, Kokkos::LayoutLeft, DeviceType>
154 getStatic2dDualView(const size_t num_rows, const size_t num_cols) {
155  using dual_view_type =
156  Kokkos::DualView<ValueType**, Kokkos::LayoutLeft, DeviceType>;
157  using d_view_type = typename dual_view_type::t_dev;
158  using h_view_type = typename dual_view_type::t_host;
159 
160  auto d_view = getStatic2dView<ValueType, DeviceType>(num_rows, num_cols);
161  // Preserve the invariant that Kokkos::create_mirror_view returns
162  // the input View here, if and only if it would have returned it if
163  // the allocating View constructor were called.
164  h_view_type h_view;
165  if (std::is_same<typename d_view_type::memory_space,
166  typename h_view_type::memory_space>::value) {
167  h_view = Kokkos::create_mirror_view(d_view);
168  } else {
169  h_view = getStatic2dView<ValueType,
170  typename h_view_type::device_type>(num_rows, num_cols);
171  }
172 
173  return dual_view_type(d_view, h_view);
174 }
175 
176 } // namespace Details
177 } // namespace Tpetra
178 
179 #endif // TPETRA_DETAILS_STATICVIEW_HPP