Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Kokkos_View_Copy_Assign.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Amesos2: Templated Direct Sparse Solver Package
6 // Copyright 2011 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //
42 // @HEADER
43 
52 #ifndef AMESOS2_KOKKOS_VIEW_COPY_ASSIGN_HPP
53 #define AMESOS2_KOKKOS_VIEW_COPY_ASSIGN_HPP
54 
55 namespace Amesos2 {
56 
57 // allocate dst size if necessary - 2 methods handle 1d and 2d
58 template<class dst_t, class src_t> // version for 1d view
59 typename std::enable_if<static_cast<int>(dst_t::Rank) == 1>::type
60 update_dst_size(dst_t & dst, const src_t & src) {
61  if(dst.extent(0) != src.extent(0)) { // templated just for 1D
62  dst = dst_t(Kokkos::ViewAllocateWithoutInitializing("dst"),
63  src.extent(0));
64  }
65 }
66 
67 template<class dst_t, class src_t> // version for 2d view
68 typename std::enable_if<static_cast<int>(dst_t::Rank) == 2>::type
69 update_dst_size(dst_t & dst, const src_t & src) { // templated just for 2d
70  if(dst.extent(0) != src.extent(0) || dst.extent(1) != src.extent(1)) {
71  dst = dst_t(Kokkos::ViewAllocateWithoutInitializing("dst"),
72  src.extent(0), src.extent(1));
73  }
74 }
75 
76 // now handle type mismatch for same memory space - here types are same
77 template<class dst_t, class src_t> // version for same memory spaces
78 typename std::enable_if<std::is_same<typename dst_t::value_type,
79  typename src_t::value_type>::value>::type
80 implement_copy_or_assign_same_mem_check_types(dst_t & dst, const src_t & src) {
81  dst = src; // just assign the ptr - no need to copy
82 }
83 
84 // now handle type mismatch for same memory space - now types are different
85 template<class dst_t, class src_t> // version for same memory spaces
86 typename std::enable_if<!std::is_same<typename dst_t::value_type,
87  typename src_t::value_type>::value>::type
88 implement_copy_or_assign_same_mem_check_types(dst_t & dst, const src_t & src) {
89  update_dst_size(dst, src); // allocates if necessary
90  Kokkos::deep_copy(dst, src); // full copy
91 }
92 
93 // implement_copy_or_assign has 2 versions for matched memory and
94 // mismatched memory. Right now we just check the memory space.
95 // a layout mismatch is going to compile fail so probably reflects an error
96 // in the initial setup.
97 template<class dst_t, class src_t> // version for same memory spaces
98 typename std::enable_if<std::is_same<typename dst_t::memory_space,
99  typename src_t::memory_space>::value>::type
100 deep_copy_or_assign_view(dst_t & dst, const src_t & src) {
101  implement_copy_or_assign_same_mem_check_types(dst, src);
102 }
103 
104 template<class dst_t, class src_t> // version for different memory spaces
105 typename std::enable_if<std::is_same<typename dst_t::value_type,
106  typename src_t::value_type>::value>::type
107 implement_copy_or_assign_diff_mem_check_types(dst_t & dst, const src_t & src) {
108  update_dst_size(dst, src); // allocates if necessary
109  Kokkos::deep_copy(dst, src); // full copy
110 }
111 
112 template<class dst_t, class src_t> // version for different memory spaces
113 typename std::enable_if<static_cast<int>(dst_t::Rank) == 1>::type
114 implement_copy_or_assign_diff_mem_diff_types_check_dim(dst_t & dst, const src_t & src) {
115  Kokkos::View<typename dst_t::value_type*, typename src_t::execution_space>
116  intermediate(Kokkos::ViewAllocateWithoutInitializing("intermediate"), src.extent(0));
117  Kokkos::deep_copy(intermediate, src); // to dst type
118  Kokkos::deep_copy(dst, intermediate); // to dst mem
119 }
120 
121 template<class dst_t, class src_t> // version for different memory spaces
122 typename std::enable_if<static_cast<int>(dst_t::Rank) == 2>::type
123 implement_copy_or_assign_diff_mem_diff_types_check_dim(dst_t & dst, const src_t & src) {
124  Kokkos::View<typename dst_t::value_type**, Kokkos::LayoutLeft, typename src_t::execution_space>
125  intermediate(Kokkos::ViewAllocateWithoutInitializing("intermediate"), src.extent(0), src.extent(1));
126  Kokkos::deep_copy(intermediate, src); // to dst type
127  Kokkos::deep_copy(dst, intermediate); // to dst mem
128 }
129 
130 template<class dst_t, class src_t> // version for different memory spaces
131 typename std::enable_if<!std::is_same<typename dst_t::value_type,
132  typename src_t::value_type>::value>::type
133 implement_copy_or_assign_diff_mem_check_types(dst_t & dst, const src_t & src) {
134  update_dst_size(dst, src); // allocates if necessary
135  // since mem space and types are different, we specify the order of operations
136  // Kokkos::deep_copy won't do both since it would be a hidden deep_copy
137  implement_copy_or_assign_diff_mem_diff_types_check_dim(dst, src);
138 }
139 
140 template<class dst_t, class src_t> // version for different memory spaces
141 typename std::enable_if<!std::is_same<typename dst_t::memory_space,
142  typename src_t::memory_space>::value>::type
143 deep_copy_or_assign_view(dst_t & dst, const src_t & src) {
144  implement_copy_or_assign_diff_mem_check_types(dst, src); // full copy
145 }
146 
147 } // end namespace Amesos2
148 
149 #endif // AMESOS2_KOKKOS_VIEW_COPY_ASSIGN_HPP