forked from ROCm/rocRAND
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocrand.hpp
3787 lines (3305 loc) · 125 KB
/
rocrand.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2024 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef ROCRAND_HPP_
#define ROCRAND_HPP_
// At least C++11 required
#if defined(__cplusplus) && __cplusplus >= 201103L
#include "rocrand/rocrand.h"
#include "rocrand/rocrand_kernel.h"
#include <exception>
#include <limits>
#include <random>
#include <sstream>
#include <string>
#include <type_traits>
#include <cassert>
namespace rocrand_cpp {
/// \rocrand_internal \addtogroup rocrandhostcpp
/// @{
/// \class error
/// \brief A run-time rocRAND error.
///
/// The error class represents an error returned
/// by a rocRAND function.
class error : public std::exception
{
public:
/// rocRAND error code type
typedef rocrand_status error_type;
/// Constructs new error object from error code \p error.
///
/// \param error - error code
explicit error(error_type error) noexcept
: m_error(error),
m_error_string(to_string(error))
{
}
/// Returns the numeric error code.
error_type error_code() const noexcept
{
return m_error;
}
/// Returns a string description of the error.
std::string error_string() const noexcept
{
return m_error_string;
}
/// Returns a C-string description of the error.
const char* what() const noexcept override
{
return m_error_string.c_str();
}
/// Static function which converts the numeric rocRAND
/// error code \p error to a human-readable string.
///
/// If the error code is unknown, a string containing
/// "Unknown rocRAND error" along with the error code
/// \p error will be returned.
static std::string to_string(error_type error)
{
switch(error)
{
case ROCRAND_STATUS_SUCCESS:
return "Success";
case ROCRAND_STATUS_VERSION_MISMATCH:
return "Header file and linked library version do not match";
case ROCRAND_STATUS_NOT_CREATED:
return "Generator was not created using rocrand_create_generator";
case ROCRAND_STATUS_ALLOCATION_FAILED:
return "Memory allocation failed during execution";
case ROCRAND_STATUS_TYPE_ERROR:
return "Generator type is wrong";
case ROCRAND_STATUS_OUT_OF_RANGE:
return "Argument out of range";
case ROCRAND_STATUS_LENGTH_NOT_MULTIPLE:
return "Length requested is not a multiple of dimension";
case ROCRAND_STATUS_DOUBLE_PRECISION_REQUIRED:
return "GPU does not have double precision";
case ROCRAND_STATUS_LAUNCH_FAILURE:
return "Kernel launch failure";
case ROCRAND_STATUS_INTERNAL_ERROR:
return "Internal library error";
default: {
std::stringstream s;
s << "Unknown rocRAND error (" << error << ")";
return s.str();
}
}
}
/// Compares two error objects for equality.
friend
bool operator==(const error& l, const error& r)
{
return l.error_code() == r.error_code();
}
/// Compares two error objects for inequality.
friend
bool operator!=(const error& l, const error& r)
{
return !(l == r);
}
private:
error_type m_error;
std::string m_error_string;
};
/// \class uniform_int_distribution
///
/// \brief Produces random integer values uniformly distributed on the interval [0, 2^(sizeof(IntType)*8) - 1].
///
/// \tparam IntType - type of generated values. Only \p unsigned \p char, \p unsigned \p short and \p unsigned \p int and \p unsigned \p long \p long \p int type is supported.
template<class IntType = unsigned int>
class uniform_int_distribution
{
static_assert(std::is_same<unsigned char, IntType>::value
|| std::is_same<unsigned short, IntType>::value
|| std::is_same<unsigned long long int, IntType>::value
|| std::is_same<unsigned int, IntType>::value,
"Only unsigned char, unsigned short, unsigned int and unsigned long long int "
"types are supported in uniform_int_distribution");
public:
/// See description for IntType template parameter.
typedef IntType result_type;
/// Default constructor
uniform_int_distribution()
{
}
/// Resets distribution's internal state if there is any.
static void reset()
{
}
/// Returns the smallest possible value that can be generated.
static constexpr IntType min()
{
return 0;
}
/// Returns the largest possible value that can be generated.
static constexpr IntType max()
{
return std::numeric_limits<IntType>::max();
}
/// \brief Fills \p output with uniformly distributed random integer values.
///
/// Generates \p size random integer values uniformly distributed
/// on the interval [0, 2^(sizeof(IntType)*8) - 1], and stores them into the device memory
/// referenced by \p output pointer.
///
/// \param g - An uniform random number generator object
/// \param output - Pointer to device memory to store results
/// \param size - Number of values to generate
///
/// Requirements:
/// * The device memory pointed by \p output must have been previously allocated
/// and be large enough to store at least \p size values of \p IntType type.
/// * If generator \p g is a quasi-random number generator (`rocrand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: rocrand_generate(), rocrand_generate_char(), rocrand_generate_short()
template<class Generator>
void operator()(Generator& g, IntType * output, size_t size)
{
rocrand_status status;
status = this->generate(g, output, size);
if(status != ROCRAND_STATUS_SUCCESS) throw rocrand_cpp::error(status);
}
/// Returns \c true if the distribution is the same as \p other.
bool operator==(const uniform_int_distribution<IntType>& other) const
{
(void) other;
return true;
}
/// Returns \c true if the distribution is different from \p other.
bool operator!=(const uniform_int_distribution<IntType>& other) const
{
return !(*this == other);
}
private:
template<class Generator>
static rocrand_status generate(Generator& g, unsigned char * output, size_t size)
{
return rocrand_generate_char(g.m_generator, output, size);
}
template<class Generator>
static rocrand_status generate(Generator& g, unsigned short * output, size_t size)
{
return rocrand_generate_short(g.m_generator, output, size);
}
template<class Generator>
static rocrand_status generate(Generator& g, unsigned int * output, size_t size)
{
return rocrand_generate(g.m_generator, output, size);
}
template<class Generator>
static rocrand_status generate(Generator& g, unsigned long long int* output, size_t size)
{
return rocrand_generate_long_long(g.m_generator, output, size);
}
};
/// \class uniform_real_distribution
///
/// \brief Produces random floating-point values uniformly distributed on the interval (0, 1].
///
/// \tparam RealType - type of generated values. Only \p float, \p double and \p half types are supported.
template<class RealType = float>
class uniform_real_distribution
{
static_assert(
std::is_same<float, RealType>::value
|| std::is_same<double, RealType>::value
|| std::is_same<half, RealType>::value,
"Only float, double, and half types are supported in uniform_real_distribution"
);
public:
/// See description for RealType template parameter.
typedef RealType result_type;
/// Default constructor
uniform_real_distribution()
{
}
/// Resets distribution's internal state if there is any.
static void reset()
{
}
/// Returns the smallest possible value that can be generated.
static constexpr RealType min()
{
return static_cast<RealType>(ROCRAND_2POW32_INV_DOUBLE);
}
/// Returns the largest possible value that can be generated.
static constexpr RealType max()
{
return 1.0;
}
/// \brief Fills \p output with uniformly distributed random floating-point values.
///
/// Generates \p size random floating-point values uniformly distributed
/// on the interval (0, 1], and stores them into the device memory referenced
/// by \p output pointer.
///
/// \param g - An uniform random number generator object
/// \param output - Pointer to device memory to store results
/// \param size - Number of values to generate
///
/// Requirements:
/// * The device memory pointed by \p output must have been previously allocated
/// and be large enough to store at least \p size values of \p RealType type.
/// * If generator \p g is a quasi-random number generator (`rocrand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: rocrand_generate_uniform(), rocrand_generate_uniform_double(), rocrand_generate_uniform_half()
template<class Generator>
void operator()(Generator& g, RealType * output, size_t size)
{
rocrand_status status;
status = this->generate(g, output, size);
if(status != ROCRAND_STATUS_SUCCESS) throw rocrand_cpp::error(status);
}
/// Returns \c true if the distribution is the same as \p other.
bool operator==(const uniform_real_distribution<RealType>& other) const
{
(void) other;
return true;
}
/// Returns \c true if the distribution is different from \p other.
bool operator!=(const uniform_real_distribution<RealType>& other) const
{
return !(*this == other);
}
private:
template<class Generator>
static rocrand_status generate(Generator& g, float * output, size_t size)
{
return rocrand_generate_uniform(g.m_generator, output, size);
}
template<class Generator>
static rocrand_status generate(Generator& g, double * output, size_t size)
{
return rocrand_generate_uniform_double(g.m_generator, output, size);
}
template<class Generator>
static rocrand_status generate(Generator& g, half * output, size_t size)
{
return rocrand_generate_uniform_half(g.m_generator, output, size);
}
};
/// \class normal_distribution
///
/// \brief Produces random numbers according to a normal distribution.
///
/// \tparam RealType - type of generated values. Only \p float, \p double and \p half types are supported.
template<class RealType = float>
class normal_distribution
{
static_assert(
std::is_same<float, RealType>::value
|| std::is_same<double, RealType>::value
|| std::is_same<half, RealType>::value,
"Only float, double and half types are supported in normal_distribution"
);
public:
/// See description for RealType template parameter.
typedef RealType result_type;
/// \class param_type
/// \brief The type of the distribution parameter set.
class param_type
{
public:
/// Alias for convenience
using distribution_type = normal_distribution<RealType>;
/// \brief Constructs a \p param_type object with the
/// given distribution parameters.
/// \param mean - mean
/// \param stddev - standard deviation
param_type(RealType mean = 0.0, RealType stddev = 1.0)
: m_mean(mean), m_stddev(stddev)
{
}
/// Copy constructor
param_type(const param_type& params) = default;
/// Copy assignment operator
param_type& operator=(const param_type& params) = default;
/// \brief Returns the deviation distribution parameter.
///
/// The default value is 0.0.
RealType mean() const
{
return m_mean;
}
/// \brief Returns the standard deviation distribution parameter.
///
/// The default value is 1.0.
RealType stddev() const
{
return m_stddev;
}
/// Returns \c true if the param_type is the same as \p other.
bool operator==(const param_type& other) const
{
return m_mean == other.m_mean && m_stddev == other.m_stddev;
}
/// Returns \c true if the param_type is different from \p other.
bool operator!=(const param_type& other) const
{
return !(*this == other);
}
private:
RealType m_mean;
RealType m_stddev;
};
/// \brief Constructs a new distribution object.
/// \param mean - A mean distribution parameter
/// \param stddev - A standard deviation distribution parameter
normal_distribution(RealType mean = 0.0, RealType stddev = 1.0)
: m_params(mean, stddev)
{
}
/// \brief Constructs a new distribution object.
/// \param params - Distribution parameters
explicit normal_distribution(const param_type& params)
: m_params(params)
{
}
/// Resets distribution's internal state if there is any.
static void reset()
{
}
/// \brief Returns the mean distribution parameter.
///
/// The mean specifies the location of the peak. The default value is 0.0.
RealType mean() const
{
return m_params.mean();
}
/// \brief Returns the standard deviation distribution parameter.
///
/// The default value is 1.0.
RealType stddev() const
{
return m_params.stddev();
}
/// Returns the smallest possible value that can be generated.
static constexpr RealType min()
{
return std::numeric_limits<RealType>::lowest();
}
/// Returns the largest possible value that can be generated.
static constexpr RealType max()
{
return std::numeric_limits<RealType>::max();
}
/// Returns the distribution parameter object
param_type param() const
{
return m_params;
}
/// Sets the distribution parameter object
void param(const param_type& params)
{
m_params = params;
}
/// \brief Fills \p output with normally distributed random floating-point values.
///
/// Generates \p size random floating-point values distributed according to a normal distribution,
/// and stores them into the device memory referenced by \p output pointer.
///
/// \param g - An uniform random number generator object
/// \param output - Pointer to device memory to store results
/// \param size - Number of values to generate
///
/// Requirements:
/// * The device memory pointed by \p output must have been previously allocated
/// and be large enough to store at least \p size values of \p RealType type.
/// * Pointer \p output must be aligned to <tt>2 * sizeof(RealType)</tt> bytes.
/// * \p size must be even.
/// * If generator \p g is a quasi-random number generator (`rocrand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: rocrand_generate_normal(), rocrand_generate_normal_double(), rocrand_generate_normal_half()
template<class Generator>
void operator()(Generator& g, RealType * output, size_t size)
{
rocrand_status status;
status = this->generate(g, output, size);
if(status != ROCRAND_STATUS_SUCCESS) throw rocrand_cpp::error(status);
}
/// \brief Returns \c true if the distribution is the same as \p other.
///
/// Two distribution are equal, if their parameters are equal.
bool operator==(const normal_distribution<RealType>& other) const
{
return this->m_params == other.m_params;
}
/// \brief Returns \c true if the distribution is different from \p other.
///
/// Two distribution are equal, if their parameters are equal.
bool operator!=(const normal_distribution<RealType>& other) const
{
return !(*this == other);
}
private:
template<class Generator>
rocrand_status generate(Generator& g, float * output, size_t size)
{
return rocrand_generate_normal(
g.m_generator, output, size, this->mean(), this->stddev()
);
}
template<class Generator>
rocrand_status generate(Generator& g, double * output, size_t size)
{
return rocrand_generate_normal_double(
g.m_generator, output, size, this->mean(), this->stddev()
);
}
template<class Generator>
rocrand_status generate(Generator& g, half * output, size_t size)
{
return rocrand_generate_normal_half(
g.m_generator, output, size, this->mean(), this->stddev()
);
}
param_type m_params;
};
/// \class lognormal_distribution
///
/// \brief Produces positive random numbers according to a log-normal distribution.
///
/// \tparam RealType - type of generated values. Only \p float, \p double and \p half types are supported.
template<class RealType = float>
class lognormal_distribution
{
static_assert(
std::is_same<float, RealType>::value
|| std::is_same<double, RealType>::value
|| std::is_same<half, RealType>::value,
"Only float, double and half types are supported in lognormal_distribution"
);
public:
/// See description for RealType template parameter.
typedef RealType result_type;
/// \class param_type
/// \brief The type of the distribution parameter set.
class param_type
{
public:
/// Alias for convenience
using distribution_type = lognormal_distribution<RealType>;
/// \brief Constructs a \p param_type object with the
/// given distribution parameters.
/// \param m - mean
/// \param s - standard deviation
param_type(RealType m = 0.0, RealType s = 1.0)
: m_mean(m), m_stddev(s)
{
}
/// Copy constructor
param_type(const param_type& params) = default;
/// Copy assignment operator
param_type& operator=(const param_type& params) = default;
/// \brief Returns the deviation distribution parameter.
///
/// The default value is 1.0.
RealType m() const
{
return m_mean;
}
/// \brief Returns the deviation distribution parameter.
///
/// The default value is 1.0.
RealType s() const
{
return m_stddev;
}
/// Returns \c true if the param_type is the same as \p other.
bool operator==(const param_type& other) const
{
return m_mean == other.m_mean && m_stddev == other.m_stddev;
}
/// Returns \c true if the param_type is different from \p other.
bool operator!=(const param_type& other) const
{
return !(*this == other);
}
private:
RealType m_mean;
RealType m_stddev;
};
/// \brief Constructs a new distribution object.
/// \param m - A mean distribution parameter
/// \param s - A standard deviation distribution parameter
lognormal_distribution(RealType m = 0.0, RealType s = 1.0)
: m_params(m, s)
{
}
/// \brief Constructs a new distribution object.
/// \param params - Distribution parameters
explicit lognormal_distribution(const param_type& params)
: m_params(params)
{
}
/// Resets distribution's internal state if there is any.
static void reset()
{
}
/// \brief Returns the mean distribution parameter.
///
/// The mean specifies the location of the peak. The default value is 0.0.
RealType m() const
{
return m_params.m();
}
/// \brief Returns the standard deviation distribution parameter.
///
/// The default value is 1.0.
RealType s() const
{
return m_params.s();
}
/// Returns the distribution parameter object
param_type param() const
{
return m_params;
}
/// Sets the distribution parameter object
void param(const param_type& params)
{
m_params = params;
}
/// Returns the smallest possible value that can be generated.
static constexpr RealType min()
{
return 0;
}
/// Returns the largest possible value that can be generated.
static RealType max()
{
return std::numeric_limits<RealType>::max();
}
/// \brief Fills \p output with log-normally distributed random floating-point values.
///
/// Generates \p size random floating-point values (greater than zero) distributed according
/// to a log-normal distribution, and stores them into the device memory referenced
/// by \p output pointer.
///
/// \param g - An uniform random number generator object
/// \param output - Pointer to device memory to store results
/// \param size - Number of values to generate
///
/// Requirements:
/// * The device memory pointed by \p output must have been previously allocated
/// and be large enough to store at least \p size values of \p RealType type.
/// * Pointer \p output must be aligned to <tt>2 * sizeof(RealType)</tt> bytes.
/// * \p size must be even.
/// * If generator \p g is a quasi-random number generator (`rocrand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: rocrand_generate_log_normal(), rocrand_generate_log_normal_double()
template<class Generator>
void operator()(Generator& g, RealType * output, size_t size)
{
rocrand_status status;
status = this->generate(g, output, size);
if(status != ROCRAND_STATUS_SUCCESS) throw rocrand_cpp::error(status);
}
/// \brief Returns \c true if the distribution is the same as \p other.
///
/// Two distribution are equal, if their parameters are equal.
bool operator==(const lognormal_distribution<RealType>& other) const
{
return this->m_params == other.m_params;
}
/// \brief Returns \c true if the distribution is different from \p other.
///
/// Two distribution are equal, if their parameters are equal.
bool operator!=(const lognormal_distribution<RealType>& other) const
{
return !(*this == other);
}
private:
template<class Generator>
rocrand_status generate(Generator& g, float * output, size_t size)
{
return rocrand_generate_log_normal(
g.m_generator, output, size, this->m(), this->s()
);
}
template<class Generator>
rocrand_status generate(Generator& g, double * output, size_t size)
{
return rocrand_generate_log_normal_double(
g.m_generator, output, size, this->m(), this->s()
);
}
template<class Generator>
rocrand_status generate(Generator& g, half * output, size_t size)
{
return rocrand_generate_log_normal_half(
g.m_generator, output, size, this->m(), this->s()
);
}
param_type m_params;
};
/// \class poisson_distribution
///
/// \brief Produces random non-negative integer values distributed according to Poisson distribution.
///
/// \tparam IntType - type of generated values. Only \p unsinged \p int type is supported.
template<class IntType = unsigned int>
class poisson_distribution
{
static_assert(
std::is_same<unsigned int, IntType>::value,
"Only unsigned int type is supported in poisson_distribution"
);
public:
/// See description for IntType template parameter.
typedef IntType result_type;
/// \class param_type
/// \brief The type of the distribution parameter set.
class param_type
{
public:
/// Alias for convenience.
using distribution_type = poisson_distribution<IntType>;
/// \brief Constructs a \p param_type object with the
/// given mean.
/// \param mean - mean to use for the distribution
param_type(double mean = 1.0)
: m_mean(mean)
{
}
/// Copy constructor
param_type(const param_type& params) = default;
/// Copy assignment operator
param_type& operator=(const param_type& params) = default;
/// \brief Returns the mean distribution parameter.
///
/// The mean (also known as lambda) is the average number
/// of events per interval. The default value is 1.0.
double mean() const
{
return m_mean;
}
/// Returns \c true if the param_type is the same as \p other.
bool operator==(const param_type& other) const
{
return m_mean == other.m_mean;
}
/// Returns \c true if the param_type is different from \p other.
bool operator!=(const param_type& other) const
{
return !(*this == other);
}
private:
double m_mean;
};
/// \brief Constructs a new distribution object.
/// \param mean - A mean distribution parameter.
poisson_distribution(double mean = 1.0)
: m_params(mean)
{
}
/// \brief Constructs a new distribution object.
/// \param params - Distribution parameters
explicit poisson_distribution(const param_type& params)
: m_params(params)
{
}
/// Resets distribution's internal state if there is any.
static void reset()
{
}
/// \brief Returns the mean distribution parameter.
///
/// The mean (also known as lambda) is the average number
/// of events per interval. The default value is 1.0.
double mean() const
{
return m_params.mean();
}
/// Returns the smallest possible value that can be generated.
static constexpr IntType min()
{
return 0;
}
/// Returns the largest possible value that can be generated.
static constexpr IntType max()
{
return std::numeric_limits<IntType>::max();
}
/// Returns the distribution parameter object
param_type param()
{
return m_params;
}
/// Sets the distribution parameter object
void param(const param_type& params)
{
m_params = params;
}
/// \brief Fills \p output with random non-negative integer values
/// distributed according to Poisson distribution.
///
/// Generates \p size random non-negative integer values distributed according
/// to Poisson distribution, and stores them into the device memory referenced
/// by \p output pointer.
///
/// \param g - An uniform random number generator object
/// \param output - Pointer to device memory to store results
/// \param size - Number of values to generate
///
/// Requirements:
/// * The device memory pointed by \p output must have been previously allocated
/// and be large enough to store at least \p size values of \p IntType type.
/// * If generator \p g is a quasi-random number generator (`rocrand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: rocrand_generate_poisson()
template<class Generator>
void operator()(Generator& g, IntType * output, size_t size)
{
rocrand_status status;
status = rocrand_generate_poisson(g.m_generator, output, size, this->mean());
if(status != ROCRAND_STATUS_SUCCESS) throw rocrand_cpp::error(status);
}
/// \brief Returns \c true if the distribution is the same as \p other.
///
/// Two distribution are equal, if their parameters are equal.
bool operator==(const poisson_distribution<IntType>& other) const
{
return this->m_params == other.m_params;
}
/// \brief Returns \c true if the distribution is different from \p other.
///
/// Two distribution are equal, if their parameters are equal.
bool operator!=(const poisson_distribution<IntType>& other) const
{
return !(*this == other);
}
private:
param_type m_params;
};
/// \brief Pseudorandom number engine based Philox algorithm.
///
/// It generates random numbers of type \p unsigned \p int on the interval [0; 2^32 - 1].
/// Random numbers are generated in sets of four.
template<unsigned long long DefaultSeed = ROCRAND_PHILOX4x32_DEFAULT_SEED>
class philox4x32_10_engine
{
public:
/// \typedef result_type
/// Type of values generated by the random number engine.
typedef unsigned int result_type;
// \typedef order_type
/// Pseudo-random number engine ordering type.
/// Represents the ordering of the results of a random number engine.
///
/// See also: order()
typedef rocrand_ordering order_type;
/// \typedef offset_type
/// Pseudo-random number engine offset type.
/// Offset represents a number of the random number engine's states
/// that should be skipped before first value is generated.
///
/// See also: offset()
typedef unsigned long long offset_type;
/// \typedef seed_type
/// Pseudo-random number engine seed type definition.
///
/// See also: seed()
typedef unsigned long long seed_type;
/// \brief The default seed equal to \p DefaultSeed.
static constexpr seed_type default_seed = DefaultSeed;
/// \brief Constructs the pseudo-random number engine.
///
/// \param seed_value - seed value to use in the initialization of the internal state, see also seed()
/// \param offset_value - number of internal states that should be skipped, see also offset()
/// \param order_value - ordering of the sequences generated by the engine, see also order()
///
/// See also: rocrand_create_generator()
philox4x32_10_engine(seed_type seed_value = DefaultSeed,
offset_type offset_value = 0,
order_type order_value = ROCRAND_ORDERING_PSEUDO_DEFAULT)
{
rocrand_status status;
status = rocrand_create_generator(&m_generator, this->type());
if(status != ROCRAND_STATUS_SUCCESS) throw rocrand_cpp::error(status);
try
{
if(offset_value > 0)
{
this->offset(offset_value);
}
this->order(order_value);
this->seed(seed_value);
}
catch(...)
{
(void)rocrand_destroy_generator(m_generator);
throw;
}
}
/// \brief Constructs the pseudo-random number engine.
///
/// The pseudo-random number engine will be created using \p generator.
/// The constructed engine take ownership over \p generator, and sets
/// passed reference to \p NULL. The lifetime of \p generator is now
/// bound to the lifetime of the engine.
///
/// \param generator - rocRAND generator
explicit philox4x32_10_engine(rocrand_generator& generator)
: m_generator(generator)
{
if(generator == NULL)
{
throw rocrand_cpp::error(ROCRAND_STATUS_NOT_CREATED);
}
generator = NULL;
}
philox4x32_10_engine(const philox4x32_10_engine&) = delete;
philox4x32_10_engine& operator=(const philox4x32_10_engine&) = delete;
/// \brief Move construct from an other engine, moving the state over.
///