Skip to content

Commit

Permalink
benchmarks: convert to catch2
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-mitchell committed Oct 27, 2023
1 parent fe873bd commit e760ee8
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 112 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ check_cxx_source_compiles(

check_cxx_compiler_flag('-march=native' HPCOMBI_HAVE_FLAG_ARCH_NATIVE)
check_cxx_compiler_flag('-mtune=native' HPCOMBI_HAVE_FLAG_TUNE_NATIVE)
# TODO check for -funroll-loops + -flax-vector-constexpr
# TODO only check for and set the flags required to make HPCombi work


#check_cxx_compiler_flag('-mavx2' HPCOMBI_HAVE_FLAG_AVX2)
#check_cxx_compiler_flag('-mavx512bw' HPCOMBI_HAVE_FLAG_AVX512BW)

Expand Down
27 changes: 19 additions & 8 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@
# http://www.gnu.org/licenses/ #
#****************************************************************************#

find_package(PkgConfig REQUIRED)
pkg_check_modules(BENCHMARK REQUIRED IMPORTED_TARGET benchmark)
find_package(Catch2 3 QUIET)

if(NOT Catch2_FOUND)
message(STATUS "Cloning Catch2 from GIT_REPOSITORY https://github.com/catchorg/Catch2.git")
Include(FetchContent)

FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0 # or a later release
)

FetchContent_MakeAvailable(Catch2)
else()
message(STATUS "Using system Catch2")
endif()

message(STATUS "Building tests")

