|
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 // 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_MultiVectorMutableDense.hpp" 00045 #include "AbstractLinAlgPack_VectorMutableDense.hpp" 00046 #include "AbstractLinAlgPack_MatrixSymOpGetGMSSymMutable.hpp" 00047 #include "AbstractLinAlgPack_SpVectorOp.hpp" 00048 #include "AbstractLinAlgPack_LinAlgOpPackHack.hpp" 00049 #include "DenseLinAlgPack_DMatrixOut.hpp" 00050 #include "ReleaseResource_ref_count_ptr.hpp" 00051 #include "Teuchos_Workspace.hpp" 00052 #include "Teuchos_Assert.hpp" 00053 00054 namespace AbstractLinAlgPack { 00055 00056 // Constructors / initializers 00057 00058 MultiVectorMutableDense::MultiVectorMutableDense( 00059 const size_type rows 00060 ,const size_type cols 00061 ) 00062 { 00063 this->initialize(rows,cols); 00064 } 00065 00066 MultiVectorMutableDense::MultiVectorMutableDense( 00067 DMatrixSlice gms 00068 ,BLAS_Cpp::Transp gms_trans 00069 ,const release_resource_ptr_t& gms_release 00070 ) 00071 { 00072 this->initialize(gms,gms_trans,gms_release); 00073 } 00074 00075 void MultiVectorMutableDense::initialize( 00076 const size_type rows 00077 ,const size_type cols 00078 ) 00079 { 00080 namespace rcp = MemMngPack; 00081 namespace rmp = MemMngPack; 00082 typedef Teuchos::RCP<DMatrix> vec_ptr_t; 00083 vec_ptr_t gms_ptr = Teuchos::rcp(new DMatrix(rows,cols)); 00084 this->initialize( 00085 (*gms_ptr)() 00086 ,BLAS_Cpp::no_trans 00087 ,Teuchos::rcp( 00088 new rmp::ReleaseResource_ref_count_ptr<DMatrix>( 00089 gms_ptr 00090 ) 00091 ) 00092 ); 00093 } 00094 00095 void MultiVectorMutableDense::initialize( 00096 DMatrixSlice gms 00097 ,BLAS_Cpp::Transp gms_trans 00098 ,const release_resource_ptr_t& gms_release 00099 ) 00100 { 00101 gms_.bind(gms); 00102 gms_trans_ = gms_trans; 00103 gms_release_ = gms_release; 00104 } 00105 00106 // Overridden from MatrixOpGetGMS 00107 00108 const DMatrixSlice MultiVectorMutableDense::get_gms_view() const 00109 { 00110 if(gms_trans_ == BLAS_Cpp::trans) { 00111 TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: We need to create a copy and transpose it! 00112 } 00113 return get_gms(); // No memory to allocate! 00114 } 00115 00116 void MultiVectorMutableDense::free_gms_view(const DMatrixSlice* gms_view) const 00117 { 00118 if(gms_trans_ == BLAS_Cpp::trans) { 00119 TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: We need to free the copy that we created in get_gms_view() 00120 } 00121 else { 00122 // Nothing to free! 00123 } 00124 } 00125 00126 // Overridden from MatrixOpGetGMSMutable 00127 00128 DMatrixSlice MultiVectorMutableDense::get_gms_view() 00129 { 00130 if(gms_trans_ == BLAS_Cpp::trans) { 00131 TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: We need to create a copy and transpose it! 00132 } 00133 return set_gms(); // No memory to allocate! 00134 } 00135 00136 void MultiVectorMutableDense::commit_gms_view(DMatrixSlice* gms_view) 00137 { 00138 if(gms_trans_ == BLAS_Cpp::trans) { 00139 TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: We need to free the copy that we created in get_gms_view() 00140 } 00141 else { 00142 // Nothing to free! 00143 } 00144 } 00145 00146 // Overridden from MultiVector 00147 00148 MultiVectorMutableDense::access_by_t 00149 MultiVectorMutableDense::access_by() const 00150 { 00151 return ROW_ACCESS | COL_ACCESS | DIAG_ACCESS; // row, column and diagonal access is available! 00152 } 00153 00154 // Overridden from MultiVectorMutable 00155 00156 MultiVectorMutableDense::vec_mut_ptr_t 00157 MultiVectorMutableDense::col(index_type j) 00158 { 00159 namespace rcp = MemMngPack; 00160 return Teuchos::rcp( 00161 new VectorMutableDense( 00162 DenseLinAlgPack::col( set_gms(), gms_trans(), j ) 00163 ,Teuchos::null ) ); 00164 } 00165 00166 MultiVectorMutableDense::vec_mut_ptr_t 00167 MultiVectorMutableDense::row(index_type i) 00168 { 00169 namespace rcp = MemMngPack; 00170 return Teuchos::rcp( 00171 new VectorMutableDense( 00172 DenseLinAlgPack::row( set_gms(), gms_trans(), i ) 00173 ,Teuchos::null ) ); 00174 } 00175 00176 MultiVectorMutableDense::vec_mut_ptr_t 00177 MultiVectorMutableDense::diag(int k) 00178 { 00179 namespace rcp = MemMngPack; 00180 return Teuchos::rcp( 00181 new VectorMutableDense( 00182 gms_.diag( gms_trans() == BLAS_Cpp::no_trans ? k : -k ) 00183 ,Teuchos::null ) ); 00184 } 00185 00186 MultiVectorMutableDense::multi_vec_mut_ptr_t 00187 MultiVectorMutableDense::mv_sub_view(const Range1D& row_rng, const Range1D& col_rng) 00188 { 00189 namespace rcp = MemMngPack; 00190 return Teuchos::rcp( 00191 new MultiVectorMutableDense( 00192 gms_( 00193 gms_trans() == BLAS_Cpp::no_trans ? row_rng : col_rng 00194 ,gms_trans() == BLAS_Cpp::no_trans ? col_rng : row_rng ) 00195 ,gms_trans() 00196 ,Teuchos::null ) ); 00197 } 00198 00199 // Overridden from MatrixBase 00200 00201 size_type MultiVectorMutableDense::rows() const 00202 { 00203 return BLAS_Cpp::rows( get_gms().rows(), get_gms().cols(), gms_trans() ); 00204 } 00205 00206 size_type MultiVectorMutableDense::cols() const 00207 { 00208 return BLAS_Cpp::cols( get_gms().rows(), get_gms().cols(), gms_trans() ); 00209 } 00210 00211 // Overridden from MatrixOp 00212 00213 void MultiVectorMutableDense::zero_out() 00214 { 00215 gms_ = 0.0; 00216 } 00217 00218 void MultiVectorMutableDense::Mt_S( value_type alpha ) 00219 { 00220 DenseLinAlgPack::Mt_S(&gms_,alpha); 00221 } 00222 00223 MatrixOp& MultiVectorMutableDense::operator=(const MatrixOp& mwo_rhs) 00224 { 00225 DenseLinAlgPack::assign( &set_gms(), MatrixDenseEncap(mwo_rhs)(), gms_trans() ); 00226 return *this; 00227 } 00228 00229 std::ostream& MultiVectorMutableDense::output(std::ostream& out) const 00230 { 00231 if(gms_trans() == BLAS_Cpp::no_trans) 00232 return out << gms_; 00233 return MatrixOpSerial::output(out); 00234 } 00235 00236 bool MultiVectorMutableDense::Mp_StM( 00237 MatrixOp* mwo_lhs, value_type alpha 00238 ,BLAS_Cpp::Transp trans_rhs 00239 ) const 00240 { 00241 return MultiVectorMutable::Mp_StM(mwo_lhs,alpha,trans_rhs); 00242 } 00243 00244 bool MultiVectorMutableDense::Mp_StM( 00245 value_type alpha,const MatrixOp& M_rhs, BLAS_Cpp::Transp trans_rhs 00246 ) 00247 { 00248 return MultiVectorMutable::Mp_StM(alpha,M_rhs,trans_rhs); 00249 } 00250 00251 bool MultiVectorMutableDense::syrk( 00252 BLAS_Cpp::Transp M_trans, value_type alpha 00253 ,value_type beta, MatrixSymOp* sym_lhs 00254 ) const 00255 { 00256 #ifdef TEUCHOS_DEBUG 00257 TEUCHOS_TEST_FOR_EXCEPTION( 00258 sym_lhs == NULL, std::invalid_argument 00259 ,"MultiVectorMutableDense::syrk(...) : Error!" ); 00260 #endif 00261 MatrixSymOpGetGMSSymMutable 00262 *sym_get_lhs = dynamic_cast<MatrixSymOpGetGMSSymMutable*>(sym_lhs); 00263 if(!sym_get_lhs) 00264 return false; 00265 MatrixDenseSymMutableEncap sym_gms_lhs(sym_get_lhs); 00266 DenseLinAlgPack::syrk( M_trans, alpha, get_gms(), beta, &sym_gms_lhs() ); 00267 return true; 00268 } 00269 00270 bool MultiVectorMutableDense::Mp_StMtM( 00271 MatrixOp* mwo_lhs, value_type alpha 00272 ,const MatrixOp& mwo_rhs1, BLAS_Cpp::Transp trans_rhs1 00273 ,BLAS_Cpp::Transp trans_rhs2 00274 ,value_type beta ) const 00275 { 00276 if(MultiVector::Mp_StMtM(mwo_lhs,alpha,mwo_rhs1,trans_rhs1,trans_rhs2,beta)) 00277 return true; 00278 return MatrixOpSerial::Mp_StMtM(mwo_lhs,alpha,mwo_rhs1,trans_rhs1,trans_rhs2,beta); 00279 } 00280 00281 bool MultiVectorMutableDense::Mp_StMtM( 00282 MatrixOp* mwo_lhs, value_type alpha 00283 ,BLAS_Cpp::Transp trans_rhs1 00284 ,const MatrixOp& mwo_rhs2, BLAS_Cpp::Transp trans_rhs2 00285 ,value_type beta ) const 00286 { 00287 if(MultiVector::Mp_StMtM(mwo_lhs,alpha,trans_rhs1,mwo_rhs2,trans_rhs2,beta)) 00288 return true; 00289 return MatrixOpSerial::Mp_StMtM(mwo_lhs,alpha,trans_rhs1,mwo_rhs2,trans_rhs2,beta); 00290 } 00291 00292 // Overridden from MatrixOpSerial 00293 00294 void MultiVectorMutableDense::Vp_StMtV( 00295 DVectorSlice* vs_lhs, value_type alpha, BLAS_Cpp::Transp trans_rhs1 00296 , const DVectorSlice& vs_rhs2, value_type beta) const 00297 { 00298 DenseLinAlgPack::Vp_StMtV( 00299 vs_lhs,alpha,gms_,BLAS_Cpp::trans_trans(gms_trans(),trans_rhs1),vs_rhs2,beta); 00300 } 00301 00302 void MultiVectorMutableDense::Vp_StMtV( 00303 DVectorSlice* vs_lhs, value_type alpha, BLAS_Cpp::Transp trans_rhs1 00304 , const SpVectorSlice& sv_rhs2, value_type beta) const 00305 { 00306 AbstractLinAlgPack::Vp_StMtV( 00307 vs_lhs,alpha,gms_,BLAS_Cpp::trans_trans(gms_trans(),trans_rhs1),sv_rhs2,beta); 00308 } 00309 00310 // protected 00311 00312 // Overridden from MultiVector 00313 00314 void MultiVectorMutableDense::apply_op( 00315 EApplyBy apply_by, const RTOpPack::RTOp& primary_op 00316 ,const size_t num_multi_vecs, const MultiVector** multi_vecs 00317 ,const size_t num_targ_multi_vecs, MultiVectorMutable** targ_multi_vecs 00318 ,RTOpPack::ReductTarget* reduct_objs[] 00319 ,const index_type primary_first_ele , const index_type primary_sub_dim , const index_type primary_global_offset 00320 ,const index_type secondary_first_ele , const index_type secondary_sub_dim 00321 ) const 00322 { 00323 MultiVector::apply_op( 00324 apply_by, primary_op, num_multi_vecs, multi_vecs, num_targ_multi_vecs, targ_multi_vecs 00325 ,reduct_objs 00326 ,primary_first_ele, primary_sub_dim, primary_global_offset, secondary_first_ele, secondary_sub_dim 00327 ); // ToDo: Provide Specialized implementation if needed? 00328 } 00329 00330 void MultiVectorMutableDense::apply_op( 00331 EApplyBy apply_by, const RTOpPack::RTOp& primary_op, const RTOpPack::RTOp& secondary_op 00332 ,const size_t num_multi_vecs, const MultiVector** multi_vecs 00333 ,const size_t num_targ_multi_vecs, MultiVectorMutable** targ_multi_vecs 00334 ,RTOpPack::ReductTarget *reduct_obj 00335 ,const index_type primary_first_ele , const index_type primary_sub_dim , const index_type primary_global_offset 00336 ,const index_type secondary_first_ele , const index_type secondary_sub_dim 00337 ) const 00338 { 00339 MultiVector::apply_op( 00340 apply_by, primary_op, secondary_op, num_multi_vecs, multi_vecs, num_targ_multi_vecs, targ_multi_vecs 00341 ,reduct_obj 00342 ,primary_first_ele, primary_sub_dim, primary_global_offset, secondary_first_ele, secondary_sub_dim 00343 ); // ToDo: Provide Specialized implementation if needed? 00344 } 00345 00346 } // end namespace AbstractLinAlgPack
1.7.6.1