Better handling of printing results board

This commit is contained in:
saundersp
2024-03-21 00:50:13 +01:00
parent f65c58d95c
commit 211dcad893
8 changed files with 307 additions and 188 deletions

View File

@ -1,6 +1,5 @@
#pragma once
#include <filesystem>
namespace fs = std::filesystem;
#include "data.hpp"
#include "toolbox.hpp"
//#include "config.hpp"
@ -53,62 +52,64 @@ bool unit_test_argsort_2d(const np::Array<T>& a, const np::Array<uint16_t>& indi
}
template <typename T, typename F, typename... Args>
T benchmark_function(const char* step_name, const F& fnc, Args &&...args) noexcept {
T benchmark_function(const char* const step_name, const int32_t& column_width, const F& fnc, Args &&...args) noexcept {
#if __DEBUG == false
printf("%s...\r", step_name);
fflush(stdout); // manual flush is mandatory, otherwise it will not be shown immediately because the output is buffered
fprintf(stderr, "%s...\r", step_name);
fflush(stderr); // manual flush is mandatory, otherwise it will not be shown immediately because the output is buffered
#endif
const std::chrono::system_clock::time_point start = perf_counter_ns();
const T res = fnc(std::forward<Args>(args)...);
const long long time_spent = duration_ns(perf_counter_ns() - start);
printf("| %-49s | %18s | %-29s |\n", step_name, thousand_sep(time_spent).c_str(), format_time_ns(time_spent).c_str());
formatted_row<3>({ column_width, -18, 29 }, { step_name, thousand_sep(time_spent).c_str(), format_time_ns(time_spent).c_str() });
return res;
}
template <typename F, typename... Args>
void benchmark_function_void(const char* step_name, const F& fnc, Args &&...args) noexcept {
void benchmark_function_void(const char* const step_name, const int32_t& column_width, const F& fnc, Args &&...args) noexcept {
#if __DEBUG == false
printf("%s...\r", step_name);
fflush(stdout); // manual flush is mandatory, otherwise it will not be shown immediately because the output is buffered
fprintf(stderr, "%s...\r", step_name);
fflush(stderr); // manual flush is mandatory, otherwise it will not be shown immediately because the output is buffered
#endif
const std::chrono::system_clock::time_point start = perf_counter_ns();
fnc(std::forward<Args>(args)...);
const long long time_spent = duration_ns(perf_counter_ns() - start);
printf("| %-49s | %18s | %-29s |\n", step_name, thousand_sep(time_spent).c_str(), format_time_ns(time_spent).c_str());
formatted_row<3>({ column_width, -18, 29 }, { step_name, thousand_sep(time_spent).c_str(), format_time_ns(time_spent).c_str() });
}
template <typename T, typename F, typename... Args>
np::Array<T> state_saver(const char* step_name, const char* filename, const bool& force_redo, const bool& save_state, const char* out_dir, const F& fnc, Args &&...args) noexcept {
np::Array<T> state_saver(const char* const step_name, const int32_t& column_width, const char* const filename, const bool& force_redo, const bool& save_state, const char* const out_dir, const F& fnc, Args &&...args) noexcept {
char filepath[BUFFER_SIZE] = { 0 };
sprintf(filepath, "%s/%s.bin", out_dir, filename);
snprintf(filepath, BUFFER_SIZE, "%s/%s.bin", out_dir, filename);
np::Array<T> bin;
if (!fs::exists(filepath) || force_redo) {
bin = std::move(benchmark_function<np::Array<T>>(step_name, fnc, std::forward<Args>(args)...));
if (!std::filesystem::exists(filepath) || force_redo) {
//bin = std::move(benchmark_function<np::Array<T>>(step_name, column_width, fnc, std::forward<Args>(args)...));
bin = benchmark_function<np::Array<T>>(step_name, column_width, fnc, std::forward<Args>(args)...);
if(save_state){
#if __DEBUG == false
printf("Saving results of %s\r", step_name);
fflush(stdout);
fprintf(stderr, "Saving results of %s\r", step_name);
fflush(stderr);
#endif
save<T>(bin, filepath);
#if __DEBUG == false
printf("%*c\r", 100, ' ');
fflush(stdout);
fprintf(stderr, "%*c\r", 100, ' '); // Clear previous clear
fflush(stderr);
#endif
}
} else {
#if __DEBUG == false
printf("Loading results of %s\r", step_name);
fflush(stdout);
fprintf(stderr, "Loading results of %s\r", step_name);
fflush(stderr);
#endif
bin = std::move(load<T>(filepath));
printf("| %-49s | %18s | %-29s |\n", step_name, "None", "loaded saved state");
//bin = std::move(load<T>(filepath));
bin = load<T>(filepath);
formatted_row<3>({ column_width, -18, 29 }, { step_name, "None", "loaded saved state" });
}
return bin;
}
template <typename T, size_t N, typename F, typename... Args>
std::array<np::Array<T>, N> state_saver(const char* step_name, const std::vector<const char*>& filenames, const bool& force_redo, const bool& save_state, const char* out_dir, const F& fnc, Args &&...args) noexcept {
std::array<np::Array<T>, N> state_saver(const char* const step_name, const int32_t& column_width, const std::vector<const char*>& filenames, const bool& force_redo, const bool& save_state, const char* const out_dir, const F& fnc, Args &&...args) noexcept {
char filepath[BUFFER_SIZE] = { 0 };
bool abs = false;
for (const char* filename : filenames){
@ -121,11 +122,12 @@ std::array<np::Array<T>, N> state_saver(const char* step_name, const std::vector
std::array<np::Array<T>, N> bin;
if (abs || force_redo) {
bin = std::move(benchmark_function<std::array<np::Array<T>, N>>(step_name, fnc, std::forward<Args>(args)...));
//bin = std::move(benchmark_function<std::array<np::Array<T>, N>>(step_name, column_width, fnc, std::forward<Args>(args)...));
bin = benchmark_function<std::array<np::Array<T>, N>>(step_name, column_width, fnc, std::forward<Args>(args)...);
if (save_state){
#if __DEBUG == false
printf("Saving results of %s\r", step_name);
fflush(stdout);
fprintf(stderr, "Saving results of %s\r", step_name);
fflush(stderr);
#endif
size_t i = 0;
for (const char* filename : filenames){
@ -133,21 +135,21 @@ std::array<np::Array<T>, N> state_saver(const char* step_name, const std::vector
save<T>(bin[i++], filepath);
}
#if __DEBUG == false
printf("%*c\r", 100, ' ');
fflush(stdout);
fprintf(stderr, "%*c\r", 100, ' '); // Clear previous print
fflush(stderr);
#endif
}
} else {
#if __DEBUG == false
printf("Loading results of %s\r", step_name);
fflush(stdout);
fprintf(stderr, "Loading results of %s\r", step_name);
fflush(stderr);
#endif
size_t i = 0;
for (const char* filename : filenames){
sprintf(filepath, "%s/%s.bin", out_dir, filename);
bin[i++] = std::move(load<T>(filepath));
snprintf(filepath, BUFFER_SIZE, "%s/%s.bin", out_dir, filename);
}
printf("| %-49s | %18s | %-29s |\n", step_name, "None", "loaded saved state");
formatted_row<3>({ column_width, -18, 29 }, { step_name, "None", "loaded saved state" });
}
return bin;
}