A little houskeeping.

Increasing counter sizes from int to long for averages.
Rename random functions to end with _co instead of _exclusive to
indicate range [close, open[, and to allow future suffixes oc, cc and oo.
This commit is contained in:
Niclas Antti
2019-03-25 12:29:59 +02:00
parent 03121c63d4
commit d40e29d5f6
6 changed files with 31 additions and 31 deletions

View File

@ -36,7 +36,7 @@ public:
void query_ended(); // ok to call without a query_started void query_ended(); // ok to call without a query_started
bool make_valid(); // make valid even if there are only filter_samples bool make_valid(); // make valid even if there are only filter_samples
bool is_valid() const; bool is_valid() const;
int num_samples() const; long num_samples() const;
maxbase::Duration average() const; maxbase::Duration average() const;
bool sync_time_reached(); // is it time to apply the average to the server? bool sync_time_reached(); // is it time to apply the average to the server?
void reset(); void reset();
@ -44,7 +44,7 @@ public:
private: private:
const int m_num_filter_samples; const int m_num_filter_samples;
const maxbase::Duration m_sync_duration; const maxbase::Duration m_sync_duration;
int m_sample_count; long m_sample_count;
std::vector<maxbase::Duration> m_samples; // N sampels from which median is used std::vector<maxbase::Duration> m_samples; // N sampels from which median is used
maxbase::CumulativeAverage m_average; maxbase::CumulativeAverage m_average;
maxbase::TimePoint m_last_start; maxbase::TimePoint m_last_start;

View File

@ -34,7 +34,7 @@ public:
* @param ave The average value * @param ave The average value
* @param num_samples How many samples were taken to construct it * @param num_samples How many samples were taken to construct it
*/ */
void add(double ave, int num_samples = 1); void add(double ave, long num_samples = 1);
/** /**
* Get the average value * Get the average value
@ -48,7 +48,7 @@ public:
* *
* @return Number of collected samples * @return Number of collected samples
*/ */
int num_samples() const; long num_samples() const;
/** /**
* Reset the average value * Reset the average value
@ -60,8 +60,8 @@ public:
CumulativeAverage& operator+=(const CumulativeAverage& rhs); CumulativeAverage& operator+=(const CumulativeAverage& rhs);
private: private:
double m_ave = 0; double m_ave = 0;
int m_num_samples = 0; long m_num_samples = 0;
int m_num_last_added = 0; long m_num_last_added = 0;
}; };
CumulativeAverage operator+(const CumulativeAverage& rhs, const CumulativeAverage& lhs); CumulativeAverage operator+(const CumulativeAverage& rhs, const CumulativeAverage& lhs);
@ -81,7 +81,7 @@ public:
* @param max_alpha The extra alpha value * @param max_alpha The extra alpha value
* @param sample_max Maximum number of samples to use * @param sample_max Maximum number of samples to use
*/ */
EMAverage(double min_alpha, double max_alpha, int sample_max); EMAverage(double min_alpha, double max_alpha, long sample_max);
/** /**
* Add a new value to the average made of `num_samples` samples * Add a new value to the average made of `num_samples` samples
@ -101,7 +101,7 @@ public:
* @param ave Value to add * @param ave Value to add
* @param num_samples Number of samples the value consists of * @param num_samples Number of samples the value consists of
*/ */
void add(double ave, int num_samples = 1); void add(double ave, long num_samples = 1);
/** /**
* Add a CumulativeAverage * Add a CumulativeAverage
@ -124,21 +124,21 @@ public:
* *
* @return The number of samples * @return The number of samples
*/ */
int num_samples() const; long num_samples() const;
/** /**
* Set maximum sample size * Set maximum sample size
* *
* @param sample_max The new sample max size * @param sample_max The new sample max size
*/ */
void set_sample_max(int sample_max); void set_sample_max(long sample_max);
/** /**
* Get maximum sample size * Get maximum sample size
* *
* @return The maximum sample size * @return The maximum sample size
*/ */
int sample_max() const; long sample_max() const;
/** /**
* Reset the average * Reset the average
@ -150,8 +150,8 @@ public:
private: private:
const double m_min_alpha; const double m_min_alpha;
const double m_max_alpha; const double m_max_alpha;
int m_sample_max; long m_sample_max;
int m_num_samples = 0; long m_num_samples = 0;
double m_ave = 0; double m_ave = 0;
}; };

View File

@ -37,8 +37,8 @@ public:
uint64_t rand(); uint64_t rand();
uint32_t rand32(); uint32_t rand32();
bool rand_bool(); bool rand_bool();
int64_t b_to_e_exclusive(int64_t b, int64_t e); int64_t b_to_e_co(int64_t b, int64_t e);
double zero_to_one_exclusive(); double zero_to_one_co();
private: private:
uint64_t rotl(const uint64_t x, int k); uint64_t rotl(const uint64_t x, int k);
std::array<uint64_t, 4> m_state; std::array<uint64_t, 4> m_state;
@ -57,8 +57,8 @@ public:
uint64_t rand(); uint64_t rand();
uint32_t rand32(); uint32_t rand32();
bool rand_bool(); bool rand_bool();
int64_t b_to_e_exclusive(int64_t b, int64_t e); int64_t b_to_e_co(int64_t b, int64_t e);
double zero_to_one_exclusive(); double zero_to_one_co();
// Borrow the mt19937_64 engine for other distributions. // Borrow the mt19937_64 engine for other distributions.
std::mt19937_64& rnd_engine(); std::mt19937_64& rnd_engine();
@ -104,7 +104,7 @@ inline bool XorShiftRandom::rand_bool()
return std::signbit(int64_t(rand())); return std::signbit(int64_t(rand()));
} }
inline double XorShiftRandom::zero_to_one_exclusive() inline double XorShiftRandom::zero_to_one_co()
{ {
uint64_t x = rand(); uint64_t x = rand();
@ -118,7 +118,7 @@ inline double XorShiftRandom::zero_to_one_exclusive()
return u.d - 1.0; return u.d - 1.0;
} }
inline int64_t XorShiftRandom::b_to_e_exclusive(int64_t b, int64_t e) inline int64_t XorShiftRandom::b_to_e_co(int64_t b, int64_t e)
{ {
// With 64 bits mod bias does not happen in practise (a very, very large e-b would be needed). // With 64 bits mod bias does not happen in practise (a very, very large e-b would be needed).
// alternative: return b + int64_t(zero_to_one_exclusive()*(e-b)); // alternative: return b + int64_t(zero_to_one_exclusive()*(e-b));
@ -140,13 +140,13 @@ inline bool StdTwisterRandom::rand_bool()
return std::signbit(int32_t(rand32())); return std::signbit(int32_t(rand32()));
} }
inline double StdTwisterRandom::zero_to_one_exclusive() inline double StdTwisterRandom::zero_to_one_co()
{ {
std::uniform_real_distribution<double> zero_to_one {0, 1}; std::uniform_real_distribution<double> zero_to_one {0, 1};
return zero_to_one(m_twister_engine_64); return zero_to_one(m_twister_engine_64);
} }
inline int64_t StdTwisterRandom::b_to_e_exclusive(int64_t b, int64_t e) inline int64_t StdTwisterRandom::b_to_e_co(int64_t b, int64_t e)
{ {
std::uniform_int_distribution<int64_t> dist {b, e - 1}; std::uniform_int_distribution<int64_t> dist {b, e - 1};
return dist(m_twister_engine_64); return dist(m_twister_engine_64);

View File

@ -17,7 +17,7 @@
namespace maxbase namespace maxbase
{ {
void CumulativeAverage::add(double ave, int num_samples) void CumulativeAverage::add(double ave, long num_samples)
{ {
m_num_samples += num_samples; m_num_samples += num_samples;
@ -38,7 +38,7 @@ double CumulativeAverage::average() const
return m_ave; return m_ave;
} }
int CumulativeAverage::num_samples() const long CumulativeAverage::num_samples() const
{ {
return m_num_samples; return m_num_samples;
} }
@ -61,17 +61,17 @@ void CumulativeAverage::reset()
m_num_last_added = 0; m_num_last_added = 0;
} }
EMAverage::EMAverage(double min_alpha, double max_alpha, int sample_max) EMAverage::EMAverage(double min_alpha, double max_alpha, long sample_max)
: m_min_alpha{min_alpha} : m_min_alpha{min_alpha}
, m_max_alpha{max_alpha} , m_max_alpha{max_alpha}
, m_sample_max{sample_max} , m_sample_max{sample_max}
{ {
} }
void EMAverage::add(double ave, int num_samples) void EMAverage::add(double ave, long num_samples)
{ {
// Give more weight to initial samples. // Give more weight to initial samples.
int sample_max = std::min(m_num_samples ? m_num_samples : 1, m_sample_max); long sample_max = std::min(m_num_samples ? m_num_samples : 1, m_sample_max);
double alpha = m_min_alpha + m_max_alpha double alpha = m_min_alpha + m_max_alpha
* std::min(double(num_samples) / sample_max, 1.0); * std::min(double(num_samples) / sample_max, 1.0);
@ -97,17 +97,17 @@ double EMAverage::average() const
return m_ave; return m_ave;
} }
int EMAverage::num_samples() const long EMAverage::num_samples() const
{ {
return m_num_samples; return m_num_samples;
} }
void EMAverage::set_sample_max(int sample_max) void EMAverage::set_sample_max(long sample_max)
{ {
m_sample_max = sample_max; m_sample_max = sample_max;
} }
int EMAverage::sample_max() const long EMAverage::sample_max() const
{ {
return m_sample_max; return m_sample_max;
} }

View File

@ -71,7 +71,7 @@ bool ResponseStat::is_valid() const
return m_average.num_samples(); return m_average.num_samples();
} }
int ResponseStat::num_samples() const long ResponseStat::num_samples() const
{ {
return m_average.num_samples(); return m_average.num_samples();
} }

View File

@ -161,7 +161,7 @@ PRWBackends::iterator backend_cmp_response_time(PRWBackends& sBackends)
} }
// Find the winner, role the ball: // Find the winner, role the ball:
double ball = maxbase::Worker::get_current()->random_engine().zero_to_one_exclusive(); double ball = maxbase::Worker::get_current()->random_engine().zero_to_one_co();
double slot_walk {0}; double slot_walk {0};
int winner {0}; int winner {0};