Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 template<>
110 class StaticKokkosAllocation<Kokkos::HostSpace> {
111 public:
112  StaticKokkosAllocation () = delete;
113  ~StaticKokkosAllocation () = delete;
114  StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
115  StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
116  StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
117  StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
118 
119  static void* resize (Kokkos::HostSpace space, const size_t size);
120 };
121 
122 template<class ValueType, class MemorySpace>
123 ValueType*
124 getStaticKokkosMemory (MemorySpace space,
125  const size_t num_entries,
126  const size_t value_size = sizeof (ValueType))
127 {
128  void* ptr = StaticKokkosAllocation<MemorySpace>::resize
129  (space, num_entries * value_size);
130  return reinterpret_cast<ValueType*> (ptr);
131 }
132 
133 } // namespace Impl
134 
135 template<class ValueType, class DeviceType>
136 Kokkos::View<ValueType*, DeviceType>
137 getStatic1dView (const size_t size)
138 {
139  using Impl::getStaticKokkosMemory;
140  using mem_space = typename DeviceType::memory_space;
141  using view_type = Kokkos::View<ValueType*, DeviceType>;
142 
143  ValueType* ptr = getStaticKokkosMemory<ValueType> (mem_space (), size);
144  return view_type (ptr, size);
145 }
146 
147 template<class ValueType, class DeviceType>
148 Kokkos::View<ValueType**, Kokkos::LayoutLeft, DeviceType>
149 getStatic2dView (const size_t num_rows, const size_t num_cols)
150 {
151  using Impl::getStaticKokkosMemory;
152  using mem_space = typename DeviceType::memory_space;
153  using view_type = Kokkos::View<ValueType**, Kokkos::LayoutLeft, DeviceType>;
154 
155  const size_t size = num_rows * num_cols;
156  ValueType* ptr = getStaticKokkosMemory<ValueType> (mem_space (), size);
157  return view_type (ptr, num_rows, num_cols);
158 }
159 
160 template<class ValueType, class DeviceType>
161 Kokkos::DualView<ValueType**, Kokkos::LayoutLeft, DeviceType>
162 getStatic2dDualView (const size_t num_rows, const size_t num_cols)
163 {
164  using dual_view_type =
165  Kokkos::DualView<ValueType**, Kokkos::LayoutLeft, DeviceType>;
166  using d_view_type = typename dual_view_type::t_dev;
167  using h_view_type = typename dual_view_type::t_host;
168 
169  auto d_view = getStatic2dView<ValueType, DeviceType> (num_rows, num_cols);
170  // Preserve the invariant that Kokkos::create_mirror_view returns
171  // the input View here, if and only if it would have returned it if
172  // the allocating View constructor were called.
173  h_view_type h_view;
174  if (std::is_same<typename d_view_type::memory_space,
175  typename h_view_type::memory_space>::value) {
176  h_view = Kokkos::create_mirror_view (d_view);
177  }
178  else {
179  h_view = getStatic2dView<ValueType,
180  typename h_view_type::device_type> (num_rows, num_cols);
181  }
182 
183  return dual_view_type (d_view, h_view);
184 }
185 
186 
187 } // namespace Details
188 } // namespace Tpetra
189 
190 #endif // TPETRA_DETAILS_STATICVIEW_HPP