AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects Version of the Day
AbstractLinAlgPack_VectorMutableDense.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 // 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 <typeinfo>
00043 #include <stdexcept>
00044 
00045 #include "AbstractLinAlgPack_VectorMutableDense.hpp"
00046 #include "AbstractLinAlgPack_VectorDenseEncap.hpp"
00047 #include "AbstractLinAlgPack_GenPermMatrixSliceOp.hpp"
00048 #include "AbstractLinAlgPack_apply_op_helper.hpp"
00049 #include "ReleaseResource_ref_count_ptr.hpp"
00050 #include "Teuchos_Workspace.hpp"
00051 #include "Teuchos_Assert.hpp"
00052 
00053 #ifdef TEUCHOS_DEBUG
00054 #define CLASS_MEMBER_PTRS \
00055 const VectorMutableDense   *_this = this; \
00056 const DVectorSlice                *_v; \
00057 const release_resource_ptr_t     *_v_release; \
00058 const VectorSpaceSerial          *_space;
00059 #else
00060 #define CLASS_MEMBER_PTRS
00061 #endif
00062 
00063 namespace AbstractLinAlgPack {
00064 
00065 VectorMutableDense::VectorMutableDense(
00066   const size_type                   dim
00067   )
00068   :space_(dim)
00069 {
00070   CLASS_MEMBER_PTRS
00071   this->initialize(dim);
00072 }
00073 
00074 VectorMutableDense::VectorMutableDense(
00075   DVectorSlice                        v
00076   ,const release_resource_ptr_t&     v_release
00077   )
00078   :space_(v.dim())
00079 {
00080   CLASS_MEMBER_PTRS
00081   this->initialize(v,v_release);
00082 }
00083 
00084 void VectorMutableDense::initialize(
00085   const size_type                   dim
00086   )
00087 {
00088   CLASS_MEMBER_PTRS
00089   namespace rcp = MemMngPack;
00090   namespace rmp = MemMngPack;
00091   typedef Teuchos::RCP<DVector> vec_ptr_t;
00092   vec_ptr_t vec_ptr = Teuchos::rcp(new DVector(dim));
00093   this->initialize(
00094     (*vec_ptr)()
00095     ,Teuchos::rcp(
00096       new rmp::ReleaseResource_ref_count_ptr<DVector>(
00097         vec_ptr
00098         )
00099       )
00100     );
00101 }
00102 
00103 void VectorMutableDense::initialize(
00104   DVectorSlice                       v
00105   ,const release_resource_ptr_t&     v_release
00106   )
00107 {
00108   CLASS_MEMBER_PTRS
00109   v_.bind(v);
00110   v_release_ = v_release;
00111   space_.initialize(v.dim());
00112   this->has_changed();
00113 }
00114 
00115 // Overridden from Vector
00116 
00117 const VectorSpace& VectorMutableDense::space() const
00118 {
00119   CLASS_MEMBER_PTRS
00120   return space_;
00121 }
00122 
00123 void VectorMutableDense::apply_op(
00124   const RTOpPack::RTOp& op
00125   ,const size_t num_vecs,      const Vector*  vecs[]
00126   ,const size_t num_targ_vecs, VectorMutable* targ_vecs[]
00127   ,RTOpPack::ReductTarget *reduct_obj
00128   ,const index_type first_ele_in, const index_type sub_dim_in, const index_type global_offset_in
00129   ) const
00130 {
00131   CLASS_MEMBER_PTRS
00132 #ifdef TEUCHOS_DEBUG
00133   AbstractLinAlgPack::apply_op_validate_input(
00134     "VectorMutableDense::apply_op(...)"
00135     ,op,num_vecs,vecs,num_targ_vecs,targ_vecs,reduct_obj,first_ele_in,sub_dim_in,global_offset_in
00136     );
00137 #endif
00138   this->apply_op_serial(
00139     op,num_vecs,vecs,num_targ_vecs,targ_vecs,reduct_obj
00140     ,first_ele_in,sub_dim_in,global_offset_in
00141     );
00142 }
00143 
00144 index_type VectorMutableDense::dim() const
00145 {
00146   return v_.dim();
00147 }
00148 
00149 value_type VectorMutableDense::get_ele(index_type i) const
00150 {
00151   return v_(i);
00152 }
00153 
00154 void VectorMutableDense::get_sub_vector(
00155   const Range1D& rng_in, RTOpPack::SubVector* sub_vec
00156   ) const
00157 {
00158   CLASS_MEMBER_PTRS
00159   const size_type  this_dim = v_.dim();
00160   const Range1D    rng = RangePack::full_range(rng_in,1,this_dim);
00161   TEUCHOS_TEST_FOR_EXCEPTION(
00162     rng.ubound() > this_dim, std::out_of_range
00163     ,"VectorMutableDense::get_sub_vector(...) : Error, "
00164     "rng = ["<<rng.lbound()<<","<<rng.ubound()<<"] "
00165     "is not in the range [1,this->dim()] = [1," << this_dim << "]!" );
00166   // Just return the dense view regardless of spare_or_dense argument
00167   sub_vec->initialize(
00168     rng.lbound()-1                             // global_offset
00169     ,rng.size()                                // sub_dim
00170     ,v_.raw_ptr()+v_.stride()*(rng.lbound()-1) // values
00171     ,v_.stride()
00172     );
00173 }
00174 
00175 void VectorMutableDense::free_sub_vector( RTOpPack::SubVector* sub_vec ) const
00176 {
00177   sub_vec->set_uninitialized(); // No memory to deallocate!
00178 }
00179 
00180 // Overridden from VectorMutable
00181 
00182 VectorMutable&
00183 VectorMutableDense::operator=(value_type alpha)
00184 {
00185   CLASS_MEMBER_PTRS
00186   v_ = alpha;
00187   this->has_changed();
00188   return *this;
00189 }
00190 
00191 VectorMutable&
00192 VectorMutableDense::operator=(const Vector& v)
00193 {
00194   CLASS_MEMBER_PTRS
00195   if( const VectorMutableDense *vp = dynamic_cast<const VectorMutableDense*>(&v) )
00196     v_ = vp->v_;
00197   else
00198     return VectorMutable::operator=(v); // Try the default implementation?
00199   this->has_changed();
00200   return *this;
00201 }
00202 
00203 VectorMutable&
00204 VectorMutableDense::operator=(const VectorMutable& v)
00205 {
00206   CLASS_MEMBER_PTRS
00207   if( const VectorMutableDense *vp = dynamic_cast<const VectorMutableDense*>(&v) )
00208     v_ = vp->v_;
00209   else
00210     return VectorMutable::operator=(v); // Try the default implementation?
00211   this->has_changed();
00212   return *this;
00213 }
00214 
00215 void VectorMutableDense::set_ele( index_type i, value_type val )
00216 {
00217   CLASS_MEMBER_PTRS
00218   v_(i) = val;
00219   this->has_changed();
00220 }
00221 
00222 VectorMutableDense::vec_mut_ptr_t
00223 VectorMutableDense::sub_view( const Range1D& rng_in )
00224 {
00225   CLASS_MEMBER_PTRS
00226   namespace rcp = MemMngPack;
00227   const size_type this_dim = this->dim();
00228   const Range1D rng = RangePack::full_range( rng_in, 1, this_dim );
00229 #ifdef TEUCHOS_DEBUG
00230   TEUCHOS_TEST_FOR_EXCEPTION(
00231     rng.ubound() > this_dim, std::out_of_range
00232     ,"VectorMutableDense::sub_view(...) : Error, "
00233     "rng = ["<<rng.lbound()<<","<<rng.ubound()<<"] "
00234     "is not in the range [1,this->dim()] = [1," << this_dim << "]!" );
00235 #endif
00236   if( rng == Range1D(1,this_dim) )
00237     return Teuchos::rcp( this, false );
00238   this->has_changed(); // This will result in a change in the vector
00239   return Teuchos::rcp( new VectorMutableDense( v_(rng), Teuchos::null ) ); 
00240 }
00241 
00242 void VectorMutableDense::get_sub_vector(
00243   const Range1D& rng_in, RTOpPack::MutableSubVector* sub_vec )
00244 {
00245   CLASS_MEMBER_PTRS
00246   const size_type  this_dim = v_.dim();
00247   const Range1D    rng = RangePack::full_range(rng_in,1,this_dim);
00248 #ifdef TEUCHOS_DEBUG
00249   TEUCHOS_TEST_FOR_EXCEPTION(
00250     rng.ubound() > this_dim, std::out_of_range
00251     ,"VectorMutableDense::get_sub_vector(...) : Error, "
00252     "rng = ["<<rng.lbound()<<","<<rng.ubound()<<"] "
00253     "is not in the range [1,this->dim()] = [1," << this_dim << "]!" );
00254 #endif
00255   sub_vec->initialize(
00256     rng.lbound()-1                             // global_offset
00257     ,rng.size()                                // sub_dim
00258     ,v_.raw_ptr()+v_.stride()*(rng.lbound()-1) // values
00259     ,v_.stride()
00260     );
00261 }
00262 
00263 void VectorMutableDense::commit_sub_vector( RTOpPack::MutableSubVector* sub_vec )
00264 {
00265   CLASS_MEMBER_PTRS
00266   sub_vec->set_uninitialized(); // No memory to deallocate!
00267   this->has_changed();          // Be aware of any final changes!
00268 }
00269 
00270 void VectorMutableDense::set_sub_vector( const RTOpPack::SparseSubVector& sub_vec )
00271 {
00272   CLASS_MEMBER_PTRS
00273   VectorMutable::set_sub_vector(sub_vec); // ToDo: Provide specialized implementation?
00274 }
00275 
00276 void VectorMutableDense::Vp_StMtV(
00277   value_type                       alpha
00278   ,const GenPermMatrixSlice        &P
00279   ,BLAS_Cpp::Transp                P_trans
00280   ,const Vector              &x
00281   ,value_type                      beta
00282   )
00283 {
00284   CLASS_MEMBER_PTRS
00285   VectorDenseEncap  x_de(x);
00286   AbstractLinAlgPack::Vp_StMtV( &v_, alpha, P, P_trans, x_de(), beta );
00287 }
00288 
00289 } // end namespace AbstractLinAlgPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends