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

Generated on Tue Oct 20 12:51:44 2009 for MOOCHO (Single Doxygen Collection) by doxygen 1.4.7