include_directories(
${CMAKE_SOURCE_DIR}/include
Expand All @@ -26,10 +42,5 @@ set(benchmark_src
foreach(f ${benchmark_src})
get_filename_component(benchName ${f} NAME_WE)
add_executable (${benchName} ${f})
target_include_directories(${benchName} PUBLIC ${BENCHMARK_INCLUDE_DIRS})
target_compile_options(${benchName} PUBLIC ${BENCHMARK_CFLAGS_OTHER})
target_link_libraries(${benchName} benchmark pthread)
# install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${benchName}
# DESTINATION bin
# RENAME ${CMAKE_PROJECT_NAME}-${benchName})
target_link_libraries(${benchName} PRIVATE Catch2::Catch2WithMain)
endforeach(f)
212 changes: 108 additions & 104 deletions benchmark/bench_bmat8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,144 +13,148 @@
// http://www.gnu.org/licenses/ //
//****************************************************************************//

#include <cstdlib>
#include <iostream>
#include <benchmark/benchmark.h>
#include <string.h>
#include <stdlib.h>
#include <string>

#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>

#include "compilerinfo.hpp"
#include "cpu_x86_impl.hpp"
#include "bench_fixture.hpp"
// #include "compilerinfo.hpp"
// #include "cpu_x86_impl.hpp"

#include "bmat8.hpp"

using namespace FeatureDetector;
using namespace std;
using HPCombi::epu8;
// using namespace FeatureDetector;
// using namespace std;
// using HPCombi::epu8;

// const Fix_perm16 sample;
const std::string SIMDSET = cpu_x86::get_highest_SIMD();
const std::string PROCID = cpu_x86::get_proc_string();
namespace HPCombi {

using namespace HPCombi;
// const Fix_perm16 sample;
const std::string PROCID = "TODO";

std::vector<BMat8> make_sample(size_t n) {
std::vector<BMat8> res {};
for (size_t i=0; i < n; i++) {
std::vector<BMat8> res{};
for (size_t i = 0; i < n; i++) {
res.push_back(BMat8::random());
}
return res;
}

std::vector<std::pair<BMat8, BMat8>> make_pair_sample(size_t n) {
std::vector<std::pair<BMat8, BMat8>> res {};
for (size_t i=0; i < n; i++) {
res.push_back(make_pair(BMat8::random(),BMat8::random()));
std::vector<std::pair<BMat8, BMat8>> res{};
for (size_t i = 0; i < n; i++) {
res.push_back(std::make_pair(BMat8::random(), BMat8::random()));
}
return res;
}

std::vector<BMat8> sample = make_sample(1000);
std::vector<std::pair<BMat8, BMat8>> pair_sample = make_pair_sample(1000);
// std::vector<BMat8> sample = {BMat8::one()};
// std::vector<BMat8> sample = {BMat8(0)};

//##################################################################################
template<typename TF, typename Sample>
void myBench(const string &name, TF pfunc, Sample &sample) {
string fullname = name + "_" + CXX_VER + "_proc-" + PROCID;
benchmark::RegisterBenchmark(fullname.c_str(),
[pfunc, sample](benchmark::State& st) {
for (auto _ : st) {
for (auto elem : sample) {
benchmark::DoNotOptimize(pfunc(elem));
}
}
});
}

#define myBenchMeth(descr, methname, smp) \
myBench(descr, [](BMat8 p) { return p.methname(); }, smp)

//##################################################################################
int Bench_row_space_size() {
myBenchMeth("row_space_size_ref", row_space_size_ref, sample);
myBenchMeth("row_space_size_bitset", row_space_size_bitset, sample);
myBenchMeth("row_space_size_incl1", row_space_size_incl1, sample);
myBenchMeth("row_space_size_incl", row_space_size_incl, sample);
myBenchMeth("row_space_size", row_space_size, sample);
return 0;
// template <typename TF, typename Sample>
// void myBench(const std::string &name, TF pfunc, Sample &sample) {
// std::string fullname = name + "_" + CXX_VER + "_proc-" + PROCID;
// benchmark::RegisterBenchmark(
// fullname.c_str(), [pfunc, sample](benchmark::State &st) {
// for (auto _ : st) {
// for (auto elem : sample) {
// benchmark::DoNotOptimize(pfunc(elem));
// }
// }
// });
// }

#define BENCHMARK_MEM_FN(mem_fn, sample) \
BENCHMARK(#mem_fn) { \
for (auto &elem : sample) { \
REQUIRE_NOTHROW(elem.mem_fn()); \
} \
return true; \
};

#define BENCHMARK_MEM_FN_PAIR(mem_fn, sample) \
BENCHMARK(#mem_fn) { \
for (auto &pair : sample) { \
REQUIRE_NOTHROW( \
std::make_pair(pair.first.mem_fn(), pair.second.mem_fn())); \
} \
return true; \
};

TEST_CASE("Row space size benchmarks 1000 BMat8", "[BMat8][000]") {
BENCHMARK_MEM_FN(row_space_size_ref, sample);
BENCHMARK_MEM_FN(row_space_size_bitset, sample);
BENCHMARK_MEM_FN(row_space_size_incl1, sample);
BENCHMARK_MEM_FN(row_space_size_incl, sample);
BENCHMARK_MEM_FN(row_space_size, sample);
}

//##################################################################################
int Bench_transpose() {
myBenchMeth("transpose_knuth", transpose, sample);
myBenchMeth("transpose_mask", transpose_mask, sample);
myBenchMeth("transpose_maskd", transpose_maskd, sample);
return 0;
TEST_CASE("Transpose benchmarks 1000 BMat8", "[BMat8][000]") {
BENCHMARK_MEM_FN(transpose, sample);
BENCHMARK_MEM_FN(transpose_mask, sample);
BENCHMARK_MEM_FN(transpose_maskd, sample);
}

int Bench_transpose2() {
myBench("transpose2_knuth",
[](std::pair<BMat8, BMat8> p) {
return make_pair(p.first.transpose(),
p.second.transpose());
}, pair_sample);
myBench("transpose2_mask",
[](std::pair<BMat8, BMat8> p) {
return make_pair(p.first.transpose_mask(),
p.second.transpose_mask());
}, pair_sample);
myBench("transpose2_maskd",
[](std::pair<BMat8, BMat8> p) {
return make_pair(p.first.transpose_maskd(),
p.second.transpose_maskd());
}, pair_sample);
myBench("transpose2",
[](std::pair<BMat8, BMat8> p) {
BMat8::transpose2(p.first, p.second);
return p;
}, pair_sample);
return 0;
TEST_CASE("Transpose pairs benchmarks 1000 BMat8", "[BMat8][000]") {
BENCHMARK_MEM_FN_PAIR(transpose, pair_sample);
BENCHMARK_MEM_FN_PAIR(transpose_mask, pair_sample);
BENCHMARK_MEM_FN_PAIR(transpose_maskd, pair_sample);
BENCHMARK("transpose2") {
for (auto &pair : pair_sample) {
REQUIRE_NOTHROW(BMat8::transpose2(pair.first, pair.second));
}
return true;
};
}
/*
int Bench_row_space_included() {
myBench("row_space_incl_ref",
[](std::pair<BMat8, BMat8> p) {
return p.first.row_space_included_ref(p.second);
}, pair_sample);
myBench("row_space_incl_bitset",
[](std::pair<BMat8, BMat8> p) {
return p.first.row_space_included_bitset(p.second);
}, pair_sample);
myBench("row_space_incl_rotate",
[](std::pair<BMat8, BMat8> p) {
return p.first.row_space_included(p.second);
}, pair_sample);
myBench(
"row_space_incl_ref",
[](std::pair<BMat8, BMat8> p) {
return p.first.row_space_included_ref(p.second);
},
pair_sample);
myBench(
"row_space_incl_bitset",
[](std::pair<BMat8, BMat8> p) {
return p.first.row_space_included_bitset(p.second);
},
pair_sample);
myBench(
"row_space_incl_rotate",
[](std::pair<BMat8, BMat8> p) {
return p.first.row_space_included(p.second);
},
pair_sample);
return 0;
}
int Bench_row_space_included2() {
myBench("row_space_incl2_rotate",
[](std::pair<BMat8, BMat8> p) {
return p.first.row_space_included(p.second)
== p.second.row_space_included(p.first);
}, pair_sample);
myBench("row_space_incl2",
[](std::pair<BMat8, BMat8> p) {
auto res = BMat8::row_space_included2(
p.first, p.second, p.second, p.first);
return res.first == res.second;
}, pair_sample);
myBench(
"row_space_incl2_rotate",
[](std::pair<BMat8, BMat8> p) {
return p.first.row_space_included(p.second) ==
p.second.row_space_included(p.first);
},
pair_sample);
myBench(
"row_space_incl2",
[](std::pair<BMat8, BMat8> p) {
auto res = BMat8::row_space_included2(p.first, p.second,
p.second, p.first); return res.first == res.second;
},
pair_sample);
return 0;
}
auto dummy = {
Bench_row_space_size(),
Bench_transpose(),
Bench_transpose2(),
Bench_row_space_included(),
Bench_row_space_included2()
};

BENCHMARK_MAIN();
auto dummy = {Bench_row_space_size(), Bench_transpose(),
Bench_transpose2(), Bench_row_space_included(),
Bench_row_space_included2()};
*/
} // namespace HPCombi

0 comments on commit e760ee8

Please sign in to comment.