From 46f1ec5a122cf7eb74efaafd2c6797cedfec137c Mon Sep 17 00:00:00 2001 From: Dappur Date: Sat, 23 Jul 2022 21:13:34 -0700 Subject: [PATCH 1/2] Correctly generate uniform distribution The default template parameter of `uniform_int_distribution` is int. As the constructor uses the same type, the passed argument `window` will be implicitly narrowed down and get overflow when its value is greater than INT_MAX. In that case, the random number generator has UB. --- examples/microbenchmarks/testParallel-PAM.cpp | 4 ++-- examples/microbenchmarks/testParallel.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/microbenchmarks/testParallel-PAM.cpp b/examples/microbenchmarks/testParallel-PAM.cpp index 15bda74..1f8bb4d 100644 --- a/examples/microbenchmarks/testParallel-PAM.cpp +++ b/examples/microbenchmarks/testParallel-PAM.cpp @@ -97,7 +97,7 @@ std::mt19937_64& get_rand_gen() { parlay::sequence uniform_input(size_t n, size_t window, bool shuffle = false) { auto g = [&](size_t i) { - uniform_int_distribution<> r_keys(1, window); + uniform_int_distribution r_keys(1, window); key_type key = r_keys(get_rand_gen()); key_type val = i; return make_pair(key, val); @@ -113,7 +113,7 @@ parlay::sequence uniform_input(size_t n, size_t window, parlay::sequence uniform_input_unsorted(size_t n, size_t window) { auto f = [&](size_t i) { - uniform_int_distribution<> r_keys(1, window); + uniform_int_distribution r_keys(1, window); key_type k = r_keys(get_rand_gen()); key_type c = r_keys(get_rand_gen()); return make_pair(k, c); diff --git a/examples/microbenchmarks/testParallel.cpp b/examples/microbenchmarks/testParallel.cpp index 3a6eda5..e6164d5 100644 --- a/examples/microbenchmarks/testParallel.cpp +++ b/examples/microbenchmarks/testParallel.cpp @@ -107,7 +107,7 @@ std::mt19937_64& get_rand_gen() { parlay::sequence uniform_input(size_t n, size_t window, bool shuffle = false) { auto g = [&](size_t i) { - uniform_int_distribution<> r_keys(1, window); + uniform_int_distribution r_keys(1, window); key_type key = r_keys(get_rand_gen()); key_type val = i; return make_pair(key, val); @@ -123,7 +123,7 @@ parlay::sequence uniform_input(size_t n, size_t window, parlay::sequence uniform_input_unsorted(size_t n, size_t window) { auto f = [&](size_t i) { - uniform_int_distribution<> r_keys(1, window); + uniform_int_distribution r_keys(1, window); key_type k = r_keys(get_rand_gen()); key_type c = r_keys(get_rand_gen()); return make_pair(k, c); From d5082aa9066ff831b94acb64ee364223a2c88fe8 Mon Sep 17 00:00:00 2001 From: Dappur Date: Sat, 23 Jul 2022 21:50:42 -0700 Subject: [PATCH 2/2] Correctly handle the '--small' flag --- .../microbenchmarks/blocksize_tuning/eval_microbenchmark.py | 4 ++-- examples/microbenchmarks/blocksize_tuning/run.py | 4 ++-- .../microbenchmarks/blocksize_vs_space/eval_microbenchmark.py | 4 ++-- examples/microbenchmarks/blocksize_vs_space/run.py | 4 ++-- examples/microbenchmarks/experiments/eval_microbenchmark.py | 4 ++-- examples/microbenchmarks/experiments/run_microbenchmark.py | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/microbenchmarks/blocksize_tuning/eval_microbenchmark.py b/examples/microbenchmarks/blocksize_tuning/eval_microbenchmark.py index d3e31aa..8ed03f9 100644 --- a/examples/microbenchmarks/blocksize_tuning/eval_microbenchmark.py +++ b/examples/microbenchmarks/blocksize_tuning/eval_microbenchmark.py @@ -5,12 +5,12 @@ import settings parser = argparse.ArgumentParser(description='Run microbenchmarks') -parser.add_argument('--small', action="store_false", help='use small-size inputs') +parser.add_argument('--small', action="store_true", help='use small-size inputs') args = parser.parse_args() use_large = True tests = settings.large_tests -if (args.small != None): +if args.small: use_large = False tests = settings.small_tests diff --git a/examples/microbenchmarks/blocksize_tuning/run.py b/examples/microbenchmarks/blocksize_tuning/run.py index 3e3449e..ff48bb5 100644 --- a/examples/microbenchmarks/blocksize_tuning/run.py +++ b/examples/microbenchmarks/blocksize_tuning/run.py @@ -6,12 +6,12 @@ # Run tests based on settings and generate raw_output files. parser = argparse.ArgumentParser(description='Run microbenchmarks') -parser.add_argument('--small', action="store_false", help='use small-size inputs') +parser.add_argument('--small', action="store_true", help='use small-size inputs') args = parser.parse_args() use_large = True tests = settings.large_tests -if (args.small != None): +if args.small: use_large = False tests = settings.small_tests diff --git a/examples/microbenchmarks/blocksize_vs_space/eval_microbenchmark.py b/examples/microbenchmarks/blocksize_vs_space/eval_microbenchmark.py index 26557cd..3c85cc9 100644 --- a/examples/microbenchmarks/blocksize_vs_space/eval_microbenchmark.py +++ b/examples/microbenchmarks/blocksize_vs_space/eval_microbenchmark.py @@ -5,12 +5,12 @@ import settings parser = argparse.ArgumentParser(description='Run microbenchmarks') -parser.add_argument('--small', action="store_false", help='use small-size inputs') +parser.add_argument('--small', action="store_true", help='use small-size inputs') args = parser.parse_args() use_large = True tests = settings.large_tests -if (args.small != None): +if args.small: use_large = False tests = settings.small_tests diff --git a/examples/microbenchmarks/blocksize_vs_space/run.py b/examples/microbenchmarks/blocksize_vs_space/run.py index b18bb6c..948b769 100644 --- a/examples/microbenchmarks/blocksize_vs_space/run.py +++ b/examples/microbenchmarks/blocksize_vs_space/run.py @@ -6,12 +6,12 @@ # Run tests based on settings and generate raw_output files. parser = argparse.ArgumentParser(description='Run microbenchmarks') -parser.add_argument('--small', action="store_false", help='use small-size inputs') +parser.add_argument('--small', action="store_true", help='use small-size inputs') args = parser.parse_args() use_large = True tests = settings.large_tests -if (args.small != None): +if args.small: use_large = False tests = settings.small_tests diff --git a/examples/microbenchmarks/experiments/eval_microbenchmark.py b/examples/microbenchmarks/experiments/eval_microbenchmark.py index a7ebfa4..f0aa732 100644 --- a/examples/microbenchmarks/experiments/eval_microbenchmark.py +++ b/examples/microbenchmarks/experiments/eval_microbenchmark.py @@ -9,13 +9,13 @@ tab = {} parser = argparse.ArgumentParser(description='Run microbenchmarks') -parser.add_argument('--small', action="store_false", help='use small-size inputs') +parser.add_argument('--small', action="store_true", help='use small-size inputs') args = parser.parse_args() use_large = True unaug_tests = settings.large_unaug_tests aug_tests = settings.large_aug_tests -if (args.small != None): +if args.small: use_large = False unaug_tests = settings.small_unaug_tests aug_tests = settings.small_aug_tests diff --git a/examples/microbenchmarks/experiments/run_microbenchmark.py b/examples/microbenchmarks/experiments/run_microbenchmark.py index 67141df..4d505fa 100644 --- a/examples/microbenchmarks/experiments/run_microbenchmark.py +++ b/examples/microbenchmarks/experiments/run_microbenchmark.py @@ -6,13 +6,13 @@ # Run tests based on settings and generate raw_output files. parser = argparse.ArgumentParser(description='Run microbenchmarks') -parser.add_argument('--small', action="store_false", help='use small-size inputs') +parser.add_argument('--small', action="store_true", help='use small-size inputs') args = parser.parse_args() use_large = True unaug_tests = settings.large_unaug_tests aug_tests = settings.large_aug_tests -if (args.small != None): +if args.small: use_large = False unaug_tests = settings.small_unaug_tests aug_tests = settings.small_aug_tests