Skip to content

Commit 8187e9a

Browse files
author
Raghuveer Devulapalli
authored
Merge pull request #106 from r-devulap/bench
Add more benchmarks
2 parents fd937a5 + 6d04f70 commit 8187e9a

File tree

7 files changed

+120
-47
lines changed

7 files changed

+120
-47
lines changed

benchmarks/bench-all.cpp

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,4 @@
1-
#include "rand_array.h"
2-
#include "x86simdsort.h"
3-
#include <benchmark/benchmark.h>
4-
5-
#define MY_BENCHMARK_CAPTURE(func, T, test_case_name, ...) \
6-
BENCHMARK_PRIVATE_DECLARE(func) \
7-
= (::benchmark::internal::RegisterBenchmarkInternal( \
8-
new ::benchmark::internal::FunctionBenchmark( \
9-
#func "/" #test_case_name "/" #T, \
10-
[](::benchmark::State &st) { \
11-
func<T>(st, __VA_ARGS__); \
12-
})))
13-
14-
#define BENCH_SORT(func, type) \
15-
MY_BENCHMARK_CAPTURE( \
16-
func, type, smallrandom_128, 128, std::string("random")); \
17-
MY_BENCHMARK_CAPTURE( \
18-
func, type, smallrandom_256, 256, std::string("random")); \
19-
MY_BENCHMARK_CAPTURE( \
20-
func, type, smallrandom_512, 512, std::string("random")); \
21-
MY_BENCHMARK_CAPTURE( \
22-
func, type, smallrandom_1k, 1024, std::string("random")); \
23-
MY_BENCHMARK_CAPTURE(func, type, random_5k, 5000, std::string("random")); \
24-
MY_BENCHMARK_CAPTURE( \
25-
func, type, random_100k, 100000, std::string("random")); \
26-
MY_BENCHMARK_CAPTURE( \
27-
func, type, random_1m, 1000000, std::string("random")); \
28-
MY_BENCHMARK_CAPTURE( \
29-
func, type, random_10m, 10000000, std::string("random")); \
30-
MY_BENCHMARK_CAPTURE( \
31-
func, type, sorted_10k, 10000, std::string("sorted")); \
32-
MY_BENCHMARK_CAPTURE( \
33-
func, type, constant_10k, 10000, std::string("constant")); \
34-
MY_BENCHMARK_CAPTURE( \
35-
func, type, reverse_10k, 10000, std::string("reverse"));
36-
37-
#define BENCH_PARTIAL(func, type) \
38-
MY_BENCHMARK_CAPTURE(func, type, k10, 10000, 10); \
39-
MY_BENCHMARK_CAPTURE(func, type, k100, 10000, 100); \
40-
MY_BENCHMARK_CAPTURE(func, type, k1000, 10000, 1000); \
41-
MY_BENCHMARK_CAPTURE(func, type, k5000, 10000, 5000);
42-
1+
#include "bench.h"
432
#include "bench-argsort.hpp"
443
#include "bench-partial-qsort.hpp"
454
#include "bench-qselect.hpp"

benchmarks/bench-vqsort.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "bench.h"
2+
#define VQSORT_ONLY_STATIC 1
3+
#include "hwy/contrib/sort/vqsort-inl.h"
4+
5+
template <typename T, class... Args>
6+
static void vqsort(benchmark::State &state, Args &&...args)
7+
{
8+
// Get args
9+
auto args_tuple = std::make_tuple(std::move(args)...);
10+
size_t arrsize = std::get<0>(args_tuple);
11+
std::string arrtype = std::get<1>(args_tuple);
12+
// set up array
13+
std::vector<T> arr = get_array<T>(arrtype, arrsize);
14+
std::vector<T> arr_bkp = arr;
15+
// benchmark
16+
for (auto _ : state) {
17+
hwy::HWY_NAMESPACE::VQSortStatic(arr.data(), arrsize, hwy::SortAscending());
18+
state.PauseTiming();
19+
arr = arr_bkp;
20+
state.ResumeTiming();
21+
}
22+
}
23+
24+
BENCH_SORT(vqsort, uint64_t)
25+
BENCH_SORT(vqsort, int64_t)
26+
BENCH_SORT(vqsort, uint32_t)
27+
BENCH_SORT(vqsort, int32_t)
28+
BENCH_SORT(vqsort, float)
29+
BENCH_SORT(vqsort, double)

