MOOCHO (Single Doxygen Collection) Version of the Day
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include <assert.h>
00043 
00044 #include "AbstractLinAlgPack_VectorMutableThyra.hpp"
00045 #include "AbstractLinAlgPack_ThyraAccessors.hpp"
00046 #include "AbstractLinAlgPack_VectorDenseEncap.hpp"
00047 #include "AbstractLinAlgPack_GenPermMatrixSliceOp.hpp"
00048 #include "RTOpPack_RTOpSubRangeDecorator.hpp"
00049 #include "Teuchos_Assert.hpp"
00050 #include "Teuchos_Workspace.hpp"
00051 #include "Teuchos_dyn_cast.hpp"
00052 #include "Teuchos_as.hpp"
00053 
00054 namespace AbstractLinAlgPack {
00055 
00056 // Constructors / Initializers
00057 
00058 VectorMutableThyra::VectorMutableThyra()
00059 {}
00060 
00061 VectorMutableThyra::VectorMutableThyra(
00062   const Teuchos::RCP<Thyra::VectorBase<value_type> >& thyra_vec
00063   )
00064 {
00065   this->initialize(thyra_vec);
00066 }
00067 
00068 void VectorMutableThyra::initialize(
00069   const Teuchos::RCP<Thyra::VectorBase<value_type> >& thyra_vec
00070   )
00071 {
00072   namespace mmp = MemMngPack;
00073   TEUCHOS_TEST_FOR_EXCEPTION(
00074     thyra_vec.get()==NULL, std::invalid_argument
00075     ,"VectorMutableThyra::initialize(thyra_vec): Error!"
00076     );
00077   thyra_vec_ = thyra_vec;
00078   space_.initialize(thyra_vec->space());
00079   this->has_changed();
00080 }
00081 
00082 Teuchos::RCP<Thyra::VectorBase<value_type> > 
00083 VectorMutableThyra::set_uninitialized()
00084 {
00085   Teuchos::RCP<Thyra::VectorBase<value_type> > tmp_thyra_vec = thyra_vec_;
00086   thyra_vec_ = Teuchos::null;
00087   space_.set_uninitialized();
00088   this->has_changed();
00089   return tmp_thyra_vec;
00090 }
00091 
00092 // Methods overridden from Vector
00093 
00094 const VectorSpace&
00095 VectorMutableThyra::space() const
00096 {
00097   return space_;
00098 }
00099 
00100 void VectorMutableThyra::apply_op(
00101   const RTOpPack::RTOp       &op
00102   ,const size_t              num_vecs
00103   ,const Vector*             vecs[]
00104   ,const size_t              num_targ_vecs
00105   ,VectorMutable*            targ_vecs[]
00106   ,RTOpPack::ReductTarget    *reduct_obj
00107   ,const index_type          first_ele
00108   ,const index_type          sub_dim
00109   ,const index_type          global_offset
00110   ) const
00111 {
00112   using Teuchos::as;
00113   using Teuchos::dyn_cast;
00114   using Teuchos::Workspace;
00115   using Teuchos::rcpFromRef;
00116   using Teuchos::RCP;
00117   using Teuchos::Ptr;
00118   Teuchos::WorkspaceStore* wss = Teuchos::get_default_workspace_store().get();
00119 
00120   // If these are in-core vectors then just let a default implementation
00121   // take care of this.
00122   if( space_.is_in_core() ) {
00123     this->apply_op_serial(
00124       op,num_vecs,vecs,num_targ_vecs,targ_vecs,reduct_obj
00125       ,first_ele,sub_dim,global_offset
00126       );
00127     return;
00128   }
00129 
00130   // Convert the non-mutable vectors into non-mutable Thyra vectors
00131   Workspace< Teuchos::RCP<const Thyra::VectorBase<value_type> > > thyra_vecs_sptr(wss,num_vecs);
00132   Workspace<Ptr<const Thyra::VectorBase<value_type> > > thyra_vecs(wss,num_vecs);
00133   for(int k = 0; k < as<int>(num_vecs); ++k ) {
00134     get_thyra_vector( space_, *vecs[k], &thyra_vecs_sptr[k] );
00135     thyra_vecs[k] = thyra_vecs_sptr[k].ptr();
00136   }
00137 
00138   // Convert the mutable vetors into mutable Thyra vectors
00139   Workspace< Teuchos::RCP<Thyra::VectorBase<value_type> > > targ_thyra_vecs_sptr(wss,num_targ_vecs);
00140   Workspace<Ptr<Thyra::VectorBase<value_type> > > targ_thyra_vecs(wss,num_targ_vecs);
00141   for(int k = 0; k < as<int>(num_targ_vecs); ++k ) {
00142     get_thyra_vector( space_, targ_vecs[k], &targ_thyra_vecs_sptr[k] );
00143     targ_thyra_vecs[k] = targ_thyra_vecs_sptr[k].ptr();
00144   }
00145 
00146   // Call the Thyra::apply_op(...)
00147   RTOpPack::RTOpSubRangeDecorator<value_type>
00148     subRangeOp(rcpFromRef(op), first_ele-1, sub_dim==0 ? -1 : sub_dim);
00149   Thyra::applyOp<value_type>(subRangeOp, thyra_vecs(), targ_thyra_vecs(),
00150     Teuchos::ptr(reduct_obj), global_offset);
00151 
00152   // Free/commit the Thyra vector views
00153   for(int k = 0; k < as<int>(num_vecs); ++k ) {
00154     free_thyra_vector( space_, *vecs[k], &thyra_vecs_sptr[k] );
00155   }
00156   for(int k = 0; k < as<int>(num_targ_vecs); ++k ) {
00157     commit_thyra_vector( space_, targ_vecs[k], &targ_thyra_vecs_sptr[k] );
00158   }
00159 
00160 }
00161 
00162 
00163 index_type VectorMutableThyra::dim() const
00164 {
00165   return space_.dim();
00166 }
00167 
00168 void VectorMutableThyra::get_sub_vector(
00169   const Range1D& rng, RTOpPack::SubVector* sub_vec
00170   ) const
00171 {
00172   RTOpPack::ConstSubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00173   thyra_vec_->acquireDetachedView(convert(rng),&_sub_vec);
00174   *sub_vec = _sub_vec;
00175 }
00176 
00177 void VectorMutableThyra::free_sub_vector(
00178   RTOpPack::SubVector* sub_vec
00179   ) const
00180 {
00181   RTOpPack::ConstSubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00182   thyra_vec_->releaseDetachedView(&_sub_vec);
00183   *sub_vec = _sub_vec;
00184 }
00185 
00186 // Methods overridden from VectorMutable
00187 
00188 void VectorMutableThyra::get_sub_vector( const Range1D& rng, RTOpPack::MutableSubVector* sub_vec  )
00189 {
00190   RTOpPack::SubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00191   thyra_vec_->acquireDetachedView(convert(rng),&_sub_vec);
00192   *sub_vec = _sub_vec;
00193 }
00194 
00195 void VectorMutableThyra::commit_sub_vector( RTOpPack::MutableSubVector* sub_vec )
00196 {
00197   RTOpPack::SubVectorView<RTOp_value_type> _sub_vec = *sub_vec;
00198   thyra_vec_->commitDetachedView(&_sub_vec);
00199   *sub_vec = _sub_vec;
00200   this->has_changed();
00201 }
00202 
00203 void VectorMutableThyra::set_sub_vector( const RTOpPack::SparseSubVector& sub_vec )
00204 {
00205   thyra_vec_->setSubVector(sub_vec);
00206   this->has_changed();
00207 }
00208 
00209 void VectorMutableThyra::Vp_StMtV(
00210   value_type                       alpha
00211   ,const GenPermMatrixSlice        &P
00212   ,BLAS_Cpp::Transp                P_trans
00213   ,const Vector                    &x
00214   ,value_type                      beta
00215   )
00216 {
00217 #ifdef TEUCHOS_DEBUG
00218   TEUCHOS_TEST_FOR_EXCEPT(!(space().is_in_core()));
00219 #endif
00220   VectorDenseMutableEncap  y_de(*this);
00221   VectorDenseEncap  x_de(x);
00222   AbstractLinAlgPack::Vp_StMtV( &y_de(), alpha, P, P_trans, x_de(), beta );
00223 }
00224 
00225 } // end namespace AbstractLinAlgPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines