aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/h48/solve_multithread.h
blob: 72f519ed88bf8e016b692f27b8d11c925c6974d6 (plain)
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
341
342
343
344
345
346
347
348
349
350
#define MAX_QUEUE_SIZE 244
#define BFS_DEPTH 2

typedef struct {
	dfsarg_solveh48_t tasks[MAX_QUEUE_SIZE];
	int front;
	int rear;
	int tasks_count;
	int active;
	pthread_mutex_t mutex;
	pthread_cond_t cond;
	pthread_cond_t active_cond;
	atomic_bool terminate;
	_Atomic long long nodes_visited_global;
	_Atomic long long table_fallbacks_global;
} task_queue_t;

STATIC void solve_h48_appendsolution_thread(dfsarg_solveh48_t *, task_queue_t *);
STATIC void init_queue(task_queue_t *);
STATIC void submit_task(task_queue_t *, dfsarg_solveh48_t);
STATIC void copy_queue(task_queue_t *, task_queue_t *, int, _Atomic int64_t *);
STATIC void *start_thread(void *);
STATIC int64_t solve_h48_bfs(dfsarg_solveh48_t *, task_queue_t *, int8_t);
STATIC int64_t solve_h48_single(dfsarg_solveh48_t *, task_queue_t *);
STATIC int64_t solve_h48_multithread(cube_t, int8_t, int8_t, int8_t, uint64_t,
    const void *, uint64_t, char *, long long [static NISSY_SIZE_SOLVE_STATS]);

STATIC void
solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq)
{
	pthread_mutex_lock(&tq->mutex);
	int64_t strl = 0;
	uint8_t invertedpremoves[MAXLEN];
	char *solution = *arg->nextsol;

	strl = writemoves(
	    arg->moves, arg->nmoves, arg->solutions_size, *arg->nextsol);

	if (strl < 0)
		goto solve_h48_appendsolution_thread_error;
	*arg->nextsol += strl-1;
	arg->solutions_size -= strl-1;

	if (arg->npremoves)
	{
		**arg->nextsol = ' ';
		(*arg->nextsol)++;
		arg->solutions_size--;

		invertmoves(arg->premoves, arg->npremoves, invertedpremoves);
		strl = writemoves(invertedpremoves,
		    arg->npremoves, arg->solutions_size, *arg->nextsol);

		if (strl < 0)
			goto solve_h48_appendsolution_thread_error;
		*arg->nextsol += strl-1;
		arg->solutions_size -= strl-1;
	}
	LOG("Solution found: %s\n", solution);

	**arg->nextsol = '\n';
	(*arg->nextsol)++;
	arg->solutions_size--;
	(*arg->nsols)++;

solve_h48_appendsolution_thread_error:
	/* We could add some logging, but writemoves() already does */
	pthread_mutex_unlock(&tq->mutex);
}

STATIC void
init_queue(task_queue_t *queue)
{
	queue->front = 0;
	queue->rear = 0;
	queue->tasks_count = 0;
	queue->active = 0;
	queue->terminate = ATOMIC_VAR_INIT(false);
	pthread_mutex_init(&queue->mutex, NULL);
	pthread_cond_init(&queue->cond, NULL);
	pthread_cond_init(&queue->active_cond, NULL);
}

STATIC void
submit_task(task_queue_t *queue, dfsarg_solveh48_t task)
{
	pthread_mutex_lock(&queue->mutex);
	queue->tasks[queue->rear] = task;
	queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
	queue->tasks_count++;
	pthread_cond_broadcast(&queue->cond);
	pthread_mutex_unlock(&queue->mutex);
}

STATIC void
copy_queue(task_queue_t *src, task_queue_t *dest, int depth, _Atomic int64_t *nsols)
{
	pthread_mutex_lock(&dest->mutex);
	for (int i = src->front; i != src->rear; i = (i + 1) % MAX_QUEUE_SIZE)
	{
			dest->tasks[i] = src->tasks[i];
			dest->tasks[i].depth = depth;
	}
	dest->front = src->front;
	dest->rear = src->rear;
	dest->tasks_count = src->tasks_count;
	pthread_cond_broadcast(&dest->cond);
	pthread_mutex_unlock(&dest->mutex);
}

