Base: Rework the way M5 provides and creates random numbers.

--HG--
extra : convert_revision : 10ec3484647b3acb8e821f8520f97d535e41e861
This commit is contained in:
Ali Saidi 2007-10-31 01:21:54 -04:00
parent 04d1cfe31c
commit 7597f87430
5 changed files with 422 additions and 133 deletions

View file

@ -70,6 +70,7 @@ Source('misc.cc')
Source('output.cc') Source('output.cc')
Source('pollevent.cc') Source('pollevent.cc')
Source('random.cc') Source('random.cc')
Source('random_mt.cc')
Source('range.cc') Source('range.cc')
Source('remote_gdb.cc') Source('remote_gdb.cc')
Source('sat_counter.cc') Source('sat_counter.cc')

View file

@ -29,60 +29,88 @@
* Ali Saidi * Ali Saidi
*/ */
#ifdef __SUNPRO_CC
#include <stdlib.h>
#include <math.h>
#endif
#include <cstdlib>
#include <cmath>
#include "base/fenv.hh" #include "base/fenv.hh"
#include "base/intmath.hh"
#include "base/misc.hh"
#include "base/random.hh" #include "base/random.hh"
#include "sim/serialize.hh"
using namespace std; using namespace std;
Random::Random()
{
// default random seed taken from original source
init(5489);
}
Random::Random(uint32_t s)
{
init(s);
}
Random::Random(uint32_t init_key[], int key_length)
{
init(init_key, key_length);
}
Random::~Random()
{
}
// 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 uint32_t
getInt32() Random::genrand(uint32_t max)
{ {
return mrand48() & 0xffffffff; int log = ceilLog2(max);
} int shift = (sizeof(uint32_t) * 8 - log);
uint32_t random;
double do {
getDouble() random = genrand() >> shift;
{ } while (random > max);
return drand48();
}
double return random;
m5round(double r)
{
#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
}
int64_t
getUniform(int64_t min, int64_t max)
{
double r;
r = drand48() * (max-min) + min;
return (int64_t)m5round(r);
} }
uint64_t uint64_t
getUniformPos(uint64_t min, uint64_t max) Random::genrand(uint64_t max)
{ {
double r; int log = ceilLog2(max);
r = drand48() * (max-min) + min; int shift = (sizeof(uint64_t) * 8 - log);
uint64_t random;
return (uint64_t)m5round(r); 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)
{
int length = N;
paramOut(os, base + ".mti", mti);
paramOut(os, base + ".length", length);
arrayParamOut(os, base + ".data", mt, length);
}
void
Random::unserialize(const string &base, Checkpoint *cp, const string &section)
{
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;

View file

@ -32,99 +32,211 @@
#ifndef __BASE_RANDOM_HH__ #ifndef __BASE_RANDOM_HH__
#define __BASE_RANDOM_HH__ #define __BASE_RANDOM_HH__
#include <ios>
#include <string>
#include "base/range.hh"
#include "sim/host.hh" #include "sim/host.hh"
uint32_t getUInt32(); class Checkpoint;
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 <typename T> class Random
struct Random;
template<> struct Random<int8_t>
{ {
static int8_t get() protected:
{ return getUInt32() & (int8_t)-1; } 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;
static int8_t uniform(int8_t min, int8_t max) uint32_t mt[N];
{ return getUniform(min, max); } 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<int8_t>(min + genrand(diff));
}
int16_t
_random(int16_t min, int16_t max)
{
uint32_t diff = max - min;
return static_cast<int16_t>(min + genrand(diff));
}
int32_t
_random(int32_t min, int32_t max)
{
uint32_t diff = max - min;
return static_cast<int32_t>(min + genrand(diff));
}
int64_t
_random(int64_t min, int64_t max)
{
uint64_t diff = max - min;
return static_cast<int64_t>(min + genrand(diff));
}
uint8_t
_random(uint8_t min, uint8_t max)
{
uint32_t diff = max - min;
return static_cast<uint8_t>(min + genrand(diff));
}
uint16_t
_random(uint16_t min, uint16_t max)
{
uint32_t diff = max - min;
return static_cast<uint16_t>(min + genrand(diff));
}
uint32_t
_random(uint32_t min, uint32_t max)
{
uint32_t diff = max - min;
return static_cast<uint32_t>(min + genrand(diff));
}
uint64_t
_random(uint64_t min, uint64_t max)
{
uint64_t diff = max - min;
return static_cast<uint64_t>(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 <typename T>
T
random()
{
T value;
_random(value);
return value;
}
template <typename T>
T
random(T min, T max)
{
return _random(min, max);
}
template <typename T>
T
random(const Range<T> &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 &section);
}; };
template<> struct Random<uint8_t> extern Random random_mt;
{
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<int16_t>
{
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<uint16_t>
{
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<int32_t>
{
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<uint32_t>
{
static uint32_t get()
{ return (uint32_t)getUInt32(); }
static uint32_t uniform(uint32_t min, uint32_t max)
{ return getUniformPos(min, max); }
};
template<> struct Random<int64_t>
{
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<uint64_t>
{
static uint64_t get()
{ return (uint64_t)getUInt32() << 32 || (uint64_t)getUInt32(); }
static uint64_t uniform(uint64_t min, uint64_t max)
{ return getUniformPos(min, max); }
};
template<> struct Random<float>
{
static float get()
{ return getDouble(); }
};
template<> struct Random<double>
{
static double get()
{ return getDouble(); }
};
#endif // __BASE_RANDOM_HH__ #endif // __BASE_RANDOM_HH__

149
src/base/random_mt.cc Normal file
View file

@ -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;
}

View file

@ -177,10 +177,9 @@ EtherLink::Link::transmit(EthPacketPtr pkt)
packet = pkt; packet = pkt;
Tick delay = (Tick)ceil(((double)pkt->length * ticksPerByte) + 1.0); Tick delay = (Tick)ceil(((double)pkt->length * ticksPerByte) + 1.0);
if (delayVar != 0) { if (delayVar != 0)
Random<Tick> var; delay += random_mt.random<Tick>(0, delayVar);
delay += var.uniform(0, delayVar);
}
DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n", DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
delay, ticksPerByte); delay, ticksPerByte);
doneEvent.schedule(curTick + delay); doneEvent.schedule(curTick + delay);