Kokkos Node API and Local Linear Algebra Kernels Version of the Day
Tsqr_Random_NormalGenerator.hpp
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //          Kokkos: Node API and Parallel Node Kernels
00005 //              Copyright (2009) 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 Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ************************************************************************
00027 //@HEADER
00028 
00029 #ifndef __TSQR_Random_NormalGenerator_hpp
00030 #define __TSQR_Random_NormalGenerator_hpp
00031 
00032 #include <Tsqr_Lapack.hpp>
00033 #include <algorithm>
00034 #include <vector>
00035 
00038 
00039 namespace TSQR {
00040   namespace Random {
00041 
00047     template< class Ordinal, class Scalar >
00048     class NormalGenerator {
00049     private:
00051       static int defaultBufferLength() { return 100; }
00052 
00053     public:
00054       typedef Ordinal ordinal_type;
00055       typedef Scalar scalar_type;
00056 
00069       NormalGenerator (const std::vector<int>& iseed,
00070            const int bufferLength = defaultBufferLength()) :
00071   iseed_ (4),
00072   buffer_ (bufferLength),
00073   buffer_length_ (bufferLength),
00074   cur_pos_ (0)
00075       {
00076   std::copy (iseed.begin(), iseed.end(), iseed_.begin());
00077   fill_buffer ();
00078       }
00079 
00090       NormalGenerator (const int bufferLength = defaultBufferLength()) :
00091   iseed_ (4),
00092   buffer_ (bufferLength),
00093   buffer_length_ (bufferLength),
00094   cur_pos_ (0)
00095       {
00096   iseed_[0] = 0;
00097   iseed_[1] = 0;
00098   iseed_[2] = 0;
00099   iseed_[3] = 1;
00100   fill_buffer ();
00101       }
00102       
00111       Scalar operator() () { return next(); }
00112 
00122       void 
00123       getSeed (std::vector<int>& iseed) const
00124       {
00125   if (iseed.size() != iseed_.size())
00126     iseed.resize (iseed_.size());
00127   std::copy (iseed_.begin(), iseed_.end(), iseed.begin());
00128       }
00129 
00130     private:
00131       std::vector<int> iseed_;
00132       std::vector<Scalar> buffer_;
00133       int buffer_length_, cur_pos_;
00134 
00135       void
00136       fill_buffer () 
00137       {
00138   LAPACK<Ordinal, Scalar> lapack;
00139 
00140   // LAPACK's _LARNV routine defines this "enum" (just an
00141   // integer, because it's Fortran) that lets users choose from
00142   // one of three different pseudorandom distributions:
00143   // uniform(0,1), uniform(-1,1), and normal(0,1).
00144   enum distribution_type { 
00145     uniform_0_1 = 1, 
00146     uniform_m1_1 = 2, 
00147     normal_0_1 = 3 
00148   };
00149   lapack.LARNV (normal_0_1, &iseed_[0], buffer_length_, &buffer_[0]);
00150       }
00151 
00152       Scalar 
00153       next () 
00154       { 
00155   // It's impossible to take the greater-than branch, but we
00156   // check for robustness' sake.
00157   if (cur_pos_ >= buffer_length_) 
00158     {
00159       fill_buffer ();
00160       cur_pos_ = 0;
00161     }
00162   return buffer_[cur_pos_++];
00163       }
00164     };
00165   } // namespace Random
00166 } // namespace TSQR
00167 
00168 
00169 #endif // __TSQR_Random_NormalGenerator_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends