Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tpetra_Details_getEntryOnHost.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_GETENTRYONHOST_HPP
43 #define TPETRA_DETAILS_GETENTRYONHOST_HPP
44 
50 
51 #include "TpetraCore_config.h"
52 #include "Kokkos_Core.hpp"
53 
54 namespace Tpetra {
55 namespace Details {
56 namespace Impl {
57 
58 template<class ViewType,
59  class IndexType = typename ViewType::size_type,
60  const bool isHostSpace =
61  std::is_same<typename ViewType::memory_space,
62  Kokkos::HostSpace>::value>
63 struct GetEntryOnHost {
64  static typename ViewType::non_const_value_type
65  getEntryOnHost (const ViewType& x,
66  const IndexType ind);
67 };
68 
69 template<class ViewType,
70  class IndexType>
71 struct GetEntryOnHost<ViewType, IndexType, true> {
72  static typename ViewType::non_const_value_type
73  getEntryOnHost (const ViewType& x,
74  const IndexType ind)
75  {
76  static_assert (ViewType::Rank == 1, "x must be a rank-1 Kokkos::View.");
77  return x(ind);
78  }
79 };
80 
81 template<class ViewType,
82  class IndexType>
83 struct GetEntryOnHost<ViewType, IndexType, false> {
84  static typename ViewType::non_const_value_type
85  getEntryOnHost (const ViewType& x,
86  const IndexType ind)
87  {
88  // Don't assume UVM. Carefully get a 0-D subview of the entry of
89  // the array, and copy to device. Do not use host mirror, because
90  // that could just be a UVM View if using UVM.
91  static_assert (ViewType::Rank == 1, "x must be a rank-1 Kokkos::View.");
92 #ifdef KOKKOS_ENABLE_CUDA
93  // Do not use Kokkos::create_mirror_view, because that could just
94  // be a UVM View if using UVM.
95  typedef typename ViewType::device_type device_type;
96  // Hide this in ifdef, to avoid unused typedef warning.
97  typedef typename device_type::execution_space dev_exec_space;
98 #endif // KOKKOS_ENABLE_CUDA
99  typedef typename ViewType::HostMirror::execution_space host_exec_space;
100  typedef Kokkos::Device<host_exec_space, Kokkos::HostSpace> host_device_type;
101  typedef typename ViewType::non_const_value_type value_type;
102 
103  value_type val;
104  Kokkos::View<value_type, host_device_type> view_h (&val);
105  auto view_d = Kokkos::subview (x, ind); // 0-D View
106 #ifdef KOKKOS_ENABLE_CUDA
107  typedef typename device_type::memory_space dev_memory_space;
108  if (std::is_same<dev_memory_space, Kokkos::CudaUVMSpace>::value) {
109  dev_exec_space::fence (); // for UVM's sake.
110  }
111 #endif // KOKKOS_ENABLE_CUDA
112  Kokkos::deep_copy (view_h, view_d);
113 #ifdef KOKKOS_ENABLE_CUDA
114  if (std::is_same<dev_memory_space, Kokkos::CudaUVMSpace>::value) {
115  dev_exec_space::fence (); // for UVM's sake.
116  }
117 #endif // KOKKOS_ENABLE_CUDA
118  return val;
119  }
120 };
121 
122 } // namespace Impl
123 
124 template<class ViewType,
125  class IndexType>
126 typename ViewType::non_const_value_type
127 getEntryOnHost (const ViewType& x,
128  const IndexType ind)
129 {
130  return Impl::GetEntryOnHost<ViewType, IndexType>::getEntryOnHost (x, ind);
131 }
132 
133 } // namespace Details
134 } // namespace Tpetra
135 
136 #endif // TPETRA_DETAILS_GETENTRYONHOST_HPP
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.