benchmarks/bench.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "rand_array.h"
2+
#include "x86simdsort.h"
3+
#include <benchmark/benchmark.h>
4+
5+
#define MY_BENCHMARK_CAPTURE(func, T, test_case_name, ...) \
6+
BENCHMARK_PRIVATE_DECLARE(func) \
7+
= (::benchmark::internal::RegisterBenchmarkInternal( \
8+
new ::benchmark::internal::FunctionBenchmark( \
9+
#func "/" #test_case_name "/" #T, \
10+
[](::benchmark::State &st) { \
11+
func<T>(st, __VA_ARGS__); \
12+
})))
13+
14+
#define BENCH_SORT(func, type) \
15+
MY_BENCHMARK_CAPTURE( \
16+
func, type, random_128, 128, std::string("random")); \
17+
MY_BENCHMARK_CAPTURE( \
18+
func, type, random_256, 256, std::string("random")); \
19+
MY_BENCHMARK_CAPTURE( \
20+
func, type, random_512, 512, std::string("random")); \
21+
MY_BENCHMARK_CAPTURE( \
22+
func, type, random_1k, 1024, std::string("random")); \
23+
MY_BENCHMARK_CAPTURE(func, type, random_5k, 5000, std::string("random")); \
24+
MY_BENCHMARK_CAPTURE( \
25+
func, type, random_100k, 100000, std::string("random")); \
26+
MY_BENCHMARK_CAPTURE( \
27+
func, type, random_1m, 1000000, std::string("random")); \
28+
MY_BENCHMARK_CAPTURE( \
29+
func, type, random_10m, 10000000, std::string("random")); \
30+
MY_BENCHMARK_CAPTURE( \
31+
func, type, smallrange_128, 128, std::string("smallrange")); \
32+
MY_BENCHMARK_CAPTURE( \
33+
func, type, smallrange_256, 256, std::string("smallrange")); \
34+
MY_BENCHMARK_CAPTURE( \
35+
func, type, smallrange_512, 512, std::string("smallrange")); \
36+
MY_BENCHMARK_CAPTURE( \
37+
func, type, smallrange_1k, 1024, std::string("smallrange")); \
38+
MY_BENCHMARK_CAPTURE(func, type, smallrange_5k, 5000, std::string("smallrange")); \
39+
MY_BENCHMARK_CAPTURE( \
40+
func, type, smallrange_100k, 100000, std::string("smallrange")); \
41+
MY_BENCHMARK_CAPTURE( \
42+
func, type, smallrange_1m, 1000000, std::string("smallrange")); \
43+
MY_BENCHMARK_CAPTURE( \
44+
func, type, smallrange_10m, 10000000, std::string("smallrange")); \
45+
MY_BENCHMARK_CAPTURE( \
46+
func, type, sorted_10k, 10000, std::string("sorted")); \
47+
MY_BENCHMARK_CAPTURE( \
48+
func, type, constant_10k, 10000, std::string("constant")); \
49+
MY_BENCHMARK_CAPTURE( \
50+
func, type, reverse_10k, 10000, std::string("reverse"));
51+
52+
#define BENCH_PARTIAL(func, type) \
53+
MY_BENCHMARK_CAPTURE(func, type, k10, 10000, 10); \
54+
MY_BENCHMARK_CAPTURE(func, type, k100, 10000, 100); \
55+
MY_BENCHMARK_CAPTURE(func, type, k1000, 10000, 1000); \
56+
MY_BENCHMARK_CAPTURE(func, type, k5000, 10000, 5000);

benchmarks/meson.build

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,15 @@ libbench += static_library('bench_qsort',
88
include_directories : [src, lib, utils],
99
cpp_args : ['-O3'],
1010
)
11+
12+
if fs.is_file('highway/hwy/contrib/sort/vqsort-inl.h')
13+
hwy = include_directories('highway')
14+
libbench += static_library('bench_vqsort',
15+
files(
16+
'bench-vqsort.cpp',
17+
),
18+
dependencies: gbench_dep,
19+
include_directories : [src, lib, utils, hwy],
20+
cpp_args : ['-O3', '-march=native'],
21+
)
22+
endif

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ project('x86-simd-sort', 'cpp',
22
version : '4.0.0',
33
license : 'BSD 3-clause',
44
default_options : ['cpp_std=c++17'])
5+
fs = import('fs')
56
cpp = meson.get_compiler('cpp')
67
src = include_directories('src')
78
lib = include_directories('lib')

run-bench.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
if args.benchcompare:
2020
baseline = ""
2121
contender = ""
22-
if "qsort" in args.benchcompare:
22+
if "vqsort" in args.benchcompare:
23+
baseline = "vqsort.*" + filterb
24+
contender = "simdsort.*" + filterb
25+
elif "qsort" in args.benchcompare:
2326
baseline = "scalarsort.*" + filterb
2427
contender = "simdsort.*" + filterb
2528
elif "select" in args.benchcompare:

utils/rand_array.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ get_uniform_rand_array_with_uniquevalues(int64_t arrsize,
6868
template <typename T>
6969
static std::vector<T>
7070
get_array(std::string arrtype,
71-
int64_t arrsize,
71+
size_t arrsize,
7272
T min = xss::fp::min<T>(),
7373
T max = xss::fp::max<T>())
7474
{
@@ -80,7 +80,7 @@ get_array(std::string arrtype,
8080
}
8181
else if (arrtype == "constant") {
8282
T temp = get_uniform_rand_array<T>(1, max, min)[0];
83-
for (auto ii = 0; ii < arrsize; ++ii) {
83+
for (size_t ii = 0; ii < arrsize; ++ii) {
8484
arr.push_back(temp);
8585
}
8686
}
@@ -90,7 +90,20 @@ get_array(std::string arrtype,
9090
std::reverse(arr.begin(), arr.end());
9191
}
9292
else if (arrtype == "smallrange") {
93-
arr = get_uniform_rand_array<T>(arrsize, 10, 1);
93+
arr = get_uniform_rand_array<T>(arrsize, 20, 1);
94+
}
95+
else if (arrtype == "random_5d") {
96+
size_t temp = std::max((size_t) 1, (size_t) (0.5 * arrsize));
97+
std::vector<T> temparr = get_uniform_rand_array<T>(temp);
98+
for (size_t ii = 0; ii < arrsize; ++ii) {
99+
if (ii < temp) {
100+
arr.push_back(temparr[ii]);
101+
}
102+
else {
103+
arr.push_back((T) 0);
104+
}
105+
}
106+
std::shuffle(arr.begin(), arr.end(), std::default_random_engine(42));
94107
}
95108
else if (arrtype == "max_at_the_end") {
96109
arr = get_uniform_rand_array<T>(arrsize, max, min);
@@ -126,7 +139,7 @@ get_array(std::string arrtype,
126139
else {
127140
val = std::numeric_limits<T>::max();
128141
}
129-
for (auto ii = 1; ii <= arrsize; ++ii) {
142+
for (size_t ii = 1; ii <= arrsize; ++ii) {
130143
if (rand() % 0x1) {
131144
arr[ii] = val;
132145
}

0 commit comments

Comments
 (0)