PetaVision  Alpha
Random.hpp
1 /*
2  * Random.hpp
3  *
4  * Created on: Aug 23, 2013
5  * Author: pschultz
6  *
7  * A class to manage Tausworth random number generators so that
8  * random number generation is not affected by details of the
9  * MPI configuration.
10  *
11  * Random(Communicator * comm, const PVLayerLoc * locptr, bool isExtended)
12  * creates an array of RNG states.
13  * The size of the array is determined by the local layer size specified in
14  * locptr,
15  * but the initial seeds are determined by global index.
16  * Hence the sequence of random numbers generated by a specific RNG is
17  * determined
18  * by global index, not local index, so it is independent of MPI configuration.
19  */
20 
21 #ifndef RANDOM_HPP_
22 #define RANDOM_HPP_
23 
24 #include "utils/cl_random.h"
25 #include <vector>
26 
27 namespace PV {
28 
29 class Random {
30  public:
31  Random(int count);
32  Random(const PVLayerLoc *locptr, bool isExtended);
33  virtual ~Random();
34 
35  taus_uint4 *getRNG(int index) { return &rngArray[index]; }
36  float uniformRandom(int localIndex = 0);
37  float uniformRandom(int localIndex, float min, float max) {
38  return min + uniformRandom(localIndex) * (max - min);
39  }
40  void uniformRandom(float *values, int localIndex, int count = 1) {
41  for (int k = 0; k < count; k++)
42  values[k] = uniformRandom(localIndex + k);
43  }
44  void uniformRandom(float *values, int localIndex, int count, float min, float max) {
45  for (int k = 0; k < count; k++)
46  values[k] = uniformRandom(localIndex + k, min, max);
47  }
48 
49  unsigned int randomUInt(int localIndex = 0);
50  void randomUInt(unsigned int *values, int localIndex, int count = 1) {
51  for (int k = 0; k < count; k++)
52  values[k] = randomUInt(localIndex + k);
53  }
54  static inline unsigned int randomUIntMax() { return CL_RANDOM_MAX; }
55 
56  protected:
57  Random();
58  int initializeFromCount(int count);
59  int initializeFromLoc(const PVLayerLoc *locptr, bool isExtended);
60 
61  private:
62  int initialize_base();
63 
64  // Member variables
65  protected:
66  std::vector<taus_uint4> rngArray;
67 };
68 
69 } /* namespace PV */
70 #endif /* RANDOM_HPP_ */