QUDA  v1.1.0
A library for QCD on GPUs
random_quda.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <lattice_field.h>
4 
5 #ifdef __CUDACC_RTC__
6 #define RNG int
7 #else
8 #include <curand_kernel.h>
9 
10 namespace quda {
11 
12 #if defined(XORWOW)
13 typedef struct curandStateXORWOW cuRNGState;
14 #elif defined(MRG32k3a)
15 typedef struct curandStateMRG32k3a cuRNGState;
16 #else
17 typedef struct curandStateMRG32k3a cuRNGState;
18 #endif
19 
23 class RNG {
24 
25  private:
26  cuRNGState *state;
27  cuRNGState *backup_state;
28  unsigned long long seed;
29  int size;
30  int size_cb;
31  int X[4];
32  void AllocateRNG();
34  public:
40  RNG(const LatticeField &meta, unsigned long long seedin);
41 
47  RNG(const LatticeFieldParam &param, unsigned long long seedin);
48 
50  void Release();
51 
53  void Init();
54 
55  unsigned long long Seed() { return seed; };
56 
57  __host__ __device__ __inline__ cuRNGState *State() { return state; };
58 
60  void restore();
61 
63  void backup();
64 };
65 
66 
74 template<class Real>
75 inline __device__ Real Random(cuRNGState &state, Real a, Real b){
76  Real res;
77  return res;
78 }
79 
80 template<>
81 inline __device__ float Random<float>(cuRNGState &state, float a, float b){
82  return a + (b - a) * curand_uniform(&state);
83 }
84 
85 template<>
86 inline __device__ double Random<double>(cuRNGState &state, double a, double b){
87  return a + (b - a) * curand_uniform_double(&state);
88 }
89 
95 template<class Real>
96 inline __device__ Real Random(cuRNGState &state){
97  Real res;
98  return res;
99 }
100 
101 template<>
102 inline __device__ float Random<float>(cuRNGState &state){
103  return curand_uniform(&state);
104 }
105 
106 template<>
107 inline __device__ double Random<double>(cuRNGState &state){
108  return curand_uniform_double(&state);
109 }
110 
111 
112 template<class Real>
113 struct uniform { };
114 template<>
115 struct uniform<float> {
116  __device__
117  static inline float rand(cuRNGState &state) {
118  return curand_uniform(&state);
119  }
120 };
121 template<>
122 struct uniform<double> {
123  __device__
124  static inline double rand(cuRNGState &state) {
125  return curand_uniform_double(&state);
126  }
127 };
128 
129 
130 
131 template<class Real>
132 struct normal { };
133 template<>
134 struct normal<float> {
135  __device__
136  static inline float rand(cuRNGState &state) {
137  return curand_normal(&state);
138  }
139 };
140 template<>
141 struct normal<double> {
142  __device__
143  static inline double rand(cuRNGState &state) {
144  return curand_normal_double(&state);
145  }
146 };
147 
148 
149 }
150 
151 #endif
Class declaration to initialize and hold CURAND RNG states.
Definition: random_quda.h:23
void backup()
Backup CURAND array states initialization.
RNG(const LatticeFieldParam &param, unsigned long long seedin)
Constructor that takes its metadata from a param.
RNG(const LatticeField &meta, unsigned long long seedin)
allocate curand rng states array in device memory
void Release()
void Init()
__host__ __device__ __inline__ cuRNGState * State()
Definition: random_quda.h:57
unsigned long long Seed()
Definition: random_quda.h:55
void restore()
Restore CURAND array states initialization.
__device__ float Random< float >(cuRNGState &state, float a, float b)
Definition: random_quda.h:81
__device__ double Random< double >(cuRNGState &state, double a, double b)
Definition: random_quda.h:86
__device__ Real Random(cuRNGState &state, Real a, Real b)
Return a random number between a and b.
Definition: random_quda.h:75
struct curandStateMRG32k3a cuRNGState
Definition: random_quda.h:17
QudaGaugeParam param
Definition: pack_test.cpp:18
static __device__ double rand(cuRNGState &state)
Definition: random_quda.h:143
static __device__ float rand(cuRNGState &state)
Definition: random_quda.h:136
static __device__ double rand(cuRNGState &state)
Definition: random_quda.h:124
static __device__ float rand(cuRNGState &state)
Definition: random_quda.h:117