1 #ifndef MSHADOW_TENSOR_RANDOM_H
2 #define MSHADOW_TENSOR_RANDOM_H
22 template<
typename Device>
34 unsigned s= std::chrono::system_clock::now().time_since_epoch().count();
43 int status = vslNewStream(&vStream_, VSL_BRNG_MT19937, seed);
44 utils::Assert( status == VSL_STATUS_OK,
"MKL VSL Random engine failed to be initialized.\n" );
53 vslDeleteStream(&vStream_);
60 inline void Seed(
int seed ){
62 int status = vslDeleteStream(&vStream_);
64 status = vslNewStream(&vStream_, VSL_BRNG_MT19937, seed);
73 SampleBinary(src, src);
90 std::uniform_real_distribution<real_t> distribution (a,b);
93 #if MSHADOW_SINGLE_PRECISION
94 int status = vsRngUniform( 0, vStream_, mat.shape[0], mat[i].dptr, a, b );
96 int status = vdRngUniform( 0, vStream_, mat.shape[0], mat[i].dptr, a, b );
98 utils::Assert(status == VSL_STATUS_OK,
"Failed to generate random number by MKL.\n" );
107 dmat[i][j] = distribution(gen_) > smat[i][j] ? 0.0f: 1.0f;
122 std::uniform_real_distribution<real_t> distribution (a,b);
125 #if MSHADOW_SINGLE_PRECISION
126 int status = vsRngUniform( 0, vStream_, mat.
shape[0], mat[i].
dptr, a, b );
128 int status = vdRngUniform( 0, vStream_, mat.
shape[0], mat[i].
dptr, a, b );
130 utils::Assert(status == VSL_STATUS_OK,
"Failed to generate random number by MKL.\n" );
139 mat[i][j] = distribution(gen_);
153 if( sigma <= 0.0f ) {
157 std::normal_distribution<real_t> distribution (mu, sigma);
160 #if MSHADOW_SINGLE_PRECISION
161 int status = vsRngGaussian( 0, vStream_, mat.
shape[0], mat[i].
dptr, mu, sigma );
163 int status = vdRngGaussian( 0, vStream_, mat.
shape[0], mat[i].
dptr, mu, sigma );
165 utils::Assert(status == VSL_STATUS_OK,
"Failed to generate random number by MKL.\n" );
179 mat[i][j] = distribution(gen_);
197 this->SampleGaussian( buffer_, 0.0f, 1.0f );
213 this->SampleUniform( buffer_, 0.0f, 1.0f );
218 inline real_t RandNext(
void ){
219 return static_cast<real_t>(rand()) / (static_cast<real_t>(RAND_MAX)+1.0f);
222 inline real_t RandNext2(
void ){
223 return (static_cast<real_t>( rand() ) + 1.0 ) / (
static_cast<real_t>(RAND_MAX) + 2.0);
233 x = 2.0f * RandNext2() - 1.0f;
234 y = 2.0f * RandNext2() - 1.0f;
236 }
while( s >= 1.0f || s == 0.0f );
237 real_t t = std::sqrt( -2.0f * std::log( s ) / s ) ;
238 xx = x * t; yy = y * t;
243 VSLStreamStatePtr vStream_;
246 TensorContainer<cpu,1> buffer_;
262 Random<gpu>(
int seed) {
263 curandStatus_t status;
264 status = curandCreateGenerator(&gen_, CURAND_RNG_PSEUDO_DEFAULT);
265 utils::Assert(status == CURAND_STATUS_SUCCESS,
"Can not create CURAND Generator");
271 curandStatus_t status;
272 status = curandDestroyGenerator(gen_);
273 utils::Assert(status == CURAND_STATUS_SUCCESS,
"Destory CURAND Gen failed");
279 inline void Seed(
int seed ){
280 curandStatus_t status;
281 status = curandSetPseudoRandomGeneratorSeed(gen_, seed);
282 utils::Assert(status == CURAND_STATUS_SUCCESS,
"Set CURAND seed failed.");
292 inline void SampleUniform(Tensor<gpu, dim> &dst,
real_t a=0.0f,
real_t b=1.0f) {
293 if( a == 0.0f && b == 1.0f ){
294 dst = this->uniform( dst.shape );
296 dst = this->uniform( dst.shape ) *(b-a) + a;
307 inline void SampleGaussian(Tensor<gpu, dim> &dst,
real_t mu = 0.0f,
real_t sigma = 1.0f) {
308 dst = this->gaussian( dst.shape, mu, sigma );
323 inline expr::ReshapeExp<Tensor<gpu,1>,dim,1> gaussian( Shape<dim> shape,
real_t mu=0.0f,
real_t sigma=1.0f){
324 size_t aligned_sz = ((shape.Size() + 1UL)>>1)<<1;
326 buffer_.Resize(
Shape1( aligned_sz ) );
327 buffer_.Resize(
Shape1( shape.Size() ) );
328 curandStatus_t status;
329 #if MSHADOW_SINGLE_PRECISION
330 status = curandGenerateNormal(gen_, buffer_.dptr, aligned_sz , mu, sigma);
332 status = curandGenerateNormalDouble(gen_, buffer_.dptr, buffer_.shape[0], mu, sigma);
334 utils::Assert(status == CURAND_STATUS_SUCCESS,
"CURAND Gen Uniform failed\n");
348 inline expr::ReshapeExp<Tensor<gpu,1>,dim,1> uniform(Shape<dim> shape) {
349 buffer_.Resize(
Shape1( shape.Size() ) );
350 curandStatus_t status;
351 #if MSHADOW_SINGLE_PRECISION
352 status = curandGenerateUniform(gen_, buffer_.dptr, buffer_.shape[0] );
354 status = curandGenerateUniformDouble(gen_, buffer_.dptr, buffer_.shape[0] );
356 utils::Assert(status == CURAND_STATUS_SUCCESS,
"CURAND Gen Uniform failed\n");
361 curandGenerator_t gen_;
363 TensorContainer<gpu, 1> buffer_;
369 #endif // MSHADOW_TENSOR_RANDOM_H
random number generator
Definition: tensor_random.h:23
expr::ReshapeExp< Tensor< cpu, 1 >, dim, 1 > uniform(Shape< dim > shape)
return a temporal expression storing standard uniform [0,1) the temporal tensor is only valid before ...
Definition: tensor_random.h:211
unsigned index_t
type that will be used for index
Definition: tensor_base.h:123
void Assert(bool exp)
assert a expression is true
Definition: tensor_base.h:285
float real_t
type that will be used for content
Definition: tensor_base.h:118
void SampleBinary(Tensor< cpu, dim > &dst, Tensor< cpu, dim > &src)
generate binary data according to a probability matrix
Definition: tensor_random.h:85
header file of tensor data structure and functions covention: this lib requires explicit memory alloc...
device name CPU
Definition: tensor.h:185
const unsigned kRandBufferSize
buffer size for each random number generator
Definition: tensor_base.h:112
expr::ReshapeExp< Tensor< cpu, 1 >, dim, 1 > gaussian(Shape< dim > shape)
return a temporal expression storing standard gaussian random variables the temporal tensor is only v...
Definition: tensor_random.h:195
MSHADOW_XINLINE Shape< 1 > Shape1(index_t s0)
construct a one dimension shape, stride will equal s0
Definition: tensor.h:142
CPU random number generator.
Definition: tensor_random.h:27
real_t * dptr
pointer to the data
Definition: tensor.h:215
MSHADOW_XINLINE Tensor< Device, 2 > FlatTo2D(void) const
flatten the tensor to 2 dimension, collapse the higher dimensions together
Definition: tensor.h:229
void SampleGaussian(Tensor< cpu, dim > &dst, real_t mu=0.0f, real_t sigma=1.0f)
generate data from standard gaussian
Definition: tensor_random.h:152
reshape the content to another shape input: Tensor<Device,dimsrc>: ishape output: Tensor<Device...
Definition: tensor_expr_ext.h:102
Shape< dimension > shape
shape of the tensor
Definition: tensor.h:217
void Seed(int seed)
seed random number generator using this seed
Definition: tensor_random.h:60
void SampleUniform(Tensor< cpu, dim > &dst, real_t a=0.0f, real_t b=1.0f)
generate data from uniform [a,b)
Definition: tensor_random.h:120
tensor container that does memory allocation and resize like STL
MSHADOW_XINLINE size_t Size(void) const
Definition: tensor.h:82
general tensor
Definition: tensor.h:206
ReshapeExp< SrcExp, dimdst, ExpInfo< SrcExp >::kDim > reshape(const Exp< SrcExp, etype > &src, Shape< dimdst > oshape)
a expression that reshapes a tensor to another shape
Definition: tensor_expr_ext.h:406