Skip to content

Commit

Permalink
add profiler support to benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
upsj committed May 25, 2023
1 parent 9a8a6a7 commit f68868e
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 24 deletions.
49 changes: 46 additions & 3 deletions benchmark/blas/blas_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,28 @@ dimensions parse_dims(rapidjson::Value& test_case)
}


std::string describe(rapidjson::Value& test_case)
{
std::stringstream ss;
auto optional_output = [&](const char* name) {
if (test_case.HasMember(name) && test_case[name].IsInt64()) {
ss << name << " = " << test_case[name].GetInt64() << " ";
}
};
optional_output("n");
optional_output("k");
optional_output("m");
optional_output("r");
optional_output("stride");
optional_output("stride_x");
optional_output("stride_y");
optional_output("stride_A");
optional_output("stride_B");
optional_output("stride_C");
return ss.str();
}


template <typename OpMap>
void apply_blas(const char* operation_name, std::shared_ptr<gko::Executor> exec,
std::shared_ptr<Timer> timer, const OpMap& operation_map,
Expand Down Expand Up @@ -501,6 +523,17 @@ void run_blas_benchmarks(std::shared_ptr<gko::Executor> exec,
{
auto operations = split(FLAGS_operations, ',');
auto& allocator = test_cases.GetAllocator();
auto profiler_hook = create_profiler_hook(exec);
if (profiler_hook) {
exec->add_logger(profiler_hook);
}
auto annotate =
[profiler_hook](const char* name) -> gko::log::profiling_scope_guard {
if (profiler_hook) {
return profiler_hook->user_range(name);
}
return {};
};

for (auto& test_case : test_cases.GetArray()) {
try {
Expand All @@ -521,10 +554,17 @@ void run_blas_benchmarks(std::shared_ptr<gko::Executor> exec,
if (do_print) {
std::clog << "Running test case: " << test_case << std::endl;
}

// annotate the test case
// This string needs to outlive `test_case_range` to make sure we
// don't use its const char* c_str() after it was freed.
auto test_case_str = describe(test_case);
auto test_case_range = annotate(test_case_str.c_str());
for (const auto& operation_name : operations) {
apply_blas(operation_name.c_str(), exec, timer, operation_map,
test_case, allocator);
{
auto operation_range = annotate(operation_name.c_str());
apply_blas(operation_name.c_str(), exec, timer,
operation_map, test_case, allocator);
}

if (do_print) {
std::clog << "Current state:" << std::endl
Expand All @@ -538,4 +578,7 @@ void run_blas_benchmarks(std::shared_ptr<gko::Executor> exec,
<< std::endl;
}
}
if (profiler_hook) {
exec->remove_logger(profiler_hook);
}
}
34 changes: 29 additions & 5 deletions benchmark/conversions/conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ int main(int argc, char* argv[])
}

auto& allocator = test_cases.GetAllocator();
auto profiler_hook = create_profiler_hook(exec);
if (profiler_hook) {
exec->add_logger(profiler_hook);
}
auto annotate =
[profiler_hook](const char* name) -> gko::log::profiling_scope_guard {
if (profiler_hook) {
return profiler_hook->user_range(name);
}
return {};
};

DefaultSystemGenerator<> generator{};

for (auto& test_case : test_cases.GetArray()) {
std::clog << "Benchmarking conversions. " << std::endl;
Expand All @@ -146,7 +159,7 @@ int main(int argc, char* argv[])
std::clog << "Running test case: " << test_case << std::endl;
gko::matrix_data<etype, itype> data;
try {
data = DefaultSystemGenerator<>::generate_matrix_data(test_case);
data = generator.generate_matrix_data(test_case);
} catch (std::exception& e) {
std::cerr << "Error setting up matrix data, what(): " << e.what()
<< std::endl;
Expand All @@ -160,6 +173,11 @@ int main(int argc, char* argv[])
std::clog << "Matrix is of size (" << data.size[0] << ", "
<< data.size[1] << ")" << std::endl;
add_or_set_member(test_case, "size", data.size[0], allocator);
// annotate the test case
// This string needs to outlive `test_case_range` to make sure we
// don't use its const char* c_str() after it was freed.
auto test_case_str = generator.describe_config(test_case);
auto test_case_range = annotate(test_case_str.c_str());
for (const auto& format_from : formats) {
try {
auto matrix_from =
Expand All @@ -175,10 +193,13 @@ int main(int argc, char* argv[])
conversion_case.HasMember(conversion_name.c_str())) {
continue;
}

convert_matrix(matrix_from.get(), format_to.c_str(),
conversion_name.c_str(), exec, test_case,
allocator);
{
auto conversion_range =
annotate(conversion_name.c_str());
convert_matrix(matrix_from.get(), format_to.c_str(),
conversion_name.c_str(), exec, test_case,
allocator);
}
std::clog << "Current state:" << std::endl
<< test_cases << std::endl;
}
Expand All @@ -202,6 +223,9 @@ int main(int argc, char* argv[])
}
}
}
if (profiler_hook) {
exec->remove_logger(profiler_hook);
}

std::cout << test_cases << std::endl;
}
28 changes: 26 additions & 2 deletions benchmark/preconditioner/preconditioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,17 @@ int main(int argc, char* argv[])
}

auto& allocator = test_cases.GetAllocator();
auto profiler_hook = create_profiler_hook(exec);
if (profiler_hook) {
exec->add_logger(profiler_hook);
}
auto annotate =
[profiler_hook](const char* name) -> gko::log::profiling_scope_guard {
if (profiler_hook) {
return profiler_hook->user_range(name);
}
return {};
};

DefaultSystemGenerator<> generator{};

Expand All @@ -308,6 +319,12 @@ int main(int argc, char* argv[])
}
std::clog << "Running test case: " << test_case << std::endl;

// annotate the test case
// This string needs to outlive `test_case_range` to make sure we
// don't use its const char* c_str() after it was freed.
auto test_case_str = generator.describe_config(test_case);
auto test_case_range = annotate(test_case_str.c_str());

auto data = generator.generate_matrix_data(test_case);

auto system_matrix =
Expand All @@ -322,8 +339,12 @@ int main(int argc, char* argv[])
<< std::endl;
add_or_set_member(test_case, "size", data.size[0], allocator);
for (const auto& precond_name : preconditioners) {
run_preconditioner(precond_name.c_str(), exec, system_matrix,
b.get(), x.get(), test_case, allocator);
{
auto precond_range = annotate(precond_name.c_str());
run_preconditioner(precond_name.c_str(), exec,
system_matrix, b.get(), x.get(),
test_case, allocator);
}
std::clog << "Current state:" << std::endl
<< test_cases << std::endl;
backup_results(test_cases);
Expand All @@ -333,6 +354,9 @@ int main(int argc, char* argv[])
<< std::endl;
}
}
if (profiler_hook) {
exec->remove_logger(profiler_hook);
}

