algotune: renamed directory to match library name
This commit is contained in:
16
misc/algotune/Makefile.am
Normal file
16
misc/algotune/Makefile.am
Normal file
@@ -0,0 +1,16 @@
|
||||
AM_CPPFLAGS = -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align
|
||||
AM_CPPFLAGS += -Wwrite-strings -Wmissing-declarations -Wno-long-long -Werror
|
||||
AM_CPPFLAGS += -Wunused-variable -std=c++14 -D_XOPEN_SOURCE -O2 -I.
|
||||
AM_CPPFLAGS += -fno-elide-constructors -Weffc++
|
||||
ACLOCAL_AMFLAGS = -I m4
|
||||
|
||||
pkgconfigdir = $(libdir)/pkgconfig
|
||||
pkgconfig_DATA = libalgotune-1.pc
|
||||
|
||||
lib_LTLIBRARIES = libalgotune.la
|
||||
nobase_include_HEADERS = algotune.h
|
||||
libalgotune_la_SOURCES = algotune.cc
|
||||
|
||||
check_PROGRAMS = algotune_test
|
||||
algotune_test_SOURCES = algotune_test.cc
|
||||
algotune_test_LDADD = libalgotune.la
|
||||
116
misc/algotune/algotune.cc
Normal file
116
misc/algotune/algotune.cc
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
#include "algotune.h"
|
||||
|
||||
namespace algotune {
|
||||
|
||||
|
||||
static std::mt19937 rng;
|
||||
static std::random_device devrand;
|
||||
|
||||
|
||||
static void
|
||||
reseed()
|
||||
{
|
||||
rng.seed(devrand());
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
rand_int(int low, int high)
|
||||
{
|
||||
std::uniform_int_distribution<int> dist(low, high);
|
||||
return dist(rng);
|
||||
}
|
||||
|
||||
|
||||
int64_t
|
||||
rand_int64(int64_t low, int64_t high)
|
||||
{
|
||||
std::uniform_int_distribution<int64_t> dist(low, high);
|
||||
return dist(rng);
|
||||
}
|
||||
|
||||
|
||||
std::vector<int>
|
||||
gen_int_vector(int size, int low, int high)
|
||||
{
|
||||
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>
|
||||
gen_int64_vector(int64_t size, int64_t low, int64_t high)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
int64_t
|
||||
stress_test_int(bool(*func)(std::vector<int>&), int size, int low, int high)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
int64_t
|
||||
stress_test_int64(bool(*func)(std::vector<int64_t>&), int64_t size, int64_t low, int64_t high)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
} // namespace algotune
|
||||
24
misc/algotune/algotune.h
Normal file
24
misc/algotune/algotune.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef __LIB_ALGOTUNE__
|
||||
#define __LIB_ALGOTUNE__
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace algotune {
|
||||
|
||||
int64_t display_step = 1000;
|
||||
|
||||
|
||||
|
||||
int rand_int(int low, int high);
|
||||
int64_t rand_int64(int64_t low, int64_t high);
|
||||
|
||||
std::vector<int> gen_int_vector(int size, int low, int high);
|
||||
std::vector<int64_t> gen_int64_vector(int64_t size, int64_t low, int64_t high);
|
||||
|
||||
int64_t stress_test_int(bool(*func)(std::vector<int>&), int size, int low, int high);
|
||||
int64_t stress_test_int64(bool(*func)(std::vector<int64_t>&), int64_t size, int64_t low, int64_t high);
|
||||
|
||||
} // namespace algotune
|
||||
|
||||
|
||||
#endif // __LIB_ALGOTUNE__
|
||||
74
misc/algotune/algotune_test.cc
Normal file
74
misc/algotune/algotune_test.cc
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "algotune.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
static int64_t
|
||||
max_pairwise_fast(vector<int> &v)
|
||||
{
|
||||
size_t idx1 = 0;
|
||||
size_t idx2 = 0;
|
||||
auto n = v.size();
|
||||
|
||||
for (size_t i = 1; i < n; i++) {
|
||||
if (v[i] > v[idx1]) {
|
||||
idx1 = i;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < n; i++) {
|
||||
if (i == idx1) continue;
|
||||
if ((v[i] > v[idx2]) || (idx2 == idx1)) {
|
||||
idx2 = i;
|
||||
}
|
||||
}
|
||||
|
||||
return static_cast<int64_t>(v[idx1]) * static_cast<int64_t>(v[idx2]);
|
||||
}
|
||||
|
||||
|
||||
static int64_t
|
||||
max_pairwise_naive(vector<int> &v)
|
||||
{
|
||||
auto n = v.size();
|
||||
int64_t best = 0;
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
for (size_t j = 0; j < n; j++) {
|
||||
if (i == j) continue;
|
||||
int64_t prod = v[i] * v[j];
|
||||
best = prod > best ? prod : best;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
|
||||
static bool
|
||||
test_max_pairwise_fast(vector<int> &v)
|
||||
{
|
||||
auto result_fast = max_pairwise_fast(v);
|
||||
auto result_naive = max_pairwise_naive(v);
|
||||
|
||||
return result_fast == result_naive;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
vector<int> v = algotune::gen_int_vector(10, 1, 10000);
|
||||
if (!test_max_pairwise_fast(v)) {
|
||||
cerr << "test failed\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
algotune::display_step = 100;
|
||||
auto iterations = algotune::stress_test_int(test_max_pairwise_fast, 10000, 1, 1000000);
|
||||
cout << "Iterations before failure: " << iterations << endl;
|
||||
return 0;
|
||||
}
|
||||
22
misc/algotune/configure.ac
Normal file
22
misc/algotune/configure.ac
Normal file
@@ -0,0 +1,22 @@
|
||||
AC_PREREQ([2.69])
|
||||
AC_INIT([libalgotune],
|
||||
[1.0.0],
|
||||
[kyle@imap.cc],
|
||||
[],
|
||||
[])
|
||||
AM_INIT_AUTOMAKE([1.15 foreign])
|
||||
|
||||
LT_INIT
|
||||
|
||||
PKG_PROG_PKG_CONFIG
|
||||
|
||||
AC_CONFIG_SRCDIR([algotune.h])
|
||||
AC_CONFIG_FILES([Makefile
|
||||
libalgotune-1.pc])
|
||||
AC_CONFIG_MACRO_DIR([m4])
|
||||
|
||||
AC_PROG_CXX
|
||||
AC_PROG_INSTALL
|
||||
AC_PROG_CC_C_O
|
||||
|
||||
AC_OUTPUT
|
||||
10
misc/algotune/libalgotune-1.pc.in
Normal file
10
misc/algotune/libalgotune-1.pc.in
Normal file
@@ -0,0 +1,10 @@
|
||||
prefix=@prefix@
|
||||
exec_prefix=@exec_prefix@
|
||||
libdir=@libdir@
|
||||
includedir=@includedir@
|
||||
|
||||
Name: @PACKAGE_NAME@
|
||||
Description: Algorithms and data structure tuning and testing
|
||||
URL: @PACKAGE_URL@
|
||||
Version: @PACKAGE_VERSION@
|
||||
Libs: -L${libdir} -Wl,-rpath,${libdir} -lalgotune
|
||||
Reference in New Issue
Block a user