Thyra Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_EpetraThyraWrappers.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
43 #include "Thyra_DefaultSpmdVectorSpace.hpp"
44 #include "Thyra_DefaultSpmdMultiVector.hpp"
45 #include "Thyra_DefaultSpmdVector.hpp"
46 #include "Thyra_DetachedVectorView.hpp"
47 #include "Thyra_DetachedMultiVectorView.hpp"
48 #include "Thyra_VectorSpaceFactoryBase.hpp"
49 #include "Thyra_ProductVectorSpaceBase.hpp"
50 #include "Teuchos_Assert.hpp"
51 #include "Teuchos_dyn_cast.hpp"
52 
54 #ifdef HAVE_MPI
56 #endif
57 
58 #include "Epetra_Map.h"
59 #include "Epetra_Comm.h"
60 #include "Epetra_SerialComm.h"
61 #ifdef HAVE_MPI
62 # include "Epetra_MpiComm.h"
63 #endif
64 #include "Epetra_MultiVector.h"
65 #include "Epetra_Vector.h"
66 
67 //
68 // Helpers
69 //
70 
71 
72 namespace {
73 
74 
76 unwrapSingleProductVectorSpace(
77  const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > &vs_in
78  )
79 {
80  using Teuchos::RCP;
81  using Teuchos::rcp_dynamic_cast;
82  using Thyra::ProductVectorSpaceBase;
83  const RCP<const ProductVectorSpaceBase<double> > pvs =
84  rcp_dynamic_cast<const ProductVectorSpaceBase<double> >(vs_in);
85  if (nonnull(pvs)) {
86  TEUCHOS_ASSERT_EQUALITY( pvs->numBlocks(), 1 );
87  return pvs->getBlock(0);
88  }
89  return vs_in;
90 }
91 
92 
93 } // namespace
94 
95 
96 //
97 // Implementations of user function
98 //
99 
100 
103 {
104  using Teuchos::rcp;
105  using Teuchos::rcp_dynamic_cast;
106  using Teuchos::set_extra_data;
107 
109  serialEpetraComm = rcp_dynamic_cast<const Epetra_SerialComm>(epetraComm);
110  if( serialEpetraComm.get() ) {
112  serialComm = rcp(new Teuchos::SerialComm<Ordinal>());
113  set_extra_data( serialEpetraComm, "serialEpetraComm", Teuchos::inOutArg(serialComm) );
114  return serialComm;
115  }
116 
117 #ifdef HAVE_MPI
118 
120  mpiEpetraComm = rcp_dynamic_cast<const Epetra_MpiComm>(epetraComm);
121  if( mpiEpetraComm.get() ) {
123  rawMpiComm = Teuchos::opaqueWrapper(mpiEpetraComm->Comm());
124  set_extra_data( mpiEpetraComm, "mpiEpetraComm", Teuchos::inOutArg(rawMpiComm) );
126  mpiComm = rcp(new Teuchos::MpiComm<Ordinal>(rawMpiComm));
127  return mpiComm;
128  }
129 
130 #endif // HAVE_MPI
131 
132  // If you get here then the failed!
133  return Teuchos::null;
134 
135 }
136 
137 
140  const RCP<const Epetra_Map> &epetra_map
141  )
142 {
143  using Teuchos::as; using Teuchos::inoutArg;
144 #ifdef TEUCHOS_DEBUG
146  !epetra_map.get(), std::invalid_argument,
147  "create_VectorSpace::initialize(...): Error!" );
148 #endif // TEUCHOS_DEBUG
150  create_Comm(Teuchos::rcpFromRef(epetra_map->Comm())).assert_not_null();
151  Teuchos::set_extra_data(epetra_map, "epetra_map", inoutArg(comm));
152  const Ordinal localSubDim = epetra_map->NumMyElements();
154  defaultSpmdVectorSpace<double>(
155  comm, localSubDim, epetra_map->NumGlobalElements());
156  TEUCHOS_ASSERT_EQUALITY(vs->dim(), as<Ordinal>(epetra_map->NumGlobalElements()));
157  // NOTE: It is impossible to trigger the above exception unless
158  // NumGlobalElemenets() overflows 'int'. However, this is a nice sanity
159  // check to stop the code early in case we seen an overflow in practice.
160  // Because this assert will only likely trigger in a non-debug build, we
161  // will unguard the assert since it is very cheap to perform.
162  Teuchos::set_extra_data( epetra_map, "epetra_map", inoutArg(vs) );
163  return vs;
164 }
165 
166 
169  const RCP<const VectorSpaceBase<double> > &parentSpace,
170  const int dim
171  )
172 {
173  using Teuchos::rcp_dynamic_cast;
174 #ifdef TEUCHOS_DEBUG
175  TEUCHOS_TEST_FOR_EXCEPT(parentSpace.get()==NULL);
176  Teuchos::dyn_cast<const SpmdVectorSpaceBase<double> >(*parentSpace);
177  TEUCHOS_TEST_FOR_EXCEPT(dim <= 0);
178 #endif
179  return parentSpace->smallVecSpcFcty()->createVecSpc(dim);
180 }
181 
182 
185  const RCP<Epetra_Vector> &epetra_v,
186  const RCP<const VectorSpaceBase<double> > &space_in
187  )
188 {
189 #ifdef TEUCHOS_DEBUG
190  TEUCHOS_TEST_FOR_EXCEPT(space_in.get()==NULL);
191 #endif
193  space = Teuchos::rcp_dynamic_cast<const SpmdVectorSpaceBase<double> >(
194  space_in,true);
195  if(!epetra_v.get())
196  return Teuchos::null;
197  // New local view of raw data
198  double *localValues;
199  epetra_v->ExtractView( &localValues );
200  // Build the Vector
202  v = Teuchos::rcp(
203  new DefaultSpmdVector<double>(
204  space,
205  Teuchos::arcp(localValues,0,epetra_v->Map().NumMyElements(),false),
206  1
207  )
208  );
209  Teuchos::set_extra_data<RCP<Epetra_Vector> >( epetra_v, "Epetra_Vector",
210  Teuchos::inOutArg(v) );
211  return v;
212 }
213 
214 
217  const RCP<const Epetra_Vector> &epetra_v,
218  const RCP<const VectorSpaceBase<double> > &space_in
219  )
220 {
221 #ifdef TEUCHOS_DEBUG
222  TEUCHOS_TEST_FOR_EXCEPT(space_in.get()==NULL);
223 #endif
225  space = Teuchos::rcp_dynamic_cast<const SpmdVectorSpaceBase<double> >(
226  space_in,true);
227  if(!epetra_v.get())
228  return Teuchos::null;
229  // New local view of raw data
230  double *localValues;
231  epetra_v->ExtractView( &localValues );
232  // Build the Vector
234  v = Teuchos::rcp(
235  new DefaultSpmdVector<double>(
236  space,
237  Teuchos::arcp(localValues,0,epetra_v->Map().NumMyElements(),false),
238  1
239  )
240  );
241  Teuchos::set_extra_data<RCP<const Epetra_Vector> >( epetra_v, "Epetra_Vector",
242  Teuchos::inOutArg(v) );
243  return v;
244 }
245 
246 
249  const RCP<Epetra_MultiVector> &epetra_mv,
250  const RCP<const VectorSpaceBase<double> > &range_in,
251  const RCP<const VectorSpaceBase<double> > &domain_in
252  )
253 {
254  using Teuchos::rcp_dynamic_cast;
255 #ifdef TEUCHOS_DEBUG
256  TEUCHOS_TEST_FOR_EXCEPT(range_in.get()==NULL);
257 #endif
259  Teuchos::rcp_dynamic_cast<const SpmdVectorSpaceBase<double> >(
260  unwrapSingleProductVectorSpace(range_in),
261  true
262  );
264  Teuchos::rcp_dynamic_cast<const ScalarProdVectorSpaceBase<double> >(
265  unwrapSingleProductVectorSpace(domain_in),
266  true
267  );
268  if (!epetra_mv.get() )
269  return Teuchos::null;
270  if ( is_null(domain) ) {
271  domain = rcp_dynamic_cast<const ScalarProdVectorSpaceBase<double> >(
272  create_LocallyReplicatedVectorSpace(range,epetra_mv->NumVectors())
273  );
274  }
275  // New local view of raw data
276  double *localValues; int leadingDim;
277  if( epetra_mv->ConstantStride() ) {
278  epetra_mv->ExtractView( &localValues, &leadingDim );
279  }
280  else {
281  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement views of non-contiguous mult-vectors!
282  }
283  // Build the MultiVector
285  mv = Teuchos::rcp(
286  new DefaultSpmdMultiVector<double>(
287  range,
288  domain,
289  Teuchos::arcp(localValues,0,leadingDim*epetra_mv->NumVectors(),false),
290  leadingDim
291  )
292  );
293  Teuchos::set_extra_data<RCP<Epetra_MultiVector> >(
294  epetra_mv, "Epetra_MultiVector", Teuchos::inOutArg(mv) );
295  return mv;
296 }
297 
298 
301  const RCP<const Epetra_MultiVector> &epetra_mv,
302  const RCP<const VectorSpaceBase<double> > &range_in,
303  const RCP<const VectorSpaceBase<double> > &domain_in
304  )
305 {
306  using Teuchos::rcp_dynamic_cast;
307 #ifdef TEUCHOS_DEBUG
308  TEUCHOS_TEST_FOR_EXCEPT(range_in.get()==NULL);
309 #endif
311  Teuchos::rcp_dynamic_cast<const SpmdVectorSpaceBase<double> >(
312  unwrapSingleProductVectorSpace(range_in),
313  true
314  );
316  Teuchos::rcp_dynamic_cast<const ScalarProdVectorSpaceBase<double> >(
317  unwrapSingleProductVectorSpace(domain_in),
318  true
319  );
320  if (!epetra_mv.get())
321  return Teuchos::null;
322  if ( is_null(domain) ) {
323  domain = rcp_dynamic_cast<const ScalarProdVectorSpaceBase<double> >(
324  create_LocallyReplicatedVectorSpace(range,epetra_mv->NumVectors())
325  );
326  }
327  // New local view of raw data
328  double *localValues; int leadingDim;
329  if( epetra_mv->ConstantStride() ) {
330  epetra_mv->ExtractView( &localValues, &leadingDim );
331  }
332  else {
333  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement views of non-contiguous mult-vectors!
334  }
335  // Build the MultiVector
337  mv = Teuchos::rcp(
338  new DefaultSpmdMultiVector<double>(
339  range,
340  domain,
341  Teuchos::arcp(localValues,0,leadingDim*epetra_mv->NumVectors(),false),
342  leadingDim
343  )
344  );
345  Teuchos::set_extra_data<RCP<const Epetra_MultiVector> >(
346  epetra_mv, "Epetra_MultiVector", Teuchos::inOutArg(mv) );
347  return mv;
348 }
349 
350 
353 {
354 
355  using Teuchos::rcp;
356  using Teuchos::ptrFromRef;
357  using Teuchos::ptr_dynamic_cast;
358  using Teuchos::SerialComm;
359 #ifdef HAVE_MPI
360  using Teuchos::MpiComm;
361 #endif
362 
363  const Ptr<const Teuchos::Comm<Ordinal> > comm = Teuchos::ptrFromRef(comm_in);
364 
365  const Ptr<const SerialComm<Ordinal> > serialComm =
366  ptr_dynamic_cast<const SerialComm<Ordinal> >(comm);
367 
368  RCP<const Epetra_Comm> epetraComm;
369 
370 #ifdef HAVE_MPI
371 
372  const Ptr<const MpiComm<Ordinal> > mpiComm =
373  ptr_dynamic_cast<const MpiComm<Ordinal> >(comm);
374 
375  TEUCHOS_TEST_FOR_EXCEPTION(is_null(mpiComm) && is_null(serialComm),
376  std::runtime_error,
377  "SPMD std::vector space has a communicator that is "
378  "neither a serial comm nor an MPI comm");
379 
380  if (nonnull(mpiComm)) {
381  epetraComm = rcp(new Epetra_MpiComm(*mpiComm->getRawMpiComm()()));
382  }
383  else {
384  epetraComm = rcp(new Epetra_SerialComm());
385  }
386 
387 #else
388 
389  TEUCHOS_TEST_FOR_EXCEPTION(is_null(serialComm), std::runtime_error,
390  "SPMD std::vector space has a communicator that is "
391  "neither a serial comm nor an MPI comm");
392 
393  epetraComm = rcp(new Epetra_SerialComm());
394 
395 #endif
396 
397  TEUCHOS_TEST_FOR_EXCEPTION(is_null(epetraComm), std::runtime_error,
398  "null communicator created");
399 
400  return epetraComm;
401 
402 }
403 
404 
406 Thyra::get_Epetra_Map(const VectorSpaceBase<double>& vs_in,
407  const RCP<const Epetra_Comm>& comm)
408 {
409 
410  using Teuchos::rcpFromRef;
411  using Teuchos::rcpFromPtr;
412  using Teuchos::rcp_dynamic_cast;
413  using Teuchos::ptrFromRef;
414  using Teuchos::ptr_dynamic_cast;
415 
416  const Ptr<const VectorSpaceBase<double> > vs_ptr = ptrFromRef(vs_in);
417 
418  const Ptr<const SpmdVectorSpaceBase<double> > spmd_vs =
419  ptr_dynamic_cast<const SpmdVectorSpaceBase<double> >(vs_ptr);
420 
421  const Ptr<const ProductVectorSpaceBase<double> > &prod_vs =
422  ptr_dynamic_cast<const ProductVectorSpaceBase<double> >(vs_ptr);
423 
424  TEUCHOS_TEST_FOR_EXCEPTION( is_null(spmd_vs) && is_null(prod_vs), std::logic_error,
425  "Error, the concrete VectorSpaceBase object of type "
426  +Teuchos::demangleName(typeid(vs_in).name())+" does not support the"
427  " SpmdVectorSpaceBase or the ProductVectorSpaceBase interfaces!" );
428 
429  const int numBlocks = (nonnull(prod_vs) ? prod_vs->numBlocks() : 1);
430 
431  // Get an array of SpmdVectorBase objects for the blocks
432 
433  Array<RCP<const SpmdVectorSpaceBase<double> > > spmd_vs_blocks;
434  if (nonnull(prod_vs)) {
435  for (int block_i = 0; block_i < numBlocks; ++block_i) {
436  const RCP<const SpmdVectorSpaceBase<double> > spmd_vs_i =
437  rcp_dynamic_cast<const SpmdVectorSpaceBase<double> >(
438  prod_vs->getBlock(block_i), true);
439  spmd_vs_blocks.push_back(spmd_vs_i);
440  }
441  }
442  else {
443  spmd_vs_blocks.push_back(rcpFromPtr(spmd_vs));
444  }
445 
446  // Find the number of local elements, summed over all blocks
447 
448  int myLocalElements = 0;
449  for (int block_i = 0; block_i < numBlocks; ++block_i) {
450  myLocalElements += spmd_vs_blocks[block_i]->localSubDim();
451  }
452 
453  // Find the GIDs owned by this processor, taken from all blocks
454 
455  int count=0;
456  int blockOffset = 0;
457  Array<int> myGIDs(myLocalElements);
458  for (int block_i = 0; block_i < numBlocks; ++block_i) {
459  const RCP<const SpmdVectorSpaceBase<double> > spmd_vs_i = spmd_vs_blocks[block_i];
460  const int lowGIDInBlock = spmd_vs_i->localOffset();
461  const int numLocalElementsInBlock = spmd_vs_i->localSubDim();
462  for (int i=0; i < numLocalElementsInBlock; ++i, ++count) {
463  myGIDs[count] = blockOffset + lowGIDInBlock + i;
464  }
465  blockOffset += spmd_vs_i->dim();
466  }
467 
468  const int globalDim = vs_in.dim();
469 
470  return Teuchos::rcp(
471  new Epetra_Map(globalDim, myLocalElements, &(myGIDs[0]), 0, *comm));
472 
473 }
474 
475 
478  const Epetra_Map &map,
479  const RCP<VectorBase<double> > &v
480  )
481 {
482  using Teuchos::get_optional_extra_data;
483 #ifdef TEUCHOS_DEBUG
485  epetra_vs = create_VectorSpace(Teuchos::rcp(&map,false));
486  THYRA_ASSERT_VEC_SPACES(
487  "Thyra::get_Epetra_Vector(map,v)", *epetra_vs, *v->space() );
488 #endif
489  //
490  // First, try to grab the Epetra_Vector straight out of the
491  // RCP since this is the fastest way.
492  //
493  const Ptr<const RCP<Epetra_Vector> >
494  epetra_v_ptr = get_optional_extra_data<RCP<Epetra_Vector> >(
495  v,"Epetra_Vector");
496  if(!is_null(epetra_v_ptr)) {
497  return *epetra_v_ptr;
498  }
499  //
500  // The assumption that we (rightly) make here is that if the vector spaces
501  // are compatible, that either the multi-vectors are both in-core or the
502  // vector spaces are both derived from SpmdVectorSpaceBase and have
503  // compatible maps.
504  //
505  const VectorSpaceBase<double> &vs = *v->range();
506  const SpmdVectorSpaceBase<double> *mpi_vs
507  = dynamic_cast<const SpmdVectorSpaceBase<double>*>(&vs);
508  const Ordinal localOffset = ( mpi_vs ? mpi_vs->localOffset() : 0 );
509  const Ordinal localSubDim = ( mpi_vs ? mpi_vs->localSubDim() : vs.dim() );
510  //
511  // Here we will extract a view of the local elements in the underlying
512  // VectorBase object. In most cases, no data will be allocated or copied
513  // and only some small objects will be created and a few virtual functions
514  // will be called so the overhead should be low and fixed.
515  //
516  // Create a *mutable* view of the local elements, this view will be set on
517  // the RCP that is returned. As a result, this view will be relased
518  // when the returned Epetra_Vector is released.
519  //
520  // Note that the input vector 'v' will be remembered through this detached
521  // view!
522  //
524  emvv = Teuchos::rcp(
525  new DetachedVectorView<double>(
526  v
527  ,Range1D(localOffset,localOffset+localSubDim-1)
528  ,true // forceContiguous
529  )
530  );
531  // Create a temporary Epetra_Vector object and give it
532  // the above local view
534  epetra_v = Teuchos::rcp(
535  new Epetra_Vector(
536  ::View // CV
537  ,map // Map
538  ,const_cast<double*>(emvv->values()) // V
539  )
540  );
541  // Give the explict view object to the above Epetra_Vector smart pointer
542  // object. In this way, when the client is finished with the Epetra_Vector
543  // view the destructor from the object in emvv will automatically commit the
544  // changes to the elements in the input v VectorBase object (reguardless of
545  // its implementation). This is truly an elegant result!
546  Teuchos::set_extra_data( emvv, "emvv", Teuchos::inOutArg(epetra_v),
548  // We are done!
549  return epetra_v;
550 }
551 
552 
555  const Epetra_Map &map,
556  const RCP<const VectorBase<double> > &v
557  )
558 {
559  using Teuchos::get_optional_extra_data;
560 #ifdef TEUCHOS_DEBUG
562  epetra_vs = create_VectorSpace(Teuchos::rcp(&map,false));
563  THYRA_ASSERT_VEC_SPACES(
564  "Thyra::get_Epetra_Vector(map,v)", *epetra_vs, *v->space() );
565 #endif
566  //
567  // First, try to grab the Epetra_Vector straight out of the
568  // RCP since this is the fastest way.
569  //
570  const Ptr<const RCP<const Epetra_Vector> >
571  epetra_v_ptr = get_optional_extra_data<RCP<const Epetra_Vector> >(
572  v,"Epetra_Vector");
573  if(!is_null(epetra_v_ptr))
574  return *epetra_v_ptr;
575  const Ptr<const RCP<Epetra_Vector> >
576  epetra_nonconst_v_ptr = get_optional_extra_data<RCP<Epetra_Vector> >(
577  v,"Epetra_Vector");
578  if(!is_null(epetra_nonconst_v_ptr))
579  return *epetra_nonconst_v_ptr;
580  //
581  // Same as above function except as stated below
582  //
583  const VectorSpaceBase<double> &vs = *v->range();
584  const SpmdVectorSpaceBase<double> *mpi_vs
585  = dynamic_cast<const SpmdVectorSpaceBase<double>*>(&vs);
586  const Ordinal localOffset = ( mpi_vs ? mpi_vs->localOffset() : 0 );
587  const Ordinal localSubDim = ( mpi_vs ? mpi_vs->localSubDim() : vs.dim() );
588  // Get an explicit *non-mutable* view of all of the elements in the multi
589  // vector. Note that 'v' will be remembered by this view!
591  evv = Teuchos::rcp(
592  new ConstDetachedVectorView<double>(
593  v
594  ,Range1D(localOffset,localOffset+localSubDim-1)
595  ,true // forceContiguous
596  )
597  );
598  // Create a temporary Epetra_Vector object and give it
599  // the above view
601  epetra_v = Teuchos::rcp(
602  new Epetra_Vector(
603  ::View // CV
604  ,map // Map
605  ,const_cast<double*>(evv->values()) // V
606  )
607  );
608  // This next statement will cause the destructor to free the view if
609  // needed (see above function). Since this view is non-mutable,
610  // only a releaseDetachedView(...) and not a commit will be called.
611  // This is the whole reason there is a seperate implementation for
612  // the const and non-const cases.
613  Teuchos::set_extra_data( evv, "evv", Teuchos::inOutArg(epetra_v),
615  // We are done!
616  return epetra_v;
617 }
618 
619 
622  const Epetra_Map &map,
623  const RCP<MultiVectorBase<double> > &mv
624  )
625 {
626  using Teuchos::get_optional_extra_data;
627 #ifdef TEUCHOS_DEBUG
629  epetra_vs = create_VectorSpace(Teuchos::rcp(&map,false));
630  THYRA_ASSERT_VEC_SPACES(
631  "Thyra::get_Epetra_MultiVector(map,mv)", *epetra_vs, *mv->range() );
632 #endif
633  //
634  // First, try to grab the Epetra_MultiVector straight out of the
635  // RCP since this is the fastest way.
636  //
637  const Ptr<const RCP<Epetra_MultiVector> >
638  epetra_mv_ptr = get_optional_extra_data<RCP<Epetra_MultiVector> >(
639  mv,"Epetra_MultiVector");
640  if(!is_null(epetra_mv_ptr)) {
641  return *epetra_mv_ptr;
642  }
643  //
644  // The assumption that we (rightly) make here is that if the vector spaces
645  // are compatible, that either the multi-vectors are both in-core or the
646  // vector spaces are both derived from SpmdVectorSpaceBase and have
647  // compatible maps.
648  //
649  const VectorSpaceBase<double> &vs = *mv->range();
650  const SpmdVectorSpaceBase<double> *mpi_vs
651  = dynamic_cast<const SpmdVectorSpaceBase<double>*>(&vs);
652  const Ordinal localOffset = ( mpi_vs ? mpi_vs->localOffset() : 0 );
653  const Ordinal localSubDim = ( mpi_vs ? mpi_vs->localSubDim() : vs.dim() );
654  //
655  // Here we will extract a view of the local elements in the underlying
656  // MultiVectorBase object. In most cases, no data will be allocated or
657  // copied and only some small objects will be created and a few virtual
658  // functions will be called so the overhead should be low and fixed.
659  //
660  // Create a *mutable* view of the local elements, this view will be set on
661  // the RCP that is returned. As a result, this view will be relased
662  // when the returned Epetra_MultiVector is released.
663  //
665  emmvv = Teuchos::rcp(
666  new DetachedMultiVectorView<double>(
667  *mv
668  ,Range1D(localOffset,localOffset+localSubDim-1)
669  )
670  );
671  // Create a temporary Epetra_MultiVector object and give it
672  // the above local view
674  epetra_mv = Teuchos::rcp(
675  new Epetra_MultiVector(
676  ::View // CV
677  ,map // Map
678  ,const_cast<double*>(emmvv->values()) // A
679  ,emmvv->leadingDim() // MyLDA
680  ,emmvv->numSubCols() // NumVectors
681  )
682  );
683  // Give the explict view object to the above Epetra_MultiVector
684  // smart pointer object. In this way, when the client is finished
685  // with the Epetra_MultiVector view the destructor from the object
686  // in emmvv will automatically commit the changes to the elements in
687  // the input mv MultiVectorBase object (reguardless of its
688  // implementation). This is truly an elegant result!
689  Teuchos::set_extra_data( emmvv, "emmvv", Teuchos::inOutArg(epetra_mv),
691  // Also set the mv itself as extra data just to be safe
692  Teuchos::set_extra_data( mv, "mv", Teuchos::inOutArg(epetra_mv) );
693  // We are done!
694  return epetra_mv;
695 }
696 
697 
700  const Epetra_Map &map,
701  const RCP<const MultiVectorBase<double> > &mv
702  )
703 {
704  using Teuchos::get_optional_extra_data;
705 #ifdef TEUCHOS_DEBUG
707  epetra_vs = create_VectorSpace(Teuchos::rcp(&map,false));
708  THYRA_ASSERT_VEC_SPACES(
709  "Thyra::get_Epetra_MultiVector(map,mv)",
710  *epetra_vs, *mv->range() );
711 #endif
712  //
713  // First, try to grab the Epetra_MultiVector straight out of the
714  // RCP since this is the fastest way.
715  //
716  const Ptr<const RCP<const Epetra_MultiVector> >
717  epetra_mv_ptr
718  = get_optional_extra_data<RCP<const Epetra_MultiVector> >(
719  mv,"Epetra_MultiVector" );
720  if(!is_null(epetra_mv_ptr)) {
721  return *epetra_mv_ptr;
722  }
723  //
724  // Same as above function except as stated below
725  //
726  const VectorSpaceBase<double> &vs = *mv->range();
727  const SpmdVectorSpaceBase<double> *mpi_vs
728  = dynamic_cast<const SpmdVectorSpaceBase<double>*>(&vs);
729  const Ordinal localOffset = ( mpi_vs ? mpi_vs->localOffset() : 0 );
730  const Ordinal localSubDim = ( mpi_vs ? mpi_vs->localSubDim() : vs.dim() );
731  // Get an explicit *non-mutable* view of all of the elements in
732  // the multi vector.
734  emvv = Teuchos::rcp(
735  new ConstDetachedMultiVectorView<double>(
736  *mv
737  ,Range1D(localOffset,localOffset+localSubDim-1)
738  )
739  );
740  // Create a temporary Epetra_MultiVector object and give it
741  // the above view
743  epetra_mv = Teuchos::rcp(
744  new Epetra_MultiVector(
745  ::View // CV
746  ,map // Map
747  ,const_cast<double*>(emvv->values()) // A
748  ,emvv->leadingDim() // MyLDA
749  ,emvv->numSubCols() // NumVectors
750  )
751  );
752  // This next statement will cause the destructor to free the view if
753  // needed (see above function). Since this view is non-mutable,
754  // only a releaseDetachedView(...) and not a commit will be called.
755  // This is the whole reason there is a seperate implementation for
756  // the const and non-const cases.
757  Teuchos::set_extra_data( emvv, "emvv", Teuchos::inOutArg(epetra_mv),
759  // Also set the mv itself as extra data just to be safe
760  Teuchos::set_extra_data( mv, "mv", Teuchos::inOutArg(epetra_mv) );
761  // We are done!
762  return epetra_mv;
763 }
764 
765 
768  const Epetra_Map &map,
769  MultiVectorBase<double> &mv
770  )
771 {
772  using Teuchos::rcpWithEmbeddedObj;
773  using Teuchos::rcpFromRef;
774  using Teuchos::outArg;
775  ArrayRCP<double> mvData;
776  Ordinal mvLeadingDim = -1;
777  SpmdMultiVectorBase<double> *mvSpmdMv = 0;
778  SpmdVectorBase<double> *mvSpmdV = 0;
779  if ((mvSpmdMv = dynamic_cast<SpmdMultiVectorBase<double>*>(&mv))) {
780  mvSpmdMv->getNonconstLocalData(outArg(mvData), outArg(mvLeadingDim));
781  }
782  else if ((mvSpmdV = dynamic_cast<SpmdVectorBase<double>*>(&mv))) {
783  mvSpmdV->getNonconstLocalData(outArg(mvData));
784  mvLeadingDim = mvSpmdV->spmdSpace()->localSubDim();
785  }
786  if (nonnull(mvData)) {
787  return rcpWithEmbeddedObj(
788  new Epetra_MultiVector(
789  ::View, map, mvData.getRawPtr(), mvLeadingDim, mv.domain()->dim()
790  ),
791  mvData
792  );
793  }
794  return ::Thyra::get_Epetra_MultiVector(map, rcpFromRef(mv));
795 }
796 
797 
800  const Epetra_Map &map,
801  const MultiVectorBase<double> &mv
802  )
803 {
804  using Teuchos::rcpWithEmbeddedObj;
805  using Teuchos::rcpFromRef;
806  using Teuchos::outArg;
807  ArrayRCP<const double> mvData;
808  Ordinal mvLeadingDim = -1;
809  const SpmdMultiVectorBase<double> *mvSpmdMv = 0;
810  const SpmdVectorBase<double> *mvSpmdV = 0;
811  if ((mvSpmdMv = dynamic_cast<const SpmdMultiVectorBase<double>*>(&mv))) {
812  mvSpmdMv->getLocalData(outArg(mvData), outArg(mvLeadingDim));
813  }
814  else if ((mvSpmdV = dynamic_cast<const SpmdVectorBase<double>*>(&mv))) {
815  mvSpmdV->getLocalData(outArg(mvData));
816  mvLeadingDim = mvSpmdV->spmdSpace()->localSubDim();
817  }
818  if (nonnull(mvData)) {
819  return rcpWithEmbeddedObj(
820  new Epetra_MultiVector(
821  ::View,map, const_cast<double*>(mvData.getRawPtr()), mvLeadingDim, mv.domain()->dim()
822  ),
823  mvData
824  );
825  }
826  return ::Thyra::get_Epetra_MultiVector(map, rcpFromRef(mv));
827 }
RCP< Epetra_MultiVector > get_Epetra_MultiVector(const Epetra_Map &map, const RCP< MultiVectorBase< double > > &mv)
Get a non-const Epetra_MultiVector view from a non-const MultiVectorBase object if possible...
bool is_null(const boost::shared_ptr< T > &p)
TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName(const std::string &mangledName)
RCP< const Epetra_Map > get_Epetra_Map(const VectorSpaceBase< double > &vs, const RCP< const Epetra_Comm > &comm)
Get (or create) an Epetra_Map object given an VectorSpaceBase object an optionally an extra Epetra_Co...
RCP< const VectorSpaceBase< double > > create_VectorSpace(const RCP< const Epetra_Map > &epetra_map)
Create an VectorSpaceBase object given an Epetra_Map object.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
T * get() const
RCP< MultiVectorBase< double > > create_MultiVector(const RCP< Epetra_MultiVector > &epetra_mv, const RCP< const VectorSpaceBase< double > > &range, const RCP< const VectorSpaceBase< double > > &domain=Teuchos::null)
Create a non-const MultiVectorBase object from a non-const Epetra_MultiVector object.
RCP< VectorBase< double > > create_Vector(const RCP< Epetra_Vector > &epetra_v, const RCP< const VectorSpaceBase< double > > &space)
Create a non-const VectorBase object from a non-const Epetra_Vector object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
T_To & dyn_cast(T_From &from)
RCP< Epetra_Vector > get_Epetra_Vector(const Epetra_Map &map, const RCP< VectorBase< double > > &v)
Get a non-const Epetra_Vector view from a non-const VectorBase object if possible.
TypeTo as(const TypeFrom &t)
const RCP< T > & assert_not_null() const
RCP< const Epetra_Comm > get_Epetra_Comm(const Teuchos::Comm< Ordinal > &comm)
Get (or create) and Epetra_Comm given a Teuchos::Comm object.
bool nonnull(const boost::shared_ptr< T > &p)
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
RCP< const VectorSpaceBase< double > > create_LocallyReplicatedVectorSpace(const RCP< const VectorSpaceBase< double > > &parentSpace, const int dim)
Create a VectorSpaceBase object that creates locally replicated vector objects.
Teuchos_Ordinal Ordinal
RCP< const Teuchos::Comm< Ordinal > > create_Comm(const RCP< const Epetra_Comm > &epetraComm)
Given an Epetra_Comm object, return an equivalent Teuchos::Comm object.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)