Skip to content

Commit

Permalink
add C-11 random feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeLing committed Jul 19, 2017
1 parent fc97d5d commit e5a20fe
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 119 deletions.
3 changes: 1 addition & 2 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -36,7 +36,6 @@

namespace shogun
{
extern uint32_t sg_random_seed;
#ifdef HAVE_CXX11
typedef std::unordered_map<BaseTag, Any> ParametersMap;
#else
Expand Down Expand Up @@ -495,7 +494,7 @@ void CSGObject::init()
m_parameters = new Parameter();
m_model_selection_parameters = new Parameter();
m_gradient_parameters=new Parameter();
m_rng = std::unique_ptr<CRandom>(new CRandom(sg_random_seed));
m_rng = std::unique_ptr<CRandom>(new CRandom());
m_generic = PT_NOT_GENERIC;
m_load_pre_called = false;
m_load_post_called = false;
Expand Down
29 changes: 1 addition & 28 deletions src/shogun/base/init.cpp
Expand Up @@ -31,15 +31,6 @@ shogun::CMap<void*, shogun::MemoryBlock>* sg_mallocs=NULL;
#include <google/protobuf/stubs/common.h>
#endif

#ifdef _WIN32
#define _CRT_RAND_S
#include <stdlib.h>
#endif

#ifdef DEV_RANDOM
#include <fcntl.h>
#endif

namespace shogun
{
Parallel* sg_parallel=NULL;
Expand Down Expand Up @@ -235,25 +226,7 @@ namespace shogun

uint32_t generate_seed()
{
uint32_t seed;
#if defined(_WIN32)
rand_s(&seed);
#elif defined(HAVE_ARC4RANDOM)
seed = arc4random();
#elif defined(DEV_RANDOM)
int fd = open(DEV_RANDOM, O_RDONLY);
ASSERT(fd >= 0);
ssize_t actual_read =
read(fd, reinterpret_cast<char*>(&seed), sizeof(seed));
close(fd);
ASSERT(actual_read == sizeof(seed));
#else
SG_SWARNING("Not safe seed for the PRNG\n");
struct timeval tv;
gettimeofday(&tv, NULL);
seed = (uint32_t)(4223517 * getpid() * tv.tv_sec * tv.tv_usec);
#endif
return seed;
return std::random_device()();
}

void set_global_seed(uint32_t seed)
Expand Down
184 changes: 99 additions & 85 deletions src/shogun/base/init.h
Expand Up @@ -14,6 +14,7 @@
#include <shogun/lib/common.h>
#include <shogun/lib/config.h>

#include <random>
#include <stdio.h>

namespace shogun
Expand All @@ -24,91 +25,104 @@ namespace shogun
class Parallel;
class CRandom;
class SGLinalg;

/** This function must be called before libshogun is used. Usually shogun does
* not provide any output messages (neither debugging nor error; apart from
* exceptions). This function allows one to specify customized output
* callback functions and a callback function to check for exceptions:
*
* @param print_message function pointer to print a message
* @param print_warning function pointer to print a warning message
* @param print_error function pointer to print an error message (this will be
* printed before shogun throws an exception)
*
* @param cancel_computations function pointer to check for exception
*
*/
void init_shogun(void (*print_message)(FILE* target, const char* str) = NULL,
void (*print_warning)(FILE* target, const char* str) = NULL,
void (*print_error)(FILE* target, const char* str) = NULL,
void (*cancel_computations)(bool &delayed, bool &immediately)=NULL);

/** init shogun with defaults */
void init_shogun_with_defaults();

/** This function must be called when one stops using libshogun. It will
* perform a number of cleanups */
void exit_shogun();

/** set the global io object
*
* @param io io object to use
*/
void set_global_io(SGIO* io);

/** get the global io object
*
* @return io object
*/
SGIO* get_global_io();

/** set the global parallel object
*
* @param parallel parallel object to use
*/
void set_global_parallel(Parallel* parallel);

/** get the global parallel object
*
* @return parallel object
*/
Parallel* get_global_parallel();

/** set the global version object
*
* @param version version object to use
*/
void set_global_version(Version* version);

/** get the global version object
*
* @return version object
*/
Version* get_global_version();

/** set the global math object
*
* @param math math object to use
*/
void set_global_math(CMath* math);

/** get the global math object
*
* @return math object
*/
CMath* get_global_math();

/** Set global random seed
* @param seed seed for random generator
*/
void set_global_seed(uint32_t seed);

/** get global random seed
* @return random seed
*/
uint32_t get_global_seed();

uint32_t generate_seed();
extern uint32_t sg_random_seed;

/** This function must be called before libshogun is used. Usually shogun
* does not provide any output messages (neither debugging nor error; apart
* from exceptions). This function allows one to specify customized output
* callback functions and a callback function to check for exceptions:
*
* @param print_message function pointer to print a message
* @param print_warning function pointer to print a warning message
* @param print_error function pointer to print an error message (this will
* be printed before shogun throws an exception)
*
* @param cancel_computations function pointer to check for exception
*
*/
void init_shogun(
void (*print_message)(FILE* target, const char* str) = NULL,
void (*print_warning)(FILE* target, const char* str) = NULL,
void (*print_error)(FILE* target, const char* str) = NULL,
void (*cancel_computations)(bool& delayed, bool& immediately) = NULL);

/** init shogun with defaults */
void init_shogun_with_defaults();

/** This function must be called when one stops using libshogun. It will
* perform a number of cleanups */
void exit_shogun();

/** set the global io object
*
* @param io io object to use
*/
void set_global_io(SGIO* io);

/** get the global io object
*
* @return io object
*/
SGIO* get_global_io();

/** set the global parallel object
*
* @param parallel parallel object to use
*/
void set_global_parallel(Parallel* parallel);

/** get the global parallel object
*
* @return parallel object
*/
Parallel* get_global_parallel();

/** set the global version object
*
* @param version version object to use
*/
void set_global_version(Version* version);

/** get the global version object
*
* @return version object
*/
Version* get_global_version();

/** set the global math object
*
* @param math math object to use
*/
void set_global_math(CMath* math);

/** get the global math object
*
* @return math object
*/
CMath* get_global_math();

/** Set global random seed
* @param seed seed for random generator
*/
void set_global_seed(uint32_t seed);

/** get global random seed
* @return random seed
*/
uint32_t get_global_seed();

/**
* Generate a seed for PRNG
*
* @return entropy for PRNG
*/
uint32_t generate_seed();

template <typename T = std::mt19937>
T get_prng()
{
return T(sg_random_seed);
}

#ifndef SWIG // SWIG should skip this part
/** get the global linalg library object
Expand Down
3 changes: 1 addition & 2 deletions src/shogun/mathematics/Math.h
Expand Up @@ -1026,8 +1026,7 @@ class CMath : public CSGObject
}
else
{
auto m_rng =
std::unique_ptr<CRandom>(new CRandom());
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
for (index_t i = 0; i < v.vlen; ++i)
swap(v[i], v[m_rng->random(i, v.vlen - 1)]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/shogun/multiclass/LaRank.h
Expand Up @@ -249,8 +249,7 @@ namespace shogun

LaRankPattern & sample ()
{
auto m_rng =
std::unique_ptr<CRandom>(new CRandom());
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
ASSERT(!empty())
while (true)
{
Expand Down

0 comments on commit e5a20fe

Please sign in to comment.