AbstractLinAlgPack_VectorMutableThyra.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <assert.h>
00030 
00031 #include "AbstractLinAlgPack_VectorMutableThyra.hpp"
00032 #include "AbstractLinAlgPack_ThyraAccessors.hpp"
00033 #include "AbstractLinAlgPack_VectorDenseEncap.hpp"
00034 #include "AbstractLinAlgPack_GenPermMatrixSliceOp.hpp"
00035 #include "RTOpPack_RTOpSubRangeDecorator.hpp"
00036 #include "Teuchos_TestForException.hpp"
00037 #include "Teuchos_Workspace.hpp"
00038 #include "Teuchos_dyn_cast.hpp"
00039 #include "Teuchos_as.hpp"
00040 
00041 namespace AbstractLinAlgPack {
00042 
00043 // Constructors / Initializers
00044 
00045 VectorMutableThyra::VectorMutableThyra()
00046 {}
00047 
00048 VectorMutableThyra::VectorMutableThyra(
00049   const Teuchos::RCP<Thyra::VectorBase<value_type> >& thyra_vec
00050   )
00051 {
00052   this->initialize(thyra_vec);
00053 }
00054 
00055 void VectorMutableThyra::initialize(
00056   const Teuchos::RCP<Thyra::VectorBase<value_type> >& thyra_vec
00057   )
00058 {
00059   namespace mmp = MemMngPack;
00060   TEST_FOR_EXCEPTION(
00061     thyra_vec.get()==NULL, std::invalid_argument
00062     ,"VectorMutableThyra::initialize(thyra_vec): Error!"
00063     );
00064   thyra_vec_ = thyra_vec;
00065   space_.initialize(thyra_vec->space());
00066   this->has_changed();
00067 }
00068 
00069 Teuchos::RCP<Thyra::VectorBase<value_type> > 
00070 VectorMutableThyra::set_uninitialized()
00071 {
00072   Teuchos::RCP<Thyra::VectorBase<value_type> > tmp_thyra_vec = thyra_vec_;
00073   thyra_vec_ = Teuchos::null;
00074   space_.set_uninitialized();
00075   this->has_changed();
00076   return tmp_thyra_vec;
00077 }
00078 
00079 // Methods overridden from Vector
00080 
00081 const VectorSpace&
00082 VectorMutableThyra::space() const
00083 {
00084   return space_;
00085 }
00086 
00087 void VectorMutableThyra::apply_op(
00088   const RTOpPack::RTOp       &op
00089   ,const size_t              num_vecs
00090   ,const Vector*             vecs[]
00091   ,const size_t              num_targ_vecs
00092   ,VectorMutable*            targ_vecs[]
00093   ,RTOpPack::ReductTarget    *reduct_obj
00094   ,const index_type          first_ele
00095   ,const index_type          sub_dim
00096   ,const index_type          global_offset
00097   ) const
00098 {
00099   using Teuchos::as;
00100   using Teuchos::dyn_cast;
00101   using Teuchos::Workspace;
00102   using Teuchos::rcpFromRef;
00103   using Teuchos::RCP;
00104   using Teuchos::Ptr;
00105   Teuchos::WorkspaceStore* wss = Teuchos::get_default_workspace_store().get();
00106 
00107   // If these are in-core vectors then just let a default implementation
00108   // take care of this.
00109   if( space_.is_in_core() ) {
00110     this->apply_op_serial(
00111       op,num_vecs,vecs,num_targ_vecs,targ_vecs,reduct_obj
00112       ,first_ele,sub_dim,global_offset
00113       );
00114     return;
00115   }
00116 
00117   // Convert the non-mutable vectors into non-mutable Thyra vectors
00118   Workspace< Teuchos::RCP<const Thyra::VectorBase<value_type> > > thyra_vecs_sptr(wss,num_vecs);
00119   Workspace<Ptr<const Thyra::VectorBase<value_type> > > thyra_vecs(wss,num_vecs);
00120   for(int k = 0; k < as<int>(num_vecs); ++k ) {
00121     get_thyra_vector( space_, *vecs[k], &thyra_vecs_sptr[k] );
00122     thyra_vecs[k] = thyra_vecs_sptr[k].ptr();
00123   }
00124 
00125   // Convert the mutable vetors into mutable Thyra vectors
00126   Workspace< Teuchos::RCP<Thyra::VectorBase<value_type> > > targ_thyra_vecs_sptr(wss,num_targ_vecs);
00127   Workspace<Ptr<Thyra::VectorBase<value_type> > > targ_thyra_vecs(wss,num_targ_vecs);
00128   for(int k = 0; k < as<int>(num_targ_vecs); ++k ) {
00129     get_thyra_vector( space_, targ_vecs[k], &targ_thyra_vecs_sptr[k] );
00130     targ_thyra_vecs[k] = targ_thyra_vecs_sptr[k].ptr();
00131   }
00132 
00133   // Call the Thyra::apply_op(...)
00134   RTOpPack::RTOpSubRangeDecorator<value_type>
00135     subRangeOp(rcpFromRef(op), first_ele-1, sub_dim==0 ? -1 : sub_dim);
00136   Thyra::applyOp<value_type>(subRangeOp, thyra_vecs(), targ_thyra_vecs(),
00137     Teuchos::ptr(reduct_obj), global_offset);
00138 
00139   // Free/commit the Thyra vector views
00140   for(int k = 0; k < as<int>(num_vecs); ++k ) {
00141     free_thyra_vector( space_, *vecs[k], &thyra_vecs_sptr[k] );
00142   }
00143   for(int k = 0; k < as<int>(num_targ_vecs); ++k ) {
00144     commit_thyra_vector( space_, targ_vecs[k], &targ_thyra_vecs_sptr[k] );
00145   }
00146 
00147 }
00148 
00149 
00150 index_type VectorMutableThyra::dim() const
00151 {
00152   return space_.dim();
00153 }
00154 
00155 void VectorMutableThyra::get_sub_vector(
00156   const Range1D& rng, RTOpPack::SubVector* sub_vec
00157   ) const
00158 {
00159   RTOpPack::ConstSubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00160   thyra_vec_->acquireDetachedView(convert(rng),&_sub_vec);
00161   *sub_vec = _sub_vec;
00162 }
00163 
00164 void VectorMutableThyra::free_sub_vector(
00165   RTOpPack::SubVector* sub_vec
00166   ) const
00167 {
00168   RTOpPack::ConstSubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00169   thyra_vec_->releaseDetachedView(&_sub_vec);
00170   *sub_vec = _sub_vec;
00171 }
00172 
00173 // Methods overridden from VectorMutable
00174 
00175 void VectorMutableThyra::get_sub_vector( const Range1D& rng, RTOpPack::MutableSubVector* sub_vec  )
00176 {
00177   RTOpPack::SubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00178   thyra_vec_->acquireDetachedView(convert(rng),&_sub_vec);
00179   *sub_vec = _sub_vec;
00180 }
00181 
00182 void VectorMutableThyra::commit_sub_vector( RTOpPack::MutableSubVector* sub_vec )
00183 {
00184   RTOpPack::SubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00185   thyra_vec_->commitDetachedView(&_sub_vec);
00186   *sub_vec = _sub_vec;
00187   this->has_changed();
00188 }
00189 
00190 void VectorMutableThyra::set_sub_vector( const RTOpPack::SparseSubVector& sub_vec )
00191 {
00192   thyra_vec_->setSubVector(sub_vec);
00193   this->has_changed();
00194 }
00195 
00196 void VectorMutableThyra::Vp_StMtV(
00197   value_type                       alpha
00198   ,const GenPermMatrixSlice        &P
00199   ,BLAS_Cpp::Transp                P_trans
00200   ,const Vector                    &x
00201   ,value_type                      beta
00202   )
00203 {
00204 #ifdef TEUCHOS_DEBUG
00205   TEST_FOR_EXCEPT(!(space().is_in_core()));
00206 #endif
00207   VectorDenseMutableEncap  y_de(*this);
00208   VectorDenseEncap  x_de(x);
00209   AbstractLinAlgPack::Vp_StMtV( &y_de(), alpha, P, P_trans, x_de(), beta );
00210 }
00211 
00212 } // end namespace AbstractLinAlgPack

Generated on Tue Jul 13 09:30:50 2010 for MOOCHO (Single Doxygen Collection) by  doxygen 1.4.7