aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorenricotenuti <tenutz_27@outlook.it>2024-10-21 16:36:13 +0200
committerenricotenuti <tenutz_27@outlook.it>2024-10-21 16:36:13 +0200
commit09fb2900d3811d76ef145e5f960e5b824bb0bfb6 (patch)
tree87a9cc8caa68218e76460841164289dd1bfe1c5b
parentea25a7ccad625c4e664dfd114147971b8a2677f3 (diff)
downloadnissy-core-09fb2900d3811d76ef145e5f960e5b824bb0bfb6.tar.gz
nissy-core-09fb2900d3811d76ef145e5f960e5b824bb0bfb6.zip
Benchmark setup
-rw-r--r--.gitignore30
-rwxr-xr-xbenchmark.sh65
-rw-r--r--tools/401_benchmark_performance/benchmark_performance.c183
-rw-r--r--tools/402_benchmark_nodes/benchmark_nodes.c201
4 files changed, 467 insertions, 12 deletions
diff --git a/.gitignore b/.gitignore
index 5e0950f..3a246b6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,28 +1,34 @@
1*.o
2*.so
3*.s
1.DS_Store 4.DS_Store
5.vscode
2cachegrind.out.* 6cachegrind.out.*
3config.mk 7config.mk
4gen
5debuggen 8debuggen
9debugrun
6flamegraph.html 10flamegraph.html
7utils/.DS_Store 11gen
12h48.err
13h48.out
14HPC*.sh
8perf.data 15perf.data
9perf.data.old 16perf.data.old
10run 17run
11debugrun 18run.core
12shell/lasttest.out 19run.DSYM
20scrambles
13shell/lasttest.err 21shell/lasttest.err
22shell/lasttest.out
14tables/* 23tables/*
15tables-old/* 24tables-old/*
16test/*/runtest
17test/.DS_Store 25test/.DS_Store
26test/last.*
18test/run 27test/run
19test/run.DSYM 28test/run.DSYM
29test/*/runtest
20tools/.DS_Store 30tools/.DS_Store
21run.DSYM 31tools/401_benchmark_performance/*.txt
22run.core 32tools/402_benchmark_nodes/*.txt
23test/last.*
24tools/results 33tools/results
25.vscode 34utils/.DS_Store
26*.o
27*.so
28*.s
diff --git a/benchmark.sh b/benchmark.sh
new file mode 100755
index 0000000..8f0ccc6
--- /dev/null
+++ b/benchmark.sh
@@ -0,0 +1,65 @@
1#!/bin/bash
2if [ -z "$1" ]; then
3 echo "No argument provided. Possible values:"
4 echo " - performance"
5 echo " - architecture"
6 echo " - memory"
7 echo " - nodes"
8 echo " - multithread"
9 exit 1
10fi
11
12input=$1
13ARGS="h48h6k2"
14THREADS=8
15
16case $input in
17 performance)
18 echo "Performance Benchmark..."
19 make clean
20 THREADS=$THREADS ./configure.sh
21 TOOL=benchmark_performance TOOLARGS="$ARGS" make tool
22 ;;
23 architecture)
24 echo "Architecture Benchmark..."
25 make clean
26 THREADS=$THREADS ./configure.sh
27 TOOL=benchmark_common TOOLARGS="$ARGS" make tool
28 make clean
29 THREADS=$THREADS ARCH=PORTABLE ./configure.sh
30 TOOL=benchmark_common TOOLARGS="$ARGS" make tool
31 ;;
32 memory)
33 echo "Memory Benchmark..."
34 for i in {0..7}; do
35 make clean
36 THREADS=$THREADS ./configure.sh
37 M_ARGS="h48h${i}k2"
38 TOOL=benchmark_common TOOLARGS="$M_ARGS" make tool
39 done
40 ;;
41 nodes)
42 echo "Nodes Benchmark..."
43 make clean
44 THREADS=1 ./configure.sh
45 TOOL=benchmark_nodes TOOLARGS="$ARGS" make tool
46 ;;
47 multithread)
48 echo "Multithread Benchmark..."
49 make clean
50 for i in 1 2 4 8 16; do
51 make clean
52 THREADS=$i ./configure.sh
53 TOOL=benchmark_common TOOLARGS="$ARGS" make tool
54 done
55 ;;
56 *)
57 echo "Invalid argument. Possible values:"
58 echo " - performance"
59 echo " - architecture"
60 echo " - memory"
61 echo " - nodes"
62 echo " - multithread"
63 exit 1
64 ;;
65esac \ 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 @@
1#include "../tool.h"
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <time.h>
6
7#define SOL_BUFFER_LEN 1000
8#define MAX_SCRAMBLES 100 // Limite massimo per il numero di scrambles che può essere letto
9
10char *scrambles[MAX_SCRAMBLES + 1];
11char *solver;
12int64_t size = 0;
13char *buf;
14
15struct ScrambleArgs {
16 int scramble_idx;
17};
18
19// Funzione di timer per un singolo scramble
20static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *), struct ScrambleArgs *args, char *solution, int *solution_length) {
21 struct timespec start, end;
22 double tdiff, tdsec, tdnano;
23
24 fflush(stdout);
25
26 clock_gettime(CLOCK_MONOTONIC, &start);
27 run(args, solution, solution_length); // Esegue la funzione run per il singolo scramble
28 clock_gettime(CLOCK_MONOTONIC, &end);
29
30 tdsec = end.tv_sec - start.tv_sec;
31 tdnano = end.tv_nsec - start.tv_nsec;
32 tdiff = tdsec + 1e-9 * tdnano;
33
34 return tdiff;
35}
36
37// Modifica della funzione run_scramble per evitare ricorsione
38void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_length) {
39 int scramble_idx = args->scramble_idx; // Estrai l'indice dello scramble
40 int64_t n;
41 char sol[SOL_BUFFER_LEN], cube[22];
42 long long stats[NISSY_SIZE_SOLVE_STATS];
43
44 printf("%d. %s\n", scramble_idx + 1, scrambles[scramble_idx]);
45 printf("Solving scramble %s\n", scrambles[scramble_idx]);
46
47 if (nissy_applymoves(NISSY_SOLVED_CUBE, scrambles[scramble_idx], cube) == -1) {
48 printf("Invalid scramble\n");
49 strcpy(solution, "Invalid scramble");
50 *solution_length = 0;
51 return;
52 }
53
54 n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, 0, 20, 1, -1, size, buf, SOL_BUFFER_LEN, sol, stats);
55 if (n == 0) {
56 printf("No solution found\n");
57 strcpy(solution, "No solution found");
58 *solution_length = 0;
59 } else {
60 printf("Solutions:\n%s\n", sol);
61 strcpy(solution, sol);
62 *solution_length = nissy_countmoves(solution);
63 }
64}
65
66void run_all_scrambles(void) {
67 double total_time = 0.0, average_time = 0.0;
68
69 printf("Solved the following scrambles with timings:\n\n");
70
71 FILE *output_file = fopen("./tools/401_benchmark_performance/compressed_results.txt", "w");
72 if (!output_file) {
73 printf("Error: Could not open output file for writing\n");
74 return;
75 }
76
77 int i;
78 for (i = 0; scrambles[i] != NULL; i++) {
79 struct ScrambleArgs args;
80 args.scramble_idx = i;
81
82 char solution[SOL_BUFFER_LEN];
83 int solution_length = 0;
84
85 double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length);
86 total_time += scramble_time;
87
88 fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]);
89 fprintf(output_file, "Solution: %s\n", solution);
90 fprintf(output_file, "Length of solution: %d\n", solution_length);
91 fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time);
92 fprintf(output_file, "-------------------------\n");
93
94 printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time);
95 printf("Lenght: %d\n", solution_length);
96 }
97
98 if (i > 0) {
99 average_time = total_time / i;
100 }
101
102 printf("---------\n");
103 printf("Total time for all scrambles: %.4fs\n", total_time);
104 printf("Average time per scramble: %.4fs\n", average_time);
105
106 fprintf(output_file, "---------\n");
107 fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time);
108 fprintf(output_file, "Average time per scramble: %.4fs\n", average_time);
109
110 fclose(output_file);
111}
112
113int read_scrambles_from_file(const char *filename, char **scrambles) {
114 FILE *file = fopen(filename, "r");
115 if (!file) {
116 printf("Error: Could not open file %s\n", filename);
117 return -1;
118 }
119
120 char line[256];
121 int count = 0;
122 while (fgets(line, sizeof(line), file)) {
123 line[strcspn(line, "\n")] = 0;
124
125 if (strncmp(line, "//", 2) == 0 || strlen(line) == 0)
126 continue;
127
128 scrambles[count] = (char *)malloc(strlen(line) + 1);
129 if (scrambles[count] == NULL) {
130 printf("Error: Memory allocation failed\n");
131 fclose(file);
132 return -1;
133 }
134 strcpy(scrambles[count], line);
135 count++;
136
137 if (count >= MAX_SCRAMBLES) {
138 printf("Warning: Reached maximum number of scrambles (%d)\n", MAX_SCRAMBLES);
139 break;
140 }
141 }
142
143 scrambles[count] = NULL;
144 fclose(file);
145 return 0;
146}
147
148int main(int argc, char **argv) {
149 char tables_filename[255];
150 char scrambles_filename[255] = "./tools/401_benchmark_performance/scrambles.txt";
151
152 if (argc < 2) {
153 printf("Error: not enough arguments. A solver must be given.\n");
154 return 1;
155 }
156
157 solver = argv[1];
158
159 srand(time(NULL));
160 nissy_setlogger(log_stderr);
161
162 sprintf(tables_filename, "tables/%s", solver);
163 if (getdata(solver, &buf, tables_filename) != 0)
164 return 1;
165
166 size = nissy_datasize(solver);
167
168 if (read_scrambles_from_file(scrambles_filename, scrambles) != 0) {
169 printf("Error: failed to read scrambles from file\n");
170 free(buf);
171 return 1;
172 }
173
174 run_all_scrambles();
175
176 free(buf);
177
178 for (int i = 0; scrambles[i] != NULL; i++) {
179 free(scrambles[i]);
180 }
181
182 return 0;
183}
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 @@
1#include "../tool.h"
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <time.h>
6
7#define SOL_BUFFER_LEN 1000
8#define MAX_SCRAMBLES 100
9
10char *scrambles[MAX_SCRAMBLES + 1];
11int scrambles_count = 0;
12char *solver;
13int64_t size = 0;
14char *buf;
15
16struct ScrambleArgs {
17 int scramble_idx;
18};
19
20static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *, long long *), struct ScrambleArgs *args, char *solution, int *solution_length, long long *stats) {
21 struct timespec start, end;
22 double tdiff, tdsec, tdnano;
23 fflush(stdout);
24
25 clock_gettime(CLOCK_MONOTONIC, &start);
26 run(args, solution, solution_length, stats);
27 clock_gettime(CLOCK_MONOTONIC, &end);
28
29 tdsec = end.tv_sec - start.tv_sec;
30 tdnano = end.tv_nsec - start.tv_nsec;
31 tdiff = tdsec + 1e-9 * tdnano;
32
33 return tdiff;
34}
35
36void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_length, long long *stats) {
37 int scramble_idx = args->scramble_idx;
38 int64_t n;
39 char sol[SOL_BUFFER_LEN], cube[22];
40
41 printf("%d. %s\n", scramble_idx + 1, scrambles[scramble_idx]);
42 printf("Solving scramble %s\n", scrambles[scramble_idx]);
43
44 if (nissy_applymoves(NISSY_SOLVED_CUBE, scrambles[scramble_idx], cube) == -1) {
45 printf("Invalid scramble\n");
46 strcpy(solution, "Invalid scramble");
47 *solution_length = 0;
48 return;
49 }
50
51 n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, 0, 20, 1, -1, size, buf, SOL_BUFFER_LEN, sol, stats);
52 if (n == 0) {
53 printf("No solution found\n");
54 strcpy(solution, "No solution found");
55 *solution_length = 0;
56 } else {
57 printf("Solutions:\n%s\n", sol);
58 strcpy(solution, sol);
59 *solution_length = nissy_countmoves(solution);
60 printf("Nodes visited: %lld\n", stats[0]);
61 printf("Table fallbacks: %lld\n", stats[1]);
62 }
63}
64
65void run_all_scrambles(void) {
66 double total_time = 0.0, average_time = 0.0;
67 long long total_nodes = 0, average_nodes = 0;
68 long long total_fallbacks = 0, average_fallbacks = 0;
69 long long stats[scrambles_count+1][NISSY_SIZE_SOLVE_STATS];
70
71 printf("Solved the following scrambles with timings:\n\n");
72
73 FILE *output_file = fopen("./tools/402_benchmark_nodes/compressed_results.txt", "w");
74 if (!output_file) {
75 printf("Error: Could not open output file for writing\n");
76 return;
77 }
78
79 int i;
80 for (i = 0; scrambles[i] != NULL; i++) {
81 struct ScrambleArgs args;
82 args.scramble_idx = i;
83
84 char solution[SOL_BUFFER_LEN];
85 int solution_length = 0;
86
87 double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length, stats[i]);
88 total_time += scramble_time;
89 total_nodes += stats[i][0];
90 total_fallbacks += stats[i][1];
91 fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]);
92 fprintf(output_file, "Solution: %s\n", solution);
93 fprintf(output_file, "Length of solution: %d\n", solution_length);
94 fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time);
95 fprintf(output_file, "Nodes visited: %lld\n", stats[i][0]);
96 fprintf(output_file, "Table fallbacks %lld\n", stats[i][1]);
97 fprintf(output_file, "-------------------------\n");
98
99 printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time);
100 printf("Lenght: %d\n", solution_length);
101 }
102
103 if (i > 0) {
104 average_time = total_time / i;
105 average_nodes = total_nodes / i;
106 average_fallbacks = total_fallbacks / i;
107 }
108
109 printf("---------\n");
110 printf("Total time for all scrambles: %.4fs\n", total_time);
111 printf("Average time per scramble: %.4fs\n", average_time);
112 printf("---------\n");
113 printf("Total nodes for all scrambles: %lld\n", total_nodes);
114 printf("Average nodes per scramble: %lld\n", average_nodes);
115 printf("Average Table fallbacks: %lld\n", average_fallbacks);
116 printf("---------\n");
117 printf("Average Time/Node: %f\n", total_time/total_nodes*1e9);
118
119 fprintf(output_file, "---------\n");
120 fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time);
121 fprintf(output_file, "Average time per scramble: %.4fs\n", average_time);
122 fprintf(output_file, "---------\n");
123 fprintf(output_file, "Total nodes for all scrambles: %lld\n", total_nodes);
124 fprintf(output_file, "Average nodes per scramble: %lld\n", average_nodes);
125 fprintf(output_file, "Average Table fallbacks: %lld\n", average_fallbacks);
126 fprintf(output_file, "---------\n");
127 fprintf(output_file, "Average Time/Node: %f\n", total_time/total_nodes*1e9);
128 fclose(output_file);
129}
130
131int read_scrambles_from_file(const char *filename, char **scrambles) {
132 FILE *file = fopen(filename, "r");
133 if (!file) {
134 printf("Error: Could not open file %s\n", filename);
135 return -1;
136 }
137
138 char line[256];
139
140 while (fgets(line, sizeof(line), file)) {
141 line[strcspn(line, "\n")] = 0;
142
143 if (strncmp(line, "//", 2) == 0 || strlen(line) == 0)
144 continue;
145
146 scrambles[scrambles_count] = (char *)malloc(strlen(line) + 1);
147 if (scrambles[scrambles_count] == NULL) {
148 printf("Error: Memory allocation failed\n");
149 fclose(file);
150 return -1;
151 }
152 strcpy(scrambles[scrambles_count], line);
153 scrambles_count++;
154
155 if (scrambles_count >= MAX_SCRAMBLES) {
156 printf("Warning: Reached maximum number of scrambles (%d)\n", MAX_SCRAMBLES);
157 break;
158 }
159 }
160
161 scrambles[scrambles_count] = NULL;
162 fclose(file);
163 return 0;
164}
165
166int main(int argc, char **argv) {
167 char tables_filename[255];
168 char scrambles_filename[255] = "./tools/402_benchmark_nodes/scrambles.txt";
169
170 if (argc < 2) {
171 printf("Error: not enough arguments. A solver must be given.\n");
172 return 1;
173 }
174
175 solver = argv[1];
176
177 srand(time(NULL));
178 nissy_setlogger(log_stderr);
179
180 sprintf(tables_filename, "tables/%s", solver);
181 if (getdata(solver, &buf, tables_filename) != 0)
182 return 1;
183
184 size = nissy_datasize(solver);
185
186 if (read_scrambles_from_file(scrambles_filename, scrambles) != 0) {
187 printf("Error: failed to read scrambles from file\n");
188 free(buf);
189 return 1;
190 }
191
192 run_all_scrambles();
193
194 free(buf);
195
196 for (int i = 0; scrambles[i] != NULL; i++) {
197 free(scrambles[i]);
198 }
199
200 return 0;
201}

Generated with cgit - Back to sebastiano.tronto.net