PSCF v1.5.0
VecRandom.cpp
1#include "VecRandom.h"
2#include <util/random/Random.h>
3#include <util/containers/Array.h>
4#include <util/global.h>
5
6namespace Pscf {
7
8 using namespace Util;
9
10 /*
11 * Default constructor.
12 */
14 : randomPtr_(nullptr)
15 {}
16
17 /*
18 * Constructor - create association with a scalar RNG.
19 */
21 : randomPtr_(&random)
22 {}
23
24 /*
25 * Destructor.
26 */
29
30 /*
31 * Create an association with a scalar RNG after construction
32 */
34 { randomPtr_ = &random; }
35
36 /*
37 * Populate array on device with random doubles in (0, 1], uniform dist.
38 */
40 {
41 UTIL_CHECK(randomPtr_);
42 UTIL_CHECK(data.capacity() > 0);
43 const int n = data.capacity();
44 for (int i = 0; i < n; ++i) {
45 data[i] = randomPtr_->uniform();
46 }
47 }
48
49 /*
50 * Populate array with random doubles uniform dist in (min, max].
51 */
52 void VecRandom<CPT>::uniform(Util::Array<double>& data, double min, double max)
53 {
54 UTIL_CHECK(randomPtr_);
55 UTIL_CHECK(data.capacity() > 0);
56 UTIL_CHECK(max > min);
57 const int n = data.capacity();
58 for (int i = 0; i < n; ++i) {
59 data[i] = randomPtr_->uniform(min, max);
60 }
61 }
62
63 /*
64 * Populate array with normal-distributed random doubles.
65 */
67 double stddev, double mean)
68 {
69 UTIL_CHECK(randomPtr_);
70 UTIL_CHECK(data.capacity() > 0);
71 const int n = data.capacity();
72 for (int i = 0; i < n; ++i) {
73 data[i] = mean + stddev * randomPtr_->gaussian();
74 }
75 }
76
77 // Free functions, used to initialize the vector RNG.
78
79 /*
80 * Associate vector and scalar random number generators, if needed.
81 *
82 * The specialization for a CUDA backend would instead do nothing.
83 */
84 template <>
85 void linkVecRandom<CPT>(VecRandom<CPT>& vr, Random & sr)
86 { vr.associate(sr); }
87
88 /*
89 * Set vector RNG seed, if needed (do nothing for CPU code).
90 *
91 * The specialization for a CUDA backend would instead set the seed.
92 */
93 template <>
94 void seedVecRandom<CPT>(VecRandom<CPT>& vr, long seed)
95 {}
96
97
98} // namespace Pscf
VecRandom()
Default constructor.
Definition VecRandom.cpp:13
Array container class template.
Definition Array.h:41
int capacity() const
Return allocated size.
Definition Array.h:250
Random number generator.
Definition Random.h:47
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.