Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_Details_createMirrorView.hpp
Go to the documentation of this file.
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_CREATEMIRRORVIEW_HPP
43#define TPETRA_DETAILS_CREATEMIRRORVIEW_HPP
44
45#include "TpetraCore_config.h"
46#include "Teuchos_Array.hpp"
47#include "Teuchos_ArrayView.hpp"
50#include "Kokkos_Core.hpp"
51#include <memory>
52#include <string>
53
61
62namespace Tpetra {
63namespace Details {
64
65namespace Impl {
66
67// Implementation detail of create_mirror_view_from_raw_host_array
68// (see below).
69template<class ValueType,
70 class OutputDeviceType,
71 const bool constInput = std::is_const<ValueType>::value,
72 const bool sameAsHost =
73 std::is_same<Kokkos::HostSpace,
74 typename OutputDeviceType::memory_space>::value>
75class CreateMirrorViewFromUnmanagedHostArray {
76public:
77 typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
78 typedef Kokkos::View<ValueType*,
79 typename output_view_type::array_layout,
80 Kokkos::HostSpace> input_view_type;
81 static output_view_type
82 doIt (ValueType* inPtr,
83 const size_t inSize,
84 const bool copy = true,
85 const char label[] = "");
86};
87
88// Implementation detail of create_mirror_view_from_raw_host_array
89// (see below).
90template<class ValueType,
91 class OutputDeviceType,
92 const bool constInput>
93class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, constInput, true> {
94public:
95 typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
96 typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
97 Kokkos::HostSpace> input_view_type;
98 static output_view_type
99 doIt (ValueType* inPtr,
100 const size_t inSize,
101 const bool /* copy */,
102 const char /* label */ [] = "")
103 {
104 static_assert (std::is_same<typename OutputDeviceType::memory_space,
105 Kokkos::HostSpace>::value,
106 "OutputDeviceType::memory_space must be the same as "
107 "Kokkos::HostSpace in order to use this specialization. "
108 "Please report this bug to the Tpetra developers.");
109 return output_view_type (inPtr, inSize);
110 }
111};
112
113// Implementation detail of create_mirror_view_from_raw_host_array
114// (see below).
115template<class ValueType,
116 class OutputDeviceType>
117class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, true, false> {
118public:
119 typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
120 typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
121 Kokkos::HostSpace> input_view_type;
122 static output_view_type
123 doIt (ValueType* inPtr,
124 const size_t inSize,
125 const bool copy = true,
126 const char label[] = "")
127 {
128 using Kokkos::view_alloc;
129 using Kokkos::WithoutInitializing;
130 static_assert (std::is_const<ValueType>::value, "ValueType must be const "
131 "in order to use this specialization. Please report this "
132 "bug to the Tpetra developers.");
133 static_assert (! std::is_same<typename OutputDeviceType::memory_space, Kokkos::HostSpace>::value,
134 "OutputDeviceType::memory_space must not be the same as "
135 "Kokkos::HostSpace in order to use this specialization. "
136 "Please report this bug to the Tpetra developers.");
137 input_view_type inView (inPtr, inSize);
138 // ValueType is const, so we have to strip away const first.
139 typedef typename output_view_type::non_const_type nc_output_view_type;
140 nc_output_view_type outView_nc;
141 if (! copy) {
142 // Label needs to be a string and not a char*, if given as an
143 // argument to Kokkos::view_alloc. This is because view_alloc
144 // also allows a raw pointer as its first argument. See
145 // https://github.com/kokkos/kokkos/issues/434.
146 outView_nc = nc_output_view_type (view_alloc (std::string (label)), inSize);
147 }
148 else {
149 // No need to initialize, if we're going to copy into it anyway.
150 outView_nc = nc_output_view_type (view_alloc (std::string (label), WithoutInitializing), inSize);
151 // DEEP_COPY REVIEW - HOST-TO-DEVICE
152 using execution_space = typename nc_output_view_type::execution_space;
153 Kokkos::deep_copy (execution_space(), outView_nc, inView);
154 }
155 return outView_nc; // this casts back to const
156 }
157};
158
159// Implementation detail of create_mirror_view_from_raw_host_array
160// (see below).
161template<class ValueType,
162 class OutputDeviceType>
163class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, false, false> {
164public:
165 typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
166 typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
167 Kokkos::HostSpace> input_view_type;
168 static output_view_type
169 doIt (ValueType* inPtr,
170 const size_t inSize,
171 const bool copy = true,
172 const char label[] = "")
173 {
174 typedef typename OutputDeviceType::memory_space out_mem_space;
175 typedef typename OutputDeviceType::execution_space out_exec_space;
176 static_assert (! std::is_const<ValueType>::value, "ValueType must not be "
177 "const in order to use this specialization. Please report "
178 "this bug to the Tpetra developers.");
179 static_assert (! std::is_same<out_mem_space, Kokkos::HostSpace>::value,
180 "OutputDeviceType::memory_space must not be the same as "
181 "Kokkos::HostSpace in order to use this specialization. "
182 "Please report this bug to the Tpetra developers.");
183 input_view_type inView (inPtr, inSize);
184 output_view_type outView =
185 Kokkos::create_mirror_view (out_mem_space (), inView);
186 if (copy) {
187 // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
188 Kokkos::deep_copy (out_exec_space(), outView, inView);
189 }
190 return outView;
191 }
192};
193
194} // namespace Impl
195
203template<class ValueType, class OutputDeviceType>
204typename Impl::CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType>::output_view_type
205create_mirror_view_from_raw_host_array (const OutputDeviceType& /* dev */,
206 ValueType* inPtr,
207 const size_t inSize,
208 const bool copy = true,
209 const char label[] = "")
210{
211 typedef Impl::CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType> impl_type;
212 return impl_type::doIt (inPtr, inSize, copy, label);
213}
214
215} // namespace Details
216} // namespace Tpetra
217
218#endif // TPETRA_DETAILS_CREATEMIRRORVIEW_HPP
Import KokkosSparse::OrdinalTraits, a traits class for "invalid" (flag) values of integer types,...
Declare and define the functions Tpetra::Details::computeOffsetsFromCounts and Tpetra::computeOffsets...
Implementation details of Tpetra.
Impl::CreateMirrorViewFromUnmanagedHostArray< ValueType, OutputDeviceType >::output_view_type create_mirror_view_from_raw_host_array(const OutputDeviceType &, ValueType *inPtr, const size_t inSize, const bool copy=true, const char label[]="")
Variant of Kokkos::create_mirror_view that takes a raw host 1-d array as input.
Namespace Tpetra contains the class and methods constituting the Tpetra library.