STATIC void *
start_thread(void *arg)
{
	task_queue_t *queue = (task_queue_t *)arg;
	while (1) {
		pthread_mutex_lock(&queue->mutex);
		while (queue->tasks_count == 0 && !queue->terminate) {
			pthread_cond_wait(&queue->cond, &queue->mutex);
		}
		if (queue->tasks_count == 0 && queue->terminate) {
			pthread_mutex_unlock(&queue->mutex);
			break;
		}

		if (queue->tasks_count > 0) {
			dfsarg_solveh48_t task = queue->tasks[queue->front];
			queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
			queue->tasks_count--;
			queue->active++;
			pthread_mutex_unlock(&queue->mutex);
			
			solve_h48_single(&task, queue);
			queue->nodes_visited_global += task.nodes_visited;
			queue->table_fallbacks_global += task.table_fallbacks;

			pthread_mutex_lock(&queue->mutex);
			queue->active--;

			if(queue->tasks_count == 0 && queue->active == 0)
				pthread_cond_signal(&queue->active_cond);
		}
		pthread_mutex_unlock(&queue->mutex);
	}
	return NULL;
}

STATIC int64_t
solve_h48_bfs(dfsarg_solveh48_t *arg_zero, task_queue_t *tq, int8_t maxmoves)
{
	dfsarg_solveh48_t queue[MAX_QUEUE_SIZE];
	int front = 0, rear = 0;
	dfsarg_solveh48_t nextarg;
	int depth = 0;
	int nodes_at_current_depth = 1;
	int nodes_at_next_depth = 0;
	queue[rear++] = *arg_zero;

	dfsarg_solveh48_t task_pool[MAX_QUEUE_SIZE];

	while (front < rear){
		dfsarg_solveh48_t arg = queue[front++];
		nodes_at_current_depth--;

		if (*arg.nsols == arg.maxsolutions)
			return 1;

		if (issolved(arg.cube)){
			if (arg.nmoves + arg.npremoves >= arg.depth && arg.nmoves + arg.npremoves <= maxmoves)
			solve_h48_appendsolution(&arg);
			continue;
		}

		arg.nissbranch = MM_NORMAL;
		uint32_t allowed = allowednextmove_h48(arg.moves, arg.nmoves, arg.nissbranch);

		for (uint8_t m = 0; m < 18; m++){
			if (allowed & (1 << m)){
				nextarg = arg;
				nextarg.nmoves = arg.nmoves + 1;
				nextarg.moves[arg.nmoves] = m;
				nextarg.cube = move(arg.cube, m);
				nextarg.inverse = premove(arg.inverse, m);

				if (nextarg.nmoves == BFS_DEPTH){
					dfsarg_solveh48_t *task = &task_pool[rear % MAX_QUEUE_SIZE];
					*task = nextarg;
					submit_task(tq, *task);
				} else {
					queue[rear++] = nextarg;
					nodes_at_next_depth++;
				}
			}
		}
		if (nodes_at_current_depth == 0){
			nodes_at_current_depth = nodes_at_next_depth;
			nodes_at_next_depth = 0;
			LOG("Found %" PRId64 " solutions, searching at depth %" PRId8 "\n", *nextarg.nsols, depth++);
		}
		if (depth == BFS_DEPTH) return 0;
	}
	return 1;
}

STATIC int64_t
solve_h48_single(dfsarg_solveh48_t *arg, task_queue_t *tq)
{
	dfsarg_solveh48_t nextarg;
	int64_t ret;
	uint8_t m;

	if (*arg->nsols == arg->maxsolutions)
		return 0;

	if (solve_h48_stop(arg))
		return 0;

	if (issolved(arg->cube)){
		if (arg->nmoves + arg->npremoves != arg->depth)
			return 0;
		solve_h48_appendsolution_thread(arg, tq);
		return 1;
	}

	nextarg = *arg;
	ret = 0;
	uint32_t allowed;
	if (arg->nissbranch & MM_INVERSE){
		allowed = allowednextmove_h48(arg->premoves, arg->npremoves, arg->nissbranch);
		for (m = 0; m < 18; m++){
			if (allowed & (1 << m)){
				nextarg.npremoves = arg->npremoves + 1;
				nextarg.premoves[arg->npremoves] = m;
				nextarg.inverse = move(arg->inverse, m);
				nextarg.cube = premove(arg->cube, m);
				ret += solve_h48_single(&nextarg, tq);
			}
		}
	} else {
		allowed = allowednextmove_h48(arg->moves, arg->nmoves, arg->nissbranch);
		for (m = 0; m < 18; m++){
			if (allowed & (1 << m)){
				nextarg.nmoves = arg->nmoves + 1;
				nextarg.moves[arg->nmoves] = m;
				nextarg.cube = move(arg->cube, m);
				nextarg.inverse = premove(arg->inverse, m);
				ret += solve_h48_single(&nextarg, tq);
			}
		}
	}

	arg->nodes_visited = nextarg.nodes_visited;
	arg->table_fallbacks = nextarg.table_fallbacks;
	return ret;
}

