From 175b0e3d50d8affc02a478cfde3ac7a4fc6583e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Fri, 15 Nov 2019 00:00:00 +0000 Subject: [PATCH] bst_vs_heap_vs_hashmap: create nicer version with separated benchmarks One data structure per executable, and gem5 instrumented versions in separate executables. Create the allowed_emulators path_properties property. TODO: wire it up to benchmark result generation and remove the old version. --- path_properties.py | 18 +++ userland/algorithm/build | 1 + userland/algorithm/set/README.adoc | 1 + userland/algorithm/set/build | 1 + userland/algorithm/set/main.hpp | 115 ++++++++++++++++++ userland/algorithm/set/std_priority_queue.cpp | 2 + .../algorithm/set/std_priority_queue_gem5.cpp | 3 + userland/algorithm/set/std_set.cpp | 2 + userland/algorithm/set/std_set_gem5.cpp | 3 + userland/algorithm/set/std_unordered_set.cpp | 2 + .../algorithm/set/std_unordered_set_gem5.cpp | 3 + userland/algorithm/set/test | 1 + userland/algorithm/test | 1 + 13 files changed, 153 insertions(+) create mode 120000 userland/algorithm/build create mode 100644 userland/algorithm/set/README.adoc create mode 120000 userland/algorithm/set/build create mode 100644 userland/algorithm/set/main.hpp create mode 100644 userland/algorithm/set/std_priority_queue.cpp create mode 100644 userland/algorithm/set/std_priority_queue_gem5.cpp create mode 100644 userland/algorithm/set/std_set.cpp create mode 100644 userland/algorithm/set/std_set_gem5.cpp create mode 100644 userland/algorithm/set/std_unordered_set.cpp create mode 100644 userland/algorithm/set/std_unordered_set_gem5.cpp create mode 120000 userland/algorithm/set/test create mode 120000 userland/algorithm/test diff --git a/path_properties.py b/path_properties.py index 93e9992..67554a8 100644 --- a/path_properties.py +++ b/path_properties.py @@ -11,6 +11,7 @@ class PathProperties: # All new properties must be listed here or else you get an error. default_properties = { 'allowed_archs': None, + 'allowed_emulators': None, # The example uses aarch32 instructions which are not present in ARMv7. # Therefore, it cannot be run in baremetal ARMv7 CPUs. # User mode simulation however seems to enable aarch32 so these run fine. @@ -271,6 +272,10 @@ class PathProperties: self.unimplemented_instructions[env['emulator']][env['arch']] & self['uses_instructions'][env['arch']] ) + ) and + ( + self['allowed_emulators'] is None or + env['emulator'] in self['allowed_emulators'] ) ) @@ -430,6 +435,19 @@ path_properties_tuples = ( 'userland': True, }, { + 'algorithm': ( + {}, + { + 'set': ( + {}, + { + 'std_priority_queue_gem5.cpp': {'allowed_emulators': {'gem5'}}, + 'std_set_gem5.cpp': {'allowed_emulators': {'gem5'}}, + 'std_unordered_set_gem5.cpp': {'allowed_emulators': {'gem5'}}, + } + ), + }, + ), 'arch': ( { 'baremetal': True, diff --git a/userland/algorithm/build b/userland/algorithm/build new file mode 120000 index 0000000..ab18017 --- /dev/null +++ b/userland/algorithm/build @@ -0,0 +1 @@ +../build \ No newline at end of file diff --git a/userland/algorithm/set/README.adoc b/userland/algorithm/set/README.adoc new file mode 100644 index 0000000..66e315e --- /dev/null +++ b/userland/algorithm/set/README.adoc @@ -0,0 +1 @@ +https://cirosantilli.com/linux-kernel-module-cheat#bst-vs-heap-vs-hashmap diff --git a/userland/algorithm/set/build b/userland/algorithm/set/build new file mode 120000 index 0000000..ab18017 --- /dev/null +++ b/userland/algorithm/set/build @@ -0,0 +1 @@ +../build \ No newline at end of file diff --git a/userland/algorithm/set/main.hpp b/userland/algorithm/set/main.hpp new file mode 100644 index 0000000..58ae5a7 --- /dev/null +++ b/userland/algorithm/set/main.hpp @@ -0,0 +1,115 @@ +// https://cirosantilli.com/linux-kernel-module-cheat#bst-vs-heap-vs-hashmap + +#include +#include +#include +#include +#if LKMC_ALGORITHM_SET_STD_PRIORITY_QUEUE +#include +#endif +#include +#if LKMC_ALGORITHM_SET_STD_SET +#include +#endif +#if LKMC_ALGORITHM_SET_STD_UNORDERED_SET +#include +#endif + +#include + +int main(int argc, char **argv) { + typedef uint64_t T; +#if LKMC_ALGORITHM_SET_STD_PRIORITY_QUEUE + std::priority_queue set; +#endif +#if LKMC_ALGORITHM_SET_STD_SET + std::set set; +#endif +#if LKMC_ALGORITHM_SET_STD_UNORDERED_SET + std::unordered_set set; +#endif + std::vector randoms; + size_t i, j = 0, n, granule, base; + unsigned int seed; +#ifndef LKMC_M5OPS_ENABLE + std::vector dts; + std::vector bases; +#endif + + // CLI arguments. + if (argc > 1) { + n = std::stoi(argv[1]); + } else { + n = 10; + } + if (argc > 2) { + granule = std::stoi(argv[2]); + } else { + granule = 1; + } + if (argc > 3) { + seed = std::stoi(argv[3]); + } else { + seed = std::random_device()(); + } + + // Action. + for (i = 0; i < n; ++i) { + randoms.push_back(i); + } + std::shuffle(randoms.begin(), randoms.end(), std::mt19937(seed)); + for (i = 0; i < n / granule; ++i) { +#ifndef LKMC_M5OPS_ENABLE + using clk = std::chrono::high_resolution_clock; + decltype(clk::now()) start, end; +#endif + base = i * granule; +#ifdef LKMC_M5OPS_ENABLE + LKMC_M5OPS_RESETSTATS; +#else + start = clk::now(); + for (j = 0; j < granule; ++j) { +#endif +#if LKMC_ALGORITHM_SET_STD_PRIORITY_QUEUE + set.emplace(randoms[base + j]); +#else + set.insert(randoms[base + j]); +#endif +#ifdef LKMC_M5OPS_ENABLE + LKMC_M5OPS_DUMPSTATS; +#else + } + end = clk::now(); + auto dt = (end - start) / granule; + bases.push_back(base); + dts.push_back(std::chrono::duration_cast(dt).count()); +#endif + } + + // Report results. +#ifndef LKMC_M5OPS_ENABLE + // Output. + std::cout << "times" << std::endl; + auto bases_it = bases.begin(); + auto dts_it = dts.begin(); + while(bases_it != bases.end()) { + const auto& base = *bases_it; + const auto& dt = *dts_it; + std::cout << base << " " << dt << std::endl; + bases_it++; + dts_it++; + } + std::cout << std::endl; + std::cout << "output" << std::endl; +#if LKMC_ALGORITHM_SET_STD_PRIORITY_QUEUE + while (!set.empty()) { + std::cout << set.top() << std::endl; + set.pop(); + } +#else + for (const auto& item : set) { + std::cout << item << std::endl; + } +#endif +#endif +} diff --git a/userland/algorithm/set/std_priority_queue.cpp b/userland/algorithm/set/std_priority_queue.cpp new file mode 100644 index 0000000..e1cda36 --- /dev/null +++ b/userland/algorithm/set/std_priority_queue.cpp @@ -0,0 +1,2 @@ +#define LKMC_ALGORITHM_SET_STD_PRIORITY_QUEUE 1 +#include "main.hpp" diff --git a/userland/algorithm/set/std_priority_queue_gem5.cpp b/userland/algorithm/set/std_priority_queue_gem5.cpp new file mode 100644 index 0000000..0febfd7 --- /dev/null +++ b/userland/algorithm/set/std_priority_queue_gem5.cpp @@ -0,0 +1,3 @@ +#define LKMC_M5OPS_ENABLE 1 +#define LKMC_ALGORITHM_SET_STD_PRIORITY_QUEUE 1 +#include "main.hpp" diff --git a/userland/algorithm/set/std_set.cpp b/userland/algorithm/set/std_set.cpp new file mode 100644 index 0000000..0e5bfb2 --- /dev/null +++ b/userland/algorithm/set/std_set.cpp @@ -0,0 +1,2 @@ +#define LKMC_ALGORITHM_SET_STD_SET 1 +#include "main.hpp" diff --git a/userland/algorithm/set/std_set_gem5.cpp b/userland/algorithm/set/std_set_gem5.cpp new file mode 100644 index 0000000..1f49b2c --- /dev/null +++ b/userland/algorithm/set/std_set_gem5.cpp @@ -0,0 +1,3 @@ +#define LKMC_M5OPS_ENABLE 1 +#define LKMC_ALGORITHM_SET_STD_SET 1 +#include "main.hpp" diff --git a/userland/algorithm/set/std_unordered_set.cpp b/userland/algorithm/set/std_unordered_set.cpp new file mode 100644 index 0000000..a4bcdcc --- /dev/null +++ b/userland/algorithm/set/std_unordered_set.cpp @@ -0,0 +1,2 @@ +#define LKMC_ALGORITHM_SET_STD_UNORDERED_SET 1 +#include "main.hpp" diff --git a/userland/algorithm/set/std_unordered_set_gem5.cpp b/userland/algorithm/set/std_unordered_set_gem5.cpp new file mode 100644 index 0000000..95c14c4 --- /dev/null +++ b/userland/algorithm/set/std_unordered_set_gem5.cpp @@ -0,0 +1,3 @@ +#define LKMC_M5OPS_ENABLE 1 +#define LKMC_ALGORITHM_SET_STD_UNORDERED_SET 1 +#include "main.hpp" diff --git a/userland/algorithm/set/test b/userland/algorithm/set/test new file mode 120000 index 0000000..419df4f --- /dev/null +++ b/userland/algorithm/set/test @@ -0,0 +1 @@ +../test \ No newline at end of file diff --git a/userland/algorithm/test b/userland/algorithm/test new file mode 120000 index 0000000..419df4f --- /dev/null +++ b/userland/algorithm/test @@ -0,0 +1 @@ +../test \ No newline at end of file