Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
62 namespace Tpetra {
63 namespace Details {
64 
65 namespace Impl {
66 
67 // Implementation detail of create_mirror_view_from_raw_host_array
68 // (see below).
69 template<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>
75 class CreateMirrorViewFromUnmanagedHostArray {
76 public:
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).
90 template<class ValueType,
91  class OutputDeviceType,
92  const bool constInput>
93 class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, constInput, true> {
94 public:
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).
115 template<class ValueType,
116  class OutputDeviceType>
117 class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, true, false> {
118 public:
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  Kokkos::deep_copy (outView_nc, inView);
152  }
153  return outView_nc; // this casts back to const
154  }
155 };
156 
157 // Implementation detail of create_mirror_view_from_raw_host_array
158 // (see below).
159 template<class ValueType,
160  class OutputDeviceType>
161 class CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType, false, false> {
162 public:
163  typedef Kokkos::View<ValueType*, OutputDeviceType> output_view_type;
164  typedef Kokkos::View<ValueType*, typename output_view_type::array_layout,
165  Kokkos::HostSpace> input_view_type;
166  static output_view_type
167  doIt (ValueType* inPtr,
168  const size_t inSize,
169  const bool copy = true,
170  const char label[] = "")
171  {
172  typedef typename OutputDeviceType::memory_space out_mem_space;
173  static_assert (! std::is_const<ValueType>::value, "ValueType must not be "
174  "const in order to use this specialization. Please report "
175  "this bug to the Tpetra developers.");
176  static_assert (! std::is_same<out_mem_space, Kokkos::HostSpace>::value,
177  "OutputDeviceType::memory_space must not be the same as "
178  "Kokkos::HostSpace in order to use this specialization. "
179  "Please report this bug to the Tpetra developers.");
180  input_view_type inView (inPtr, inSize);
181  output_view_type outView =
182  Kokkos::create_mirror_view (out_mem_space (), inView);
183  if (copy) {
184  Kokkos::deep_copy (outView, inView);
185  }
186  return outView;
187  }
188 };
189 
190 } // namespace Impl
191 
199 template<class ValueType, class OutputDeviceType>
200 typename Impl::CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType>::output_view_type
201 create_mirror_view_from_raw_host_array (const OutputDeviceType& /* dev */,
202  ValueType* inPtr,
203  const size_t inSize,
204  const bool copy = true,
205  const char label[] = "")
206 {
207  typedef Impl::CreateMirrorViewFromUnmanagedHostArray<ValueType, OutputDeviceType> impl_type;
208  return impl_type::doIt (inPtr, inSize, copy, label);
209 }
210 
211 } // namespace Details
212 } // namespace Tpetra
213 
214 #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 function Tpetra::Details::computeOffsetsFromCounts, an implementation detail o...