MADNESS  version 0.9
ran.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31  $Id$
32 */
33 #ifndef MADNESS_MISC_RAN_H__INCLUDED
34 #define MADNESS_MISC_RAN_H__INCLUDED
35 
36 #include <madness/madness_config.h>
38 
39 #include <complex>
40 typedef std::complex<float> float_complex;
41 typedef std::complex<double> double_complex;
42 
43 
44 namespace madness {
45 
46  struct RandomState {
47  int cur;
48  double u[1279];
49  };
50 
52 
72  class Random : private Mutex {
73  private:
74  const int r;
75  const int s;
76  const double beta;
77  volatile int cur;
78  volatile double* const u;
79  unsigned int simple_state;
80 
81  void generate();
82 
83  unsigned int simple();
84 
85  public:
86  Random(unsigned int seed = 5461);
87 
88  virtual ~Random();
89 
90  double get() {
91  ScopedMutex<Mutex> safe(this);
92  if (cur >= r) generate();
93  return u[cur++];
94  }
95 
97  template <typename T>
98  void getv(int n, T * restrict v) {
99  ScopedMutex<Mutex> safe(this);
100  while (n) {
101  if (cur >= r) generate();
102  int ndo = std::min(n,r-cur);
103  const double* ucur = const_cast<const double*>(u) + cur;
104  for (int i=0; i<ndo; ++i) v[i] = (T)(ucur[i]);
105  n -= ndo;
106  v += ndo;
107  cur += ndo;
108  }
109  }
110 
112  void getbytes(int n, unsigned char * restrict v);
113 
115  RandomState getstate() const;
116 
118  void setstate(const RandomState &s);
119 
121  void setstate(unsigned int seed);
122 
124  static void test();
125  };
126 
127 
129  extern Random default_random_generator;
130 
132  template <class T> T RandomValue();
133 
135  template <> double RandomValue<double> ();
136 
138  template <> float RandomValue<float> ();
139 
141  template <> int RandomValue<int> ();
142 
144  template <> long RandomValue<long> ();
145 
148 
151 
152  template <class T> void RandomVector(int n, T* t) {
153  for (int i=0; i<n; ++i) t[i] = RandomValue<T>();
154  }
155 
156  template <> void RandomVector<double>(int n, double* t);
157 
158  template <> void RandomVector<float>(int n, float* t);
159 
160  template <> void RandomVector<double_complex>(int n, double_complex* t);
161 
162  template <> void RandomVector<float_complex>(int n, float_complex* t);
163 }
164 
165 #endif // MADNESS_MISC_RAN_H__INCLUDED
Random(unsigned int seed=5461)
Definition: ran.cc:71
int cur
Definition: ran.h:47
Mutex that is applied/released at start/end of a scope.
Definition: worldmutex.h:186
Mutex using pthread mutex operations.
Definition: worldmutex.h:94
std::complex< double > double_complex
Definition: lineplot.cc:16
float_complex RandomValue< float_complex >()
Random float_complex.
Definition: ran.cc:246
static void test()
Test the generator.
Definition: ran.cc:182
void RandomVector< float >(int n, float *t)
Definition: ran.cc:262
int RandomValue< int >()
Random int.
Definition: ran.cc:250
float RandomValue< float >()
Random float.
Definition: ran.cc:238
double u[1279]
Definition: ran.h:48
void RandomVector(int n, T *t)
Definition: ran.h:152
std::complex< double > double_complex
Definition: ran.h:41
void RandomVector< double >(int n, double *t)
Definition: ran.cc:258
T RandomValue()
Random value that wraps the default Fibonacci generator.
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
void RandomVector< float_complex >(int n, float_complex *t)
Definition: ran.cc:270
void RandomVector< double_complex >(int n, double_complex *t)
Definition: ran.cc:266
const mpreal min(const mpreal &x, const mpreal &y)
Definition: mpreal.h:2675
Implements Dqueue, Thread, ThreadBase and ThreadPool.
RandomState getstate() const
Returns full state of the generator.
Definition: ran.cc:167
double RandomValue< double >()
Random double.
Definition: ran.cc:234
void setstate(const RandomState &s)
Restores state of the generator.
Definition: ran.cc:175
long RandomValue< long >()
Random long.
Definition: ran.cc:254
double_complex RandomValue< double_complex >()
Random double_complex.
Definition: ran.cc:242
virtual ~Random()
Definition: ran.cc:127
std::complex< float > float_complex
Definition: ran.h:40
void getbytes(int n, unsigned char *restrict v)
Returns vector of random bytes in [0,256)
Definition: ran.cc:154
Random default_random_generator
The default random number stream.
Definition: ran.cc:231
Definition: ran.h:46
#define restrict
Definition: config.h:403
A random number generator (portable, vectorized, and thread-safe)
Definition: ran.h:72
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
void getv(int n, T *restrict v)
Returns a vector of uniform doubles in [0,1)
Definition: ran.h:98