1 #ifndef MSHADOW_TENSOR_RANDOM_H
2 #define MSHADOW_TENSOR_RANDOM_H
18 template<
typename Device>
31 int status = vslNewStream(&vStream_, VSL_BRNG_MT19937, seed);
32 utils::Assert( status == VSL_STATUS_OK,
"MKL VSL Random engine failed to be initialized.\n" );
40 vslDeleteStream(&vStream_);
47 inline void Seed(
int seed ){
49 int status = vslDeleteStream(&vStream_);
51 status = vslNewStream(&vStream_, VSL_BRNG_MT19937, seed);
69 #if MSHADOW_SINGLE_PRECISION
70 int status = vsRngUniform( 0, vStream_, mat.
shape[0], mat[i].
dptr, a, b );
72 int status = vdRngUniform( 0, vStream_, mat.
shape[0], mat[i].
dptr, a, b );
74 utils::Assert(status == VSL_STATUS_OK,
"Failed to generate random number by MKL.\n" );
78 mat[i][j] = this->RandNext()*(b-a) + a;
98 #if MSHADOW_SINGLE_PRECISION
99 int status = vsRngGaussian( 0, vStream_, mat.
shape[0], mat[i].
dptr, mu, sigma );
101 int status = vdRngGaussian( 0, vStream_, mat.
shape[0], mat[i].
dptr, mu, sigma );
103 utils::Assert(status == VSL_STATUS_OK,
"Failed to generate random number by MKL.\n" );
105 real_t g1 = 0.0f, g2 = 0.0f;
108 this->SampleNormal2D( g1, g2 );
109 mat[i][j] = mu + g1 * sigma;
111 mat[i][j] = mu + g2 * sigma;
130 this->SampleGaussian( buffer_, 0.0f, 1.0f );
146 this->SampleUniform( buffer_, 0.0f, 1.0f );
151 inline real_t RandNext(
void ){
152 return static_cast<real_t>(rand()) / (static_cast<real_t>(RAND_MAX)+1.0f);
155 inline real_t RandNext2(
void ){
156 return (static_cast<real_t>( rand() ) + 1.0 ) / (
static_cast<real_t>(RAND_MAX) + 2.0);
166 x = 2.0f * RandNext2() - 1.0f;
167 y = 2.0f * RandNext2() - 1.0f;
169 }
while( s >= 1.0f || s == 0.0f );
170 real_t t = std::sqrt( -2.0f * std::log( s ) / s ) ;
171 xx = x * t; yy = y * t;
176 VSLStreamStatePtr vStream_;
179 TensorContainer<cpu,1> buffer_;
192 Random<gpu>(
int seed) {
193 curandStatus_t status;
194 status = curandCreateGenerator(&gen_, CURAND_RNG_PSEUDO_DEFAULT);
195 utils::Assert(status == CURAND_STATUS_SUCCESS,
"Can not create CURAND Generator");
201 curandStatus_t status;
202 status = curandDestroyGenerator(gen_);
203 utils::Assert(status == CURAND_STATUS_SUCCESS,
"Destory CURAND Gen failed");
209 inline void Seed(
int seed ){
210 curandStatus_t status;
211 status = curandSetPseudoRandomGeneratorSeed(gen_, seed);
212 utils::Assert(status == CURAND_STATUS_SUCCESS,
"Set CURAND seed failed.");
222 inline void SampleUniform(Tensor<gpu, dim> &dst,
real_t a=0.0f,
real_t b=1.0f) {
223 if( a == 0.0f && b == 1.0f ){
224 dst = this->uniform( dst.shape );
226 dst = this->uniform( dst.shape ) *(b-a) + a;
237 inline void SampleGaussian(Tensor<gpu, dim> &dst,
real_t mu = 0.0f,
real_t sigma = 1.0f) {
238 dst = this->gaussian( dst.shape, mu, sigma );
253 inline expr::ReshapeExp<Tensor<gpu,1>,dim,1> gaussian( Shape<dim> shape,
real_t mu=0.0f,
real_t sigma=1.0f){
254 size_t aligned_sz = ((shape.Size() + 1UL)>>1)<<1;
256 buffer_.Resize(
Shape1( aligned_sz ) );
257 buffer_.Resize(
Shape1( shape.Size() ) );
258 curandStatus_t status;
259 #if MSHADOW_SINGLE_PRECISION
260 status = curandGenerateNormal(gen_, buffer_.dptr, aligned_sz , mu, sigma);
262 status = curandGenerateNormalDouble(gen_, buffer_.dptr, buffer_.shape[0], mu, sigma);
264 utils::Assert(status == CURAND_STATUS_SUCCESS,
"CURAND Gen Uniform failed\n");
278 inline expr::ReshapeExp<Tensor<gpu,1>,dim,1> uniform(Shape<dim> shape) {
279 buffer_.Resize(
Shape1( shape.Size() ) );
280 curandStatus_t status;
281 #if MSHADOW_SINGLE_PRECISION
282 status = curandGenerateUniform(gen_, buffer_.dptr, buffer_.shape[0] );
284 status = curandGenerateUniformDouble(gen_, buffer_.dptr, buffer_.shape[0] );
286 utils::Assert(status == CURAND_STATUS_SUCCESS,
"CURAND Gen Uniform failed\n");
291 curandGenerator_t gen_;
293 TensorContainer<gpu, 1> buffer_;
299 #endif // MSHADOW_TENSOR_RANDOM_H
random number generator
Definition: tensor_random.h:19
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:144
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
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:128
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:23
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:91
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:47
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:65
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