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// 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_CREATEMIRRORVIEW_HPP
11#define TPETRA_DETAILS_CREATEMIRRORVIEW_HPP
12
13#include "TpetraCore_config.h"
14#include "Teuchos_Array.hpp"
15#include "Teuchos_ArrayView.hpp"
18#include "Kokkos_Core.hpp"
19#include <memory>
20#include <string>
21
29
30namespace Tpetra {
31namespace Details {
32
33namespace Impl {
34
35// Implementation detail of create_mirror_view_from_raw_host_array
36// (see below).
37template<class ValueType,
38 class OutputDeviceType,
39 const bool constInput = std::is_const<ValueType>::value,
40 const bool sameAsHost =
41 std::is_same<Kokkos::HostSpace,
42 typename OutputDeviceType::memory_space>::value>
43class CreateMirrorViewFromUnmanagedHostArray {
44public:
45 typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
46 typedef Kokkos::View<ValueType*,
47 typename output_view_type::array_layout,
48 Kokkos::HostSpace> input_view_type;
49 static output_view_type
50 doIt (ValueType* inPtr,
51 const size_t inSize,
52 const bool copy = true,
53 const char label[] = "");
54};
55
56// Implementation detail of create_mirror_view_from_raw_host_array
57// (see below).
58template<class ValueType,
59 class OutputDeviceType,
60 const bool constInput>
61class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, constInput, true> {
62public:
63 typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
64 typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
65 Kokkos::HostSpace> input_view_type;
66 static output_view_type
67 doIt (ValueType* inPtr,
68 const size_t inSize,
69 const bool /* copy */,
70 const char /* label */ [] = "")
71 {
72 static_assert (std::is_same<typename OutputDeviceType::memory_space,
73 Kokkos::HostSpace>::value,
74 "OutputDeviceType::memory_space must be the same as "
75 "Kokkos::HostSpace in order to use this specialization. "
76 "Please report this bug to the Tpetra developers.");
77 return output_view_type (inPtr, inSize);
78 }
79};
80
81// Implementation detail of create_mirror_view_from_raw_host_array
82// (see below).
83template<class ValueType,
84 class OutputDeviceType>
85class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, true, false> {
86public:
87 typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
88 typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
89 Kokkos::HostSpace> input_view_type;
90 static output_view_type
91 doIt (ValueType* inPtr,
92 const size_t inSize,
93 const bool copy = true,
94 const char label[] = "")
95 {
96 using Kokkos::view_alloc;
97 using Kokkos::WithoutInitializing;
98 static_assert (std::is_const<ValueType>::value, "ValueType must be const "
99 "in order to use this specialization. Please report this "
100 "bug to the Tpetra developers.");
101 static_assert (! std::is_same<typename OutputDeviceType::memory_space, Kokkos::HostSpace>::value,
102 "OutputDeviceType::memory_space must not be the same as "
103 "Kokkos::HostSpace in order to use this specialization. "
104 "Please report this bug to the Tpetra developers.");
105 input_view_type inView (inPtr, inSize);
106 // ValueType is const, so we have to strip away const first.
107 typedef typename output_view_type::non_const_type nc_output_view_type;
108 nc_output_view_type outView_nc;
109 if (! copy) {
110 // Label needs to be a string and not a char*, if given as an
111 // argument to Kokkos::view_alloc. This is because view_alloc
112 // also allows a raw pointer as its first argument. See
113 // https://github.com/kokkos/kokkos/issues/434.
114 outView_nc = nc_output_view_type (view_alloc (std::string (label)), inSize);
115 }
116 else {
117 // No need to initialize, if we're going to copy into it anyway.
118 outView_nc = nc_output_view_type (view_alloc (std::string (label), WithoutInitializing), inSize);
119 // DEEP_COPY REVIEW - HOST-TO-DEVICE
120 using execution_space = typename nc_output_view_type::execution_space;
121 Kokkos::deep_copy (execution_space(), outView_nc, inView);
122 }
123 return outView_nc; // this casts back to const
124 }
125};
126
127// Implementation detail of create_mirror_view_from_raw_host_array
128// (see below).
129template<class ValueType,
130 class OutputDeviceType>
131class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, false, false> {
132public:
133 typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
134 typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
135 Kokkos::HostSpace> input_view_type;
136 static output_view_type
137 doIt (ValueType* inPtr,
138 const size_t inSize,
139 const bool copy = true,
140 const char label[] = "")
141 {
142 typedef typename OutputDeviceType::memory_space out_mem_space;
143 typedef typename OutputDeviceType::execution_space out_exec_space;
144 static_assert (! std::is_const<ValueType>::value, "ValueType must not be "
145 "const in order to use this specialization. Please report "
146 "this bug to the Tpetra developers.");
147 static_assert (! std::is_same<out_mem_space, Kokkos::HostSpace>::value,
148 "OutputDeviceType::memory_space must not be the same as "
149 "Kokkos::HostSpace in order to use this specialization. "
150 "Please report this bug to the Tpetra developers.");
151 input_view_type inView (inPtr, inSize);
152 output_view_type outView =
153 Kokkos::create_mirror_view (out_mem_space (), inView);
154 if (copy) {
155 // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
156 Kokkos::deep_copy (out_exec_space(), outView, inView);
157 }
158 return outView;
159 }
160};
161
162} // namespace Impl
163
171template<class ValueType, class OutputDeviceType>
172typename Impl::CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType>::output_view_type
173create_mirror_view_from_raw_host_array (const OutputDeviceType& /* dev */,
174 ValueType* inPtr,
175 const size_t inSize,
176 const bool copy = true,
177 const char label[] = "")
178{
179 typedef Impl::CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType> impl_type;
180 return impl_type::doIt (inPtr, inSize, copy, label);
181}
182
183} // namespace Details
184} // namespace Tpetra
185
186#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...
Nonmember function that computes a residual Computes R = B - A * X.
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.