-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
185 lines (177 loc) · 5.36 KB
/
main.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <cinttypes>
#include <chrono>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <tuple>
#include "to_board.hpp"
#include "solver.cuh"
#include "thinker.cuh"
void output_board(const ull p, const ull o) {
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
int index = i*8+j;
if ((p >> index) & 1) {
printf("x");
} else if ((o >> index) & 1) {
printf("o");
} else {
printf(".");
}
}
printf("\n");
}
printf("\n");
}
std::string hand_to_s(const hand move) {
if (move == hand::PASS) {
return "ps";
} else {
std::string res(1, static_cast<char>(static_cast<int>(move) % 8 + 'A'));
res += static_cast<int>(move) / 8 + '1';
return res;
}
}
struct FPCloser {
void operator()(FILE* const fp) const {
fclose(fp);
}
};
using file_ptr = std::unique_ptr<FILE, FPCloser>;
std::vector<Board> load_boards(const char* filename) {
file_ptr fp_in(fopen(filename, "r"));
int n;
fscanf(fp_in.get(), "%d\n", &n);
std::vector<Board> vboard(n);
for (int i = 0; i < n; ++i) {
char buf[33];
const auto ptr = fgets(buf, 32, fp_in.get());
if (ptr == nullptr) {
std::cerr << "Failed to load boards" << std::endl;
exit(EXIT_FAILURE);
}
//fscanf(fp_in.get(), "%s", buf);
vboard[i] = toBoard(buf);
}
std::sort(std::begin(vboard), std::end(vboard));
vboard.erase(std::unique(std::begin(vboard), std::end(vboard)), std::end(vboard));
return vboard;
}
void think(int argc, char **argv) {
using namespace std::literals;
const auto vboard = load_boards(argv[1]);
file_ptr fp_out(fopen(argv[2], "w"));
int max_depth = std::stoi(argv[3]);
int depth = std::stoi(argv[4]);
Evaluator evaluator("subboard.txt", "value/value52");
size_t n = vboard.size();
fprintf(stderr, "n = %zu\n", n);
constexpr size_t batch_size = 2000000;
size_t batch_count = (n + batch_size - 1) / batch_size;
std::vector<BatchedThinkTask> vb;
constexpr size_t table_size = 50000001;
Table table(table_size);
for (size_t i = 0; i < batch_count; ++i) {
int size = min(batch_size, n - i*batch_size);
BatchedThinkTask bt(size, depth, table, evaluator);
for (int j = 0; j < size; ++j) {
const auto [p, o] = vboard[i*batch_size+j];
bt.abp[j] = AlphaBetaProblem(p, o);
}
vb.emplace_back(std::move(bt));
}
auto start = std::chrono::high_resolution_clock::now();
for (const auto &b : vb) {
b.launch();
}
while (true) {
bool finished = true;
for (const auto &b : vb) {
if (!b.is_ready()) finished = false;
}
if (finished) break;
}
const auto status = cudaGetLastError();
auto ended = std::chrono::high_resolution_clock::now();
fprintf(stderr, "%s (%s), elapsed: %ldms, table update count: %" PRId64 ", table hit: %" PRId64 ", table find: %" PRId64 "\n",
cudaGetErrorName(status),
cudaGetErrorString(status),
(ended - start) / 1ms,
table.get_update_count(), table.get_hit_count(), table.get_lookup_count());
ull total = 0;
char buf[17];
for (const auto &b : vb) {
total += *b.total;
for (int j = 0; j < b.size; ++j) {
fromBoard(Board(b.abp[j].player, b.abp[j].opponent), buf);
fprintf(fp_out.get(), "%s %d %s\n", buf, b.result[j],
hand_to_s(b.bestmove[j]).c_str());
}
}
fprintf(stderr, "total nodes: %" PRId64 "\n", total);
}
void solve(int argc, char **argv) {
using namespace std::literals;
const auto vboard = load_boards(argv[1]);
file_ptr fp_out(fopen(argv[2], "w"));
int max_depth = std::stoi(argv[3]);
const size_t n = vboard.size();
fprintf(stderr, "n = %zu\n", n);
constexpr size_t batch_size = 2000000;
size_t batch_count = (n + batch_size - 1) / batch_size;
std::vector<BatchedTask> vb;
constexpr size_t table_size = 50000001;
Table table(table_size);
for (size_t i = 0; i < batch_count; ++i) {
int size = min(batch_size, n - i*batch_size);
BatchedTask bt(size, max_depth, table);
for (int j = 0; j < size; ++j) {
const auto [p, o] = vboard[i*batch_size+j];
bt.abp[j] = AlphaBetaProblem(p, o);
}
vb.emplace_back(std::move(bt));
}
fprintf(stderr, "start!\n");
auto start = std::chrono::high_resolution_clock::now();
for (const auto &b : vb) {
b.launch();
}
while (true) {
bool finished = true;
for (const auto &b : vb) {
if (!b.is_ready()) finished = false;
}
if (finished) break;
}
const auto status = cudaGetLastError();
auto ended = std::chrono::high_resolution_clock::now();
fprintf(stderr, "%s (%s), elapsed: %ldms, table update count: %" PRId64 ", table hit: %" PRId64 ", table find: %" PRId64 "\n",
cudaGetErrorName(status),
cudaGetErrorString(status),
(ended - start) / 1ms,
table.get_update_count(), table.get_hit_count(), table.get_lookup_count());
ull total = 0;
char buf[17];
for (const auto &b : vb) {
total += *b.total;
for (int j = 0; j < b.size; ++j) {
fromBoard(Board(b.abp[j].player, b.abp[j].opponent), buf);
fprintf(fp_out.get(), "%s %d\n", buf, b.result[j]);
}
}
fprintf(stderr, "total nodes: %" PRId64 "\n", total);
}
int main(int argc, char **argv) {
if (argc < 4) {
fprintf(stderr, "usage: %s INPUT OUTPUT DEPTH [THINK_DEPTH]\n", argv[0]);
return 1;
}
if (argc == 5) {
think(argc, argv);
return 0;
}
solve(argc, argv);
}