Amesos2 - Direct Sparse Solver Interfaces Version of the Day
Amesos2_MultiVecAdapter_def.hpp
1// @HEADER
2// *****************************************************************************
3// Amesos2: Templated Direct Sparse Solver Package
4//
5// Copyright 2011 NTESS and the Amesos2 contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10
11#ifndef AMESOS2_MULTIVECADAPTER_DEF_HPP
12#define AMESOS2_MULTIVECADAPTER_DEF_HPP
13
16
17#include "Amesos2_Util.hpp" // for getDistributionMap
18
19namespace Amesos2{
20
21 namespace Util {
22
24 // Pointer-getting utilities //
26
27 template <typename MV, typename V>
28 typename vector_pointer_helper<MV, V>::ptr_return_type *
29 vector_pointer_helper<MV, V>::get_pointer_to_vector ( const Teuchos::Ptr< MV > &mv ) {
30 return mv->getMVPointer_impl();
31 }
32
33 template <typename MV, typename V>
34 typename vector_pointer_helper<MV, V>::ptr_return_type *
35 vector_pointer_helper<MV, V>::get_pointer_to_vector ( Teuchos::Ptr< MV > &mv ) {
36 return mv->getMVPointer_impl();
37 }
38
39 template <typename MV, typename V>
40 typename vector_pointer_helper<MV, V>::ptr_return_type *
41 vector_pointer_helper<MV, V>::get_pointer_to_vector ( const Teuchos::Ptr< const MV > &mv ) {
42 return mv->getMVPointer_impl();
43 }
44
45 template <typename MV, typename V>
46 typename vector_pointer_helper<MV, V>::ptr_return_type *
47 vector_pointer_helper<MV, V>::get_pointer_to_vector ( Teuchos::Ptr< const MV > &mv ) {
48 return mv->getMVPointer_impl();
49 }
50
51
53 // Copy-getting utilities //
55
56 /*
57 * If the multivector scalar type and the desired scalar tpye are
58 * the same, then we can do a simple straight copy.
59 */
60 template <typename MV>
61 void same_type_get_copy<MV>::apply(const Teuchos::Ptr<const MV> mv,
62 const Teuchos::ArrayView<typename MV::scalar_t>& v,
63 const size_t ldx,
64 Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
65 EDistribution distribution )
66 {
67 mv->get1dCopy (v, ldx, distribution_map, distribution);
68 }
69
70 /*
71 * In the case where the scalar type of the multi-vector and the
72 * corresponding S type are different, then we need to first get a
73 * copy of the scalar values, then convert each one into the S
74 * type before inserting into the vals array.
75 */
76 template <typename MV, typename S>
77 void diff_type_get_copy<MV,S>::
78 apply (const Teuchos::Ptr<const MV> mv,
79 const Teuchos::ArrayView<S>& v,
80 const size_t ldx,
81 Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
82 EDistribution distribution )
83 {
84 typedef typename MV::scalar_t mv_scalar_t;
85 typedef typename Teuchos::Array<mv_scalar_t>::size_type size_type;
86
87 TEUCHOS_TEST_FOR_EXCEPTION(
88 mv.getRawPtr () == NULL, std::invalid_argument,
89 "Amesos2::diff_type_get_copy::apply: mv is null.");
90 TEUCHOS_TEST_FOR_EXCEPTION(
91 distribution_map.getRawPtr () == NULL, std::invalid_argument,
92 "Amesos2::diff_type_get_copy::apply: distribution_map is null.");
93
94 const size_type vals_length = v.size ();
95 Teuchos::Array<mv_scalar_t> vals_tmp (vals_length);
96
97 mv->get1dCopy (vals_tmp (), ldx, distribution_map, distribution);
98 for (size_type i = 0; i < vals_length; ++i) {
99 v[i] = Teuchos::as<S> (vals_tmp[i]);
100 }
101 }
102
109 template <class MV, typename S>
111 do_get (const Teuchos::Ptr<const MV>& mv,
112 const Teuchos::ArrayView<S>& vals,
113 const size_t ldx,
114 Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
115 EDistribution distribution)
116 {
117 // Dispatch to the copy function appropriate for the type
118 std::conditional_t<std::is_same_v<typename MV::scalar_t,S>,
119 same_type_get_copy<MV>,
120 diff_type_get_copy<MV,S> >::apply (mv, vals, ldx, distribution_map, distribution);
121 }
122
123 template <class MV, typename S>
125 do_get (const Teuchos::Ptr<const MV>& mv,
126 const Teuchos::ArrayView<S>& vals,
127 const size_t ldx,
128 EDistribution distribution,
129 typename MV::global_ordinal_t indexBase)
130 {
131 typedef typename MV::local_ordinal_t lo_t;
132 typedef typename MV::global_ordinal_t go_t;
133 typedef typename MV::global_size_t gs_t;
134 typedef typename MV::node_t node_t;
135
136 TEUCHOS_TEST_FOR_EXCEPTION(
137 mv.getRawPtr () == NULL, std::invalid_argument,
138 "Amesos2::get_1d_copy_helper::do_get(5 args): mv is null.");
139
140 Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
141 = Amesos2::Util::getDistributionMap<lo_t,go_t,gs_t,node_t> (distribution,
142 mv->getGlobalLength (),
143 mv->getComm (),
144 indexBase,
145 mv->getMap());
146
147 do_get (mv, vals, ldx, Teuchos::ptrInArg (*map), distribution);
148 }
149
150 template <class MV, typename S>
151 void get_1d_copy_helper<MV,S>::do_get(const Teuchos::Ptr<const MV>& mv,
152 const Teuchos::ArrayView<S>& vals,
153 const size_t ldx)
154 {
155 typedef Tpetra::Map<typename MV::local_ordinal_t,
156 typename MV::global_ordinal_t,
157 typename MV::node_t> map_type;
158 TEUCHOS_TEST_FOR_EXCEPTION(
159 mv.getRawPtr () == NULL, std::invalid_argument,
160 "Amesos2::get_1d_copy_helper::do_get(3 args): mv is null.");
161
162 Teuchos::RCP<const map_type> map = mv->getMap ();
163 TEUCHOS_TEST_FOR_EXCEPTION(
164 map.is_null (), std::invalid_argument,
165 "Amesos2::get_1d_copy_helper::do_get(3 args): mv->getMap() is null.");
166
167
168 do_get (mv, vals, ldx, Teuchos::ptrInArg (*map), ROOTED); // ROOTED the default here for now
169 }
170
171 template <class MV, typename KV>
172 bool get_1d_copy_helper_kokkos_view<MV,KV>::
173 do_get (bool bInitialize,
174 const Teuchos::Ptr<const MV>& mv,
175 KV& kokkos_vals,
176 const size_t ldx,
177 Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
178 EDistribution distribution)
179 {
180 return mv->get1dCopy_kokkos_view(bInitialize, kokkos_vals, ldx, distribution_map, distribution);
181 }
182
183 template <class MV, typename KV>
184 bool get_1d_copy_helper_kokkos_view<MV,KV>::
185 do_get (bool bInitialize,
186 const Teuchos::Ptr<const MV>& mv,
187 KV& kokkos_vals,
188 const size_t ldx,
189 EDistribution distribution,
190 typename MV::global_ordinal_t indexBase)
191 {
192 typedef typename MV::local_ordinal_t lo_t;
193 typedef typename MV::global_ordinal_t go_t;
194 typedef typename MV::global_size_t gs_t;
195 typedef typename MV::node_t node_t;
196
197 TEUCHOS_TEST_FOR_EXCEPTION(
198 mv.getRawPtr () == NULL, std::invalid_argument,
199 "Amesos2::get_1d_copy_helper_kokkos_view::do_get(5 args): mv is null.");
200
201 Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
202 = Amesos2::Util::getDistributionMap<lo_t,go_t,gs_t,node_t> (distribution,
203 mv->getGlobalLength (),
204 mv->getComm (),
205 indexBase,
206 mv->getMap());
207
208 return do_get (bInitialize, mv, kokkos_vals, ldx, Teuchos::ptrInArg (*map), distribution);
209 }
210
211 template <class MV, typename KV>
212 bool get_1d_copy_helper_kokkos_view<MV,KV>::
213 do_get (bool bInitialize,
214 const Teuchos::Ptr<const MV>& mv,
215 KV& kokkos_vals,
216 const size_t ldx)
217 {
218 typedef Tpetra::Map<typename MV::local_ordinal_t,
219 typename MV::global_ordinal_t,
220 typename MV::node_t> map_type;
221 TEUCHOS_TEST_FOR_EXCEPTION(
222 mv.getRawPtr () == NULL, std::invalid_argument,
223 "Amesos2::get_1d_copy_helper::do_get(3 args): mv is null.");
224
225 Teuchos::RCP<const map_type> map = mv->getMap ();
226 TEUCHOS_TEST_FOR_EXCEPTION(
227 map.is_null (), std::invalid_argument,
228 "Amesos2::get_1d_copy_helper_kokkos_view::do_get(3 args): mv->getMap() is null.");
229
230 return do_get (bInitialize, mv, kokkos_vals, ldx, Teuchos::ptrInArg (*map), ROOTED); // ROOTED the default here for now
231 }
232
233
235 // Copy-puting utilities //
237
238 template <typename MV>
239 void same_type_data_put<MV>::apply(const Teuchos::Ptr<MV>& mv,
240 const Teuchos::ArrayView<typename MV::scalar_t>& data,
241 const size_t ldx,
242 Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
243 EDistribution distribution )
244 {
245 mv->put1dData (data, ldx, distribution_map, distribution);
246 }
247
248 /*
249 * In the case where the scalar type of the multi-vector and the
250 * corresponding S type are different, then we need to first get a
251 * copy of the scalar values, then convert each one into the S
252 * type before inserting into the vals array.
253 */
254 template <typename MV, typename S>
255 void diff_type_data_put<MV,S>::apply(const Teuchos::Ptr<MV>& mv,
256 const Teuchos::ArrayView<S>& data,
257 const size_t ldx,
258 Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
259 EDistribution distribution )
260 {
261 typedef typename MV::scalar_t mv_scalar_t;
262 typedef typename Teuchos::Array<mv_scalar_t>::size_type size_type;
263
264 TEUCHOS_TEST_FOR_EXCEPTION(
265 mv.getRawPtr () == NULL, std::invalid_argument,
266 "Amesos2::diff_type_data_put(4 args): mv is null.");
267
268 const size_type vals_length = data.size ();
269 Teuchos::Array<mv_scalar_t> data_tmp (vals_length);
270
271 for (size_type i = 0; i < vals_length; ++i) {
272 data_tmp[i] = Teuchos::as<mv_scalar_t> (data[i]);
273 }
274
275 mv->put1dData (data_tmp (), ldx, distribution_map, distribution);
276 }
277
278
285 template <class MV, typename S>
286 void put_1d_data_helper<MV,S>::do_put(const Teuchos::Ptr<MV>& mv,
287 const Teuchos::ArrayView<S>& data,
288 const size_t ldx,
289 Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
290 EDistribution distribution )
291 {
292 // Dispatch to the copy function appropriate for the type
293 std::conditional_t<std::is_same_v<typename MV::scalar_t,S>,
294 same_type_data_put<MV>,
295 diff_type_data_put<MV,S> >::apply(mv, data, ldx, distribution_map, distribution);
296 }
297
298 template <class MV, typename S>
299 void put_1d_data_helper<MV,S>::do_put(const Teuchos::Ptr<MV>& mv,
300 const Teuchos::ArrayView<S>& data,
301 const size_t ldx,
302 EDistribution distribution, typename MV::global_ordinal_t indexBase)
303 {
304 typedef typename MV::local_ordinal_t lo_t;
305 typedef typename MV::global_ordinal_t go_t;
306 typedef typename MV::global_size_t gs_t;
307 typedef typename MV::node_t node_t;
308
309 const Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
310 = Amesos2::Util::getDistributionMap<lo_t,go_t,gs_t,node_t>(distribution,
311 mv->getGlobalLength(),
312 mv->getComm(),
313 indexBase,
314 mv->getMap());
315
316 do_put(mv, data, ldx, Teuchos::ptrInArg(*map), distribution);
317 }
318
319 template <class MV, typename S>
320 void put_1d_data_helper<MV,S>::do_put (const Teuchos::Ptr<MV>& mv,
321 const Teuchos::ArrayView<S>& data,
322 const size_t ldx)
323 {
324 const Teuchos::RCP<const Tpetra::Map<typename MV::local_ordinal_t,
325 typename MV::global_ordinal_t,
326 typename MV::node_t> > map
327 = mv->getMap();
328 do_put (mv, data, ldx, Teuchos::ptrInArg (*map), ROOTED); // Default as ROOTED for now
329 }
330
331 template <class MV, typename KV>
332 void put_1d_data_helper_kokkos_view<MV,KV>::do_put(const Teuchos::Ptr<MV>& mv,
333 KV& kokkos_data,
334 const size_t ldx,
335 Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
336 EDistribution distribution )
337 {
338 mv->put1dData_kokkos_view(kokkos_data, ldx, distribution_map, distribution);
339 }
340
341 template <class MV, typename KV>
342 void put_1d_data_helper_kokkos_view<MV,KV>::do_put(const Teuchos::Ptr<MV>& mv,
343 KV& kokkos_data,
344 const size_t ldx,
345 EDistribution distribution, typename MV::global_ordinal_t indexBase)
346 {
347 typedef typename MV::local_ordinal_t lo_t;
348 typedef typename MV::global_ordinal_t go_t;
349 typedef typename MV::global_size_t gs_t;
350 typedef typename MV::node_t node_t;
351
352 const Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
353 = Amesos2::Util::getDistributionMap<lo_t,go_t,gs_t,node_t>(distribution,
354 mv->getGlobalLength(),
355 mv->getComm(),
356 indexBase,
357 mv->getMap());
358
359 do_put(mv, kokkos_data, ldx, Teuchos::ptrInArg(*map), distribution);
360 }
361
362 template <class MV, typename KV>
363 void put_1d_data_helper_kokkos_view<MV,KV>::do_put (const Teuchos::Ptr<MV>& mv,
364 KV& kokkos_data,
365 const size_t ldx)
366 {
367 const Teuchos::RCP<const Tpetra::Map<typename MV::local_ordinal_t,
368 typename MV::global_ordinal_t,
369 typename MV::node_t> > map
370 = mv->getMap();
371 do_put (mv, kokkos_data, ldx, Teuchos::ptrInArg (*map), ROOTED); // Default as ROOTED for now
372 }
373
374 } // end namespace Util
375
376} // end namespace Amesos2
377
378#endif // AMESOS2_MULTIVECADAPTER_DEF_HPP
Amesos2::MultiVecAdapter specialization for the Kokkos::View class.
Amesos2::MultiVecAdapter specialization for the Tpetra::MultiVector class.
EDistribution
Definition Amesos2_TypeDecl.hpp:89
Utility functions for Amesos2.
static void do_get(const Teuchos::Ptr< const MV > &mv, const Teuchos::ArrayView< S > &vals, const size_t ldx, Teuchos::Ptr< const Tpetra::Map< typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t > > distribution_map, EDistribution distribution=ROOTED)
Helper class for getting 1-D copies of multivectors.
Definition Amesos2_MultiVecAdapter_def.hpp:111
static void do_put(const Teuchos::Ptr< MV > &mv, const Teuchos::ArrayView< S > &data, const size_t ldx, Teuchos::Ptr< const Tpetra::Map< typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t > > distribution_map, EDistribution distribution=ROOTED)
Helper class for putting 1-D data arrays into multivectors.
Definition Amesos2_MultiVecAdapter_def.hpp:286