Stride Reference Manual  - generated for commit 9643b11
Rn.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include "RnInfo.h"
24 
25 #include <trng/discrete_dist.hpp>
26 #include <trng/lcg64.hpp>
27 #include <trng/uniform01_dist.hpp>
28 #include <trng/uniform_int_dist.hpp>
29 #include <functional>
30 #include <pcg/pcg_random.hpp>
31 #include <randutils/randutils.hpp>
32 #include <string>
33 #include <vector>
34 
35 namespace stride {
36 namespace util {
37 
41 template <typename E>
42 class Rn : protected std::vector<randutils::random_generator<E, randutils::seed_seq_fe128>>
43 {
44 public:
45  using EngineType = E;
46  using RnType = randutils::random_generator<E, randutils::seed_seq_fe128>;
47  using ContainerType = std::vector<randutils::random_generator<E, randutils::seed_seq_fe128>>;
48 
49  using ContainerType::operator[];
50  using ContainerType::at;
51  using ContainerType::size;
52 
53 public:
56 
58  explicit Rn(const RnInfo& info)
61  {
62  Initialize(info);
63  }
64 
66  Rn(const Rn&) = delete;
67 
69  Rn& operator=(const Rn&) = delete;
70 
72  bool operator==(const Rn& other);
73 
75  RnInfo GetInfo() const;
76 
78  std::function<double()> GetUniform01Generator(unsigned int i = 0U)
79  {
80  return ContainerType::at(i).variate_generator(trng::uniform01_dist<double>());
81  }
82 
84  std::function<int()> GetUniformIntGenerator(int a, int b, unsigned int i = 0U)
85  {
86  return ContainerType::at(i).variate_generator(trng::uniform_int_dist(a, b));
87  }
88 
90  //std::function<int()> GetDiscreteGenerator(const std::vector<double>& weights, unsigned int i = 0U)
91  //{
92  // return ContainerType::at(i).variate_generator(trng::discrete_dist(weights.begin(), weights.end()));
93  //}
94 
96  template<typename It>
97  std::function<int()> GetDiscreteGenerator(It begin, It end, unsigned int i = 0U)
98  {
99  return ContainerType::at(i).variate_generator(trng::discrete_dist(begin, end));
100  }
101 
103  void Initialize(const RnInfo& info);
104 
106  bool IsEmpty() const { return ContainerType::empty() || (m_stream_count == 0U); }
107 
109  void Shuffle(std::vector<unsigned int>& indices, unsigned int i)
110  {
111  ContainerType::at(i).shuffle(indices.begin(), indices.end());
112  }
113 
114 private:
116  void Seed(randutils::seed_seq_fe128& seseq);
117 
118 private:
119  std::string m_seed_seq_init;
120  unsigned int m_stream_count;
121 };
122 
123 template <>
124 void Rn<pcg64>::Seed(randutils::seed_seq_fe128& seseq);
125 
126 extern template class Rn<pcg64>;
127 extern template class Rn<trng::lcg64>;
128 
129 } // namespace util
130 } // namespace stride
std::function< double()> GetUniform01Generator(unsigned int i=0U)
Return a generator for uniform doubles in [0, 1[ using i-th random engine.
Definition: Rn.h:78
Interface of RnPcg.
std::function< int()> GetDiscreteGenerator(It begin, It end, unsigned int i=0U)
Return generator for integers [0, n-1[ with non-negative weights p_j (i=0,..,n-1) using i-th random e...
Definition: Rn.h:97
std::function< int()> GetUniformIntGenerator(int a, int b, unsigned int i=0U)
Return a generator for uniform ints in [a, b[ (a < b) using i-th random engine.
Definition: Rn.h:84
void Seed(randutils::seed_seq_fe128 &seseq)
Actual first-time seeding. Procedure varies according to engine type, see specialisations.
Definition: Rn.cpp:104
randutils::random_generator< pcg64, randutils::seed_seq_fe128 > RnType
Definition: Rn.h:46
void Initialize(const RnInfo &info)
Initalize with data in Info.
Definition: Rn.cpp:62
std::vector< randutils::random_generator< pcg64, randutils::seed_seq_fe128 >> ContainerType
Definition: Rn.h:47
Rn(const RnInfo &info)
Initializes.
Definition: Rn.h:58
Rn()
Default constructor build empty manager.
Definition: Rn.h:55
bool operator==(const Rn &other)
Equality of states.
Definition: Rn.cpp:36
std::string m_seed_seq_init
Seed sequence initializer used with engines.
Definition: Rn.h:119
Rn & operator=(const Rn &)=delete
No copy assignment.
bool IsEmpty() const
Is this een empty (i.e. non-initialized Rn)?
Definition: Rn.h:106
Manages random number generation in parallel (OpenMP) calculations.
Definition: Rn.h:42
RnInfo GetInfo() const
Return the state of the random engines.
Definition: Rn.cpp:48
void Shuffle(std::vector< unsigned int > &indices, unsigned int i)
Random shuffle of vector of unsigned int indices using i-th engine.
Definition: Rn.h:109
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
Information on random number management state.
Definition: RnInfo.h:32
unsigned int m_stream_count
Number of threads/streams set up with the engine.
Definition: Rn.h:120