Skip to content

Commit

Permalink
Add dSFMT wrapper API to Random
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed May 6, 2013
1 parent c5f7097 commit 41f4a1d
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 23 deletions.
94 changes: 80 additions & 14 deletions src/shogun/mathematics/Random.cpp
Expand Up @@ -29,6 +29,7 @@ CRandom::~CRandom()
{
SG_FREE(m_sfmt_32);
SG_FREE(m_sfmt_64);
SG_FREE(m_dsfmt);
}

void CRandom::set_seed(uint32_t seed)
Expand All @@ -45,48 +46,113 @@ void CRandom::init()
{
m_sfmt_32 = SG_MALLOC(sfmt_t, 1);
m_sfmt_64 = SG_MALLOC(sfmt_t, 1);
m_dsfmt = SG_MALLOC(dsfmt_t, 1);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_INIT(&m_state_lock);
#endif
reinit(m_seed);
}

uint32_t CRandom::random_32()
uint32_t CRandom::random_32() const
{
return sfmt_genrand_uint32(m_sfmt_32);
}

uint64_t CRandom::random_64()
uint64_t CRandom::random_64() const
{
return sfmt_genrand_uint64(m_sfmt_64);
}

void CRandom::fill_array_32(uint32_t* array, int size)
void CRandom::fill_array(uint32_t* array, int32_t size) const
{
sfmt_fill_array32(m_sfmt_32, array, size);
if ((size >= sfmt_get_min_array_size32(m_sfmt_32)) && (size % 4) == 0)
{
sfmt_fill_array32(m_sfmt_32, array, size);
}
else
{
for (int32_t i=0; i < size; i++)
array[i] = random_32();
}
}

void CRandom::fill_array_64(uint64_t* array, int size)
void CRandom::fill_array(uint64_t* array, int32_t size) const
{
sfmt_fill_array64(m_sfmt_64, array, size);
if ((size >= sfmt_get_min_array_size64(m_sfmt_64)) && (size % 2) == 0)
{
sfmt_fill_array64(m_sfmt_64, array, size);
}
else
{
for (int32_t i=0; i < size; i++)
array[i] = random_64();
}
}

float64_t CRandom::random_close()

void CRandom::fill_array_oc(float64_t* array, int32_t size) const
{
return sfmt_genrand_real1(m_sfmt_32);
if ((size >= dsfmt_get_min_array_size()) && (size % 2) == 0)
{
dsfmt_fill_array_open_close(m_dsfmt, array, size);
}
else
{
for (int32_t i=0; i < size; i++)
array[i] = dsfmt_genrand_open_close(m_dsfmt);
}
}

void CRandom::fill_array_co(float64_t* array, int32_t size) const
{
if ((size >= dsfmt_get_min_array_size()) && (size % 2) == 0)
{
dsfmt_fill_array_close_open(m_dsfmt, array, size);
}
else
{
for (int32_t i=0; i < size; i++)
array[i] = dsfmt_genrand_close_open(m_dsfmt);
}
}

void CRandom::fill_array_oo(float64_t* array, int32_t size) const
{
if ((size >= dsfmt_get_min_array_size()) && (size % 2) == 0)
{
dsfmt_fill_array_open_open(m_dsfmt, array, size);
}
else
{
for (int32_t i=0; i < size; i++)
array[i] = dsfmt_genrand_open_open(m_dsfmt);
}
}

void CRandom::fill_array_c1o2(float64_t* array, int32_t size) const
{
if ((size >= dsfmt_get_min_array_size()) && (size % 2) == 0)
{
dsfmt_fill_array_close1_open2(m_dsfmt, array, size);
}
else
{
for (int32_t i=0; i < size; i++)
array[i] = dsfmt_genrand_close1_open2(m_dsfmt);
}
}

float64_t CRandom::random_open()
float64_t CRandom::random_close() const
{
return sfmt_genrand_real3(m_sfmt_32);
return sfmt_genrand_real1(m_sfmt_32);
}

float64_t CRandom::random_half_open()
float64_t CRandom::random_open() const
{
return sfmt_genrand_real2(m_sfmt_32);
return dsfmt_genrand_open_open(m_dsfmt);
}

float64_t CRandom::random_float_res53()
float64_t CRandom::random_half_open() const
{
return sfmt_genrand_res53(m_sfmt_64);
return dsfmt_genrand_close_open(m_dsfmt);
}
96 changes: 87 additions & 9 deletions src/shogun/mathematics/Random.h
Expand Up @@ -14,6 +14,7 @@
#include <shogun/base/SGObject.h>

#include <shogun/lib/external/SFMT/SFMT.h>
#include <shogun/lib/external/dSFMT/dSFMT.h>

#define RAND_MAX_32 4294967296.0
#define RAND_MAX_64 18446744073709551616.0L
Expand Down Expand Up @@ -47,18 +48,91 @@ namespace shogun
*/
uint32_t get_seed() const;

uint32_t random_32();

uint64_t random_64();
/**
* Generate an unsinged 32-bit random integer
*
* @return the random 32-bit unsigned integer
*/
uint32_t random_32() const;

/**
* Generate an unsinged 64-bit random integer
*
* @return the random 64-bit unsigned integer
*/
uint64_t random_64() const;

/**
* Fill an array of unsinged 32 bit integer
*
* @param array 32-bit unsigened int array to be filled
* @param size size of the array
*/
void fill_array(uint32_t* array, int32_t size) const;

/**
* Fill an array of unsinged 64 bit integer
*
* @param array 64-bit unsigened int array to be filled
* @param size size of the array
*/
void fill_array(uint64_t* array, int32_t size) const;

/**
* Fills an array of float64_t with randoms
* from the (0,1] interval
*
* @param array
* @param size
*/
void fill_array_oc(float64_t* array, int32_t size) const;

void fill_array_32(uint32_t* array, int size);
/**
* Fills an array of float64_t with randoms
* from the [0,1) interval
*
* @param array
* @param size
*/
void fill_array_co(float64_t* array, int32_t size) const;

void fill_array_64(uint64_t* array, int size);
/**
* Fills an array of float64_t with randoms
* from the (0,1) interval
*
* @param array
* @param size
*/
void fill_array_oo(float64_t* array, int32_t size) const;

/**
* Fills an array of float64_t with randoms
* from the [1,2) interval
*
* @param array
* @param size
*/

float64_t random_close();
float64_t random_open();
float64_t random_half_open();
float64_t random_float_res53();
void fill_array_c1o2(float64_t* array, int32_t size) const;

/**
* Get random
* @return a float64_t random from [0,1] interval
*/
float64_t random_close() const;

/**
* Get random
* @return a float64_t random from (0,1) interval
*/
float64_t random_open() const;

/**
* Get random
*
* @return a float64_t random from [0,1) interval
*/
float64_t random_half_open() const;

virtual const char* get_name() const { return "Random"; }

Expand All @@ -78,6 +152,7 @@ namespace shogun
m_seed = seed;
sfmt_init_gen_rand(m_sfmt_32, m_seed);
sfmt_init_gen_rand(m_sfmt_64, m_seed);
dsfmt_init_gen_rand(m_dsfmt, m_seed);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_state_lock);
#endif
Expand All @@ -93,6 +168,9 @@ namespace shogun
/** SFMT struct for 64-bit random */
sfmt_t* m_sfmt_64;

/** dSFMT struct */
dsfmt_t* m_dsfmt;

#ifdef HAVE_PTHREAD
/** state lock */
PTHREAD_LOCK_T m_state_lock;
Expand Down

0 comments on commit 41f4a1d

Please sign in to comment.