From fbcef4955e2c0b40ab55dd6c3f844eafd54865a0 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 12 Apr 2018 21:24:50 -0700 Subject: [PATCH] algotune: finish library. --- .vscode/settings.json | 3 +- misc/algotest/Makefile.am | 5 ++ misc/algotest/algotune.cc | 87 ++++++++++++++++++++++++++++++---- misc/algotest/algotune.h | 12 +++-- misc/algotest/algotune_test.cc | 74 +++++++++++++++++++++++++++++ misc/algotest/configure.ac | 6 +-- 6 files changed, 169 insertions(+), 18 deletions(-) create mode 100644 misc/algotest/algotune_test.cc diff --git a/.vscode/settings.json b/.vscode/settings.json index cce2d28..82bc566 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,6 @@ ], "files.associations": { "*.ipp": "c" - } + }, + "python.linting.enabled": false } \ No newline at end of file diff --git a/misc/algotest/Makefile.am b/misc/algotest/Makefile.am index e16d0e8..f7a5256 100644 --- a/misc/algotest/Makefile.am +++ b/misc/algotest/Makefile.am @@ -2,6 +2,7 @@ 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 @@ -9,3 +10,7 @@ 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 diff --git a/misc/algotest/algotune.cc b/misc/algotest/algotune.cc index 41f2b08..2357db1 100644 --- a/misc/algotest/algotune.cc +++ b/misc/algotest/algotune.cc @@ -1,3 +1,5 @@ +#include +#include #include #include @@ -5,45 +7,110 @@ namespace algotune { + +static std::mt19937 rng; +static std::random_device devrand; + + +static void +reseed() +{ + rng.seed(devrand()); +} + + int rand_int(int low, int high) { - return -1; + std::uniform_int_distribution dist(low, high); + return dist(rng); } int64_t -rand_int(int64_t low, int64_t high) +rand_int64(int64_t low, int64_t high) { - return -1; + std::uniform_int_distribution dist(low, high); + return dist(rng); } std::vector gen_int_vector(int size, int low, int high) { - return vector(0); + assert(low < high); + reseed(); + std::vector v(size); + + for (int i = 0; i < size; i++) { + v[i] = rand_int(low, high); + } + + return v; } std::vector -gen_int_vector(int size, int low, int high) +gen_int64_vector(int64_t size, int64_t low, int64_t high) { - return vector(0); + assert(low < high); + reseed(); + std::vector v(size); + + for (int64_t i = 0; i < size; i++) { + v[i] = rand_int64(low, high); + } + + return v; } -void -stress_test_int(bool(*func)(std::vector)) +int64_t +stress_test_int(bool(*func)(std::vector&), int size, int low, int high) { + assert(func != nullptr); + std::vector 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; } -void -stress_test_int64(bool(*func)(std::vector&), int64_t size, int64_t low, int64_t high) { + assert(func != nullptr); + std::vector 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 diff --git a/misc/algotest/algotune.h b/misc/algotest/algotune.h index 9c4e789..cd7bc14 100644 --- a/misc/algotest/algotune.h +++ b/misc/algotest/algotune.h @@ -5,14 +5,18 @@ namespace algotune { +int64_t display_step = 1000; + + + int rand_int(int low, int high); -int64_t rand_int(int64_t low, int64_t high); +int64_t rand_int64(int64_t low, int64_t high); std::vector gen_int_vector(int size, int low, int high); -std::vector gen_int_vector(int size, int low, int high); +std::vector gen_int64_vector(int64_t size, int64_t low, int64_t high); -void stress_test_int(bool(*func)(std::vector)); -void stress_test_int64(bool(*func)(std::vector&), int size, int low, int high); +int64_t stress_test_int64(bool(*func)(std::vector&), int64_t size, int64_t low, int64_t high); } // namespace algotune diff --git a/misc/algotest/algotune_test.cc b/misc/algotest/algotune_test.cc new file mode 100644 index 0000000..fe6e5c2 --- /dev/null +++ b/misc/algotest/algotune_test.cc @@ -0,0 +1,74 @@ +#include +#include + +#include "algotune.h" + +using namespace std; + + +static int64_t +max_pairwise_fast(vector &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(v[idx1]) * static_cast(v[idx2]); +} + + +static int64_t +max_pairwise_naive(vector &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 &v) +{ + auto result_fast = max_pairwise_fast(v); + auto result_naive = max_pairwise_naive(v); + + return result_fast == result_naive; +} + + +int +main(void) +{ + vector 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; +} diff --git a/misc/algotest/configure.ac b/misc/algotest/configure.ac index 672ebd7..cd2192c 100644 --- a/misc/algotest/configure.ac +++ b/misc/algotest/configure.ac @@ -1,10 +1,10 @@ -AC_PREREQ([2.68]) +AC_PREREQ([2.69]) AC_INIT([libalgotune], [1.0.0], [kyle@imap.cc], [], []) -AM_INIT_AUTOMAKE([1.11 foreign]) +AM_INIT_AUTOMAKE([1.15 foreign]) LT_INIT @@ -13,10 +13,10 @@ 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 -