AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects Version of the Day
AbstractLinAlgPack_VectorSubView.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 <assert.h>
00043 
00044 #include <stdexcept>
00045 
00046 #include "AbstractLinAlgPack_VectorMutableSubView.hpp"
00047 #include "Teuchos_Assert.hpp"
00048 #include "Teuchos_Workspace.hpp"
00049 #include "Teuchos_dyn_cast.hpp"
00050 
00051 namespace AbstractLinAlgPack {
00052 
00053 VectorSubView::VectorSubView( const vec_ptr_t& vec, const Range1D& rng )
00054 {
00055   initialize(vec,rng);
00056 }
00057 
00058 void VectorSubView::initialize( const vec_ptr_t& vec, const Range1D& rng )
00059 {
00060   namespace rcp = MemMngPack;
00061 #ifdef TEUCHOS_DEBUG
00062   TEUCHOS_TEST_FOR_EXCEPTION(
00063     vec.get() == NULL, std::invalid_argument
00064     ,"VectorSubView::initialize(...) : Error!" );
00065 #endif
00066   typedef VectorSpace::space_ptr_t   space_ptr_t;
00067   space_.initialize( Teuchos::rcp(&vec->space(),false), rng );
00068   full_vec_ = vec;
00069   this->has_changed();
00070 }
00071 
00072 void VectorSubView::set_uninitialized()
00073 {
00074   space_.set_uninitialized();
00075   full_vec_ = Teuchos::null;
00076   this->has_changed();
00077 }
00078 
00079 // Overridden from Vector
00080 
00081 const VectorSpace& VectorSubView::space() const
00082 {
00083   return space_;
00084 }
00085 
00086 index_type VectorSubView::dim() const
00087 {
00088   return space_.dim();
00089 }
00090 
00091 void VectorSubView::apply_op(
00092   const RTOpPack::RTOp& op
00093   ,const size_t num_vecs, const Vector* vecs[]
00094   ,const size_t num_targ_vecs, VectorMutable* targ_vecs[]
00095   ,RTOpPack::ReductTarget* reduct_obj
00096   ,const index_type first_ele_in, const index_type sub_dim_in, const index_type global_offset_in
00097   ) const
00098 {
00099   using Teuchos::dyn_cast;
00100   using Teuchos::Workspace;
00101   Teuchos::WorkspaceStore* wss = Teuchos::get_default_workspace_store().get();
00102   // If these are in-core vectors then just let a default implementation
00103   // take care of this.
00104   if( this->space().is_in_core() ) {
00105     this->apply_op_serial(
00106       op,num_vecs,vecs,num_targ_vecs,targ_vecs,reduct_obj
00107       ,first_ele_in,sub_dim_in,global_offset_in
00108       );
00109     return;
00110   }
00111   // These are not in-core vectors so ...
00112   int k;
00113   const index_type this_dim = this->dim();
00114 #ifdef TEUCHOS_DEBUG
00115   TEUCHOS_TEST_FOR_EXCEPTION(
00116     sub_dim_in < 0
00117     || !(1 <= first_ele_in && first_ele_in <= this_dim)
00118     || ( sub_dim_in > 0 && (sub_dim_in - (first_ele_in - 1) > this_dim) )
00119     , std::logic_error
00120     ,"VectorSubView::apply_op(...): Error, first_ele_in = "
00121     << first_ele_in << ", global_offset_in = " << global_offset_in
00122     << ", sub_dim_in = " << sub_dim_in << " and this->dim() = this_dim  = "
00123     << this_dim << " are not compatible." );
00124 #endif
00125   const index_type this_offset = space_impl().rng().lbound() - 1;
00126   const index_type
00127     this_sub_dim = ( sub_dim_in 
00128              ? sub_dim_in
00129              : space_impl().rng().size() - (first_ele_in - 1)
00130                  );
00131   Workspace<const Vector*>    vecs_full(wss,num_vecs);
00132   for( k = 0; k < num_vecs; ++k )
00133     vecs_full[k] = dyn_cast<const VectorSubView>(*vecs[k]).full_vec().get();
00134   Workspace<VectorMutable*>   targ_vecs_full(wss,num_targ_vecs);
00135   for( k = 0; k < num_targ_vecs; ++k )
00136     targ_vecs_full[k] = dyn_cast<VectorMutableSubView>(*targ_vecs[k]).full_vec().get();
00137   AbstractLinAlgPack::apply_op(
00138     op
00139     ,num_vecs,      num_vecs      ? &vecs_full[0]      : NULL
00140     ,num_targ_vecs, num_targ_vecs ? &targ_vecs_full[0] : NULL
00141     ,reduct_obj
00142     ,this_offset + first_ele_in     // first_ele
00143     ,this_sub_dim                   // sub_dim
00144     ,global_offset_in               // global_dim
00145     );
00146 }
00147 
00148 value_type VectorSubView::get_ele(index_type i) const
00149 {
00150   space_.validate_range(Range1D(i,i));
00151   return full_vec_->get_ele( space_.rng().lbound() + i - 1 );
00152 }
00153 
00154 Vector::vec_ptr_t
00155 VectorSubView::sub_view( const Range1D& rng_in ) const
00156 {
00157   namespace rcp = MemMngPack;
00158   const index_type this_dim = this->dim();
00159   const Range1D rng = RangePack::full_range(rng_in,1,this_dim);
00160   space_.validate_range(rng);
00161   const index_type this_offset = space_.rng().lbound() - 1;
00162   return Teuchos::rcp(
00163     new VectorSubView(
00164       full_vec_
00165       ,Range1D( 
00166         this_offset  + rng.lbound()
00167         ,this_offset + rng.ubound() )
00168       ) );
00169 }
00170 
00171 void VectorSubView::get_sub_vector( const Range1D& rng_in, RTOpPack::SubVector* sub_vec ) const
00172 {
00173 #ifdef TEUCHOS_DEBUG
00174   TEUCHOS_TEST_FOR_EXCEPTION( !sub_vec, std::logic_error, "VectorSubView::get_sub_vector(...): Error!" );
00175 #endif
00176   const index_type this_dim = this->dim();
00177   const Range1D rng = RangePack::full_range(rng_in,1,this_dim);
00178   space_.validate_range(rng);
00179   const index_type this_offset = space_.rng().lbound() - 1;
00180   full_vec_->get_sub_vector( rng + this_offset, sub_vec );
00181   sub_vec->setGlobalOffset( sub_vec->globalOffset() - this_offset );
00182 }
00183 
00184 void VectorSubView::free_sub_vector( RTOpPack::SubVector* sub_vec ) const
00185 {
00186 #ifdef TEUCHOS_DEBUG
00187   TEUCHOS_TEST_FOR_EXCEPTION( !sub_vec, std::logic_error, "VectorSubView::free_sub_vector(...): Error!" );
00188 #endif
00189   const index_type this_offset = space_.rng().lbound() - 1;
00190   sub_vec->setGlobalOffset( sub_vec->globalOffset() + this_offset );
00191   full_vec_->free_sub_vector( sub_vec );
00192 }
00193 
00194 } // end namespace AbstractLinAlgPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends