Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_MultiVecAdapter_decl.hpp
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 
55 #ifndef AMESOS2_MULTIVEC_ADAPTER_DECL_HPP
56 #define AMESOS2_MULTIVEC_ADAPTER_DECL_HPP
57 
58 #include <Teuchos_RCP.hpp>
59 #include <Teuchos_Ptr.hpp>
60 #include <Teuchos_ArrayView.hpp>
61 #include <Tpetra_Map.hpp>
62 
63 #include "Amesos2_TypeDecl.hpp"
64 #include "Amesos2_VectorTraits.hpp"
65 
66 namespace Amesos2 {
67 
68 
175  template <class MV>
176  struct MultiVecAdapter {};
177 
178 
186  template <class MV>
187  Teuchos::RCP<MultiVecAdapter<MV> >
188  createMultiVecAdapter(Teuchos::RCP<MV> mv){
189  using Teuchos::rcp;
190 
191  if(mv.is_null()) return Teuchos::null;
192  return( rcp(new MultiVecAdapter<MV>(mv)) );
193  }
194 
195  template <class MV>
196  Teuchos::RCP<const MultiVecAdapter<MV> >
197  createConstMultiVecAdapter(Teuchos::RCP<const MV> mv){
198  using Teuchos::rcp;
199  using Teuchos::rcp_const_cast;
200 
201  if(mv.is_null()) return Teuchos::null;
202  return( rcp(new MultiVecAdapter<MV>(Teuchos::rcp_const_cast<MV,const MV>(mv))).getConst() );
203  }
204 
205 
207  // Utilities for getting and putting data from MultiVecs //
209 
210  namespace Util {
211 
218  template <typename MV, typename V>
220 
221  typedef typename VectorTraits<V>::ptr_scalar_type ptr_return_type ;
222 
223  static ptr_return_type * get_pointer_to_vector ( const Teuchos::Ptr< MV> &mv ) ;
224 
225  static ptr_return_type * get_pointer_to_vector ( Teuchos::Ptr< MV> &mv ) ;
226 
227  static ptr_return_type * get_pointer_to_vector ( const Teuchos::Ptr< const MV > &mv ) ;
228 
229  static ptr_return_type * get_pointer_to_vector ( Teuchos::Ptr< const MV > &mv ) ;
230  };
231 
232  /*
233  * If the multivector scalar type and the desired scalar tpye are
234  * the same, then we can do a simple straight copy.
235  */
236  template <typename MV>
237  struct same_type_get_copy {
238  static void apply(const Teuchos::Ptr<const MV> mv,
239  const Teuchos::ArrayView<typename MV::scalar_t>& v,
240  const size_t ldx,
241  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
242  EDistribution distribution );
243  };
244 
245  /*
246  * In the case where the scalar type of the multi-vector and the
247  * corresponding S type are different, then we need to first get a
248  * copy of the scalar values, then convert each one into the S
249  * type before inserting into the vals array.
250  */
251  template <typename MV, typename S>
252  struct diff_type_get_copy {
253  static void apply(const Teuchos::Ptr<const MV> mv,
254  const Teuchos::ArrayView<S>& v,
255  const size_t ldx,
256  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
257  EDistribution distribution );
258  };
259 
266  template <class MV, typename S>
268  static void
269  do_get (const Teuchos::Ptr<const MV>& mv,
270  const Teuchos::ArrayView<S>& vals,
271  const size_t ldx,
272  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
273  EDistribution distribution = ROOTED);
274 
275  static void
276  do_get (const Teuchos::Ptr<const MV>& mv,
277  const Teuchos::ArrayView<S>& vals,
278  const size_t ldx,
279  EDistribution distribution,
280  typename MV::global_ordinal_t indexBase = 0);
281 
282  static void
283  do_get (const Teuchos::Ptr<const MV>& mv,
284  const Teuchos::ArrayView<S>& vals,
285  const size_t ldx);
286  };
287 
288  /*
289  do_get
290 
291  Return type (bool):
292  true: The input kokkos_vals view is now pointing directly to the adapter's data (same memory and type).
293  If this is x for an Ax=b solve, you don't need 'do_put x' after the solve since you modified the adapter directly.
294  false: The input kokkos_vals view is now resized to match the adapter's size.
295  kokkos_vals will only have the adapter values deep_copied if bInitialize is true (see below).
296  If this is x for an Ax=b solve, you must call 'do_put x' after the solve to deep copy back to the adapter.
297 
298  Inputs
299  bInitialize (bool): tells the adapter whether kokkos_vals needs to have the values of the adapter.
300  true: We require kokkos_vals to have the same size and values of the adapter.
301  For b in Ax=b solves, set bInitialize true because you need the size and values of the adapter.
302  false: We require kokkos_vals to have the same size as the adapter but we don't need the values.
303  For x in Ax=b solves, set bInitialize false because you just need the size, not the values.
304 
305  Note: When this method returns true, meaning direct assignment of the view occurred,
306  bInitialize is not used because you already have the values whether you need them or not.
307 
308  kokkos_vals (View<scalar_t**>): The view which will contain the x or b data.
309  Do not allocate the size of kokkos_vals, let the do_get method do it for you.
310  This is because kokkos_vals may be set to point directly to the adapter memory
311  and then any pre-allocation of size will have been wasted.
312  */
313  template <class MV, typename KV>
314  struct get_1d_copy_helper_kokkos_view {
315  static bool
316  do_get (bool bInitialize,
317  const Teuchos::Ptr<const MV>& mv,
318  KV& kokkos_vals,
319  const size_t ldx,
320  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
321  EDistribution distribution = ROOTED);
322 
323  static bool
324  do_get (bool bInitialize,
325  const Teuchos::Ptr<const MV>& mv,
326  KV& kokkos_vals,
327  const size_t ldx,
328  EDistribution distribution,
329  typename MV::global_ordinal_t indexBase = 0);
330 
331  static bool
332  do_get (bool bInitialize,
333  const Teuchos::Ptr<const MV>& mv,
334  KV& kokkos_vals,
335  const size_t ldx);
336  };
337 
338  /*
339  * If the multivector scalar type and the desired scalar tpye are
340  * the same, then we can do a simple straight copy.
341  */
342  template <typename MV>
343  struct same_type_data_put {
344  static void apply(const Teuchos::Ptr<MV>& mv,
345  const Teuchos::ArrayView<typename MV::scalar_t>& data,
346  const size_t ldx,
347  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
348  EDistribution distribution );
349  };
350 
351  /*
352  * In the case where the scalar type of the multi-vector and the
353  * corresponding S type are different, then we need to first get a
354  * copy of the scalar values, then convert each one into the S
355  * type before inserting into the vals array.
356  */
357  template <typename MV, typename S>
358  struct diff_type_data_put {
359  static void apply(const Teuchos::Ptr<MV>& mv,
360  const Teuchos::ArrayView<S>& data,
361  const size_t ldx,
362  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
363  EDistribution distribution );
364  };
365 
372  template <class MV, typename S>
374  static void do_put(const Teuchos::Ptr<MV>& mv,
375  const Teuchos::ArrayView<S>& data,
376  const size_t ldx,
377  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
378  EDistribution distribution = ROOTED);
379 
380  static void do_put(const Teuchos::Ptr<MV>& mv,
381  const Teuchos::ArrayView<S>& data,
382  const size_t ldx,
383  EDistribution distribution, typename MV::global_ordinal_t indexBase = 0);
384 
385  static void do_put(const Teuchos::Ptr<MV>& mv,
386  const Teuchos::ArrayView<S>& data,
387  const size_t ldx);
388  };
389 
390  template <class MV, typename KV>
391  struct put_1d_data_helper_kokkos_view {
392  static void do_put(const Teuchos::Ptr<MV>& mv,
393  KV& kokkos_data,
394  const size_t ldx,
395  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map,
396  EDistribution distribution = ROOTED);
397 
398  static void do_put(const Teuchos::Ptr<MV>& mv,
399  KV& kokkos_data,
400  const size_t ldx,
401  EDistribution distribution, typename MV::global_ordinal_t indexBase = 0);
402 
403  static void do_put(const Teuchos::Ptr<MV>& mv,
404  KV& kokkos_data,
405  const size_t ldx);
406  };
407  }
408 } // end namespace Amesos2
409 
412 #ifdef HAVE_AMESOS2_EPETRA
414 #endif
415 
416 #endif // AMESOS2_MULTIVEC_ADAPTER_DECL_HPP
Teuchos::RCP< MultiVecAdapter< MV > > createMultiVecAdapter(Teuchos::RCP< MV > mv)
Factory creation method for MultiVecAdapters.
Definition: Amesos2_MultiVecAdapter_decl.hpp:188
Amesos2::MultiVecAdapter specialization for the Kokkos::View class.
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:267
Helper struct for getting pointers to the MV data - only used when number of vectors = 1 and single M...
Definition: Amesos2_MultiVecAdapter_decl.hpp:219
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:146
Amesos2::MultiVecAdapter specialization for the Tpetra::MultiVector class.
Amesos2::MultiVecAdapter specialization for the Epetra_MultiVector class.
Enum and other types declarations for Amesos2.
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:321
EDistribution
Definition: Amesos2_TypeDecl.hpp:123
Helper class for putting 1-D data arrays into multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:373
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:176