Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
30 namespace Tpetra {
31 namespace Details {
32 
33 namespace Impl {
34 
35 // Implementation detail of create_mirror_view_from_raw_host_array
36 // (see below).
37 template <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>
43 class CreateMirrorViewFromUnmanagedHostArray {
44  public:
45  typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
46  typedef Kokkos::View<ValueType*,
47  typename output_view_type::array_layout,
48  Kokkos::HostSpace>
49  input_view_type;
50  static output_view_type
51  doIt(ValueType* inPtr,
52  const size_t inSize,
53  const bool copy = true,
54  const char label[] = "");
55 };
56 
57 // Implementation detail of create_mirror_view_from_raw_host_array
58 // (see below).
59 template <class ValueType,
60  class OutputDeviceType,
61  const bool constInput>
62 class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, constInput, true> {
63  public:
64  typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
65  typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
66  Kokkos::HostSpace>
67  input_view_type;
68  static output_view_type
69  doIt(ValueType* inPtr,
70  const size_t inSize,
71  const bool /* copy */,
72  const char /* label */[] = "") {
73  static_assert(std::is_same<typename OutputDeviceType::memory_space,
74  Kokkos::HostSpace>::value,
75  "OutputDeviceType::memory_space must be the same as "
76  "Kokkos::HostSpace in order to use this specialization. "
77  "Please report this bug to the Tpetra developers.");
78  return output_view_type(inPtr, inSize);
79  }
80 };
81 
82 // Implementation detail of create_mirror_view_from_raw_host_array
83 // (see below).
84 template <class ValueType,
85  class OutputDeviceType>
86 class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, true, false> {
87  public:
88  typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
89  typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
90  Kokkos::HostSpace>
91  input_view_type;
92  static output_view_type
93  doIt(ValueType* inPtr,
94  const size_t inSize,
95  const bool copy = true,
96  const char label[] = "") {
97  using Kokkos::view_alloc;
98  using Kokkos::WithoutInitializing;
99  static_assert(std::is_const<ValueType>::value,
100  "ValueType must be const "
101  "in order to use this specialization. Please report this "
102  "bug to the Tpetra developers.");
103  static_assert(!std::is_same<typename OutputDeviceType::memory_space, Kokkos::HostSpace>::value,
104  "OutputDeviceType::memory_space must not be the same as "
105  "Kokkos::HostSpace in order to use this specialization. "
106  "Please report this bug to the Tpetra developers.");
107  input_view_type inView(inPtr, inSize);
108  // ValueType is const, so we have to strip away const first.
109  typedef typename output_view_type::non_const_type nc_output_view_type;
110  nc_output_view_type outView_nc;
111  if (!copy) {
112  // Label needs to be a string and not a char*, if given as an
113  // argument to Kokkos::view_alloc. This is because view_alloc
114  // also allows a raw pointer as its first argument. See
115  // https://github.com/kokkos/kokkos/issues/434.
116  outView_nc = nc_output_view_type(view_alloc(std::string(label)), inSize);
117  } else {
118  // No need to initialize, if we're going to copy into it anyway.
119  outView_nc = nc_output_view_type(view_alloc(std::string(label), WithoutInitializing), inSize);
120  // DEEP_COPY REVIEW - HOST-TO-DEVICE
121  using execution_space = typename nc_output_view_type::execution_space;
122  Kokkos::deep_copy(execution_space(), outView_nc, inView);
123  }
124  return outView_nc; // this casts back to const
125  }
126 };
127 
128 // Implementation detail of create_mirror_view_from_raw_host_array
129 // (see below).
130 template <class ValueType,
131  class OutputDeviceType>
132 class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, false, false> {
133  public:
134  typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
135  typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
136  Kokkos::HostSpace>
137  input_view_type;
138  static output_view_type
139  doIt(ValueType* inPtr,
140  const size_t inSize,
141  const bool copy = true,
142  const char label[] = "") {
143  typedef typename OutputDeviceType::memory_space out_mem_space;
144  typedef typename OutputDeviceType::execution_space out_exec_space;
145  static_assert(!std::is_const<ValueType>::value,
146  "ValueType must not be "
147  "const in order to use this specialization. Please report "
148  "this bug to the Tpetra developers.");
149  static_assert(!std::is_same<out_mem_space, Kokkos::HostSpace>::value,
150  "OutputDeviceType::memory_space must not be the same as "
151  "Kokkos::HostSpace in order to use this specialization. "
152  "Please report this bug to the Tpetra developers.");
153  input_view_type inView(inPtr, inSize);
154  output_view_type outView =
155  Kokkos::create_mirror_view(out_mem_space(), inView);
156  if (copy) {
157  // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
158  Kokkos::deep_copy(out_exec_space(), outView, inView);
159  }
160  return outView;
161  }
162 };
163 
164 } // namespace Impl
165 
173 template <class ValueType, class OutputDeviceType>
174 typename Impl::CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType>::output_view_type
175 create_mirror_view_from_raw_host_array(const OutputDeviceType& /* dev */,
176  ValueType* inPtr,
177  const size_t inSize,
178  const bool copy = true,
179  const char label[] = "") {
180  typedef Impl::CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType> impl_type;
181  return impl_type::doIt(inPtr, inSize, copy, label);
182 }
183 
184 } // namespace Details
185 } // namespace Tpetra
186 
187 #endif // TPETRA_DETAILS_CREATEMIRRORVIEW_HPP
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.
Import KokkosSparse::OrdinalTraits, a traits class for &quot;invalid&quot; (flag) values of integer types...
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
Declare and define the functions Tpetra::Details::computeOffsetsFromCounts and Tpetra::computeOffsets...