Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_EpetraMultiVecAdapter_def.hpp
Go to the documentation of this file.
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 
53 #ifndef AMESOS2_EPETRA_MULTIVEC_ADAPTER_DEF_HPP
54 #define AMESOS2_EPETRA_MULTIVEC_ADAPTER_DEF_HPP
55 
56 #include <Teuchos_as.hpp>
57 
58 #include <Epetra_SerialComm.h>
59 #ifdef HAVE_MPI
60 #include <Epetra_MpiComm.h>
61 #endif
62 #include <Epetra_LocalMap.h>
63 #include <Epetra_Import.h>
64 #include <Epetra_Export.h>
65 
67 #include "Amesos2_Util.hpp"
68 
69 namespace Amesos2 {
70 
72  : mv_(adapter.mv_)
73  , mv_map_(adapter.mv_map_)
74 { }
75 
76 MultiVecAdapter<Epetra_MultiVector>::MultiVecAdapter(const Teuchos::RCP<multivec_t>& m)
77  : mv_(m)
78 {
79  mv_map_ = Teuchos::rcpFromRef(mv_->Map());
80 }
81 
82 
84 {
85  return !mv_->DistributedGlobal();
86 }
87 
89 {
90  return mv_->DistributedGlobal();
91 }
92 
93 
94 const Teuchos::RCP<const Teuchos::Comm<int> >
96 {
97  return Util::to_teuchos_comm(Teuchos::rcpFromRef(mv_->Comm()));
98 }
99 
100 
102 {
103  return Teuchos::as<size_t>(mv_->MyLength());
104 }
105 
106 
108 {
109  return Teuchos::as<size_t>(mv_->NumVectors());
110 }
111 
112 
115 {
116  return Teuchos::as<global_size_t>(mv_->GlobalLength());
117 }
118 
119 
121 {
122  return Teuchos::as<size_t>(mv_->NumVectors());
123 }
124 
125 
127 {
128  return Teuchos::as<size_t>(mv_->Stride());
129 }
130 
131 
133 {
134  return mv_->ConstantStride();
135 }
136 
137 
138 Teuchos::RCP<const Tpetra::Vector<MultiVecAdapter<Epetra_MultiVector>::scalar_t,
143 {
144  using Teuchos::RCP;
145  using Teuchos::ArrayRCP;
146  using Tpetra::MultiVector;
147 
148  typedef scalar_t st;
149  typedef local_ordinal_t lot;
150  typedef global_ordinal_t got;
151  typedef node_t nt;
152 
153  RCP<MultiVector<st,lot,got,nt> > vector = rcp(new MultiVector<st,lot,got,nt>(this->getMap(),1));
154 
155  // Copy vector contents into Tpetra multi-vector
156  ArrayRCP<st> it = vector->getDataNonConst(0);
157  double* vector_data = mv_->operator[](Teuchos::as<int>(j)); // values from j^th vector
158  Tpetra::global_size_t size = vector->getGlobalLength();
159 
160  for( Tpetra::global_size_t i = 0; i < size; ++i ){
161  *it = vector_data[i];
162  }
163 
164  return vector->getVector(j);
165 }
166 
167 
168 // Implementation is essentially the same as getVector()
169 Teuchos::RCP<Tpetra::Vector<MultiVecAdapter<Epetra_MultiVector>::scalar_t,
170  MultiVecAdapter<Epetra_MultiVector>::local_ordinal_t,
171  MultiVecAdapter<Epetra_MultiVector>::global_ordinal_t,
174 {
175  using Teuchos::RCP;
176  using Teuchos::ArrayRCP;
177  using Tpetra::MultiVector;
178 
179  typedef scalar_t st;
180  typedef local_ordinal_t lot;
181  typedef global_ordinal_t got;
182  typedef node_t nt;
183 
184  RCP<MultiVector<st,lot,got,nt> > vector = rcp(new MultiVector<st,lot,got,nt>(this->getMap(),1));
185 
186  // Copy vector contents into Tpetra multi-vector
187  ArrayRCP<st> it = vector->getDataNonConst(0);
188  double* vector_data = mv_->operator[](Teuchos::as<int>(j)); // values from j^th vector
189  Tpetra::global_size_t size = vector->getGlobalLength();
190 
191  for( Tpetra::global_size_t i = 0; i < size; ++i ){
192  *it = vector_data[i];
193  }
194 
195  return vector->getVectorNonConst(j);
196 }
197 
198 
200  const Teuchos::ArrayView<MultiVecAdapter<Epetra_MultiVector>::scalar_t>& av,
201  size_t lda,
202  Teuchos::Ptr<
203  const Tpetra::Map<MultiVecAdapter<Epetra_MultiVector>::local_ordinal_t,
204  MultiVecAdapter<Epetra_MultiVector>::global_ordinal_t,
205  MultiVecAdapter<Epetra_MultiVector>::node_t> > distribution_map ) const
206 {
207  using Teuchos::rcpFromPtr;
208  using Teuchos::as;
209 
210  const size_t num_vecs = getGlobalNumVectors();
211 
212 #ifdef HAVE_AMESOS2_DEBUG
213  const size_t requested_vector_length = distribution_map->getNodeNumElements();
214  TEUCHOS_TEST_FOR_EXCEPTION( lda < requested_vector_length,
215  std::invalid_argument,
216  "Given stride is not large enough for local vector length" );
217  TEUCHOS_TEST_FOR_EXCEPTION( as<size_t>(av.size()) < (num_vecs-1) * lda + requested_vector_length,
218  std::invalid_argument,
219  "MultiVector storage not large enough given leading dimension "
220  "and number of vectors" );
221 #endif
222 
223  Epetra_Map e_dist_map
224  = *Util::tpetra_map_to_epetra_map<local_ordinal_t,
225  global_ordinal_t,
226  global_size_t,
227  node_t>(*distribution_map);
228 
229  multivec_t redist_mv(e_dist_map, as<int>(num_vecs));
230  const Epetra_Import importer(e_dist_map, *mv_map_); // Note, target/source order is reversed in Tpetra
231  redist_mv.Import(*mv_, importer, Insert);
232 
233  // Finally, do copy
234  redist_mv.ExtractCopy(av.getRawPtr(), lda);
235 }
236 
237 
238 Teuchos::ArrayRCP<MultiVecAdapter<Epetra_MultiVector>::scalar_t>
240 {
241  // TEUCHOS_TEST_FOR_EXCEPTION( !this->isConstantStride(),
242  // std::logic_error,
243  // "get1dViewNonConst() : can only get 1d view if stride is constant");
244 
245  // if( local ){
246  // TEUCHOS_TEST_FOR_EXCEPTION(
247  // true,
248  // std::logic_error,
249  // "Amesos2::MultiVecAdapter<Epetra_MultiVector> : 1D views not yet supported for local-local Epetra multi-vectors");
250 
251  // // localize();
252  // // /* Use the global element list returned by
253  // // * mv_->getMap()->getNodeElementList() to get a subCopy of mv_ which we
254  // // * assign to l_l_mv_, then return get1dViewNonConst() of l_l_mv_
255  // // */
256  // // l_l_mv_ = Teuchos::null;
257 
258  // // Teuchos::Array<GlobalOrdinal> nodeElements_go(mv_->Map().NumMyElements());
259  // // mv_->Map().MyGlobalElements(nodeElements_go.getRawPtr());
260  // // Teuchos::Array<size_t> nodeElements_st(nodeElements_go.size());
261 
262  // // // Convert the node element to a list of size_t type objects
263  // // typename Teuchos::ArrayView<const GlobalOrdinal>::iterator it_go;
264  // // Teuchos::Array<size_t>::iterator it_st = nodeElements_st.begin();
265  // // for( it_go = nodeElements_go.begin(); it_go != nodeElements_go.end(); ++it_go ){
266  // // *(it_st++) = Teuchos::as<size_t>(*it_go);
267  // // }
268 
269  // // l_l_mv_ = l_mv_->subViewNonConst(nodeElements_st);
270 
271  // // return(l_l_mv_->get1dViewNonConst());
272  // } else {
273  // scalar_t* values;
274  // int lda;
275 
276  // if( !isLocal() ){
277  // this->localize();
278  // l_mv_->ExtractView(&values, &lda);
279  // } else {
280  // mv_->ExtractView(&values, &lda);
281  // }
282 
283  // TEUCHOS_TEST_FOR_EXCEPTION( lda != Teuchos::as<int>(this->getStride()),
284  // std::logic_error,
285  // "Stride reported during extraction not consistent with what multivector reports");
286 
287  // return Teuchos::arcp(values,0,lda*this->getGlobalNumVectors(),false);
288  // }
289  return Teuchos::null;
290 }
291 
292 
293 void
295  const Teuchos::ArrayView<const MultiVecAdapter<Epetra_MultiVector>::scalar_t>& new_data,
296  size_t lda,
297  Teuchos::Ptr<
298  const Tpetra::Map<MultiVecAdapter<Epetra_MultiVector>::local_ordinal_t,
299  MultiVecAdapter<Epetra_MultiVector>::global_ordinal_t,
301 {
302  using Teuchos::rcpFromPtr;
303  using Teuchos::as;
304 
305  const size_t num_vecs = getGlobalNumVectors();
306  // TODO: check that the following const_cast is safe
307  double* data_ptr = const_cast<double*>(new_data.getRawPtr());
308  const Epetra_BlockMap e_source_map
309  = *Util::tpetra_map_to_epetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(*source_map);
310  const multivec_t source_mv(Copy, e_source_map, data_ptr, as<int>(lda), as<int>(num_vecs));
311  const Epetra_Import importer(*mv_map_, e_source_map);
312 
313  mv_->Import(source_mv, importer, Insert);
314 }
315 
316 
318 {
319  std::ostringstream oss;
320  oss << "Amesos2 adapter wrapping: Epetra_MultiVector";
321  return oss.str();
322 }
323 
324 
326  Teuchos::FancyOStream& os,
327  const Teuchos::EVerbosityLevel verbLevel=
328  Teuchos::Describable::verbLevel_default) const
329 {
330  // TODO: implement!
331 }
332 
333 
334 Teuchos::RCP<const Tpetra::Map<MultiVecAdapter<Epetra_MultiVector>::local_ordinal_t,
335  MultiVecAdapter<Epetra_MultiVector>::global_ordinal_t,
338 {
339  return Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(*mv_map_);
340 }
341 
342 
344 = "Amesos2 adapter for Epetra_MultiVector";
345 
346 
347 } // end namespace Amesos2
348 
349 #endif // AMESOS2_EPETRA_MULTIVEC_ADAPTER_DEF_HPP
Utility functions for Amesos2.
const RCP< const Teuchos::Comm< int > > to_teuchos_comm(RCP< const Epetra_Comm > c)
Transform an Epetra_Comm object into a Teuchos::Comm object.
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
Amesos2::MultiVecAdapter specialization for the Epetra_MultiVector class.
RCP< Epetra_Map > tpetra_map_to_epetra_map(const Tpetra::Map< LO, GO, Node > &map)
Transform a Tpetra::Map object into an Epetra_Map.
Definition: Amesos2_Util.hpp:653
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:175