From 09fb2900d3811d76ef145e5f960e5b824bb0bfb6 Mon Sep 17 00:00:00 2001 From: enricotenuti Date: Mon, 21 Oct 2024 16:36:13 +0200 Subject: Benchmark setup --- .gitignore | 30 +-- benchmark.sh | 65 +++++++ .../benchmark_performance.c | 183 +++++++++++++++++++ tools/402_benchmark_nodes/benchmark_nodes.c | 201 +++++++++++++++++++++ 4 files changed, 467 insertions(+), 12 deletions(-) create mode 100755 benchmark.sh create mode 100644 tools/401_benchmark_performance/benchmark_performance.c create mode 100644 tools/402_benchmark_nodes/benchmark_nodes.c diff --git a/.gitignore b/.gitignore index 5e0950f..3a246b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,28 +1,34 @@ +*.o +*.so +*.s .DS_Store +.vscode cachegrind.out.* config.mk -gen debuggen +debugrun flamegraph.html -utils/.DS_Store +gen +h48.err +h48.out +HPC*.sh perf.data perf.data.old run -debugrun -shell/lasttest.out +run.core +run.DSYM +scrambles shell/lasttest.err +shell/lasttest.out tables/* tables-old/* -test/*/runtest test/.DS_Store +test/last.* test/run test/run.DSYM +test/*/runtest tools/.DS_Store -run.DSYM -run.core -test/last.* +tools/401_benchmark_performance/*.txt +tools/402_benchmark_nodes/*.txt tools/results -.vscode -*.o -*.so -*.s +utils/.DS_Store diff --git a/benchmark.sh b/benchmark.sh new file mode 100755 index 0000000..8f0ccc6 --- /dev/null +++ b/benchmark.sh @@ -0,0 +1,65 @@ +#!/bin/bash +if [ -z "$1" ]; then + echo "No argument provided. Possible values:" + echo " - performance" + echo " - architecture" + echo " - memory" + echo " - nodes" + echo " - multithread" + exit 1 +fi + +input=$1 +ARGS="h48h6k2" +THREADS=8 + +case $input in + performance) + echo "Performance Benchmark..." + make clean + THREADS=$THREADS ./configure.sh + TOOL=benchmark_performance TOOLARGS="$ARGS" make tool + ;; + architecture) + echo "Architecture Benchmark..." + make clean + THREADS=$THREADS ./configure.sh + TOOL=benchmark_common TOOLARGS="$ARGS" make tool + make clean + THREADS=$THREADS ARCH=PORTABLE ./configure.sh + TOOL=benchmark_common TOOLARGS="$ARGS" make tool + ;; + memory) + echo "Memory Benchmark..." + for i in {0..7}; do + make clean + THREADS=$THREADS ./configure.sh + M_ARGS="h48h${i}k2" + TOOL=benchmark_common TOOLARGS="$M_ARGS" make tool + done + ;; + nodes) + echo "Nodes Benchmark..." + make clean + THREADS=1 ./configure.sh + TOOL=benchmark_nodes TOOLARGS="$ARGS" make tool + ;; + multithread) + echo "Multithread Benchmark..." + make clean + for i in 1 2 4 8 16; do + make clean + THREADS=$i ./configure.sh + TOOL=benchmark_common TOOLARGS="$ARGS" make tool + done + ;; + *) + echo "Invalid argument. Possible values:" + echo " - performance" + echo " - architecture" + echo " - memory" + echo " - nodes" + echo " - multithread" + exit 1 + ;; +esac \ No newline at end of file diff --git a/tools/401_benchmark_performance/benchmark_performance.c b/tools/401_benchmark_performance/benchmark_performance.c new file mode 100644 index 0000000..97cb51a --- /dev/null +++ b/tools/401_benchmark_performance/benchmark_performance.c @@ -0,0 +1,183 @@ +#include "../tool.h" +#include +#include +#include +#include + +#define SOL_BUFFER_LEN 1000 +#define MAX_SCRAMBLES 100 // Limite massimo per il numero di scrambles che può essere letto + +char *scrambles[MAX_SCRAMBLES + 1]; +char *solver; +int64_t size = 0; +char *buf; + +struct ScrambleArgs { + int scramble_idx; +}; + +// Funzione di timer per un singolo scramble +static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *), struct ScrambleArgs *args, char *solution, int *solution_length) { + struct timespec start, end; + double tdiff, tdsec, tdnano; + + fflush(stdout); + + clock_gettime(CLOCK_MONOTONIC, &start); + run(args, solution, solution_length); // Esegue la funzione run per il singolo scramble + clock_gettime(CLOCK_MONOTONIC, &end); + + tdsec = end.tv_sec - start.tv_sec; + tdnano = end.tv_nsec - start.tv_nsec; + tdiff = tdsec + 1e-9 * tdnano; + + return tdiff; +} + +// Modifica della funzione run_scramble per evitare ricorsione +void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_length) { + int scramble_idx = args->scramble_idx; // Estrai l'indice dello scramble + int64_t n; + char sol[SOL_BUFFER_LEN], cube[22]; + long long stats[NISSY_SIZE_SOLVE_STATS]; + + printf("%d. %s\n", scramble_idx + 1, scrambles[scramble_idx]); + printf("Solving scramble %s\n", scrambles[scramble_idx]); + + if (nissy_applymoves(NISSY_SOLVED_CUBE, scrambles[scramble_idx], cube) == -1) { + printf("Invalid scramble\n"); + strcpy(solution, "Invalid scramble"); + *solution_length = 0; + return; + } + + n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, 0, 20, 1, -1, size, buf, SOL_BUFFER_LEN, sol, stats); + if (n == 0) { + printf("No solution found\n"); + strcpy(solution, "No solution found"); + *solution_length = 0; + } else { + printf("Solutions:\n%s\n", sol); + strcpy(solution, sol); + *solution_length = nissy_countmoves(solution); + } +} + +void run_all_scrambles(void) { + double total_time = 0.0, average_time = 0.0; + + printf("Solved the following scrambles with timings:\n\n"); + + FILE *output_file = fopen("./tools/401_benchmark_performance/compressed_results.txt", "w"); + if (!output_file) { + printf("Error: Could not open output file for writing\n"); + return; + } + + int i; + for (i = 0; scrambles[i] != NULL; i++) { + struct ScrambleArgs args; + args.scramble_idx = i; + + char solution[SOL_BUFFER_LEN]; + int solution_length = 0; + + double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length); + total_time += scramble_time; + + fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); + fprintf(output_file, "Solution: %s\n", solution); + fprintf(output_file, "Length of solution: %d\n", solution_length); + fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); + fprintf(output_file, "-------------------------\n"); + + printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); + printf("Lenght: %d\n", solution_length); + } + + if (i > 0) { + average_time = total_time / i; + } + + printf("---------\n"); + printf("Total time for all scrambles: %.4fs\n", total_time); + printf("Average time per scramble: %.4fs\n", average_time); + + fprintf(output_file, "---------\n"); + fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); + fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); + + fclose(output_file); +} + +int read_scrambles_from_file(const char *filename, char **scrambles) { + FILE *file = fopen(filename, "r"); + if (!file) { + printf("Error: Could not open file %s\n", filename); + return -1; + } + + char line[256]; + int count = 0; + while (fgets(line, sizeof(line), file)) { + line[strcspn(line, "\n")] = 0; + + if (strncmp(line, "//", 2) == 0 || strlen(line) == 0) + continue; + + scrambles[count] = (char *)malloc(strlen(line) + 1); + if (scrambles[count] == NULL) { + printf("Error: Memory allocation failed\n"); + fclose(file); + return -1; + } + strcpy(scrambles[count], line); + count++; + + if (count >= MAX_SCRAMBLES) { + printf("Warning: Reached maximum number of scrambles (%d)\n", MAX_SCRAMBLES); + break; + } + } + + scrambles[count] = NULL; + fclose(file); + return 0; +} + +int main(int argc, char **argv) { + char tables_filename[255]; + char scrambles_filename[255] = "./tools/401_benchmark_performance/scrambles.txt"; + + if (argc < 2) { + printf("Error: not enough arguments. A solver must be given.\n"); + return 1; + } + + solver = argv[1]; + + srand(time(NULL)); + nissy_setlogger(log_stderr); + + sprintf(tables_filename, "tables/%s", solver); + if (getdata(solver, &buf, tables_filename) != 0) + return 1; + + size = nissy_datasize(solver); + + if (read_scrambles_from_file(scrambles_filename, scrambles) != 0) { + printf("Error: failed to read scrambles from file\n"); + free(buf); + return 1; + } + + run_all_scrambles(); + + free(buf); + + for (int i = 0; scrambles[i] != NULL; i++) { + free(scrambles[i]); + } + + return 0; +} diff --git a/tools/402_benchmark_nodes/benchmark_nodes.c b/tools/402_benchmark_nodes/benchmark_nodes.c new file mode 100644 index 0000000..9dc0914 --- /dev/null +++ b/tools/402_benchmark_nodes/benchmark_nodes.c @@ -0,0 +1,201 @@ +#include "../tool.h" +#include +#include +#include +#include + +#define SOL_BUFFER_LEN 1000 +#define MAX_SCRAMBLES 100 + +char *scrambles[MAX_SCRAMBLES + 1]; +int scrambles_count = 0; +char *solver; +int64_t size = 0; +char *buf; + +struct ScrambleArgs { + int scramble_idx; +}; + +static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *, long long *), struct ScrambleArgs *args, char *solution, int *solution_length, long long *stats) { + struct timespec start, end; + double tdiff, tdsec, tdnano; + fflush(stdout); + + clock_gettime(CLOCK_MONOTONIC, &start); + run(args, solution, solution_length, stats); + clock_gettime(CLOCK_MONOTONIC, &end); + + tdsec = end.tv_sec - start.tv_sec; + tdnano = end.tv_nsec - start.tv_nsec; + tdiff = tdsec + 1e-9 * tdnano; + + return tdiff; +} + +void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_length, long long *stats) { + int scramble_idx = args->scramble_idx; + int64_t n; + char sol[SOL_BUFFER_LEN], cube[22]; + + printf("%d. %s\n", scramble_idx + 1, scrambles[scramble_idx]); + printf("Solving scramble %s\n", scrambles[scramble_idx]); + + if (nissy_applymoves(NISSY_SOLVED_CUBE, scrambles[scramble_idx], cube) == -1) { + printf("Invalid scramble\n"); + strcpy(solution, "Invalid scramble"); + *solution_length = 0; + return; + } + + n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, 0, 20, 1, -1, size, buf, SOL_BUFFER_LEN, sol, stats); + if (n == 0) { + printf("No solution found\n"); + strcpy(solution, "No solution found"); + *solution_length = 0; + } else { + printf("Solutions:\n%s\n", sol); + strcpy(solution, sol); + *solution_length = nissy_countmoves(solution); + printf("Nodes visited: %lld\n", stats[0]); + printf("Table fallbacks: %lld\n", stats[1]); + } +} + +void run_all_scrambles(void) { + double total_time = 0.0, average_time = 0.0; + long long total_nodes = 0, average_nodes = 0; + long long total_fallbacks = 0, average_fallbacks = 0; + long long stats[scrambles_count+1][NISSY_SIZE_SOLVE_STATS]; + + printf("Solved the following scrambles with timings:\n\n"); + + FILE *output_file = fopen("./tools/402_benchmark_nodes/compressed_results.txt", "w"); + if (!output_file) { + printf("Error: Could not open output file for writing\n"); + return; + } + + int i; + for (i = 0; scrambles[i] != NULL; i++) { + struct ScrambleArgs args; + args.scramble_idx = i; + + char solution[SOL_BUFFER_LEN]; + int solution_length = 0; + + double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length, stats[i]); + total_time += scramble_time; + total_nodes += stats[i][0]; + total_fallbacks += stats[i][1]; + fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); + fprintf(output_file, "Solution: %s\n", solution); + fprintf(output_file, "Length of solution: %d\n", solution_length); + fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); + fprintf(output_file, "Nodes visited: %lld\n", stats[i][0]); + fprintf(output_file, "Table fallbacks %lld\n", stats[i][1]); + fprintf(output_file, "-------------------------\n"); + + printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); + printf("Lenght: %d\n", solution_length); + } + + if (i > 0) { + average_time = total_time / i; + average_nodes = total_nodes / i; + average_fallbacks = total_fallbacks / i; + } + + printf("---------\n"); + printf("Total time for all scrambles: %.4fs\n", total_time); + printf("Average time per scramble: %.4fs\n", average_time); + printf("---------\n"); + printf("Total nodes for all scrambles: %lld\n", total_nodes); + printf("Average nodes per scramble: %lld\n", average_nodes); + printf("Average Table fallbacks: %lld\n", average_fallbacks); + printf("---------\n"); + printf("Average Time/Node: %f\n", total_time/total_nodes*1e9); + + fprintf(output_file, "---------\n"); + fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); + fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); + fprintf(output_file, "---------\n"); + fprintf(output_file, "Total nodes for all scrambles: %lld\n", total_nodes); + fprintf(output_file, "Average nodes per scramble: %lld\n", average_nodes); + fprintf(output_file, "Average Table fallbacks: %lld\n", average_fallbacks); + fprintf(output_file, "---------\n"); + fprintf(output_file, "Average Time/Node: %f\n", total_time/total_nodes*1e9); + fclose(output_file); +} + +int read_scrambles_from_file(const char *filename, char **scrambles) { + FILE *file = fopen(filename, "r"); + if (!file) { + printf("Error: Could not open file %s\n", filename); + return -1; + } + + char line[256]; + + while (fgets(line, sizeof(line), file)) { + line[strcspn(line, "\n")] = 0; + + if (strncmp(line, "//", 2) == 0 || strlen(line) == 0) + continue; + + scrambles[scrambles_count] = (char *)malloc(strlen(line) + 1); + if (scrambles[scrambles_count] == NULL) { + printf("Error: Memory allocation failed\n"); + fclose(file); + return -1; + } + strcpy(scrambles[scrambles_count], line); + scrambles_count++; + + if (scrambles_count >= MAX_SCRAMBLES) { + printf("Warning: Reached maximum number of scrambles (%d)\n", MAX_SCRAMBLES); + break; + } + } + + scrambles[scrambles_count] = NULL; + fclose(file); + return 0; +} + +int main(int argc, char **argv) { + char tables_filename[255]; + char scrambles_filename[255] = "./tools/402_benchmark_nodes/scrambles.txt"; + + if (argc < 2) { + printf("Error: not enough arguments. A solver must be given.\n"); + return 1; + } + + solver = argv[1]; + + srand(time(NULL)); + nissy_setlogger(log_stderr); + + sprintf(tables_filename, "tables/%s", solver); + if (getdata(solver, &buf, tables_filename) != 0) + return 1; + + size = nissy_datasize(solver); + + if (read_scrambles_from_file(scrambles_filename, scrambles) != 0) { + printf("Error: failed to read scrambles from file\n"); + free(buf); + return 1; + } + + run_all_scrambles(); + + free(buf); + + for (int i = 0; scrambles[i] != NULL; i++) { + free(scrambles[i]); + } + + return 0; +} -- cgit v1.3