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