forked from ROCm/rocRAND
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiprand.hpp
1673 lines (1469 loc) · 53.9 KB
/
hiprand.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 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 HIPRAND_HPP_
#define HIPRAND_HPP_
// At least C++11 required
#if defined(__cplusplus) && __cplusplus >= 201103L
#include <random>
#include <exception>
#include <string>
#include <sstream>
#include <type_traits>
#include <limits>
#include "hiprand.h"
#include "hiprand_kernel.h"
namespace hiprand_cpp {
/// \addtogroup hiprandhostcpp
/// @{
/// \class error
/// \brief A run-time hipRAND error.
///
/// The error class represents an error returned
/// by a hipRAND function.
class error : public std::exception
{
public:
/// hipRAND error code type
typedef hiprandStatus_t error_type;
/// Constructs new error object from error code \p error.
///
/// \param error - error code
error(error_type error) noexcept
: m_error(error),
m_error_string(to_string(error))
{
}
~error() noexcept
{
}
/// 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
{
return m_error_string.c_str();
}
/// Static function which converts the numeric hipRAND
/// error code \p error to a human-readable string.
///
/// If the error code is unknown, a string containing
/// "Unknown hipRAND error" along with the error code
/// \p error will be returned.
static std::string to_string(error_type error)
{
switch(error)
{
case HIPRAND_STATUS_SUCCESS:
return "Success";
case HIPRAND_STATUS_VERSION_MISMATCH:
return "Header file and linked library version do not match";
case HIPRAND_STATUS_NOT_INITIALIZED:
return "Generator was not created using hiprandCreateGenerator";
case HIPRAND_STATUS_ALLOCATION_FAILED:
return "Memory allocation failed during execution";
case HIPRAND_STATUS_TYPE_ERROR:
return "Generator type is wrong";
case HIPRAND_STATUS_OUT_OF_RANGE:
return "Argument out of range";
case HIPRAND_STATUS_LENGTH_NOT_MULTIPLE:
return "Length requested is not a multiple of dimension";
case HIPRAND_STATUS_DOUBLE_PRECISION_REQUIRED:
return "GPU does not have double precision";
case HIPRAND_STATUS_LAUNCH_FAILURE:
return "Kernel launch failure";
case HIPRAND_STATUS_PREEXISTING_FAILURE:
return "Preexisting failure on library entry";
case HIPRAND_STATUS_INITIALIZATION_FAILED:
return "Initialization of HIP failed";
case HIPRAND_STATUS_ARCH_MISMATCH:
return "Architecture mismatch, GPU does not support requested feature";
case HIPRAND_STATUS_INTERNAL_ERROR:
return "Internal library error";
case HIPRAND_STATUS_NOT_IMPLEMENTED:
return "Feature not implemented yet";
default: {
std::stringstream s;
s << "Unknown hipRAND 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^32 - 1].
///
/// \tparam IntType - type of generated values. Only \p unsigned \p char, \p unsigned \p short and \p unsigned \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 int, IntType>::value,
"Only unsigned int type is supported in uniform_int_distribution"
);
public:
typedef IntType result_type;
/// Default constructor
uniform_int_distribution()
{
}
/// Resets distribution's internal state if there is any.
void reset()
{
}
/// Returns the smallest possible value that can be generated.
IntType min() const
{
return 0;
}
/// Returns the largest possible value that can be generated.
IntType max() const
{
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^32 - 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 (`hiprand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: hiprandGenerate(), hiprandGenerateChar(), hiprandGenerateShort()
template<class Generator>
void operator()(Generator& g, IntType * output, size_t size)
{
hiprandStatus_t status;
status = this->generate(g, output, size);
if(status != HIPRAND_STATUS_SUCCESS) throw hiprand_cpp::error(status);
}
/// Returns \c true if the distribution is the same as \p other.
bool operator==(const uniform_int_distribution<IntType>& other)
{
(void) other;
return true;
}
/// Returns \c true if the distribution is different from \p other.
bool operator!=(const uniform_int_distribution<IntType>& other)
{
return !(*this == other);
}
private:
template<class Generator>
hiprandStatus_t generate(Generator& g, unsigned char * output, size_t size)
{
return hiprandGenerateChar(g.m_generator, output, size);
}
template<class Generator>
hiprandStatus_t generate(Generator& g, unsigned short * output, size_t size)
{
return hiprandGenerateShort(g.m_generator, output, size);
}
template<class Generator>
hiprandStatus_t generate(Generator& g, unsigned int * output, size_t size)
{
return hiprandGenerate(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:
typedef RealType result_type;
/// Default constructor
uniform_real_distribution()
{
}
/// Resets distribution's internal state if there is any.
void reset()
{
}
/// Returns the smallest possible value that can be generated.
RealType min() const
{
if(std::is_same<float, RealType>::value)
{
return static_cast<RealType>(2.3283064e-10f);
}
return static_cast<RealType>(2.3283064365386963e-10);
}
/// Returns the largest possible value that can be generated.
RealType max() const
{
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 (`hiprand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: hiprandGenerateUniform(), hiprandGenerateUniformDouble(), hiprandGenerateUniformHalf()
template<class Generator>
void operator()(Generator& g, RealType * output, size_t size)
{
hiprandStatus_t status;
status = this->generate(g, output, size);
if(status != HIPRAND_STATUS_SUCCESS) throw hiprand_cpp::error(status);
}
/// Returns \c true if the distribution is the same as \p other.
bool operator==(const uniform_real_distribution<RealType>& other)
{
(void) other;
return true;
}
/// Returns \c true if the distribution is different from \p other.
bool operator!=(const uniform_real_distribution<RealType>& other)
{
return !(*this == other);
}
private:
template<class Generator>
hiprandStatus_t generate(Generator& g, float * output, size_t size)
{
return hiprandGenerateUniform(g.m_generator, output, size);
}
template<class Generator>
hiprandStatus_t generate(Generator& g, double * output, size_t size)
{
return hiprandGenerateUniformDouble(g.m_generator, output, size);
}
template<class Generator>
hiprandStatus_t generate(Generator& g, half * output, size_t size)
{
return hiprandGenerateUniformHalf(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.
///
/// See also: <a href="https://en.wikipedia.org/wiki/Normal_distribution">Wikipedia:Normal distribution</a>.
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:
typedef RealType result_type;
/// \class param_type
/// \brief The type of the distribution parameter set.
class param_type
{
public:
using distribution_type = normal_distribution<RealType>;
param_type(RealType mean = 0.0, RealType stddev = 1.0)
: m_mean(mean), m_stddev(stddev)
{
}
param_type(const param_type& params) = default;
/// \brief Returns the deviation distribution parameter.
///
/// The default value is 1.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)
{
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)
{
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
normal_distribution(const param_type& params)
: m_params(params)
{
}
/// Resets distribution's internal state if there is any.
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.
RealType min() const
{
return std::numeric_limits<RealType>::lowest();
}
/// Returns the largest possible value that can be generated.
RealType max() const
{
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 (`hiprand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: hiprandGenerateNormal(), hiprandGenerateNormalDouble(), hiprandGenerateNormalHalf()
template<class Generator>
void operator()(Generator& g, RealType * output, size_t size)
{
hiprandStatus_t status;
status = this->generate(g, output, size);
if(status != HIPRAND_STATUS_SUCCESS) throw hiprand_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)
{
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)
{
return !(*this == other);
}
private:
template<class Generator>
hiprandStatus_t generate(Generator& g, float * output, size_t size)
{
return hiprandGenerateNormal(
g.m_generator, output, size, this->mean(), this->stddev()
);
}
template<class Generator>
hiprandStatus_t generate(Generator& g, double * output, size_t size)
{
return hiprandGenerateNormalDouble(
g.m_generator, output, size, this->mean(), this->stddev()
);
}
template<class Generator>
hiprandStatus_t generate(Generator& g, half * output, size_t size)
{
return hiprandGenerateNormalHalf(
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.
///
/// See also: <a href="https://en.wikipedia.org/wiki/Log-normal_distribution">Wikipedia:Log-normal distribution</a>.
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:
typedef RealType result_type;
/// \class param_type
/// \brief The type of the distribution parameter set.
class param_type
{
public:
using distribution_type = lognormal_distribution<RealType>;
param_type(RealType m = 0.0, RealType s = 1.0)
: m_mean(m), m_stddev(s)
{
}
param_type(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)
{
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)
{
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
lognormal_distribution(const param_type& params)
: m_params(params)
{
}
/// Resets distribution's internal state if there is any.
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.
RealType min() const
{
return 0;
}
/// Returns the largest possible value that can be generated.
RealType max() const
{
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 (`hiprand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: hiprandGenerateLogNormal(), hiprandGenerateLogNormalDouble(), hiprandGenerateLogNormalHalf()
template<class Generator>
void operator()(Generator& g, RealType * output, size_t size)
{
hiprandStatus_t status;
status = this->generate(g, output, size);
if(status != HIPRAND_STATUS_SUCCESS) throw hiprand_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)
{
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)
{
return !(*this == other);
}
private:
template<class Generator>
hiprandStatus_t generate(Generator& g, float * output, size_t size)
{
return hiprandGenerateLogNormal(
g.m_generator, output, size, this->m(), this->s()
);
}
template<class Generator>
hiprandStatus_t generate(Generator& g, double * output, size_t size)
{
return hiprandGenerateLogNormalDouble(
g.m_generator, output, size, this->m(), this->s()
);
}
template<class Generator>
hiprandStatus_t generate(Generator& g, half * output, size_t size)
{
return hiprandGenerateLogNormalHalf(
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.
///
/// See also: <a href="https://en.wikipedia.org/wiki/Poisson_distribution">Wikipedia:Poisson distribution</a>.
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:
typedef IntType result_type;
/// \class param_type
/// \brief The type of the distribution parameter set.
class param_type
{
public:
using distribution_type = poisson_distribution<IntType>;
param_type(double mean = 1.0)
: m_mean(mean)
{
}
param_type(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)
{
return m_mean == other.m_mean;
}
/// Returns \c true if the param_type is different from \p other.
bool operator!=(const param_type& other)
{
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
poisson_distribution(const param_type& params)
: m_params(params)
{
}
/// Resets distribution's internal state if there is any.
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.
IntType min() const
{
return 0;
}
/// Returns the largest possible value that can be generated.
IntType max() const
{
return std::numeric_limits<IntType>::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 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 (`hiprand_cpp::sobol32_engine`),
/// then \p size must be a multiple of that generator's dimension.
///
/// See also: hiprandGeneratePoisson()
template<class Generator>
void operator()(Generator& g, IntType * output, size_t size)
{
hiprandStatus_t status;
status = hiprandGeneratePoisson(g.m_generator, output, size, this->mean());
if(status != HIPRAND_STATUS_SUCCESS) throw hiprand_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)
{
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)
{
return !(*this == other);
}
private:
param_type m_params;
};
/// \brief Pseudorandom number engine based Philox algorithm.
///
/// philox4x32_10_engine implements
/// a <a href="https://en.wikipedia.org/wiki/Counter-based_random_number_generator_(CBRNG)">
/// Counter-based random number generator</a> called Philox, which was developed by
/// a group at D. E. Shaw Research.
/// 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 = HIPRAND_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 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()
///
/// See also: hiprandCreateGenerator()
philox4x32_10_engine(seed_type seed_value = DefaultSeed,
offset_type offset_value = 0)
{
hiprandStatus_t status;
status = hiprandCreateGenerator(&m_generator, this->type());
if(status != HIPRAND_STATUS_SUCCESS) throw hiprand_cpp::error(status);
if(offset_value > 0)
{
this->offset(offset_value);
}
this->seed(seed_value);
}
/// \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 - hipRAND generator
philox4x32_10_engine(hiprandGenerator_t& generator)
: m_generator(generator)
{
if(generator == NULL)
{
throw hiprand_cpp::error(HIPRAND_STATUS_NOT_INITIALIZED);
}
generator = NULL;
}
/// Destructs the engine.
///
/// See also: hiprandDestroyGenerator()
~philox4x32_10_engine() noexcept(false)
{
hiprandStatus_t status = hiprandDestroyGenerator(m_generator);
if(status != HIPRAND_STATUS_SUCCESS) throw hiprand_cpp::error(status);
}
/// \brief Sets the random number engine's \p hipStream for kernel launches.
/// \param value - new \p hipStream to use
void stream(hipStream_t value)
{
hiprandStatus_t status = hiprandSetStream(m_generator, value);
if(status != HIPRAND_STATUS_SUCCESS) throw hiprand_cpp::error(status);
}
/// \brief Sets the offset of a random number engine.
///
/// Offset represents a number of the random number engine's states
/// that should be skipped before first value is generated.
///
/// - This operation resets the engine's internal state.
/// - This operation does not change the engine's seed or the number of dimensions.
///
/// \param value - New absolute offset
///
/// See also: hiprandSetGeneratorOffset()
void offset(offset_type value)
{
hiprandStatus_t status = hiprandSetGeneratorOffset(this->m_generator, value);
if(status != HIPRAND_STATUS_SUCCESS) throw hiprand_cpp::error(status);
}
/// \brief Sets the seed of the pseudo-random number engine.
///
/// - This operation resets the engine's internal state.
/// - This operation does not change the engine's offset.
///
/// \param value - New seed value