diff options
author | Jeroen Vermeulen <jtv@precisiontranslationtools.com> | 2015-04-22 16:43:29 +0300 |
---|---|---|
committer | Jeroen Vermeulen <jtv@precisiontranslationtools.com> | 2015-04-22 16:43:29 +0300 |
commit | 75bfb758822cc926a1852c6a86a7ce5d153a1320 (patch) | |
tree | 7a967ee360aeb4898450888b31fc2ad602d9c7e6 /mert | |
parent | 1083999d3e4447ac347b8bcbb04a0733ae8c3654 (diff) |
Thread-safe, platform-agnostic randomizer.
Some places in mert use srandom()/random(), but these are POSIX-specific.
The standard alternative, srand()/rand(), is not thread-safe. This module
wraps srand()/rand() in mutexes (very short-lived, so should not cost much)
so that it relies on just Boost and the C standard library, not on a Unix-like
environment.
This may reduce the width of the random numbers on some platforms: it goes
from "long int" to just "int". If that is a problem, we may have to use
Boost's randomizer utilities, or eventually, the C++ ones.
Diffstat (limited to 'mert')
-rw-r--r-- | mert/Data.cpp | 3 | ||||
-rw-r--r-- | mert/Point.cpp | 5 | ||||
-rw-r--r-- | mert/evaluator.cpp | 7 | ||||
-rw-r--r-- | mert/mert.cpp | 5 |
4 files changed, 12 insertions, 8 deletions
diff --git a/mert/Data.cpp b/mert/Data.cpp index 49c1239e5..428886b99 100644 --- a/mert/Data.cpp +++ b/mert/Data.cpp @@ -17,6 +17,7 @@ #include "util/exception.hh" #include "util/file_piece.hh" +#include "util/random.hh" #include "util/tokenize_piece.hh" #include "util/string_piece.hh" #include "FeatureDataIterator.h" @@ -286,7 +287,7 @@ void Data::createShards(size_t shard_count, float shard_size, const string& scor } else { //create shards by randomly sampling for (size_t i = 0; i < floor(shard_size+0.5); ++i) { - shard_contents.push_back(rand() % data_size); + shard_contents.push_back(util::rand_int() % data_size); } } diff --git a/mert/Point.cpp b/mert/Point.cpp index 55dc6a6b2..562249492 100644 --- a/mert/Point.cpp +++ b/mert/Point.cpp @@ -3,6 +3,7 @@ #include <cmath> #include <cstdlib> #include "util/exception.hh" +#include "util/random.hh" #include "FeatureStats.h" #include "Optimizer.h" @@ -58,8 +59,8 @@ void Point::Randomize() UTIL_THROW_IF(m_max.size() != Point::m_dim, util::Exception, "Error"); for (unsigned int i = 0; i < size(); i++) { - operator[](i) = m_min[i] + - static_cast<float>(random()) / static_cast<float>(RAND_MAX) * (m_max[i] - m_min[i]); + const float scale = (m_max[i] - m_min[i]) / float(RAND_MAX); + operator[](i) = m_min[i] + util::rand_int() * scale; } } diff --git a/mert/evaluator.cpp b/mert/evaluator.cpp index 026abf397..61775a354 100644 --- a/mert/evaluator.cpp +++ b/mert/evaluator.cpp @@ -16,6 +16,7 @@ #include "Timer.h" #include "Util.h" #include "Data.h" +#include "util/random.hh" using namespace std; using namespace MosesTuning; @@ -94,7 +95,7 @@ void EvaluatorUtil::evaluate(const string& candFile, int bootstrap, bool nbest_i for (int i = 0; i < bootstrap; ++i) { ScoreData scoredata(g_scorer); for (int j = 0; j < n; ++j) { - int randomIndex = random() % n; + int randomIndex = util::rand_int() % n; scoredata.add(entries[randomIndex], j); } g_scorer->setScoreData(&scoredata); @@ -284,10 +285,10 @@ void InitSeed(const ProgramOption *opt) { if (opt->has_seed) { cerr << "Seeding random numbers with " << opt->seed << endl; - srandom(opt->seed); + util::rand_int_init(opt->seed); } else { cerr << "Seeding random numbers with system clock " << endl; - srandom(time(NULL)); + util::rand_int_init(); } } diff --git a/mert/mert.cpp b/mert/mert.cpp index 275aa7b09..e79ba7c6b 100644 --- a/mert/mert.cpp +++ b/mert/mert.cpp @@ -24,6 +24,7 @@ #include "Types.h" #include "Timer.h" #include "Util.h" +#include "util/random.hh" #include "moses/ThreadPool.h" @@ -289,10 +290,10 @@ int main(int argc, char **argv) if (option.has_seed) { cerr << "Seeding random numbers with " << option.seed << endl; - srandom(option.seed); + util::rand_int_init(option.seed); } else { cerr << "Seeding random numbers with system clock " << endl; - srandom(time(NULL)); + util::rand_int_init(); } if (option.sparse_weights_file.size()) ++option.pdim; |