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