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 //
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_STATICVIEW_HPP
43 #define TPETRA_DETAILS_STATICVIEW_HPP
44 
45 #include "TpetraCore_config.h"
46 //#include "Tpetra_Details_Behavior.hpp"
47 #include "Kokkos_DualView.hpp"
48 
49 namespace Tpetra {
50 namespace Details {
51 namespace Impl {
52 
53 template<class MemorySpace>
54 class StaticKokkosAllocation {
55 public:
56  StaticKokkosAllocation () = delete;
57  ~StaticKokkosAllocation () = delete;
58  StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
59  StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
60  StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
61  StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
62 
63  // Allocation automatically registers deallocation to happen at
64  // Kokkos::finalize. Reallocation only happens if needed.
65  static void* resize (MemorySpace space, const size_t size);
66 };
67 
68 #ifdef KOKKOS_ENABLE_CUDA
69 template<>
70 class StaticKokkosAllocation<Kokkos::CudaSpace> {
71 public:
72  StaticKokkosAllocation () = delete;
73  ~StaticKokkosAllocation () = delete;
74  StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
75  StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
76  StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
77  StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
78 
79  static void* resize (Kokkos::CudaSpace space, const size_t size);
80 };
81 
82 template<>
83 class StaticKokkosAllocation<Kokkos::CudaUVMSpace> {
84 public:
85  StaticKokkosAllocation () = delete;
86  ~StaticKokkosAllocation () = delete;
87  StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
88  StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
89  StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
90  StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
91 
92  static void* resize (Kokkos::CudaUVMSpace space, const size_t size);
93 };
94 
95 template<>
96 class StaticKokkosAllocation<Kokkos::CudaHostPinnedSpace> {
97 public:
98  StaticKokkosAllocation () = delete;
99  ~StaticKokkosAllocation () = delete;
100  StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
101  StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
102  StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
103  StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
104 
105  static void* resize (Kokkos::CudaHostPinnedSpace space, const size_t size);
106 };
107 #endif // KOKKOS_ENABLE_CUDA
108 
109 #ifdef KOKKOS_ENABLE_HIP
110 template<>
111 class StaticKokkosAllocation<Kokkos::HIPSpace> {
112 public:
113  StaticKokkosAllocation () = delete;
114  ~StaticKokkosAllocation () = delete;
115  StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
116  StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
117  StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
118  StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
119 
120  static void* resize (Kokkos::HIPSpace space, const size_t size);
121 };
122 
123 template<>
124 class StaticKokkosAllocation<Kokkos::HIPHostPinnedSpace> {
125 public:
126  StaticKokkosAllocation () = delete;
127  ~StaticKokkosAllocation () = delete;
128  StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
129  StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
130  StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
131  StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
132 
133  static void* resize (Kokkos::HIPHostPinnedSpace space, const size_t size);
134 };
135 #endif // KOKKOS_ENABLE_HIP
136 
137 template<>
138 class StaticKokkosAllocation<Kokkos::HostSpace> {
139 public:
140  StaticKokkosAllocation () = delete;
141  ~StaticKokkosAllocation () = delete;
142  StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
143  StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
144  StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
145  StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
146 
147  static void* resize (Kokkos::HostSpace space, const size_t size);
148 };
149 
150 template<class ValueType, class MemorySpace>
151 ValueType*
152 getStaticKokkosMemory (MemorySpace space,
153  const size_t num_entries,
154  const size_t value_size = sizeof (ValueType))
155 {
156  void* ptr = StaticKokkosAllocation<MemorySpace>::resize
157  (space, num_entries * value_size);
158  return reinterpret_cast<ValueType*> (ptr);
159 }
160 
161 } // namespace Impl
162 
163 template<class ValueType, class DeviceType>
164 Kokkos::View<ValueType*, DeviceType>
165 getStatic1dView (const size_t size)
166 {
167  using Impl::getStaticKokkosMemory;
168  using mem_space = typename DeviceType::memory_space;
169  using view_type = Kokkos::View<ValueType*, DeviceType>;
170 
171  ValueType* ptr = getStaticKokkosMemory<ValueType> (mem_space (), size);
172  return view_type (ptr, size);
173 }
174 
175 template<class ValueType, class DeviceType>
176 Kokkos::View<ValueType**, Kokkos::LayoutLeft, DeviceType>
177 getStatic2dView (const size_t num_rows, const size_t num_cols)
178 {
179  using Impl::getStaticKokkosMemory;
180  using mem_space = typename DeviceType::memory_space;
181  using view_type = Kokkos::View<ValueType**, Kokkos::LayoutLeft, DeviceType>;
182 
183  const size_t size = num_rows * num_cols;
184  ValueType* ptr = getStaticKokkosMemory<ValueType> (mem_space (), size);
185  return view_type (ptr, num_rows, num_cols);
186 }
187 
188 template<class ValueType, class DeviceType>
189 Kokkos::DualView<ValueType**, Kokkos::LayoutLeft, DeviceType>
190 getStatic2dDualView (const size_t num_rows, const size_t num_cols)
191 {
192  using dual_view_type =
193  Kokkos::DualView<ValueType**, Kokkos::LayoutLeft, DeviceType>;
194  using d_view_type = typename dual_view_type::t_dev;
195  using h_view_type = typename dual_view_type::t_host;
196 
197  auto d_view = getStatic2dView<ValueType, DeviceType> (num_rows, num_cols);
198  // Preserve the invariant that Kokkos::create_mirror_view returns
199  // the input View here, if and only if it would have returned it if
200  // the allocating View constructor were called.
201  h_view_type h_view;
202  if (std::is_same<typename d_view_type::memory_space,
203  typename h_view_type::memory_space>::value) {
204  h_view = Kokkos::create_mirror_view (d_view);
205  }
206  else {
207  h_view = getStatic2dView<ValueType,
208  typename h_view_type::device_type> (num_rows, num_cols);
209  }
210 
211  return dual_view_type (d_view, h_view);
212 }
213 
214 
215 } // namespace Details
216 } // namespace Tpetra
217 
218 #endif // TPETRA_DETAILS_STATICVIEW_HPP