aboutsummaryrefslogtreecommitdiff
path: root/tools/stats_tables_h48
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-07-20 22:51:23 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-07-20 22:51:23 +0200
commitf64a5a67a37015bd5066efd9b1edb6ac3e0b7a97 (patch)
tree52984e67e1cde946c58a8e266a88a0dafe85419a /tools/stats_tables_h48
parent5727f06c5694a4831881322876e3ba4d8ce3b743 (diff)
downloadnissy-core-f64a5a67a37015bd5066efd9b1edb6ac3e0b7a97.tar.gz
nissy-core-f64a5a67a37015bd5066efd9b1edb6ac3e0b7a97.zip
Fixed gendata tool and cleaned up some stuff
Diffstat (limited to 'tools/stats_tables_h48')
-rw-r--r--tools/stats_tables_h48/stats_tables_h48.c151
1 files changed, 151 insertions, 0 deletions
diff --git a/tools/stats_tables_h48/stats_tables_h48.c b/tools/stats_tables_h48/stats_tables_h48.c
new file mode 100644
index 0000000..7118992
--- /dev/null
+++ b/tools/stats_tables_h48/stats_tables_h48.c
@@ -0,0 +1,151 @@
1#include <pthread.h>
2#include <time.h>
3#include "../timerun.h"
4#include "../../src/cube.h"
5
6#define MAXMOVES 20
7#define NTHREADS 32
8#define NCUBES_PER_THREAD 10000
9#define LOG_EVERY (NCUBES_PER_THREAD / 10)
10
11typedef struct {
12 int n;
13 int thread_id;
14 int64_t v[12][100];
15} thread_arg_t;
16
17const char *filename = "tables/h48h0k4";
18char *buf;
19
20uint64_t rand64(void) {
21 uint64_t i, ret;
22
23 for (i = 0, ret = 0; i < 64; i++)
24 ret |= (uint64_t)(rand() % 2) << i;
25
26 return ret;
27}
28
29void log_stderr(const char *str, ...) {
30 va_list args;
31
32 va_start(args, str);
33 vfprintf(stderr, str, args);
34 va_end(args);
35}
36
37static void *
38run_thread(void *arg)
39{
40 char sols[12], cube[22];
41 int64_t ep, eo, cp, co;
42 int i, j;
43
44 thread_arg_t *a = (thread_arg_t *)arg;
45
46 for (i = 0; i < a->n; i++) {
47 ep = rand64();
48 eo = rand64();
49 cp = rand64();
50 co = rand64();
51 nissy_getcube(ep, eo, cp, co, "fix", cube);
52 nissy_solve(cube, "h48stats", "", "",
53 0, MAXMOVES, 1, -1, buf, sols);
54 for (j = 0; j < 12; j++)
55 a->v[j][(int)sols[j]]++;
56 if ((i+1) % LOG_EVERY == 0)
57 fprintf(stderr, "[thread %d] %d cubes solved...\n",
58 a->thread_id, i+1);
59 }
60
61 return NULL;
62}
63
64void run(void) {
65 int64_t i, j, k, tot;
66 double avg;
67 pthread_t thread[NTHREADS];
68 thread_arg_t arg[NTHREADS];
69
70 for (i = 0; i < NTHREADS; i++) {
71 arg[i] = (thread_arg_t) {
72 .thread_id = i,
73 .n = NCUBES_PER_THREAD,
74 .v = {{0}}
75 };
76 pthread_create(&thread[i], NULL, run_thread, &arg[i]);
77 }
78
79 for (i = 0; i < NTHREADS; i++)
80 pthread_join(thread[i], NULL);
81
82 for (j = 0; j < 12; j++) {
83 printf("Data for h=%" PRId64 "\n", j);
84 for (k = 0, avg = 0.0; k <= 16; k++) {
85 for (i = 0, tot = 0; i < NTHREADS; i++)
86 tot += arg[i].v[j][k];
87 printf("%" PRId64 "\t%" PRId64 "\n", k, tot);
88 avg += tot * k;
89 }
90 avg /= (double)(NCUBES_PER_THREAD * NTHREADS);
91 printf("Average: %.4lf\n", avg);
92 printf("\n");
93 }
94}
95
96int getdata(int64_t size) {
97 int64_t s;
98 FILE *f;
99
100 buf = malloc(size);
101
102 if ((f = fopen(filename, "rb")) == NULL) {
103 fprintf(stderr, "Table file not found, generating them."
104 " This can take a while.\n");
105 s = nissy_gendata("h48stats", "", buf);
106 if (s != size) {
107 fprintf(stderr, "Error generating table");
108 if (s != -1)
109 fprintf(stderr, " (got %" PRId64 " bytes)", s);
110 fprintf(stderr, "\n");
111 return 1;
112 }
113 if ((f = fopen(filename, "wb")) == NULL) {
114 fprintf(stderr, "Could not write tables to file %s"
115 ", will be regenerated next time.\n", filename);
116 } else {
117 fwrite(buf, size, 1, f);
118 fclose(f);
119 }
120 } else {
121 fprintf(stderr, "Reading tables from file %s\n", filename);
122 fread(buf, size, 1, f);
123 fclose(f);
124 }
125
126 return 0;
127}
128
129int main(void) {
130 int64_t size;
131
132 srand(time(NULL));
133
134 nissy_setlogger(log_stderr);
135 size = nissy_datasize("h48stats", "");
136 if (size == -1) {
137 printf("h48 stats: error in datasize\n");
138 return 1;
139 }
140
141 if (getdata(size) != 0) {
142 printf("Error getting table, stopping\n");
143 free(buf);
144 return 1;
145 }
146
147 timerun(run, "h48 table stats");
148
149 free(buf);
150 return 0;
151}

Generated with cgit - Back to sebastiano.tronto.net