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>::
82  apply (const Teuchos::Ptr<const MV>& mv,
83  const Teuchos::ArrayView<S>& v,
84  const size_t& ldx,
85  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map)
86  {
87  typedef typename MV::scalar_t mv_scalar_t;
88  typedef typename Teuchos::Array<mv_scalar_t>::size_type size_type;
89 
90  TEUCHOS_TEST_FOR_EXCEPTION(
91  mv.getRawPtr () == NULL, std::invalid_argument,
92  "Amesos2::diff_type_get_copy::apply: mv is null.");
93  TEUCHOS_TEST_FOR_EXCEPTION(
94  distribution_map.getRawPtr () == NULL, std::invalid_argument,
95  "Amesos2::diff_type_get_copy::apply: distribution_map is null.");
96 
97  const size_type vals_length = v.size ();
98  Teuchos::Array<mv_scalar_t> vals_tmp (vals_length);
99 
100  mv->get1dCopy (vals_tmp (), ldx, distribution_map);
101  for (size_type i = 0; i < vals_length; ++i) {
102  v[i] = Teuchos::as<S> (vals_tmp[i]);
103  }
104  }
105 
112  template <class MV, typename S>
114  do_get (const Teuchos::Ptr<const MV>& mv,
115  const Teuchos::ArrayView<S>& vals,
116  const size_t ldx,
117  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map)
118  {
119  // Dispatch to the copy function appropriate for the type
120  if_then_else<is_same<typename MV::scalar_t,S>::value,
121  same_type_get_copy<MV>,
122  diff_type_get_copy<MV,S> >::type::apply (mv, vals, ldx, distribution_map);
123  }
124 
125  template <class MV, typename S>
127  do_get (const Teuchos::Ptr<const MV>& mv,
128  const Teuchos::ArrayView<S>& vals,
129  const size_t ldx,
130  EDistribution distribution,
131  typename MV::global_ordinal_t indexBase)
132  {
133  typedef typename MV::local_ordinal_t lo_t;
134  typedef typename MV::global_ordinal_t go_t;
135  typedef typename MV::global_size_t gs_t;
136  typedef typename MV::node_t node_t;
137 
138  TEUCHOS_TEST_FOR_EXCEPTION(
139  mv.getRawPtr () == NULL, std::invalid_argument,
140  "Amesos2::get_1d_copy_helper::do_get(5 args): mv is null.");
141 
142  Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
143  = Amesos2::Util::getDistributionMap<lo_t,go_t,gs_t,node_t> (distribution,
144  mv->getGlobalLength (),
145  mv->getComm (), indexBase);
146  do_get (mv, vals, ldx, Teuchos::ptrInArg (*map));
147  }
148 
149  template <class MV, typename S>
150  void get_1d_copy_helper<MV,S>::do_get(const Teuchos::Ptr<const MV>& mv,
151  const Teuchos::ArrayView<S>& vals,
152  const size_t ldx)
153  {
154  typedef Tpetra::Map<typename MV::local_ordinal_t,
155  typename MV::global_ordinal_t,
156  typename MV::node_t> map_type;
157  TEUCHOS_TEST_FOR_EXCEPTION(
158  mv.getRawPtr () == NULL, std::invalid_argument,
159  "Amesos2::get_1d_copy_helper::do_get(3 args): mv is null.");
160 
161  Teuchos::RCP<const map_type> map = mv->getMap ();
162  TEUCHOS_TEST_FOR_EXCEPTION(
163  map.is_null (), std::invalid_argument,
164  "Amesos2::get_1d_copy_helper::do_get(3 args): mv->getMap() is null.");
165 
166  do_get (mv, vals, ldx, Teuchos::ptrInArg (*map));
167  }
168 
169 
171  // Copy-puting utilities //
173 
174  template <typename MV>
175  void same_type_data_put<MV>::apply(const Teuchos::Ptr<MV>& mv,
176  const Teuchos::ArrayView<typename MV::scalar_t>& data,
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  {
180  mv->put1dData (data, ldx, distribution_map);
181  }
182 
183  /*
184  * In the case where the scalar type of the multi-vector and the
185  * corresponding S type are different, then we need to first get a
186  * copy of the scalar values, then convert each one into the S
187  * type before inserting into the vals array.
188  */
189  template <typename MV, typename S>
190  void diff_type_data_put<MV,S>::apply(const Teuchos::Ptr<MV>& mv,
191  const Teuchos::ArrayView<S>& data,
192  const size_t& ldx,
193  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map )
194  {
195  typedef typename MV::scalar_t mv_scalar_t;
196  typedef typename Teuchos::Array<mv_scalar_t>::size_type size_type;
197 
198  TEUCHOS_TEST_FOR_EXCEPTION(
199  mv.getRawPtr () == NULL, std::invalid_argument,
200  "Amesos2::diff_type_data_put(4 args): mv is null.");
201 
202  const size_type vals_length = data.size ();
203  Teuchos::Array<mv_scalar_t> data_tmp (vals_length);
204 
205  for (size_type i = 0; i < vals_length; ++i) {
206  data_tmp[i] = Teuchos::as<mv_scalar_t> (data[i]);
207  }
208 
209  mv->put1dData (data_tmp (), ldx, distribution_map);
210  }
211 
212 
219  template <class MV, typename S>
220  void put_1d_data_helper<MV,S>::do_put(const Teuchos::Ptr<MV>& mv,
221  const Teuchos::ArrayView<S>& data,
222  const size_t ldx,
223  Teuchos::Ptr<const Tpetra::Map<typename MV::local_ordinal_t, typename MV::global_ordinal_t, typename MV::node_t> > distribution_map )
224  {
225  // Dispatch to the copy function appropriate for the type
226  if_then_else<is_same<typename MV::scalar_t,S>::value,
227  same_type_data_put<MV>,
228  diff_type_data_put<MV,S> >::type::apply(mv, data, ldx, distribution_map);
229  }
230 
231  template <class MV, typename S>
232  void put_1d_data_helper<MV,S>::do_put(const Teuchos::Ptr<MV>& mv,
233  const Teuchos::ArrayView<S>& data,
234  const size_t ldx,
235  EDistribution distribution, typename MV::global_ordinal_t indexBase)
236  {
237  typedef typename MV::local_ordinal_t lo_t;
238  typedef typename MV::global_ordinal_t go_t;
239  typedef typename MV::global_size_t gs_t;
240  typedef typename MV::node_t node_t;
241 
242  const Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
243  = Amesos2::Util::getDistributionMap<lo_t,go_t,gs_t,node_t>(distribution,
244  mv->getGlobalLength(),
245  mv->getComm(), indexBase);
246  do_put(mv, data, ldx, Teuchos::ptrInArg(*map));
247  }
248 
249  template <class MV, typename S>
250  void put_1d_data_helper<MV,S>::do_put (const Teuchos::Ptr<MV>& mv,
251  const Teuchos::ArrayView<S>& data,
252  const size_t ldx)
253  {
254  const Teuchos::RCP<const Tpetra::Map<typename MV::local_ordinal_t,
255  typename MV::global_ordinal_t,
256  typename MV::node_t> > map
257  = mv->getMap();
258  do_put (mv, data, ldx, Teuchos::ptrInArg (*map));
259  }
260 
261  } // end namespace Util
262 
263 } // end namespace Amesos2
264 
265 #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:114
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:220
Amesos2::MultiVecAdapter specialization for the Tpetra::MultiVector class.
EDistribution
Definition: Amesos2_TypeDecl.hpp:123