gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
random.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
41/*
42 * Mersenne twister random number generator.
43 */
44
45#ifndef __BASE_RANDOM_HH__
46#define __BASE_RANDOM_HH__
47
48#include <random>
49#include <string>
50#include <type_traits>
51#include <vector>
52
53#include "base/compiler.hh"
54#include "base/logging.hh"
55#include "base/types.hh"
56
57namespace gem5
58{
59
60class Random
61{
62 friend class RandomTest;
63
64 public:
65 using RandomPtr = std::shared_ptr<Random>;
67
68 static RandomPtr genRandom(Random* r = nullptr)
69 {
70 if (instances == nullptr)
71 instances = new Instances();
72
73 /*
74 * If `r` is null we create a new RNG with the global seed and wrap
75 * it in a `shared_ptr`. Otherwise, we wrap the existing RNG in a
76 * `shared_ptr` and add it to the vector of instances.
77 *
78 * This is done to ensure that the RNG is not deleted while it is still
79 * in use. The `shared_ptr` will keep the RNG alive until all references
80 * to it are gone.
81 */
82 auto randpoint = r ? std::shared_ptr<Random>(r, [](Random*){})
83 : std::shared_ptr<Random>(new Random(globalSeed));
84 // Check if randpoint is not already in the vector
85 bool exists = false;
86 for (const auto& weak_ptr : *instances) {
87 if (!weak_ptr.expired() && weak_ptr.lock() == randpoint) {
88 exists = true;
89 break;
90 }
91 }
92 if (!exists) {
93 instances->emplace_back(randpoint);
94 }
95
96 return randpoint;
97 }
98
99 static RandomPtr genRandom(uint32_t s)
100 {
101 if (instances == nullptr)
102 instances = new Instances();
103
104 auto randpoint = std::shared_ptr<Random>(new Random(s));
105 // Check if randpoint is not already in the vector
106 bool exists = false;
107 for (const auto& weak_ptr : *instances) {
108 if (!weak_ptr.expired() && weak_ptr.lock() == randpoint) {
109 exists = true;
110 break;
111 }
112 }
113 if (!exists) {
114 instances->emplace_back(randpoint);
115 }
116
117 return randpoint;
118 }
119
120 static uint64_t globalSeed;
121
125 std::mt19937_64 gen;
126
127 private:
141 static_assert(nullptr == 0x0, "nullptr is not 0x0, Random instance tracking will fail");
143
148 Random() = delete;
149 Random(uint32_t s);
150
151 Random(const Random& rng) = delete;
152 Random& operator=(const Random& rng) = delete;
153
154 Random(Random&& rng) = delete;
155 Random& operator=(Random&& rng) = delete;
156
157 public: // end of api_base_utils
159 ~Random();
160
161 void init(uint32_t s);
162
168 static void reseedAll(uint64_t seed)
169 {
170 globalSeed = seed;
171
172 if (instances == nullptr)
173 return;
174
175 for (auto rng_ptr : *instances)
176 rng_ptr.lock()->init(seed);
177 }
178
185 template <typename T>
186 typename std::enable_if_t<std::is_integral_v<T>, T>
188 {
189 // [0, max_value] for integer types
190 return gen() % std::numeric_limits<T>::max();
191 }
192
196 template <typename T>
197 typename std::enable_if_t<std::is_floating_point_v<T>, T>
199 {
200 // [0, 1) for real types
201 warn_once("FP random numbers are not uniformly distributed.");
202 return ((T) gen()) /
203 ((T) std::numeric_limits<uint64_t>::max());
204 }
205
209 template <typename T>
210 typename std::enable_if_t<std::is_integral_v<T>, T>
211 random(T min, T max)
212 {
213 assert(min <= max);
214 // + 1 to handle cases where min == max
215 T r = gen() % (max - min + 1) + min;
216 return r;
217 }
218};
219
220} // namespace gem5
221
222#endif // __BASE_RANDOM_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
static uint64_t globalSeed
Definition random.hh:120
std::vector< std::weak_ptr< Random > > Instances
Definition random.hh:66
static RandomPtr genRandom(Random *r=nullptr)
Definition random.hh:68
std::shared_ptr< Random > RandomPtr
Definition random.hh:65
static void reseedAll(uint64_t seed)
Facility to reseed all live instances and ensure future default constructed instances also use the ne...
Definition random.hh:168
static RandomPtr genRandom(uint32_t s)
Definition random.hh:99
friend class RandomTest
Definition random.hh:62
static Instances * instances
Collection of all live instances of Random to enable global reseeding.
Definition random.hh:142
std::enable_if_t< std::is_floating_point_v< T >, T > random()
Definition random.hh:198
STL vector class.
Definition stl.hh:37
Random & operator=(const Random &rng)=delete
Random(const Random &rng)=delete
std::enable_if_t< std::is_integral_v< T >, T > random(T min, T max)
Definition random.hh:211
Random & operator=(Random &&rng)=delete
Random(Random &&rng)=delete
std::enable_if_t< std::is_integral_v< T >, T > random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
Definition random.hh:187
Random()=delete
std::mt19937_64 gen
Definition random.hh:125
#define warn_once(...)
Definition logging.hh:292
Bitfield< 4 > s
const FlagsType init
This Stat is Initialized.
Definition info.hh:55
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36

Generated on Mon May 26 2025 09:19:07 for gem5 by doxygen 1.13.2