Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_MultiVecAdapter_def.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 
44 
45 #ifndef AMESOS2_MULTIVECADAPTER_DEF_HPP
46 #define AMESOS2_MULTIVECADAPTER_DEF_HPP
47 
49 // EpetraMultiVecAdapter_def.hpp not included because the specialization is not a template
50 
51 #include "Amesos2_Util.hpp" // for getDistributionMap
52 
53 namespace Amesos2{
54 
55  namespace Util {
56 
58  // Copy-getting utilities //
60 
61  /*
62  * If the multivector scalar type and the desired scalar tpye are
63  * the same, then we can do a simple straight copy.
64  */
65  template <typename MV>
66  void same_type_get_copy<MV>::apply(const Teuchos::Ptr<const MV>& mv,
67  const Teuchos::ArrayView<typename MV::scalar_t>& v,
68  const size_t ldx,
69  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map )
70  {
71  mv->get1dCopy(v, ldx, distribution_map);
72  }
73 
74  /*
75  * In the case where the scalar type of the multi-vector and the
76  * corresponding S type are different, then we need to first get a
77  * copy of the scalar values, then convert each one into the S
78  * type before inserting into the vals array.
79  */
80  template <typename MV, typename S>
81  void diff_type_get_copy<MV,S>::apply(const Teuchos::Ptr<const MV>& mv,
82  const Teuchos::ArrayView<S>& v,
83  const size_t& ldx,
84  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map )
85  {
86  typedef typename MV::scalar_t mv_scalar_t;
87 
88  int vals_length = v.size();
89  Teuchos::Array<mv_scalar_t> vals_tmp(vals_length);
90 
91  mv->get1dCopy(vals_tmp(), ldx, distribution_map);
92 
93  for ( int i = 0; i < vals_length; ++i ){
94  v[i] = Teuchos::as<S>(vals_tmp[i]);
95  }
96  }
97 
104  template <class MV, typename S>
105  void get_1d_copy_helper<MV,S>::do_get(const Teuchos::Ptr<const MV>& mv,
106  const Teuchos::ArrayView<S>& vals,
107  const size_t ldx,
108  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map )
109  {
110  // Dispatch to the copy function appropriate for the type
111  if_then_else<is_same<typename MV::scalar_t,S>::value,
112  same_type_get_copy<MV>,
113  diff_type_get_copy<MV,S> >::type::apply(mv, vals, ldx, distribution_map);
114  }
115 
116  template <class MV, typename S>
117  void get_1d_copy_helper<MV,S>::do_get(const Teuchos::Ptr<const MV>& mv,
118  const Teuchos::ArrayView<S>& vals,
119  const size_t ldx,
120  EDistribution distribution, typename MV::global_ordinal_t indexBase)
121  {
122  typedef typename MV::local_ordinal_t lo_t;
123  typedef typename MV::global_ordinal_t go_t;
124  typedef typename MV::global_size_t gs_t;
125  typedef typename MV::node_t node_t;
126 
127  const Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
128  = Amesos2::Util::getDistributionMap<lo_t,go_t,gs_t,node_t>(distribution,
129  mv->getGlobalLength(),
130  mv->getComm(), indexBase);
131  do_get(mv, vals, ldx, Teuchos::ptrInArg(*map));
132  }
133 
134  template <class MV, typename S>
135  void get_1d_copy_helper<MV,S>::do_get(const Teuchos::Ptr<const MV>& mv,
136  const Teuchos::ArrayView<S>& vals,
137  const size_t ldx)
138  {
139  const Teuchos::RCP<const Tpetra::Map<typename MV::local_ordinal_t,
140  typename MV::global_ordinal_t,
141  typename MV::node_t> > map
142  = mv->getMap();
143  do_get(mv, vals, ldx, Teuchos::ptrInArg(*map));
144  }
145 
146 
148  // Copy-puting utilities //
150 
151  template <typename MV>
152  void same_type_data_put<MV>::apply(const Teuchos::Ptr<MV>& mv,
153  const Teuchos::ArrayView<typename MV::scalar_t>& data,
154  const size_t ldx,
155  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map )
156  {
157  mv->put1dData(data, ldx, distribution_map);
158  }
159 
160  /*
161  * In the case where the scalar type of the multi-vector and the
162  * corresponding S type are different, then we need to first get a
163  * copy of the scalar values, then convert each one into the S
164  * type before inserting into the vals array.
165  */
166  template <typename MV, typename S>
167  void diff_type_data_put<MV,S>::apply(const Teuchos::Ptr<MV>& mv,
168  const Teuchos::ArrayView<S>& data,
169  const size_t& ldx,
170  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map )
171  {
172  typedef typename MV::scalar_t mv_scalar_t;
173 
174  int vals_length = data.size();
175  Teuchos::Array<mv_scalar_t> data_tmp(vals_length);
176 
177  for ( int i = 0; i < vals_length; ++i ){
178  data_tmp[i] = Teuchos::as<mv_scalar_t>(data[i]);
179  }
180 
181  mv->put1dData(data_tmp(), ldx, distribution_map);
182  }
183 
184 
191  template <class MV, typename S>
192  void put_1d_data_helper<MV,S>::do_put(const Teuchos::Ptr<MV>& mv,
193  const Teuchos::ArrayView<S>& data,
194  const size_t ldx,
195  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map )
196  {
197  // Dispatch to the copy function appropriate for the type
198  if_then_else<is_same<typename MV::scalar_t,S>::value,
199  same_type_data_put<MV>,
200  diff_type_data_put<MV,S> >::type::apply(mv, data, ldx, distribution_map);
201  }
202 
203  template <class MV, typename S>
204  void put_1d_data_helper<MV,S>::do_put(const Teuchos::Ptr<MV>& mv,
205  const Teuchos::ArrayView<S>& data,
206  const size_t ldx,
207  EDistribution distribution, typename MV::global_ordinal_t indexBase)
208  {
209  typedef typename MV::local_ordinal_t lo_t;
210  typedef typename MV::global_ordinal_t go_t;
211  typedef typename MV::global_size_t gs_t;
212  typedef typename MV::node_t node_t;
213 
214  const Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
215  = Amesos2::Util::getDistributionMap<lo_t,go_t,gs_t,node_t>(distribution,
216  mv->getGlobalLength(),
217  mv->getComm(), indexBase);
218  do_put(mv, data, ldx, Teuchos::ptrInArg(*map));
219  }
220 
221  template <class MV, typename S>
222  void put_1d_data_helper<MV,S>::do_put(const Teuchos::Ptr<MV>& mv,
223  const Teuchos::ArrayView<S>& data,
224  const size_t ldx)
225  {
226  const Teuchos::RCP<const Tpetra::Map<typename MV::local_ordinal_t,
227  typename MV::global_ordinal_t,
228  typename MV::node_t> > map
229  = mv->getMap();
230  do_put(mv, data, ldx, Teuchos::ptrInArg(*map));
231  }
232 
233  } // end namespace Util
234 
235 } // end namespace Amesos2
236 
237 #endif // AMESOS2_EPETRAMULTIVECADAPTER_DEF
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)
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_def.hpp:105
Utility functions for Amesos2.
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
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)
Helper class for putting 1-D data arrays into multivectors.
Definition: Amesos2_MultiVecAdapter_def.hpp:192
Amesos2::MultiVecAdapter specialization for the Tpetra::MultiVector class.
EDistribution
Definition: Amesos2_TypeDecl.hpp:115