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> 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).
58 template<class ValueType,
59  class OutputDeviceType,
60  const bool constInput>
61 class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, constInput, true> {
62 public:
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).
83 template<class ValueType,
84  class OutputDeviceType>
85 class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, true, false> {
86 public:
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).
129 template<class ValueType,
130  class OutputDeviceType>
131 class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, false, false> {
132 public:
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 
171 template<class ValueType, class OutputDeviceType>
172 typename Impl::CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType>::output_view_type
173 create_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
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...