sandbox/misc/algotune/algotune.cc

117 lines
1.9 KiB
C++
Raw Normal View History

2018-04-13 04:24:50 +00:00
#include <cassert>
#include <iostream>
#include <random>
#include <vector>
#include "algotune.h"
namespace algotune {
2018-04-13 04:24:50 +00:00
static std::mt19937 rng;
static std::random_device devrand;
2018-04-13 04:53:47 +00:00
void
2018-04-13 04:24:50 +00:00
reseed()
{
rng.seed(devrand());
}
int
rand_int(int low, int high)
{
2018-04-13 04:24:50 +00:00
std::uniform_int_distribution<int> dist(low, high);
return dist(rng);
}
int64_t
2018-04-13 04:24:50 +00:00
rand_int64(int64_t low, int64_t high)
{
2018-04-13 04:24:50 +00:00
std::uniform_int_distribution<int64_t> dist(low, high);
return dist(rng);
}
std::vector<int>
gen_int_vector(int size, int low, int high)
{
2018-04-13 04:24:50 +00:00
assert(low < high);
reseed();
std::vector<int> v(size);
for (int i = 0; i < size; i++) {
v[i] = rand_int(low, high);
}
return v;
}
std::vector<int64_t>
2018-04-13 04:24:50 +00:00
gen_int64_vector(int64_t size, int64_t low, int64_t high)
{
2018-04-13 04:24:50 +00:00
assert(low < high);
reseed();
std::vector<int64_t> v(size);
for (int64_t i = 0; i < size; i++) {
v[i] = rand_int64(low, high);
}
return v;
}
2018-04-13 04:24:50 +00:00
int64_t
stress_test_int(bool(*func)(std::vector<int>&), int size, int low, int high)
{
2018-04-13 04:24:50 +00:00
assert(func != nullptr);
std::vector<int> v;
bool result = true;
int64_t iterations = 0;
int vsize = 0;
while (result) {
iterations++;
vsize = rand_int(0, size);
v = gen_int_vector(vsize, low, high);
result = func(v);
if ((iterations > 1) && ((iterations % display_step) == 0)) {
std::cout << "Iteration: " << iterations << std::endl;
}
}
return iterations;
}
2018-04-13 04:24:50 +00:00
int64_t
stress_test_int64(bool(*func)(std::vector<int64_t>&), int64_t size, int64_t low, int64_t high)
{
2018-04-13 04:24:50 +00:00
assert(func != nullptr);
std::vector<int64_t> v;
bool result = true;
int64_t iterations = 0;
int64_t vsize = 0;
while (result) {
iterations++;
vsize = rand_int64(0, size);
v = gen_int64_vector(vsize, low, high);
result = func(v);
if ((iterations > 1) && ((iterations % display_step) == 0)) {
std::cout << "Iteration: " << iterations << std::endl;
}
}
return iterations;
}
2018-04-13 04:24:50 +00:00
} // namespace algotune