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