-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver_cpu_opt.hpp
89 lines (70 loc) · 3.24 KB
/
solver_cpu_opt.hpp
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
// Copyright (c) Michael M. Magruder (https://github.com/mikemag)
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#pragma once
#include <cassert>
#include <vector>
#include "solver.hpp"
// CPU Implementation w/ Some Optimizations
//
// This is a version that runs on the CPU and is structured very close to the CUDA version. It's much faster than the
// reference impl, and includes the most common gameplay shortcuts.
//
// For instance, on one machine for 5p8c Knuth the ref impl takes 63.5160s, and this one takes 8.6398s.
template <typename SolverConfig_>
class SolverCPUFaster : public Solver {
using CodewordT = typename SolverConfig_::CodewordT;
using RegionIDT = RegionID<unsigned __int128, SolverConfig_::CodewordT::WINNING_SCORE.result>;
public:
using SolverConfig = SolverConfig_;
constexpr static const char* name = "CPU Faster";
SolverCPUFaster() : counters(counterDescs.descs.size()) {}
std::chrono::nanoseconds playAllGames(uint32_t packedInitialGuess) override;
void dump() override;
vector<uint32_t> getGuessesForGame(uint32_t packedCodeword) override;
void printStats() override {
for (auto& c : counterDescs.descs) {
cout << c.desc << ": " << commaString(counters[c.index]) << endl;
}
}
void recordStats(StatsRecorder& sr) override {
sr.add("Use Sym Opt", applySymOpt);
for (auto& c : counterDescs.descs) {
sr.add(c.name, counters[c.index]);
}
}
constexpr static CounterDescriptors<1> counterDescs{{
{"Scores", "Codeword comparisons"},
}};
private:
struct ACrCacheKey {
uint32_t isZero;
uint32_t isFree;
bool operator==(const ACrCacheKey& other) const { return isZero == other.isZero && isFree == other.isFree; }
};
struct ACrCacheKeyHash {
size_t operator()(const ACrCacheKey& key) const {
return std::hash<uint32_t>{}(key.isZero) ^ std::hash<uint32_t>{}(key.isFree);
}
};
std::unordered_map<ACrCacheKey, vector<CodewordT>, ACrCacheKeyHash> symACCache;
vector<vector<CodewordT>> nextMovesList;
vector<RegionIDT> regionIDs;
vector<unsigned long long int> counters;
CodewordT nextGuess(const vector<CodewordT>& allCodewords, const vector<CodewordT>& possibleSolutions,
const vector<CodewordT>& usedCodewords);
bool shortcutSmallSets(const vector<CodewordT>& possibleSolutions, CodewordT& nextGuess);
vector<typename SolverConfig::CodewordT> getReducedAC(const vector<CodewordT>& allCodewords,
const vector<CodewordT>& possibleSolutions,
const vector<CodewordT>& usedCodewords, uint depth);
uint32_t getPackedCodewordForRegion(int level, uint32_t regionIndex) const override {
return nextMovesList[level][regionIndex].packedCodeword();
}
uint8_t getStandardScore(uint8_t score) override { return score; }
// Tuned on a 2019 MBP w/ 2.4 GHz 8-Core Intel Core i9.
// A little brute force, would be interesting to see if there's something more nuanced some day.
constexpr static bool applySymOpt =
SolverConfig::SYMOPT && constPow<uint64_t>(SolverConfig::COLOR_COUNT, SolverConfig::PIN_COUNT) > 256;
};
#include "solver_cpu_opt.inl"