AbstractLinAlgPack_VectorMutableThyra.cpp

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 "Teuchos_TestForException.hpp"
00036 #include "Teuchos_Workspace.hpp"
00037 #include "Teuchos_dyn_cast.hpp"
00038 #include "Teuchos_as.hpp"
00039 
00040 namespace AbstractLinAlgPack {
00041 
00042 // Constructors / Initializers
00043 
00044 VectorMutableThyra::VectorMutableThyra()
00045 {}
00046 
00047 VectorMutableThyra::VectorMutableThyra(
00048   const Teuchos::RCP<Thyra::VectorBase<value_type> >& thyra_vec
00049   )
00050 {
00051   this->initialize(thyra_vec);
00052 }
00053 
00054 void VectorMutableThyra::initialize(
00055   const Teuchos::RCP<Thyra::VectorBase<value_type> >& thyra_vec
00056   )
00057 {
00058   namespace mmp = MemMngPack;
00059   TEST_FOR_EXCEPTION(
00060     thyra_vec.get()==NULL, std::invalid_argument
00061     ,"VectorMutableThyra::initialize(thyra_vec): Error!"
00062     );
00063   thyra_vec_ = thyra_vec;
00064   space_.initialize(thyra_vec->space());
00065   this->has_changed();
00066 }
00067 
00068 Teuchos::RCP<Thyra::VectorBase<value_type> > 
00069 VectorMutableThyra::set_uninitialized()
00070 {
00071   Teuchos::RCP<Thyra::VectorBase<value_type> > tmp_thyra_vec = thyra_vec_;
00072   thyra_vec_ = Teuchos::null;
00073   space_.set_uninitialized();
00074   this->has_changed();
00075   return tmp_thyra_vec;
00076 }
00077 
00078 // Methods overridden from Vector
00079 
00080 const VectorSpace&
00081 VectorMutableThyra::space() const
00082 {
00083   return space_;
00084 }
00085 
00086 void VectorMutableThyra::apply_op(
00087   const RTOpPack::RTOp       &op
00088   ,const size_t              num_vecs
00089   ,const Vector*             vecs[]
00090   ,const size_t              num_targ_vecs
00091   ,VectorMutable*            targ_vecs[]
00092   ,RTOpPack::ReductTarget    *reduct_obj
00093   ,const index_type          first_ele
00094   ,const index_type          sub_dim
00095   ,const index_type          global_offset
00096   ) const
00097 {
00098   using Teuchos::as;
00099   using Teuchos::dyn_cast;
00100   namespace mmp = MemMngPack;
00101   using Teuchos::Workspace;
00102   Teuchos::WorkspaceStore* wss = Teuchos::get_default_workspace_store().get();
00103   // If these are in-core vectors then just let a default implementation
00104   // take care of this.
00105   if( space_.is_in_core() ) {
00106     this->apply_op_serial(
00107       op,num_vecs,vecs,num_targ_vecs,targ_vecs,reduct_obj
00108       ,first_ele,sub_dim,global_offset
00109       );
00110     return;
00111   }
00112   // Convert the non-mutable vectors into non-mutable Thyra vectors
00113   Workspace< Teuchos::RCP<const Thyra::VectorBase<value_type> > > thyra_vecs_sptr(wss,num_vecs);
00114   Workspace<const Thyra::VectorBase<value_type>*> thyra_vecs(wss,num_vecs);
00115   for(int k = 0; k < as<int>(num_vecs); ++k ) {
00116     get_thyra_vector( space_, *vecs[k], &thyra_vecs_sptr[k] );
00117     thyra_vecs[k] = &*thyra_vecs_sptr[k];
00118   }
00119   // Convert the mutable vetors into mutable Thyra vectors
00120   Workspace< Teuchos::RCP<Thyra::VectorBase<value_type> > > targ_thyra_vecs_sptr(wss,num_targ_vecs);
00121   Workspace<Thyra::VectorBase<value_type>*> targ_thyra_vecs(wss,num_targ_vecs);
00122   for(int k = 0; k < as<int>(num_targ_vecs); ++k ) {
00123     get_thyra_vector( space_, targ_vecs[k], &targ_thyra_vecs_sptr[k] );
00124     targ_thyra_vecs[k] = &*targ_thyra_vecs_sptr[k];
00125   }
00126   // Call the Thyra::apply_op(...)
00127   Thyra::applyOp(
00128     op
00129     ,num_vecs,      num_vecs      ? &thyra_vecs[0]      : NULL
00130     ,num_targ_vecs, num_targ_vecs ? &targ_thyra_vecs[0] : NULL
00131     ,reduct_obj
00132     ,first_ele-1,sub_dim==0?-1:sub_dim,global_offset
00133     );
00134   // Free/commit the Thyra vector views
00135   for(int k = 0; k < as<int>(num_vecs); ++k ) {
00136     free_thyra_vector( space_, *vecs[k], &thyra_vecs_sptr[k] );
00137   }
00138   for(int k = 0; k < as<int>(num_targ_vecs); ++k ) {
00139     commit_thyra_vector( space_, targ_vecs[k], &targ_thyra_vecs_sptr[k] );
00140   }
00141 }
00142 
00143 index_type VectorMutableThyra::dim() const
00144 {
00145   return space_.dim();
00146 }
00147 
00148 void VectorMutableThyra::get_sub_vector(
00149   const Range1D& rng, RTOpPack::SubVector* sub_vec
00150   ) const
00151 {
00152   RTOpPack::ConstSubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00153   thyra_vec_->acquireDetachedView(convert(rng),&_sub_vec);
00154   *sub_vec = _sub_vec;
00155 }
00156 
00157 void VectorMutableThyra::free_sub_vector(
00158   RTOpPack::SubVector* sub_vec
00159   ) const
00160 {
00161   RTOpPack::ConstSubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00162   thyra_vec_->releaseDetachedView(&_sub_vec);
00163   *sub_vec = _sub_vec;
00164 }
00165 
00166 // Methods overridden from VectorMutable
00167 
00168 void VectorMutableThyra::get_sub_vector( const Range1D& rng, RTOpPack::MutableSubVector* sub_vec  )
00169 {
00170   RTOpPack::SubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00171   thyra_vec_->acquireDetachedView(convert(rng),&_sub_vec);
00172   *sub_vec = _sub_vec;
00173 }
00174 
00175 void VectorMutableThyra::commit_sub_vector( RTOpPack::MutableSubVector* sub_vec )
00176 {
00177   RTOpPack::SubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00178   thyra_vec_->commitDetachedView(&_sub_vec);
00179   *sub_vec = _sub_vec;
00180   this->has_changed();
00181 }
00182 
00183 void VectorMutableThyra::set_sub_vector( const RTOpPack::SparseSubVector& sub_vec )
00184 {
00185   thyra_vec_->setSubVector(sub_vec);
00186   this->has_changed();
00187 }
00188 
00189 void VectorMutableThyra::Vp_StMtV(
00190   value_type                       alpha
00191   ,const GenPermMatrixSlice        &P
00192   ,BLAS_Cpp::Transp                P_trans
00193   ,const Vector                    &x
00194   ,value_type                      beta
00195   )
00196 {
00197 #ifdef TEUCHOS_DEBUG
00198   TEST_FOR_EXCEPT(!(space().is_in_core()));
00199 #endif
00200   VectorDenseMutableEncap  y_de(*this);
00201   VectorDenseEncap  x_de(x);
00202   AbstractLinAlgPack::Vp_StMtV( &y_de(), alpha, P, P_trans, x_de(), beta );
00203 }
00204 
00205 } // end namespace AbstractLinAlgPack

Generated on Wed May 12 21:51:33 2010 for MOOCHO/Thyra Adapter Software by  doxygen 1.4.7