Stride Reference Manual  - generated for commit 9643b11
Rn.cpp
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 #include "Rn.h"
22 #include "StringUtils.h"
23 
24 #include <cctype>
25 #include <randutils/randutils.hpp>
26 #include <sstream>
27 #include <stdexcept>
28 
29 using namespace std;
30 using namespace randutils;
31 
32 namespace stride {
33 namespace util {
34 
35 template <typename E>
36 bool Rn<E>::operator==(const Rn& other)
37 {
38  bool status = m_stream_count == other.m_stream_count;
39  if (status) {
40  for (size_t i = 0; i < size(); ++i) {
41  status = status && ((*this)[i] == other[i]);
42  }
43  }
44  return status;
45 }
46 
47 template <typename E>
49 {
50  RnInfo info;
51  std::stringstream ss;
52  for (auto& e : *this) {
53  ss << e.engine();
54  }
55  info.m_seed_seq_init = m_seed_seq_init;
56  info.m_state = ss.str();
57  info.m_stream_count = m_stream_count;
58  return info;
59 }
60 
61 template <typename E>
62 void Rn<E>::Initialize(const RnInfo& info)
63 {
64  if (m_stream_count != info.m_stream_count) {
65  m_stream_count = info.m_stream_count;
66  this->resize(m_stream_count);
67  }
68  m_seed_seq_init = info.m_seed_seq_init;
69 
70  auto state = info.m_state;
71  if (state.empty()) {
72  std::vector<unsigned int> seseq_init_vec;
73  const auto string_vec{Split(m_seed_seq_init, ",")};
74  for (const auto& e : Split(m_seed_seq_init, ",")) {
75  if (!CheckAllDigits(e)) {
76  throw std::runtime_error("Rn::Seed> Error in seeding definiton: " + e);
77  }
78  seseq_init_vec.push_back(FromString<unsigned int>(e));
79  }
80  randutils::seed_seq_fe128 seseq(seseq_init_vec.begin(), seseq_init_vec.end());
81 
82  Seed(seseq);
83  } else {
84  std::stringstream ss(state);
85  for (size_t i = 0; i < m_stream_count; ++i) {
86  ss >> (*this)[i].engine();
87  }
88  }
89 }
90 
91 template <>
92 void Rn<pcg64>::Seed(randutils::seed_seq_fe128& seseq)
93 {
94  if (2 * m_stream_count > 64) {
95  throw std::runtime_error("RnPcg64 generate seed vector, cannot handle large n.");
96  }
97  auto seeds = pcg_extras::generate_vector<pcg64::state_type, 64>(seseq);
98  for (size_t i = 0; i < m_stream_count; ++i) {
99  (*this)[i].engine().seed(seeds[i + 1], seeds[i]);
100  }
101 }
102 
103 template <typename E>
104 void Rn<E>::Seed(randutils::seed_seq_fe128& seseq)
105 {
106  auto seeds = pcg_extras::generate_one<unsigned long>(seseq);
107  for (size_t i = 0; i < m_stream_count; ++i) {
108  (*this)[i].engine().seed(seeds);
109  (*this)[i].engine().split(m_stream_count, i);
110  }
111 }
112 
113 template class Rn<pcg64>;
114 template class Rn<trng::lcg64>;
115 
116 } // namespace util
117 } // namespace stride
std::string m_seed_seq_init
Seed for the engine.
Definition: RnInfo.h:35
Interface of RnPcg.
STL namespace.
std::vector< std::string > Split(const std::string &s, const std::string &delimiters)
Split a string (in order of occurence) by splitting it on the given delimiters.
Definition: StringUtils.h:58
unsigned int m_stream_count
Number of streams set up with the engine.
Definition: RnInfo.h:39
Manages random number generation in parallel (OpenMP) calculations.
Definition: Rn.h:42
bool operator==(const SegmentedVector< T, N, Safe > &lhs, const SegmentedVector< T, N, Safe > &rhs)
Helper function for equality test (equal size and all elements equal).
Miscellaneous string utilities.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
std::string m_state
Long string representing current state.
Definition: RnInfo.h:38
Information on random number management state.
Definition: RnInfo.h:32
bool CheckAllDigits(const std::string &s)
All characters in string are digits (or string is empty)
Definition: StringUtils.h:36
unsigned int m_stream_count
Number of threads/streams set up with the engine.
Definition: Rn.h:120