IMP logo
IMP Reference Guide  2.5.0
The Integrative Modeling Platform
random_utils.h
Go to the documentation of this file.
1 /**
2  * \file IMP/random_utils.h \brief Random number utility functions used by IMP.
3  *
4  * Copyright 2007-2015 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPKERNEL_RANDOM_UTILS_H
9 #define IMPKERNEL_RANDOM_UTILS_H
10 
11 #include <IMP/kernel_config.h>
12 #include <IMP/Vector.h>
13 #include <IMP/random.h>
14 #ifdef IMP_KERNEL_CUDA_LIB
15 // #warning "random_utils - kernel CUDA_LIB!"
16 #include "IMP/internal/random_number_generation_cuda.h"
17 #else
18 // #warning "random_utils - kernel CUDA_BOOST!"
19 #include "IMP/internal/random_number_generation_boost.h"
20 #endif
21 
22 #include <boost/random/mersenne_twister.hpp>
23 #include <boost/random/uniform_real.hpp>
24 
25 IMPKERNEL_BEGIN_NAMESPACE
26 
27 //! Fill the double array with random normally distributed values.
28 /** The pre-allocated array is filled with n double numbers with
29  random normally distributed values with specified mean and
30  standard deviation.
31 
32  @param v vector array that will be resized to n
33  @param n size of array
34  @param mean mean of normal distribution
35  @param stddev standard deviation
36 
37  @note Implementation relies on random_number_generator (a boost
38  random number generator), or on the CUDA random number
39  generator if kernel is built with IMP_KERNEL_CUDA_LIB cmake
40  flag. Either is initially seeded with get_random_seed().
41 */
42 template<typename RealType>
44 (Vector<RealType>& v, unsigned int n,
45  RealType mean=0.0, RealType stddev=1.0)
46 {
47  if(n==0) return;
48  if(n>v.size())
49  v.resize(n);
50 #ifdef IMP_KERNEL_CUDA_LIB
51  IMPcuda::kernel::internal::init_gpu_rng_once(get_random_seed());
52  IMPcuda::kernel::internal::get_random_numbers_normal_cuda
53  (&v[0], n, mean, stddev);
54 #else
55  internal::get_random_numbers_normal_boost(&v[0], n, mean, stddev);
56 #endif
57 }
58 
59 //! Fill the float array with random normally distributed values.
60 /** Fill a pre-allocated array of n float numbers with random normally
61  distributed values with specified mean and standard deviation.
62 
63  @param v vector array that will be resized to n
64  @param n size of array
65 
66  @note Implementation relies on random_number_generator (a boost
67  random number generator), or on the CUDA random number generator
68  if kernel is built with CUDA flag. Either is initially seeded with
69  get_random_seed().
70  */
71 template<typename RealType>
73 (Vector<RealType>& v, unsigned int n)
74 {
75  if(n==0) return;
76  if(n>v.size())
77  v.resize(n);
78 #ifdef IMP_KERNEL_CUDA_LIB
79  IMPcuda::kernel::internal::init_gpu_rng_once(get_random_seed());
80  IMPcuda::kernel::internal::get_random_numbers_uniform_cuda (&v[0], n);
81 #else
82  internal::get_random_numbers_uniform_boost(&v[0], n);
83 #endif
84 }
85 
86 //! Return a uniformly distributed float number in range [0..1)
87 /** @note the random number is retrieved from a cache of random
88  numbers generated using GPU if compiled with CUDA, or from boost
89  without a cache otherwise.
90  */
92 
93 //! Return a uniformly distributed float number in range [min..max)
94 /** @note the random number is retrieved from a cache of random
95  numbers generated using GPU if compiled with CUDA, or from boost
96  without a cache otherwise.
97  */
98 float get_random_float_uniform(float min, float max);
99 
100 //! Return a uniformly distributed double number in range [0..1)
101 /** @note the random number is retrieved from a cache of random
102  numbers generated using GPU if compiled with CUDA, or from boost
103  without a cache otherwise.
104  */
106 
107 //! Return a uniformly distributed double number in range [min..max)
108 /** @note the random number is retrieved from a cache of random
109  numbers generated using GPU if compiled with CUDA, or from boost
110  without a cache otherwise.
111  */
112 double get_random_double_uniform(double min, double max);
113 
114 /************ implementation of inline functions *******/
115 
116 inline float
118 {
119  // use cache only with cuda
120 #ifdef IMP_KERNEL_CUDA_LIB
121  const static unsigned int cache_n=20000000;
122  static IMP::Vector<float> cache;
123  static unsigned int i=0;
124  if(i>=cache.size()){
125  get_random_numbers_uniform(cache, cache_n);
126  i=0;
127  }
128  return cache[i++];
129 #else
130  static boost::uniform_real<float> rand(0.0, 1.0);
131  return rand(random_number_generator);
132 #endif
133 }
134 
135 inline float
136 get_random_float_uniform(float min, float max)
137 {
138  // use cache only with cuda
139 #ifdef IMP_KERNEL_CUDA_LIB
140  return get_random_float_uniform()*(max-min)+min;
141 #else
142  ::boost::uniform_real<float> rand(min, max);
143  return rand(random_number_generator);
144 #endif
145 }
146 
147 
148 inline double
150 {
151 #ifdef IMP_KERNEL_CUDA_LIB
152  const static unsigned int cache_n=20000000;
153  static IMP::Vector<double> cache;
154  static unsigned int i=0;
155  if(i>=cache.size()){
156  get_random_numbers_uniform(cache, cache_n);
157  i=0;
158  }
159  return cache[i++];
160 #else
161  static boost::uniform_real<double> rand(0.0, 1.0);
162  return rand(random_number_generator);
163 #endif
164 }
165 
166 inline double
167 get_random_double_uniform(double min, double max)
168 {
169 #ifdef IMP_KERNEL_CUDA_LIB
170  return get_random_double_uniform()*(max-min)+min;
171 #else
172  ::boost::uniform_real<double> rand(min, max);
173  return rand(random_number_generator);
174 #endif
175 }
176 
177 
178 
179 IMPKERNEL_END_NAMESPACE
180 
181 #endif /* IMPKERNEL_RANDOM_UTILS_H */
void get_random_numbers_uniform(Vector< RealType > &v, unsigned int n)
Fill the float array with random normally distributed values.
Definition: random_utils.h:73
boost::uint64_t get_random_seed()
Return the initial random seed.
double get_random_double_uniform(double min, double max)
Return a uniformly distributed double number in range [min..max)
Definition: random_utils.h:167
float get_random_float_uniform(float min, float max)
Return a uniformly distributed float number in range [min..max)
Definition: random_utils.h:136
A class for storing lists of IMP items.
void get_random_numbers_normal(Vector< RealType > &v, unsigned int n, RealType mean=0.0, RealType stddev=1.0)
Fill the double array with random normally distributed values.
Definition: random_utils.h:44
Random number generators used by IMP.
RandomNumberGenerator random_number_generator
A shared non-GPU random number generator.