MCKL
Monte Carlo Kernel Library
philox.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/philox.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_RANDOM_PHILOX_HPP
33 #define MCKL_RANDOM_PHILOX_HPP
34 
38 #include <mckl/random/counter.hpp>
40 
41 #if MCKL_HAS_SSE2
43 #endif
44 
45 #if MCKL_HAS_AVX2
47 #endif
48 
49 #if MCKL_HAS_AVX512
51 #endif
52 
55 #ifndef MCKL_PHILOX_ROUNDS
56 #define MCKL_PHILOX_ROUNDS 10
57 #endif
58 
59 namespace mckl {
60 
61 namespace internal {
62 
63 #if MCKL_USE_AVX512
64 template <typename T, std::size_t K, std::size_t Rounds, typename Constants>
65 using PhiloxGeneratorImpl = PhiloxGeneratorAVX512Impl<T, K, Rounds, Constants>;
66 #elif MCKL_USE_AVX2
67 template <typename T, std::size_t K, std::size_t Rounds, typename Constants>
69 #elif MCKL_USE_SSE2
70 template <typename T, std::size_t K, std::size_t Rounds, typename Constants>
71 using PhiloxGeneratorImpl = PhiloxGeneratorSSE2Impl<T, K, Rounds, Constants>;
72 #else // MCKL_USE_AVX2
73 template <typename T, std::size_t K, std::size_t Rounds, typename Constants>
74 using PhiloxGeneratorImpl =
76 #endif // MCKL_USE_AVX2
77 
78 } // namespace internal
79 
87 template <typename T, std::size_t K, std::size_t Rounds = MCKL_PHILOX_ROUNDS,
88  typename Constants = PhiloxConstants<T, K>>
90 {
91  static_assert(std::is_unsigned<T>::value,
92  "**PhiloxGenerator** used with T other than unsigned integer types");
93 
94  static_assert(K != 0 && K % 2 == 0,
95  "**PhiloxGenerator** used with K other than multiples of 2");
96 
97  static_assert(
98  Rounds != 0, "**PhiloxGenerator** used with rounds equal to zero");
99 
100  public:
102  using key_type = std::array<T, K / 2>;
103 
104  static constexpr std::size_t size() { return sizeof(T) * K; }
105 
106  key_type key() const { return key_; }
107 
108  void reset(const key_type &key) { key_ = key; }
109 
110  void operator()(const void *plain, void *cipher) const
111  {
113  plain, cipher, key_);
114  }
115 
116  template <typename ResultType>
117  void operator()(ctr_type &ctr, ResultType *r) const
118  {
120  ctr, r, key_);
121  }
122 
123  template <typename ResultType>
124  void operator()(ctr_type &ctr, std::size_t n, ResultType *r) const
125  {
127  ctr, n, r, key_);
128  }
129 
130  friend bool operator==(
133  {
134  return gen1.key_ == gen2.key_;
135  }
136 
137  friend bool operator!=(
140  {
141  return !(gen1 == gen2);
142  }
143 
144  template <typename CharT, typename Traits>
145  friend std::basic_ostream<CharT, Traits> &operator<<(
146  std::basic_ostream<CharT, Traits> &os,
148  {
149  if (!os) {
150  return os;
151  }
152 
153  os << gen.key_;
154 
155  return os;
156  }
157 
158  template <typename CharT, typename Traits>
159  friend std::basic_istream<CharT, Traits> &operator>>(
160  std::basic_istream<CharT, Traits> &is,
162  {
163  if (!is) {
164  return is;
165  }
166 
168  gen_tmp.key_.fill(0);
169  is >> std::ws >> gen_tmp.key_;
170 
171  if (is) {
172  gen = std::move(gen_tmp);
173  }
174 
175  return is;
176  }
177 
178  private:
179  key_type key_;
180 }; // class PhiloxGenerator
181 
184 template <typename ResultType, typename T, std::size_t K,
185  std::size_t Rounds = MCKL_PHILOX_ROUNDS,
186  typename Constants = PhiloxConstants<T, K>>
187 using PhiloxEngine =
189 
192 template <typename ResultType>
194 
197 template <typename ResultType>
199 
202 template <typename ResultType>
204 
207 template <typename ResultType>
209 
213 
217 
221 
225 
229 
233 
237 
241 
242 } // namespace mckl
243 
244 #endif // MCKL_RANDOM_PHILOX_HPP
Counter based RNG engine.
Definition: counter.hpp:62
static void eval(const void *plain, void *cipher, const std::array< T, K/2 > &key)
Default Philox constants.
Counter< T, K > ctr_type
Definition: philox.hpp:101
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const PhiloxGenerator< T, K, Rounds, Constants > &gen)
Definition: philox.hpp:145
key_type key() const
Definition: philox.hpp:106
static constexpr std::size_t size()
Definition: philox.hpp:104
void operator()(ctr_type &ctr, ResultType *r) const
Definition: philox.hpp:117
void operator()(ctr_type &ctr, std::size_t n, ResultType *r) const
Definition: philox.hpp:124
#define MCKL_PHILOX_ROUNDS
PhiloxGenerator default rounds.
Definition: philox.hpp:56
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, PhiloxGenerator< T, K, Rounds, Constants > &gen)
Definition: philox.hpp:159
void reset(const key_type &key)
Definition: philox.hpp:108
friend bool operator!=(const PhiloxGenerator< T, K, Rounds, Constants > &gen1, const PhiloxGenerator< T, K, Rounds, Constants > &gen2)
Definition: philox.hpp:137
typename internal::CounterImpl< T, K >::type Counter
A counter type with the same width as std::array<T, K> but with possibly fewer elements.
Definition: increment.hpp:104
Philox RNG generator.
Definition: philox.hpp:89
friend bool operator==(const PhiloxGenerator< T, K, Rounds, Constants > &gen1, const PhiloxGenerator< T, K, Rounds, Constants > &gen2)
Definition: philox.hpp:130
void operator()(const void *plain, void *cipher) const
Definition: philox.hpp:110
Definition: mcmc.hpp:40
std::array< T, K/2 > key_type
Definition: philox.hpp:102
PhiloxGeneratorAVX2Impl< T, K, Rounds, Constants > PhiloxGeneratorImpl
Definition: philox.hpp:68