std::cout << test_cases << std::endl;
}
32 changes: 28 additions & 4 deletions benchmark/solver/solver_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,17 @@ void run_solver_benchmarks(std::shared_ptr<gko::Executor> exec,
}

auto& allocator = test_cases.GetAllocator();
auto profiler_hook = create_profiler_hook(exec);
if (profiler_hook) {
exec->add_logger(profiler_hook);
}
auto annotate =
[profiler_hook](const char* name) -> gko::log::profiling_scope_guard {
if (profiler_hook) {
return profiler_hook->user_range(name);
}
return {};
};

for (auto& test_case : test_cases.GetArray()) {
try {
Expand All @@ -628,6 +639,12 @@ void run_solver_benchmarks(std::shared_ptr<gko::Executor> exec,
})) {
continue;
}
// annotate the test case
// This string needs to outlive `test_case_range` to make sure we
// don't use its const char* c_str() after it was freed.
auto test_case_str = system_generator.describe_config(test_case);
auto test_case_range = annotate(test_case_str.c_str());

if (do_print) {
std::clog << "Running test case: " << test_case << std::endl;
}
Expand Down Expand Up @@ -660,16 +677,20 @@ void run_solver_benchmarks(std::shared_ptr<gko::Executor> exec,
allocator);
auto precond_solver_name = begin(precond_solvers);
for (const auto& solver_name : solvers) {
auto solver_range = annotate(solver_name.c_str());
for (const auto& precond_name : preconds) {
if (do_print) {
std::clog
<< "\tRunning solver: " << *precond_solver_name
<< std::endl;
}
solve_system(solver_name, precond_name,
precond_solver_name->c_str(), exec, timer,
system_matrix, b.get(), x.get(), test_case,
allocator);
{
auto precond_range = annotate(precond_name.c_str());
solve_system(solver_name, precond_name,
precond_solver_name->c_str(), exec, timer,
system_matrix, b.get(), x.get(), test_case,
allocator);
}
if (do_print) {
backup_results(test_cases);
}
Expand All @@ -686,6 +707,9 @@ void run_solver_benchmarks(std::shared_ptr<gko::Executor> exec,
}
}
}
if (profiler_hook) {
exec->remove_logger(profiler_hook);
}
}


