OpenMPCD
LinearCongruent.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the `OpenMPCD::CUDA::Random::Generators::LinearCongruent` class.
4  */
5 
6 #ifndef OPENMPCD_CUDA_RANDOM_GENERATORS_LINEARCONGRUENT_HPP
7 #define OPENMPCD_CUDA_RANDOM_GENERATORS_LINEARCONGRUENT_HPP
8 
10 #include <OpenMPCD/Exceptions.hpp>
13 
14 #include <boost/static_assert.hpp>
15 #include <boost/type_traits/is_integral.hpp>
16 
17 
18 namespace OpenMPCD
19 {
20 namespace CUDA
21 {
22 
23 /**
24  * Contains functionality related to random numbers.
25  */
26 namespace Random
27 {
28 
29 /**
30  * Contains (pseudo-)random number generators.
31  */
32 namespace Generators
33 {
34 
35 /**
36  * A linear congruential generator (LCG).
37  *
38  * Given a starting value \f$ X_0 \f$, which is the seed, the generator produces
39  * a sequence of numbers defined by
40  * \f[ X_{n+1} = \left( a X_N + c \right) \bmod m \f]
41  * where \f$ a \f$ is the `multiplier`, \f$ c \f$ is the `increment`, and
42  * \f$ m \f$ is the `modulus`. The first value to be generated is \f$ X_1 \f$.
43  *
44  * @tparam T The underlying data type, which must be integral.
45  * @tparam multiplier The `multiplier` \f$ 0 < a < m \f$.
46  * @tparam increment The `increment` \f$ 0 \le c < m \f$.
47  * @tparam modulus The `modulus` \f$ 0 < m \f$.
48  */
49 template<
50  typename T,
51  T multiplier,
52  T increment,
53  T modulus>
55 {
56 public:
57  /**
58  * The constructor.
59  *
60  * @throw OpenMPCD::InvalidArgumentError
61  * If `OPENMPCD_DEBUG` is defined, throws if `seed >= modulus` or
62  * if `seed < 0`.
63  *
64  * @param[in] seed The starting value \f$ 0 \le X_0 < m \f$.
65  */
67  LinearCongruent(const T seed) : current(seed)
68  {
69  BOOST_STATIC_ASSERT(boost::is_integral<T>::value);
70  BOOST_STATIC_ASSERT(0 < modulus);
71  BOOST_STATIC_ASSERT(0 < multiplier);
72  BOOST_STATIC_ASSERT(0 <= increment);
73  BOOST_STATIC_ASSERT(multiplier < modulus);
74  BOOST_STATIC_ASSERT(increment < modulus);
75 
77  seed > 0, InvalidArgumentException);
79  seed < modulus, InvalidArgumentException);
80  }
81 
82 public:
83  /**
84  * Generates the next number.
85  */
88  {
89  current = (multiplier * current + increment) % modulus;
90  return current;
91  }
92 
93 private:
94  T current; ///< The last value returned.
95 }; //class LinearCongruent
96 
97 } //namespace Generators
98 } //namespace Random
99 } //namespace CUDA
100 } //namespace OpenMPCD
101 
102 
103 #endif //OPENMPCD_CUDA_RANDOM_GENERATORS_LINEARCONGRUENT_HPP
CompilerDetection.hpp
Exceptions.hpp
OpenMPCD::CUDA::Random::Generators::LinearCongruent::LinearCongruent
OPENMPCD_CUDA_HOST_AND_DEVICE LinearCongruent(const T seed)
The constructor.
Definition: LinearCongruent.hpp:67
OPENMPCD_CUDA_HOST_AND_DEVICE
#define OPENMPCD_CUDA_HOST_AND_DEVICE
Denotes a function to be callable both from the Host and from a CUDA Device.
Definition: Macros.hpp:15
OPENMPCD_DEBUG_ASSERT.hpp
OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE
#define OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE(assertion, ExceptionType)
Definition: OPENMPCD_DEBUG_ASSERT.hpp:76
OpenMPCD::CUDA::Random::Generators::LinearCongruent::operator()
OPENMPCD_CUDA_HOST_AND_DEVICE T operator()()
Generates the next number.
Definition: LinearCongruent.hpp:87
Macros.hpp
OpenMPCD::InvalidArgumentException
Invalid argument exception.
Definition: Exceptions.hpp:128
OpenMPCD::CUDA::Random::Generators::LinearCongruent
A linear congruential generator (LCG).
Definition: LinearCongruent.hpp:54