From: Ali Saidi Date: Wed, 31 Oct 2007 05:21:54 +0000 (-0400) Subject: Base: Rework the way M5 provides and creates random numbers. X-Git-Tag: m5_2.0_beta4~20 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7597f87430bc25547c594dd09f10be4953f987c3;p=gem5.git Base: Rework the way M5 provides and creates random numbers. --HG-- extra : convert_revision : 10ec3484647b3acb8e821f8520f97d535e41e861 --- diff --git a/src/base/SConscript b/src/base/SConscript index ca68bfb60..393cf57ec 100644 --- a/src/base/SConscript +++ b/src/base/SConscript @@ -70,6 +70,7 @@ Source('misc.cc') Source('output.cc') Source('pollevent.cc') Source('random.cc') +Source('random_mt.cc') Source('range.cc') Source('remote_gdb.cc') Source('sat_counter.cc') diff --git a/src/base/random.cc b/src/base/random.cc index 8a2e3c1c0..7daa90b9c 100644 --- a/src/base/random.cc +++ b/src/base/random.cc @@ -29,60 +29,88 @@ * Ali Saidi */ -#ifdef __SUNPRO_CC -#include -#include -#endif - -#include -#include - #include "base/fenv.hh" +#include "base/intmath.hh" +#include "base/misc.hh" #include "base/random.hh" +#include "sim/serialize.hh" using namespace std; -uint32_t -getInt32() +Random::Random() { - return mrand48() & 0xffffffff; + // default random seed taken from original source + init(5489); } -double -getDouble() +Random::Random(uint32_t s) { - return drand48(); + init(s); } -double -m5round(double r) +Random::Random(uint32_t init_key[], int key_length) { -#if defined(__sun) - double val; - int oldrnd = m5_fegetround(); - m5_fesetround(M5_FE_TONEAREST); - val = rint(r); - m5_fesetround(oldrnd); - return val; -#else - return round(r); -#endif + init(init_key, key_length); } -int64_t -getUniform(int64_t min, int64_t max) +Random::~Random() { - double r; - r = drand48() * (max-min) + min; +} - return (int64_t)m5round(r); +// To preserve the uniform random distribution between min and max, +// and allow all numbers to be represented, we generate a uniform +// random number to the nearest power of two greater than max. If +// this number doesn't fall between 0 and max, we try again. Anything +// else would skew the distribution. +uint32_t +Random::genrand(uint32_t max) +{ + int log = ceilLog2(max); + int shift = (sizeof(uint32_t) * 8 - log); + uint32_t random; + + do { + random = genrand() >> shift; + } while (random > max); + + return random; } uint64_t -getUniformPos(uint64_t min, uint64_t max) +Random::genrand(uint64_t max) +{ + int log = ceilLog2(max); + int shift = (sizeof(uint64_t) * 8 - log); + uint64_t random; + + do { + random = (uint64_t)genrand() << 32 | (uint64_t)genrand(); + random = random >> shift; + } while (random > max); + + return random; +} + +void +Random::serialize(const string &base, ostream &os) { - double r; - r = drand48() * (max-min) + min; + int length = N; + paramOut(os, base + ".mti", mti); + paramOut(os, base + ".length", length); + arrayParamOut(os, base + ".data", mt, length); +} - return (uint64_t)m5round(r); +void +Random::unserialize(const string &base, Checkpoint *cp, const string §ion) +{ + int length; + + paramIn(cp, section, base + ".mti", mti); + paramIn(cp, section, base + ".length", length); + if (length != N) + panic("cant unserialize random number data. length != %d\n", length); + + arrayParamIn(cp, section, base + ".data", mt, length); } + +Random random_mt; diff --git a/src/base/random.hh b/src/base/random.hh index 0cd88728d..401ba086f 100644 --- a/src/base/random.hh +++ b/src/base/random.hh @@ -32,99 +32,211 @@ #ifndef __BASE_RANDOM_HH__ #define __BASE_RANDOM_HH__ -#include "sim/host.hh" - -uint32_t getUInt32(); -double getDouble(); -double m5random(double r); -uint64_t getUniformPos(uint64_t min, uint64_t max); -int64_t getUniform(int64_t min, int64_t max); - -template -struct Random; - -template<> struct Random -{ - static int8_t get() - { return getUInt32() & (int8_t)-1; } - - static int8_t uniform(int8_t min, int8_t max) - { return getUniform(min, max); } -}; - -template<> struct Random -{ - static uint8_t get() - { return getUInt32() & (uint8_t)-1; } - - static uint8_t uniform(uint8_t min, uint8_t max) - { return getUniformPos(min, max); } -}; - -template<> struct Random -{ - static int16_t get() - { return getUInt32() & (int16_t)-1; } - - static int16_t uniform(int16_t min, int16_t max) - { return getUniform(min, max); } -}; - -template<> struct Random -{ - static uint16_t get() - { return getUInt32() & (uint16_t)-1; } - - static uint16_t uniform(uint16_t min, uint16_t max) - { return getUniformPos(min, max); } -}; - -template<> struct Random -{ - static int32_t get() - { return (int32_t)getUInt32(); } - - static int32_t uniform(int32_t min, int32_t max) - { return getUniform(min, max); } -}; - -template<> struct Random -{ - static uint32_t get() - { return (uint32_t)getUInt32(); } - - static uint32_t uniform(uint32_t min, uint32_t max) - { return getUniformPos(min, max); } -}; +#include +#include -template<> struct Random -{ - static int64_t get() - { return (int64_t)getUInt32() << 32 || (uint64_t)getUInt32(); } - - static int64_t uniform(int64_t min, int64_t max) - { return getUniform(min, max); } -}; - -template<> struct Random -{ - static uint64_t get() - { return (uint64_t)getUInt32() << 32 || (uint64_t)getUInt32(); } +#include "base/range.hh" +#include "sim/host.hh" - static uint64_t uniform(uint64_t min, uint64_t max) - { return getUniformPos(min, max); } -}; +class Checkpoint; -template<> struct Random +class Random { - static float get() - { return getDouble(); } + protected: + static const int N = 624; + static const int M = 397; + static const uint32_t MATRIX_A = (uint32_t)0x9908b0df; + static const uint32_t UPPER_MASK = (uint32_t)0x80000000; + static const uint32_t LOWER_MASK = (uint32_t)0x7fffffff; + + uint32_t mt[N]; + int mti; + + uint32_t genrand(); + uint32_t genrand(uint32_t max); + uint64_t genrand(uint64_t max); + + void + _random(int8_t &value) + { + value = genrand() & (int8_t)-1; + } + + void + _random(int16_t &value) + { + value = genrand() & (int16_t)-1; + } + + void + _random(int32_t &value) + { + value = (int32_t)genrand(); + } + + void + _random(int64_t &value) + { + value = (int64_t)genrand() << 32 | (int64_t)genrand(); + } + + void + _random(uint8_t &value) + { + value = genrand() & (uint8_t)-1; + } + + void + _random(uint16_t &value) + { + value = genrand() & (uint16_t)-1; + } + + void + _random(uint32_t &value) + { + value = genrand(); + } + + void + _random(uint64_t &value) + { + value = (uint64_t)genrand() << 32 | (uint64_t)genrand(); + } + + // [0,1] + void + _random(float &value) + { + // ieee floats have 23 bits of mantissa + value = (genrand() >> 9) / 8388608.0; + } + + // [0,1] + void + _random(double &value) + { + double number = genrand() * 2097152.0 + (genrand() >> 11); + value = number / 9007199254740992.0; + } + + + // Range based versions of the random number generator + int8_t + _random(int8_t min, int8_t max) + { + uint32_t diff = max - min; + return static_cast(min + genrand(diff)); + } + + int16_t + _random(int16_t min, int16_t max) + { + uint32_t diff = max - min; + return static_cast(min + genrand(diff)); + } + + int32_t + _random(int32_t min, int32_t max) + { + uint32_t diff = max - min; + return static_cast(min + genrand(diff)); + } + + int64_t + _random(int64_t min, int64_t max) + { + uint64_t diff = max - min; + return static_cast(min + genrand(diff)); + } + + uint8_t + _random(uint8_t min, uint8_t max) + { + uint32_t diff = max - min; + return static_cast(min + genrand(diff)); + } + + uint16_t + _random(uint16_t min, uint16_t max) + { + uint32_t diff = max - min; + return static_cast(min + genrand(diff)); + } + + uint32_t + _random(uint32_t min, uint32_t max) + { + uint32_t diff = max - min; + return static_cast(min + genrand(diff)); + } + + uint64_t + _random(uint64_t min, uint64_t max) + { + uint64_t diff = max - min; + return static_cast(min + genrand(diff)); + } + + public: + Random(); + Random(uint32_t s); + Random(uint32_t init_key[], int key_length); + ~Random(); + + void init(uint32_t s); + void init(uint32_t init_key[], int key_length); + + template + T + random() + { + T value; + _random(value); + return value; + } + + template + T + random(T min, T max) + { + return _random(min, max); + } + + template + T + random(const Range &range) + { + return _random(range.start, range.end); + } + + // [0,1] + double + gen_real1() + { + return genrand() / 4294967296.0; + } + + // [0,1) + double + gen_real2() + { + return genrand() / 4294967295.0; + } + + // (0,1) + double + gen_real3() + { + return ((double)genrand() + 0.5) / 4294967296.0; + } + + public: + void serialize(const std::string &base, std::ostream &os); + void unserialize(const std::string &base, Checkpoint *cp, + const std::string §ion); }; -template<> struct Random -{ - static double get() - { return getDouble(); } -}; +extern Random random_mt; #endif // __BASE_RANDOM_HH__ diff --git a/src/base/random_mt.cc b/src/base/random_mt.cc new file mode 100644 index 000000000..1492240ee --- /dev/null +++ b/src/base/random_mt.cc @@ -0,0 +1,149 @@ +/* + * A C-program for MT19937, with initialization improved 2002/1/26. + * Coded by Takuji Nishimura and Makoto Matsumoto. + * + * Before using, initialize the state by using init_genrand(seed) + * or init_by_array(init_key, key_length). + * + * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. The names of its contributors may not be used to endorse or + * promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * Any feedback is very welcome. + * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html + * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) + */ + +#include "base/random.hh" + +/* initializes mt[N] with a seed */ +void +Random::init(uint32_t s) +{ + mti = N + 1; + mt[0] = s & (uint32_t)0xffffffff; + + for (mti = 1; mti < N; mti++) { + mt[mti] = 1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti; + /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ + /* In the previous versions, MSBs of the seed affect */ + /* only MSBs of the array mt[]. */ + /* 2002/01/09 modified by Makoto Matsumoto */ + mt[mti] &= (uint32_t)0xffffffff; + /* for >32 bit machines */ + } +} + +/* initialize by an array with array-length */ +/* init_key is the array for initializing keys */ +/* key_length is its length */ +/* slight change for C++, 2004/2/26 */ +void +Random::init(uint32_t init_key[], int key_length) +{ + int i = 1; + int j = 0; + int k = (N > key_length) ? N : key_length; + + init(19650218); + + for (; k; k--) { + mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * (uint32_t)1664525)) + + init_key[j] + j; /* non linear */ + + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ + + i++; + j++; + + if (i >= N) { + mt[0] = mt[N - 1]; + i = 1; + } + + if (j >= key_length) + j = 0; + } + + for (k = N - 1; k; k--) { + /* non linear */ + mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941UL)) - i; + + /* for WORDSIZE > 32 machines */ + mt[i] &= (uint32_t)0xffffffff; + i++; + + if (i >= N) { + mt[0] = mt[N - 1]; + i = 1; + } + } + + /* MSB is 1; assuring non-zero initial array */ + mt[0] = (uint32_t)0x80000000; +} + +/* generates a random number on [0,0xffffffff]-interval */ +uint32_t +Random::genrand() +{ + uint32_t y; + static uint32_t mag01[2] = { 0, MATRIX_A}; + + if (mti >= N) { /* generate N words at one time */ + int kk; + + for (kk = 0; kk < N - M; kk++) { + y = mt[kk] & UPPER_MASK | mt[kk+1] & LOWER_MASK; + mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL]; + } + for (; kk < N - 1; kk++) { + y = mt[kk] & UPPER_MASK | mt[kk+1] & LOWER_MASK; + mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; + } + + y = mt[N - 1] & UPPER_MASK | mt[0] & LOWER_MASK; + mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL]; + + mti = 0; + } + + y = mt[mti++]; + + /* Tempering */ + y ^= (y >> 11); + y ^= (y << 7) & (uint32_t)0x9d2c5680; + y ^= (y << 15) & (uint32_t)0xefc60000; + y ^= (y >> 18); + + return y; +} diff --git a/src/dev/etherlink.cc b/src/dev/etherlink.cc index 4130a7b3f..b1266000b 100644 --- a/src/dev/etherlink.cc +++ b/src/dev/etherlink.cc @@ -177,10 +177,9 @@ EtherLink::Link::transmit(EthPacketPtr pkt) packet = pkt; Tick delay = (Tick)ceil(((double)pkt->length * ticksPerByte) + 1.0); - if (delayVar != 0) { - Random var; - delay += var.uniform(0, delayVar); - } + if (delayVar != 0) + delay += random_mt.random(0, delayVar); + DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n", delay, ticksPerByte); doneEvent.schedule(curTick + delay);