STATIC int64_t
solve_h48_multithread(
	cube_t cube,
	int8_t minmoves,
	int8_t maxmoves,
	int8_t maxsolutions,
	uint64_t data_size,
	const void *data,
	uint64_t solutions_size,
	char *solutions,
	long long stats[static NISSY_SIZE_SOLVE_STATS]
)
{
	_Atomic int64_t nsols = 0;
	int p_depth = 0;
	dfsarg_solveh48_t arg;
	tableinfo_t info, fbinfo;
	pthread_t threads[THREADS];

	if (readtableinfo_n(data_size, data, 2, &info) != NISSY_OK)
		goto solve_h48_multithread_error_data;

	arg = (dfsarg_solveh48_t){
		.cube = cube,
		.inverse = inverse(cube),
		.nsols = &nsols,
		.depth = minmoves,
		.maxsolutions = maxsolutions,
		.h = info.h48h,
		.k = info.bits,
		.base = info.base,
		.cocsepdata = (uint32_t *)((char *)data + INFOSIZE),
		.h48data = (uint8_t *)data + COCSEP_FULLSIZE + INFOSIZE,
		.solutions_size = solutions_size,
		.nextsol = &solutions,
		.nodes_visited = 0,
		.table_fallbacks = 0
	};

	if (info.bits == 2) {
		if (readtableinfo_n(data_size, data, 3, &fbinfo) != NISSY_OK)
			goto solve_h48_multithread_error_data;
		/* We only support h0k4 as fallback table */
		if (fbinfo.h48h != 0 || fbinfo.bits != 4)
			goto solve_h48_multithread_error_data;
		arg.h48data_fallback = arg.h48data + info.next;
	} else {
		arg.h48data_fallback = NULL;
	}

	task_queue_t q;
	init_queue(&q);
	if (solve_h48_bfs(&arg, &q, maxmoves))
		return nsols;

	task_queue_t nq;
	init_queue(&nq);

	nq.nodes_visited_global = nq.table_fallbacks_global = 0;
	for (int i = 0; i < THREADS; i++) {
		pthread_create(&threads[i], NULL, &start_thread, &nq);
	}

	nsols = 0;
	for (p_depth = minmoves > BFS_DEPTH ? minmoves : BFS_DEPTH;
		 p_depth <= maxmoves && nsols < maxsolutions;
		 p_depth++)
	{
		LOG("Found %" PRId64 " solutions, "
		    "searching at depth %" PRId8 "\n", nsols, p_depth);
		copy_queue(&q, &nq, p_depth, &nsols);
		
		pthread_mutex_lock(&nq.mutex);
		while (nq.active > 0 || nq.tasks_count > 0)
			pthread_cond_wait(&nq.active_cond, &nq.mutex);
		pthread_mutex_unlock(&nq.mutex);
	}

	atomic_store(&nq.terminate, true);
	pthread_cond_broadcast(&nq.cond);

	for (int i = 0; i < THREADS; i++) {
		pthread_join(threads[i], NULL);
	}
	**arg.nextsol = '\0';

	stats[0] = nq.nodes_visited_global;
	stats[1] = nq.table_fallbacks_global;
	LOG("Nodes visited: %lld\nTable fallbacks: %lld\n",
	    nq.nodes_visited_global, nq.table_fallbacks_global);

	return nsols;

solve_h48_multithread_error_data: LOG("solve_h48: error reading table\n"); return NISSY_ERROR_DATA;
}

Generated with cgit - Back to sebastiano.tronto.net