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 // bInitialize:
78 // If bInitialize is false, then the data needs to be allocated but not initialized.
79 // If we are about to solve into x we don't care about setting the original values.
80 // In this case, we are assigning the view directly so bInitialize does not matter.
81 // bAssigned:
82 // bAssigned tells the caller if the data was simply assigned, so it is set true in this case.
83 template<class dst_t, class src_t> // version for same memory spaces
84 typename std::enable_if<std::is_same<typename dst_t::value_type,
85  typename src_t::value_type>::value>::type
86 implement_copy_or_assign_same_mem_check_types(
87  [[maybe_unused]] bool bInitialize,
88  dst_t & dst,
89  const src_t & src,
90  bool & bAssigned
91 ) {
92  dst = src; // just assign the ptr - no need to copy
93  bAssigned = true;
94 }
95 
96 
97 // deep-copy version (no checking)
98 // bInitialize:
99 // If bInitialize is false, then the data needs to be allocated but not initialized.
100 // If we are about to solve into x we don't care about setting the original values.
101 // In this case, we are allocating so we first make the memory via update_dst_size.
102 // Then we only copy from the source if bInitialize is true.
103 // bAssigned:
104 // bAssigned tells the caller if the data was simply assigned, so it is set false in this case.
105 template<class dst_t, class src_t> // actual implementation
106 void deep_copy_only(bool bInitialize, dst_t & dst, const src_t & src, bool & bAssigned) {
107  update_dst_size(dst, src); // allocates if necessary
108  if(bInitialize) { // bInitialize false would be for solver getting x, where the actual values are not needed
109  Kokkos::deep_copy(dst, src); // full copy
110  }
111  bAssigned = false;
112 }
113 
114 template<class dst_t, class src_t> // actual implementation
115 void deep_copy_only(dst_t & dst, const src_t & src) {
116  bool bAssigned;
117  deep_copy_only(true, dst, src, bAssigned);
118 }
119 
120 // now handle type mismatch for same memory space - now types are different
121 // bInitialize:
122 // If bInitialize is false, then the data needs to be allocated but not initialized.
123 // If we are about to solve into x we don't care about setting the original values.
124 // In this case, we are allocating so we first make the memory via update_dst_size.
125 // Then we only copy from the source if bInitialize is true.
126 // bAssigned:
127 // bAssigned tells the caller if the data was simply assigned, so it is set false in this case.
128 template<class dst_t, class src_t> // version for same memory spaces
129 typename std::enable_if<!std::is_same<typename dst_t::value_type,
130  typename src_t::value_type>::value>::type
131 implement_copy_or_assign_same_mem_check_types(bool bInitialize, dst_t & dst, const src_t & src, bool & bAssigned) {
132  update_dst_size(dst, src); // allocates if necessary
133  if(bInitialize) { // bInitialize false would be for solver getting x, where the actual values are not needed
134  Kokkos::deep_copy(dst, src); // full copy
135  }
136  bAssigned = false;
137 }
138 
139 // implement_copy_or_assign has 2 versions for matched memory and
140 // mismatched memory. Right now we just check the memory space.
141 // a layout mismatch is going to compile fail so probably reflects an error
142 // in the initial setup.
143 template<class dst_t, class src_t> // version for same memory spaces
144 typename std::enable_if<std::is_same<typename dst_t::memory_space,
145  typename src_t::memory_space>::value>::type
146 deep_copy_or_assign_view(bool bInitialize, dst_t & dst, const src_t & src, bool & bAssigned) {
147  implement_copy_or_assign_same_mem_check_types(bInitialize, dst, src, bAssigned);
148 }
149 
150 // for convenience this version does not take bInitialize input and bAssigned ouput
151 // then it's assumed you want bInitialize true and don't need to know bAssigned
152 template<class dst_t, class src_t> // version for same memory spaces
153 typename std::enable_if<std::is_same<typename dst_t::memory_space,
154  typename src_t::memory_space>::value>::type
155 deep_copy_or_assign_view(dst_t & dst, const src_t & src) {
156  bool bAssigned; // output not needed
157  implement_copy_or_assign_same_mem_check_types(true, dst, src, bAssigned);
158 }
159 
160 template<class dst_t, class src_t> // version for different memory spaces
161 typename std::enable_if<std::is_same<typename dst_t::value_type,
162  typename src_t::value_type>::value>::type
163 implement_copy_or_assign_diff_mem_check_types(bool bInitialize, dst_t & dst, const src_t & src, bool & bAssigned) {
164  update_dst_size(dst, src); // allocates if necessary
165  if(bInitialize) { // bInitialize false would be for solver getting x, where the actual values are not needed
166  Kokkos::deep_copy(dst, src); // full copy
167  }
168  bAssigned = false;
169 }
170 
171 template<class dst_t, class src_t> // version for different memory spaces
172 typename std::enable_if<static_cast<int>(dst_t::rank) == 1>::type
173 implement_copy_or_assign_diff_mem_diff_types_check_dim(dst_t & dst, const src_t & src) {
174  Kokkos::View<typename dst_t::value_type*, typename src_t::execution_space>
175  intermediate(Kokkos::ViewAllocateWithoutInitializing("intermediate"), src.extent(0));
176  Kokkos::deep_copy(intermediate, src); // to dst type
177  Kokkos::deep_copy(dst, intermediate); // to dst mem
178 }
179 
180 template<class dst_t, class src_t> // version for different memory spaces
181 typename std::enable_if<static_cast<int>(dst_t::rank) == 2>::type
182 implement_copy_or_assign_diff_mem_diff_types_check_dim(dst_t & dst, const src_t & src) {
183  Kokkos::View<typename dst_t::value_type**, Kokkos::LayoutLeft, typename src_t::execution_space>
184  intermediate(Kokkos::ViewAllocateWithoutInitializing("intermediate"), src.extent(0), src.extent(1));
185  Kokkos::deep_copy(intermediate, src); // to dst type
186  Kokkos::deep_copy(dst, intermediate); // to dst mem
187 }
188 
189 template<class dst_t, class src_t> // version for different memory spaces
190 typename std::enable_if<!std::is_same<typename dst_t::value_type,
191  typename src_t::value_type>::value>::type
192 implement_copy_or_assign_diff_mem_check_types(bool bInitialize, dst_t & dst, const src_t & src, bool & bAssigned) {
193  update_dst_size(dst, src); // allocates if necessary
194  bAssigned = false;
195  if(bInitialize) { // bInitialize false would be for solver getting x, where the actual values are not needed
196  // since mem space and types are different, we specify the order of operations
197  // Kokkos::deep_copy won't do both since it would be a hidden deep_copy
198  implement_copy_or_assign_diff_mem_diff_types_check_dim(dst, src);
199  }
200 }
201 
202 template<class dst_t, class src_t> // version for different memory spaces
203 typename std::enable_if<!std::is_same<typename dst_t::memory_space,
204  typename src_t::memory_space>::value>::type
205 deep_copy_or_assign_view(bool bInitialize, dst_t & dst, const src_t & src, bool & bAssigned) {
206  implement_copy_or_assign_diff_mem_check_types(bInitialize, dst, src, bAssigned); // full copy
207 }
208 
209 // for convenience this version does not take bInitialize input and bAssigned ouput
210 // then it's assumed you want bInitialize true and don't need to know bAssigned
211 template<class dst_t, class src_t> // version for different memory spaces
212 typename std::enable_if<!std::is_same<typename dst_t::memory_space,
213  typename src_t::memory_space>::value>::type
214 deep_copy_or_assign_view(dst_t & dst, const src_t & src) {
215  bool bAssigned; // output not needed
216  implement_copy_or_assign_diff_mem_check_types(true, dst, src, bAssigned); // full copy
217 }
218 
219 } // end namespace Amesos2
220 
221 #endif // AMESOS2_KOKKOS_VIEW_COPY_ASSIGN_HPP