PSCF v1.5.0
cuda/VecRandom.h
1#ifndef PSCF_VEC_RANDOM_CU_H
2#define PSCF_VEC_RANDOM_CU_H
3
4#include "CUT.h"
5#include "BackendArray.h"
6
7#include <curand.h>
8
9/*
10* PSCF - Polymer Self-Consistent Field
11*
12* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
13* Distributed under the terms of the GNU General Public License.
14*/
15
16// Forward declarations
17namespace Util {
18 class Random;
19}
20namespace Pscf {
21
22 template <typename T> class VecRandom;
23
24 template <class T>
25 void linkVecRandom(VecRandom<T>& vr, Util::Random & sr);
26
27 template <class T>
28 void seedVecRandom(VecRandom<T>& vr, long seed);
29}
30
31namespace Pscf {
32
33 using namespace Util;
34
46 template<>
48 {
49
50 public:
51
55 VecRandom();
56
60 virtual ~VecRandom();
61
67 void setSeed(unsigned long long seed);
68
75
82
90 void uniform(BackendArray<float,CUT>& data, float min, float max);
91
99 void uniform(BackendArray<double,CUT>& data, double min, double max);
100
109 float stddev, float mean = 0.0);
110
119 double stddev, double mean = 0.0);
120
126 long seed();
127
128 private:
129
131 curandGenerator_t gen_;
132
134 unsigned long long seed_;
135
137 bool isInitialized_;
138
140 BackendArray<float,CUT> floatWork_;
141
143 BackendArray<double,CUT> doubleWork_;
144
145 void errorCheck(curandStatus_t const & error);
146
147 };
148
149 // Inline member function
150
151 /*
152 * Returns value of random seed (private member variable idum)
153 */
154 inline long VecRandom<CUT>::seed()
155 { return seed_; }
156
157 // Related free functions
158
168 template <>
169 void linkVecRandom(VecRandom<CUT>& vr, Random & sr);
170
182 template <>
183 void seedVecRandom(VecRandom<CUT>& vr, long seed);
184
185}
186#endif
Backend identifier class for the GPU-enabled CUDA backend.
Definition CUT.h:31
void normal(BackendArray< float, CUT > &data, float stddev, float mean=0.0)
Populate array on device with normal-distributed random floats.
Definition VecRandom.cu:174
VecRandom()
Constructor.
Definition VecRandom.cu:79
void uniform(BackendArray< float, CUT > &data)
Populate array with uniform random floats in (0, 1].
Definition VecRandom.cu:120
long seed()
Returns value of random seed (private member variable seed_).
void setSeed(unsigned long long seed)
Set value of random seed (private member variable seed_).
Definition VecRandom.cu:101
Random number generator.
Definition Random.h:47
PSCF package top-level namespace.
Utility classes for scientific computation.