Expand Down
31 changes: 27 additions & 4 deletions benchmark/sparse_blas/sparse_blas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,22 @@ int main(int argc, char* argv[])
print_general_information(extra_information);

auto& allocator = test_cases.GetAllocator();
auto profiler_hook = create_profiler_hook(exec);
if (profiler_hook) {
exec->add_logger(profiler_hook);
}
auto annotate =
[profiler_hook](const char* name) -> gko::log::profiling_scope_guard {
if (profiler_hook) {
return profiler_hook->user_range(name);
}
return {};
};

auto operations = split(FLAGS_operations, ',');

DefaultSystemGenerator<> generator{};

for (auto& test_case : test_cases.GetArray()) {
try {
// set up benchmark
Expand All @@ -180,8 +193,12 @@ int main(int argc, char* argv[])
}
auto& sp_blas_case = test_case[benchmark_name];
std::clog << "Running test case: " << test_case << std::endl;
auto data =
DefaultSystemGenerator<>::generate_matrix_data(test_case);
// annotate the test case
// This string needs to outlive `test_case_range` to make sure we
// don't use its const char* c_str() after it was freed.
auto test_case_str = generator.describe_config(test_case);
auto test_case_range = annotate(test_case_str.c_str());
auto data = generator.generate_matrix_data(test_case);
data.ensure_row_major_order();
std::clog << "Matrix is of size (" << data.size[0] << ", "
<< data.size[1] << "), " << data.nonzeros.size()
Expand All @@ -196,8 +213,11 @@ int main(int argc, char* argv[])
for (const auto& operation_name : operations) {
if (FLAGS_overwrite ||
!sp_blas_case.HasMember(operation_name.c_str())) {
apply_sparse_blas(operation_name.c_str(), exec, mtx.get(),
sp_blas_case, allocator);
{
auto operation_range = annotate(operation_name.c_str());
apply_sparse_blas(operation_name.c_str(), exec,
mtx.get(), sp_blas_case, allocator);
}
std::clog << "Current state:" << std::endl
<< test_cases << std::endl;
backup_results(test_cases);
Expand All @@ -215,6 +235,9 @@ int main(int argc, char* argv[])
}
}
}
if (profiler_hook) {
exec->remove_logger(profiler_hook);
}

std::cout << test_cases << std::endl;
}
29 changes: 26 additions & 3 deletions benchmark/spmv/spmv_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ void run_spmv_benchmark(std::shared_ptr<gko::Executor> exec,
std::shared_ptr<Timer> timer, bool do_print)
{
auto& allocator = test_cases.GetAllocator();
auto profiler_hook = create_profiler_hook(exec);
if (profiler_hook) {
exec->add_logger(profiler_hook);
}
auto annotate =
[profiler_hook](const char* name) -> gko::log::profiling_scope_guard {
if (profiler_hook) {
return profiler_hook->user_range(name);
}
return {};
};

for (auto& test_case : test_cases.GetArray()) {
try {
Expand All @@ -181,6 +192,12 @@ void run_spmv_benchmark(std::shared_ptr<gko::Executor> exec,
if (do_print) {
std::clog << "Running test case: " << test_case << std::endl;
}
// annotate the test case
// This string needs to outlive `test_case_range` to make sure we
// don't use its const char* c_str() after it was freed.
auto test_case_str = system_generator.describe_config(test_case);
auto test_case_range = annotate(test_case_str.c_str());

auto data = system_generator.generate_matrix_data(test_case);

auto nrhs = FLAGS_nrhs;
Expand Down Expand Up @@ -213,9 +230,12 @@ void run_spmv_benchmark(std::shared_ptr<gko::Executor> exec,
exec->synchronize();
}
for (const auto& format_name : formats) {
apply_spmv(format_name.c_str(), exec, system_generator, timer,
data, b.get(), x.get(), answer.get(), test_case,
allocator);
{
auto format_range = annotate(format_name.c_str());
apply_spmv(format_name.c_str(), exec, system_generator,
timer, data, b.get(), x.get(), answer.get(),
test_case, allocator);
}
if (do_print) {
std::clog << "Current state:" << std::endl
<< test_cases << std::endl;
Expand Down Expand Up @@ -246,6 +266,9 @@ void run_spmv_benchmark(std::shared_ptr<gko::Executor> exec,
}
}
}
if (profiler_hook) {
exec->remove_logger(profiler_hook);
}
}

#endif // GINKGO_BENCHMARK_SPMV_SPMV_COMMON_HPP
Loading

0 comments on commit f68868e

Please sign in to comment.