-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5words_threaded.cpp
340 lines (303 loc) · 8.63 KB
/
5words_threaded.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <algorithm>
#include <array>
#include <chrono>
#include <condition_variable>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define DO_PRINT // print the results or not to save time
//#define PRINT_ALL // printing the full 831 vs 538
#define PRINT_DEBUG //
#define letter_buckets 6 // a cache size 0-26, but somewhere around 4-7 is best
#define bucket_shift (26 - letter_buckets)
#define total_buckets (1 << letter_buckets)
map<char, int> letter2mask;
map<int, char> mask2letter;
map<int, vector<vector<int>>> lowhighs;
#ifdef PRINT_ALL
unordered_map<int, vector<string>> mask2words;
#else
unordered_map<int, string> mask2word;
#endif
vector<vector<int>> bucket_matcher(total_buckets);
typedef std::chrono::high_resolution_clock::time_point TimeVar;
#define duration(a) \
std::chrono::duration_cast<std::chrono::nanoseconds>(a).count()
#define timeNow() std::chrono::high_resolution_clock::now()
void printTime(string label, TimeVar t1) {
#ifdef PRINT_DEBUG
cout << label << " " << duration(timeNow() - t1) / 1e6 << " msec\n";
#endif
}
ofstream out("solutions.txt");
using WordArray = array<int, 5>;
struct State {
int totalbits;
int numwords;
WordArray words;
bool skipped;
bool stop;
};
mutex queueMutex;
condition_variable queueCVar;
deque<State> queue;
void findwords(std::vector<WordArray>& solutions,
int totalbits,
int numwords,
WordArray words,
bool skipped,
bool force = false) {
if (numwords == 5) {
solutions.push_back(words);
return;
}
if (!force && numwords == 1) {
{
std::unique_lock lock{queueMutex};
queue.push_back({.totalbits = totalbits,
.numwords = numwords,
.words = words,
.skipped = skipped,
.stop = false});
}
queueCVar.notify_one();
return;
}
while (true) {
int first = (totalbits + 1) & (~totalbits);
int highs = totalbits >> bucket_shift;
for (auto h : bucket_matcher[highs]) {
for (auto m : lowhighs[first][h]) {
if (totalbits & m) {
continue;
}
// used[numwords] = m;
// srch(mask | m, single, depth + 1);
words[numwords] = m;
findwords(solutions, totalbits | m, numwords + 1, words, skipped);
// findwords(solutions, totalbits | w, numwords + 1, words, i + 1,
// skipped);
}
}
if (skipped)
break;
skipped = true;
totalbits |= first;
}
/*
// walk over all letters in a certain order until we find an unused one
for (int i = maxLetter; i < 26; i++) {
int letter = letterorder[i];
int m = 1 << letter;
if (totalbits & m)
continue;
// take all words from the index of this letter and add each word to the
// solution if all letters of the word aren't used before.
for (int w : letterindex[i]) {
if (totalbits & w)
continue;
words[numwords] = w;
findwords(solutions, totalbits | w, numwords + 1, words, i + 1,
skipped);
OUTPUT(if (numwords == 0) std::cout
<< "\33[2K\rFound " << numsolutions
<< " solutions. Running time: " << (timeUS() - start) << "us");
}
if (skipped)
break;
skipped = true;
}*/
}
void findthread(std::vector<WordArray>& solutions) {
std::vector<WordArray> mysolutions;
std::unique_lock lock{queueMutex};
for (;;) {
if (queue.empty())
queueCVar.wait(lock, [] { return !queue.empty(); });
State state = queue.front();
queue.pop_front();
if (state.stop)
break;
lock.unlock();
findwords(mysolutions, state.totalbits, state.numwords, state.words,
state.skipped, true);
lock.lock();
}
solutions.insert(solutions.end(), mysolutions.begin(), mysolutions.end());
}
int findwords(std::vector<WordArray>& solutions) {
std::vector<std::jthread> threads;
auto numThreads = std::thread::hardware_concurrency() - 1;
printf("threads: %d\n", numThreads);
threads.reserve(numThreads);
for (int i = 0; i < numThreads; i++) {
threads.emplace_back([&]() { findthread(solutions); });
}
WordArray words = {};
findwords(solutions, 0, 0, words, 0, false);
{
std::unique_lock lock{queueMutex};
for (int i = 0; i < numThreads; i++)
queue.push_back({.stop = true});
queueCVar.notify_all();
}
threads.clear();
return int(solutions.size());
}
int main() {
TimeVar t1 = timeNow();
// bucket_matcher cache?
for (int high = 0; high < total_buckets; ++high) {
for (int h = 0; h < total_buckets; ++h) {
if ((h & high) == 0) {
bucket_matcher[high].push_back(h);
}
}
}
int letter_rare[26];
for (int i = 0; i < 26; ++i) {
letter_rare[i] = 'a' + i;
}
vector<string> allwords;
string filename = "words_alpha.txt";
{
ifstream file(filename, ios::binary | ios::ate);
streamsize size = file.tellg();
file.seekg(0, ios::beg);
vector<char> buffer(size);
if (file.read(buffer.data(), size)) {
int linestart = 0;
while (linestart < size) {
int dupMask = 0;
bool finishLine = false;
for (int i = 0; i < 6; ++i) {
char c = buffer[linestart + i];
if (c == '\n') {
if (i == 5) {
// we have 5 letter word
string word(buffer.begin() + linestart,
buffer.begin() + linestart + 5);
// cout << linestart << word << endl;
allwords.push_back(word);
for (char& c : word) {
letter_rare[c - 'a'] += 1 << 8;
}
linestart += 6;
break;
} else {
// less than 5 letter word
linestart += i + 1;
break;
}
} else {
if (i == 5) {
// more than 5 letter word
finishLine = true;
linestart += 6;
break;
} else {
// idk, still reading
int ii = 1 << (c - 'a');
if (dupMask & ii) {
linestart += i + 1;
finishLine = true;
break;
}
dupMask |= ii;
}
}
}
if (finishLine) {
while (buffer[linestart++] != '\n') {
}
}
}
}
}
sort(begin(letter_rare), end(letter_rare));
for (int i = 0; i < 26; ++i) {
int lr = letter_rare[i];
char letter = lr & 0xff;
int ii = 1 << i;
letter2mask[letter] = ii;
mask2letter[ii] = letter;
lowhighs[ii] = vector<vector<int>>(total_buckets);
}
printTime("read words", t1);
for (auto word : allwords) {
int mask = 0;
for (auto letter : word) {
mask |= letter2mask[letter];
}
#ifdef PRINT_ALL
if (mask2words.count(mask)) {
mask2words[mask].push_back(word);
} else {
mask2words[mask] = {word};
#else
if (!mask2word.count(mask)) {
mask2word[mask] = word;
#endif
int low = (~(mask - 1)) & mask; // first set bit
int highs = mask >> bucket_shift; // top n bits
lowhighs[low][highs].push_back(mask);
}
}
#ifdef PRINT_DEBUG
printf("words: %d anagrams: %d\n", (int)allwords.size(),
#ifdef PRINT_ALL
(int)mask2words.size()
#else
(int)mask2word.size()
#endif
);
#endif
printTime("preprocess", t1);
// I'm not great at multithreading yet so stealing tech from oisyn
std::vector<WordArray> solutions;
solutions.reserve(10000);
int num = findwords(solutions);
printTime("found words", t1);
#ifdef DO_PRINT
std::ofstream out("solutions.txt");
for (auto& words : solutions) {
#ifdef PRINT_ALL
for (auto word0 : mask2words[words[0]]) {
for (auto word1 : mask2words[words[1]]) {
for (auto word2 : mask2words[words[2]]) {
for (auto word3 : mask2words[words[3]]) {
for (auto word4 : mask2words[words[4]]) {
// printf("%s %s %s %s %s %c\n", word0.c_str(), word1.c_str(),
// word2.c_str(), word3.c_str(), word4.c_str(), single);
out << word0 << " " << word1 << " " << word2 << " " << word3
<< " " << word4 << endl;
}
}
}
}
}
#else
for (auto w : words) {
out << mask2word[w] << " ";
}
out << "\n";
#endif
};
#endif
#ifdef PRINT_DEBUG
printf("solutions: %d \n", num);
#endif
printTime("total", t1);
return 0;
}