vSMC
vSMC: Scalable Monte Carlo
u01_distribution.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/u01_distribution.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2016, 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 VSMC_RNG_U01_DISTRIBUTION_HPP
33 #define VSMC_RNG_U01_DISTRIBUTION_HPP
34 
36 #include <vsmc/rng/u01.hpp>
38 
39 namespace vsmc
40 {
41 
42 namespace internal
43 {
44 
45 template <typename RNGType>
46 using U01UIntType = typename std::conditional<(RNGBits<RNGType>::value > 32),
48 
49 } // namespace vsmc::internal
50 
53 template <typename RealType>
54 class U01Distribution
55 {
57  U01, u01, RealType, floating_point, FLOATING_POINT)
58 
59  public:
60  result_type min() const { return 0; }
61 
62  result_type max() const { return 1; }
63 
64  void reset() {}
65 
66  private:
67  template <typename RNGType>
68  result_type generate(RNGType &rng, const param_type &)
69  {
70  return u01_co<internal::U01UIntType<RNGType>, result_type>(
72  }
73 }; // class U01Distribution
74 
77 template <typename RealType>
79 {
81  U01CC, u01_cc, RealType, floating_point, FLOATING_POINT)
82 
83  public:
84  result_type min() const { return 0; }
85 
86  result_type max() const { return 1; }
87 
88  void reset() {}
89 
90  private:
91  template <typename RNGType>
92  result_type generate(RNGType &rng, const param_type &)
93  {
94  return u01_cc<internal::U01UIntType<RNGType>, result_type>(
96  }
97 }; // class U01CODistribution
98 
101 template <typename RealType>
103 {
105  U01CO, u01_co, RealType, floating_point, FLOATING_POINT)
106 
107  public:
108  result_type min() const { return 0; }
109 
110  result_type max() const { return 1; }
111 
112  void reset() {}
113 
114  private:
115  template <typename RNGType>
116  result_type generate(RNGType &rng, const param_type &)
117  {
118  return u01_co<internal::U01UIntType<RNGType>, result_type>(
120  }
121 }; // class U01CODistribution
122 
125 template <typename RealType>
127 {
129  U01OC, u01_oc, RealType, floating_point, FLOATING_POINT)
130 
131  public:
132  result_type min() const { return 0; }
133 
134  result_type max() const { return 1; }
135 
136  void reset() {}
137 
138  private:
139  template <typename RNGType>
140  result_type generate(RNGType &rng, const param_type &)
141  {
142  return u01_oc<internal::U01UIntType<RNGType>, result_type>(
144  }
145 }; // class U01CODistribution
146 
149 template <typename RealType>
151 {
153  U01OO, u01_oo, RealType, floating_point, FLOATING_POINT)
154 
155  public:
156  result_type min() const { return 0; }
157 
158  result_type max() const { return 1; }
159 
160  void reset() {}
161 
162  private:
163  template <typename RNGType>
164  result_type generate(RNGType &rng, const param_type &)
165  {
166  return u01_oo<internal::U01UIntType<RNGType>, result_type>(
168  }
169 }; // class U01CODistribution
170 
171 namespace internal
172 {
173 
174 template <std::size_t K, typename RealType, typename RNGType>
175 inline void u01_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
176 {
178  uniform_bits_distribution(rng, n, s);
179  u01_co<U01UIntType<RNGType>, RealType>(n, s, r);
180 }
181 
182 template <std::size_t K, typename RealType, typename RNGType>
183 inline void u01_cc_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
184 {
186  uniform_bits_distribution(rng, n, s);
187  u01_cc<U01UIntType<RNGType>, RealType>(n, s, r);
188  for (std::size_t i = 0; i != n; ++i)
189  r[i] = u01_cc<U01UIntType<RNGType>, RealType>(s[i]);
190 }
191 
192 template <std::size_t K, typename RealType, typename RNGType>
193 inline void u01_co_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
194 {
196  uniform_bits_distribution(rng, n, s);
197  u01_co<U01UIntType<RNGType>, RealType>(n, s, r);
198 }
199 
200 template <std::size_t K, typename RealType, typename RNGType>
201 inline void u01_oc_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
202 {
204  uniform_bits_distribution(rng, n, s);
205  u01_oc<U01UIntType<RNGType>, RealType>(n, s, r);
206 }
207 
208 template <std::size_t K, typename RealType, typename RNGType>
209 inline void u01_oo_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
210 {
212  uniform_bits_distribution(rng, n, s);
213  u01_oo<U01UIntType<RNGType>, RealType>(n, s, r);
214 }
215 
216 } // namespace vsmc::internal
217 
220 template <typename RealType, typename RNGType>
221 inline void u01_distribution(RNGType &rng, std::size_t n, RealType *r)
222 {
223  static_assert(std::is_floating_point<RealType>::value,
224  "**u01_distribution** USED WITH RealType OTHER THAN FLOATING POINT "
225  "TYPES");
226 
227  const std::size_t k = 1024;
228  const std::size_t m = n / k;
229  const std::size_t l = n % k;
230  for (std::size_t i = 0; i != m; ++i, r += k)
231  internal::u01_distribution_impl<k>(rng, k, r);
232  internal::u01_distribution_impl<k>(rng, l, r);
233 }
234 
237 template <typename RealType, typename RNGType>
238 inline void u01_cc_distribution(RNGType &rng, std::size_t n, RealType *r)
239 {
240  static_assert(std::is_floating_point<RealType>::value,
241  "**u01_cc_distribution** USED WITH RealType OTHER THAN FLOATING POINT "
242  "TYPES");
243 
244  const std::size_t k = 1024;
245  const std::size_t m = n / k;
246  const std::size_t l = n % k;
247  for (std::size_t i = 0; i != m; ++i, r += k)
248  internal::u01_cc_distribution_impl<k>(rng, k, r);
249  internal::u01_cc_distribution_impl<k>(rng, l, r);
250 }
251 
254 template <typename RealType, typename RNGType>
255 inline void u01_co_distribution(RNGType &rng, std::size_t n, RealType *r)
256 {
257  static_assert(std::is_floating_point<RealType>::value,
258  "**u01_co_distribution** USED WITH RealType OTHER THAN FLOATING POINT "
259  "TYPES");
260 
261  const std::size_t k = 1024;
262  const std::size_t m = n / k;
263  const std::size_t l = n % k;
264  for (std::size_t i = 0; i != m; ++i, r += k)
265  internal::u01_co_distribution_impl<k>(rng, k, r);
266  internal::u01_co_distribution_impl<k>(rng, l, r);
267 }
268 
271 template <typename RealType, typename RNGType>
272 inline void u01_oc_distribution(RNGType &rng, std::size_t n, RealType *r)
273 {
274  static_assert(std::is_floating_point<RealType>::value,
275  "**u01_oc_distribution** USED WITH RealType OTHER THAN FLOATING POINT "
276  "TYPES");
277 
278  const std::size_t k = 1024;
279  const std::size_t m = n / k;
280  const std::size_t l = n % k;
281  for (std::size_t i = 0; i != m; ++i, r += k)
282  internal::u01_oc_distribution_impl<k>(rng, k, r);
283  internal::u01_oc_distribution_impl<k>(rng, l, r);
284 }
285 
288 template <typename RealType, typename RNGType>
289 inline void u01_oo_distribution(RNGType &rng, std::size_t n, RealType *r)
290 {
291  static_assert(std::is_floating_point<RealType>::value,
292  "**u01_oo_distribution** USED WITH RealType OTHER THAN FLOATING POINT "
293  "TYPES");
294 
295  const std::size_t k = 1024;
296  const std::size_t m = n / k;
297  const std::size_t l = n % k;
298  for (std::size_t i = 0; i != m; ++i, r += k)
299  internal::u01_oo_distribution_impl<k>(rng, k, r);
300  internal::u01_oo_distribution_impl<k>(rng, l, r);
301 }
302 
308 
309 } // namespace vsmc
310 
311 #endif // VSMC_RNG_U01_HPP
Definition: monitor.hpp:49
RealType u01_oo(UIntType u) noexcept
Convert uniform unsigned integers to floating points on (0, 1)
Definition: u01.hpp:266
#define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_0(Name, name, T)
Definition: common.hpp:379
#define VSMC_DEFINE_RNG_DISTRIBUTION_0(Name, name, T, t, Type)
Definition: common.hpp:364
uint uint32_t
Definition: opencl.h:39
Standard uniform distribution on (0, 1].
ulong uint64_t
Definition: opencl.h:40
result_type max() const
void u01_oo_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on (0, 1)
typename std::conditional<(RNGBits< RNGType >::value > 32), std::uint64_t, std::uint32_t >::type U01UIntType
RealType u01_co(UIntType u) noexcept
Convert uniform unsigned integers to floating points on [0, 1)
Definition: u01.hpp:250
void u01_distribution(RNGType &, std::size_t, RealType *)
Generate standard uniform random variates.
Standard uniform distribution on (0, 1)
void u01_oo_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
result_type max() const
void u01_co_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on [0, 1)
RealType u01_oc(UIntType u) noexcept
Convert uniform unsigned integers to floating points on (0, 1].
Definition: u01.hpp:258
RealType u01_cc(UIntType u) noexcept
Convert uniform unsigned integers to floating points on [0, 1].
Definition: u01.hpp:242
Standard uniform distribution on [0, 1].
void u01_oc_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
void u01_cc_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on [0, 1].
void u01_cc_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
void uniform_bits_distribution(RNGType &, std::size_t, UIntType *)
Standard uniform distribution.
Definition: common.hpp:597
void u01_co_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
result_type max() const
void u01_oc_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on (0, 1].
void u01_distribution_impl(RNGType &rng, std::size_t n, RealType *r)
Standard uniform distribution on [0, 1)
Generate uniform bits of given type.
result_type max() const
result_type max() const