RTOp Package Browser (Single Doxygen Collection) Version of the Day
RTOpPack_TOpRandomize.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
00005 //       Operations
00006 //                Copyright (2006) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 
00043 #ifndef RTOPPACK_TOP_RANDOMIZE_HPP
00044 #define RTOPPACK_TOP_RANDOMIZE_HPP
00045 
00046 #include "RTOpPack_RTOpTHelpers.hpp"
00047 
00048 
00049 namespace RTOpPack {
00050 
00051 
00064 template<class Scalar>
00065 class TOpRandomize : public RTOpT<Scalar> {
00066 public:
00067   using RTOpT<Scalar>::apply_op;
00069   static void set_static_seed( const unsigned int static_seed )
00070     { static_seed_ = static_seed; }
00072   static unsigned int get_static_seed() { return static_seed_; }
00074   TOpRandomize(
00075     const Scalar& l = -ScalarTraits<Scalar>::one(),
00076     const Scalar& u = +ScalarTraits<Scalar>::one()
00077     )
00078     {
00079       this->setOpNameBase("TOpRandomize");
00080       set_bounds(l, u);
00081       set_seed(static_seed_);
00082       ++static_seed_; // By default we will just increment the seed!
00083     }
00085   void set_bounds( const Scalar& l, const Scalar& u )
00086     { l_ = l; u_ = u; }
00088   void set_seed( const unsigned int seed ) { seed_ = seed; }
00090   unsigned int get_seed() const { return seed_; }
00094   void apply_op_impl(
00095     const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
00096     const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
00097     const Ptr<ReductTarget> &reduct_obj_inout
00098     ) const
00099     {
00100       typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
00101       typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
00102 
00103 #ifdef TEUCHOS_DEBUG
00104       validate_apply_op<Scalar>(*this, 0, 1, false,
00105         sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
00106 #endif
00107       
00108       const index_type subDim = targ_sub_vecs[0].subDim();
00109       const index_type globalOffset =  targ_sub_vecs[0].globalOffset();
00110 
00111       iter_t z0_val = targ_sub_vecs[0].values().begin();
00112       const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
00113 
00114       // Linear coefficients for translating from [-1,+1] to [l,b]
00115       const Scalar a = Scalar(0.5)*(u_ - l_);
00116       const Scalar b = Scalar(0.5)*(u_ + l_);
00117       for( index_type i = 0; i < subDim; ++i, z0_val += z0_s )
00118       {
00119         Teuchos::ScalarTraits<Scalar>::seedrandom(seed_+globalOffset+i);
00120         *z0_val = a * Teuchos::ScalarTraits<Scalar>::random() + b;
00121         // Above should be in the range [l,b]
00122       }
00123     }
00125 private:
00126   static unsigned int static_seed_;
00127   unsigned int seed_;
00128   Scalar l_;
00129   Scalar u_;
00130 };
00131 
00132 
00133 template<class Scalar>
00134 unsigned int TOpRandomize<Scalar>::static_seed_ = 0;
00135 
00136 
00137 } // namespace RTOpPack
00138 
00139 
00140 #endif // RTOPPACK_TOP_RANDOMIZE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines