Blame view

src/kernels/cl_aux_functions.c 33.4 KB
94b2b13d   Pedro Roque   PHACT source
1
2
3
4
/*
 * cl_aux_functions.c
 *
 *  Created on: 09/09/2019
4d26a735   Pedro Roque   Increased recogni...
5
 *      Author: pedro
94b2b13d   Pedro Roque   PHACT source
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
 */

#ifndef __OPENCL_VERSION__
#include <stdbool.h>
#include <limits.h>
#include <sys/types.h>
#include "cl_aux_functions.h"
#include "../utils/cl_syntax.h"
#endif

#if CUDA_VERSION
#include "../utils/cu_syntax.h"
#endif

#include "../config.h"
#include "../domains.h"
#include "cl_constraints.h"
#include "cl_variables.h"
#include "cl_ttl.h"

#if CL_D_TYPE == CL_BITMAP
#include "cl_bitmaps.h"
#elif CL_D_TYPE == CL_INTERVAL
#include  "cl_intervals.h"
#endif

/*
 * Adds a variable to the vector of variables to propagate
 * vs_id_to_prop_ - circular vector with the ids of the variables to propagate
 * vs_prop_ - vector with all CSP variables
 * v_id_to_prop - id of the variable to propagate
 */
4d26a735   Pedro Roque   Increased recogni...
38
CUDA_FUNC void v_add_to_prop(CL_MEMORY unsigned short* vs_id_to_prop_, CL_MEMORY VARS_PROP* vs_prop_, int v_id_to_prop) {
94b2b13d   Pedro Roque   PHACT source
39
40
41
42
43

	// if that variable is not already set for propagation
	if (vs_prop_[v_id_to_prop].to_prop == 0) {

		// add that variable id to the vector of variables to propagate
4d26a735   Pedro Roque   Increased recogni...
44
		vs_id_to_prop_[vs_id_to_prop_[1]] = convert_ushort(v_id_to_prop);
94b2b13d   Pedro Roque   PHACT source
45
46

		if (vs_id_to_prop_[1] < CL_N_VS + 2) {
4d26a735   Pedro Roque   Increased recogni...
47
			vs_id_to_prop_[1] = convert_ushort(vs_id_to_prop_[1] + 1);
94b2b13d   Pedro Roque   PHACT source
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
		} else {
			vs_id_to_prop_[1] = 2;
		}

		// mark that variable as added to vs_id_to_prop_ vector
		vs_prop_[v_id_to_prop].to_prop = 1;
	}

#if CL_CHECK_ERRORS
	int i;
	if (vs_id_to_prop_[0] <= vs_id_to_prop_[1]) {
		for (i = vs_id_to_prop_[0]; i < vs_id_to_prop_[1]; i++) {
			if (vs_id_to_prop_[i] == v_id_to_prop) {
				break;
			}
		}
		if (vs_id_to_prop_[i] != v_id_to_prop) {
			printf((__constant char *)"\n###error 31\n");
		}

	} else {
		for (i = vs_id_to_prop_[0]; i < CL_N_VS + 2; i++) {
			if (vs_id_to_prop_[i] == v_id_to_prop) {
				break;
			}
		}
		if (vs_id_to_prop_[i] != v_id_to_prop) {
			for (i = 2; i < vs_id_to_prop_[1]; i++) {
				if (vs_id_to_prop_[i] == v_id_to_prop) {
					break;
				}
			}
		}
		if (vs_id_to_prop_[i] != v_id_to_prop) {
			printf((__constant char *)"\n###error 32\n");
		}
	}
#endif

}

/*
4d26a735   Pedro Roque   Increased recogni...
90
 * Return the next variable to propagate by selecting the first one on vs_id_to_prop_ vector, or CL_N_VS if none
94b2b13d   Pedro Roque   PHACT source
91
92
 * vs_id_to_prop_ - circular vector with the ids of the variables to propagate
 * vs_prop_ - vector with all CSP variables
4d26a735   Pedro Roque   Increased recogni...
93
 * vs - vector with all CSP variables common to all work-items (not used with this heuristic)
94b2b13d   Pedro Roque   PHACT source
94
 */
4d26a735   Pedro Roque   Increased recogni...
95
CUDA_FUNC void v_get_id_to_prop(CL_MEMORY unsigned short* vs_id_to_prop_, CL_MEMORY VARS_PROP* vs_prop_, unsigned int* prop_v_id TTL_CTR) {
94b2b13d   Pedro Roque   PHACT source
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
	int v_idx;

	// If variables to propagate vector is empty
	if (vs_id_to_prop_[0] == vs_id_to_prop_[1]) {
		// Restart the first and next index on variables to propagate vector
		vs_id_to_prop_[0] = 2;
		vs_id_to_prop_[1] = 2;

		*prop_v_id = CL_N_VS;

#if CL_CHECK_ERRORS
		int i;
		for (i = 0; i < CL_N_VS; i++) {
			CHECK_TTL(ttl_ctr, 153)

4d26a735   Pedro Roque   Increased recogni...
111
112
113
			if (vs_prop_[i].to_prop == 1) {
				printf((__constant char *)"\n###error 33\n");
			}
94b2b13d   Pedro Roque   PHACT source
114
115
116
117
		}
#endif

	} else {
4d26a735   Pedro Roque   Increased recogni...
118
		v_idx = convert_int(vs_id_to_prop_[vs_id_to_prop_[0]]);
94b2b13d   Pedro Roque   PHACT source
119
120
121
122
123

		// mark that variable as not added to vs_id_to_prop_ vector
		vs_prop_[vs_id_to_prop_[vs_id_to_prop_[0]]].to_prop = 0;

		if (vs_id_to_prop_[0] < CL_N_VS + 2) {
4d26a735   Pedro Roque   Increased recogni...
124
			vs_id_to_prop_[0] = convert_ushort(vs_id_to_prop_[0] + 1);
94b2b13d   Pedro Roque   PHACT source
125
126
127
128
		} else {
			vs_id_to_prop_[0] = 2;
		}

4d26a735   Pedro Roque   Increased recogni...
129
		*prop_v_id = convert_ushort(v_idx);
94b2b13d   Pedro Roque   PHACT source
130
	}
4d26a735   Pedro Roque   Increased recogni...
131
	CHECK_TTL(ttl_ctr, 232)
94b2b13d   Pedro Roque   PHACT source
132
133
}

4d26a735   Pedro Roque   Increased recogni...
134
#if CL_LABEL_M == CL_FIRST_FAIL
94b2b13d   Pedro Roque   PHACT source
135
/*
4d26a735   Pedro Roque   Increased recogni...
136
 * Return the next variable to label by selecting the one not labeled yet and with less values on its domain, or CL_N_VS if none
94b2b13d   Pedro Roque   PHACT source
137
138
 * vs_prop_ - vector with all CSP variables
 * vs - vector with all CSP variables common to all work-items
4d26a735   Pedro Roque   Increased recogni...
139
 */
94b2b13d   Pedro Roque   PHACT source
140
CUDA_FUNC void v_get_id_to_label(CL_MEMORY VARS_PROP* vs_prop_, CL_VS_MEM VARS* vs, unsigned int* prop_v_id TTL_CTR) {
4d26a735   Pedro Roque   Increased recogni...
141
142
143
144
145
146
147
148
149
150
	int vals_cnt = 0xFFFF;
	int n_vals;
	int v_idx = CL_N_VS;
	int i;

	for (i = 0; i < CL_N_VS; i++) {
		CHECK_TTL(ttl_ctr, 3)

#if CL_CHECK_ERRORS
		bool empty;
94b2b13d   Pedro Roque   PHACT source
151
		cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
4d26a735   Pedro Roque   Increased recogni...
152
153
		if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
			printf((__constant char *)"\n###error 34\n");
94b2b13d   Pedro Roque   PHACT source
154
155
		}
#endif
4d26a735   Pedro Roque   Increased recogni...
156
157
158
159
160

		n_vals = V_N_VALS(vs_prop_[i]);
		if (vs[i].to_label && n_vals > 1 && n_vals < vals_cnt) {
			vals_cnt = n_vals;

94b2b13d   Pedro Roque   PHACT source
161
162
			v_idx = i;

4d26a735   Pedro Roque   Increased recogni...
163
			// The minimum values of a variable to label will be two, so stop searching
94b2b13d   Pedro Roque   PHACT source
164
			if (n_vals == 2) {
4d26a735   Pedro Roque   Increased recogni...
165
166
167
168
169
170
171
				i = CL_N_VS;
			}
		}
	}

#if CL_TO_LABEL_THRESHOLD > 0
	if (v_idx == CL_N_VS) {
94b2b13d   Pedro Roque   PHACT source
172
		for (i = 0; i < CL_N_VS; i++) {
4d26a735   Pedro Roque   Increased recogni...
173
174
175
176
177
			CHECK_TTL(ttl_ctr, 3)

#if CL_CHECK_ERRORS
			bool empty;
			cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
94b2b13d   Pedro Roque   PHACT source
178
179
			if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
				printf((__constant char *)"\n###error 35\n");
4d26a735   Pedro Roque   Increased recogni...
180
			}
94b2b13d   Pedro Roque   PHACT source
181
#endif
4d26a735   Pedro Roque   Increased recogni...
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

			n_vals = V_N_VALS(vs_prop_[i]);
			if (!vs[i].to_label && n_vals > 1 && n_vals < vals_cnt) {
				vals_cnt = n_vals;

				v_idx = i;

				// The minimum values of a variable to label will be two, so stop searching
				if (n_vals == 2) {
					i = CL_N_VS;
				}
			}
		}
	}
#endif

	*prop_v_id = convert_ushort(v_idx);
}

#elif CL_LABEL_M == CL_INPUT_ORDER
/*
 * Return the next variable to label by selecting the first one not labeled yet, or CL_N_VS if none
 * vs_prop_ - vector with all CSP variables
 * vs - vector with all CSP variables common to all work-items
 */
94b2b13d   Pedro Roque   PHACT source
207
208
CUDA_FUNC void v_get_id_to_label(CL_MEMORY VARS_PROP* vs_prop_, CL_VS_MEM VARS* vs, unsigned int* prop_v_id TTL_CTR) {
	int v_idx = CL_N_VS;
4d26a735   Pedro Roque   Increased recogni...
209
210
211
212
213
	int i;

	for (i = 0; i < CL_N_VS; i++) {
		CHECK_TTL(ttl_ctr, 29)

94b2b13d   Pedro Roque   PHACT source
214
215
#if CL_CHECK_ERRORS
		bool empty;
4d26a735   Pedro Roque   Increased recogni...
216
217
218
219
220
221
222
223
		cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
		if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
			printf((__constant char *)"\n###error 36\n");
		}
#endif

		if (vs[i].to_label && V_N_VALS(vs_prop_[i]) > 1) {
			v_idx = i;
94b2b13d   Pedro Roque   PHACT source
224
			i = CL_N_VS;
4d26a735   Pedro Roque   Increased recogni...
225
		}
94b2b13d   Pedro Roque   PHACT source
226
	}
4d26a735   Pedro Roque   Increased recogni...
227
228
229

#if CL_TO_LABEL_THRESHOLD > 0
	if (v_idx == CL_N_VS) {
94b2b13d   Pedro Roque   PHACT source
230
231
		for (i = 0; i < CL_N_VS; i++) {
			CHECK_TTL(ttl_ctr, 29)
4d26a735   Pedro Roque   Increased recogni...
232
233
234
235

#if CL_CHECK_ERRORS
		bool empty;
		cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
94b2b13d   Pedro Roque   PHACT source
236
237
		if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
			printf((__constant char *)"\n###error 37\n");
4d26a735   Pedro Roque   Increased recogni...
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
		}
#endif

			if (!vs[i].to_label && V_N_VALS(vs_prop_[i]) > 1) {
				v_idx = i;
				i = CL_N_VS;
			}
		}
	}
#endif

	*prop_v_id = convert_ushort(v_idx);
}

#elif CL_LABEL_M == CL_OCCURRENCE
/*
 * Return the next variable to label by selecting the one not labeled yet that is more constrained, or CL_N_VS if none
 * vs_prop_ - vector with all CSP variables local to this work-item
 * vs - vector with all CSP variables common to all work-items
 */
CUDA_FUNC void v_get_id_to_label(CL_MEMORY VARS_PROP* vs_prop_, CL_VS_MEM VARS* vs, unsigned int* prop_v_id TTL_CTR) {
	int max_cs_cnt = 0;
	int v_idx = CL_N_VS;
	int i;

	for (i = 0; i < CL_N_VS; i++) {
		CHECK_TTL(ttl_ctr, 30)

#if CL_CHECK_ERRORS
		bool empty;
		cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
		if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
			printf((__constant char *)"\n###error 38\n");
		}
#endif

94b2b13d   Pedro Roque   PHACT source
274
275
		if (vs[i].to_label && vs[i].n_cs > max_cs_cnt && V_N_VALS(vs_prop_[i]) > 1) {
			max_cs_cnt = vs[i].n_cs;
4d26a735   Pedro Roque   Increased recogni...
276
277
278
279
280

			v_idx = i;
		}
	}

94b2b13d   Pedro Roque   PHACT source
281
#if CL_TO_LABEL_THRESHOLD > 0
4d26a735   Pedro Roque   Increased recogni...
282
283
	if (v_idx == CL_N_VS) {
		for (i = 0; i < CL_N_VS; i++) {
94b2b13d   Pedro Roque   PHACT source
284
285
			CHECK_TTL(ttl_ctr, 30)

4d26a735   Pedro Roque   Increased recogni...
286
#if CL_CHECK_ERRORS
94b2b13d   Pedro Roque   PHACT source
287
			bool empty;
4d26a735   Pedro Roque   Increased recogni...
288
289
290
291
292
293
294
295
296
			cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
			if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
				printf((__constant char *)"\n###error 39\n");
			}
#endif

			if (!vs[i].to_label && vs[i].n_cs > max_cs_cnt && V_N_VALS(vs_prop_[i]) > 1) {
				max_cs_cnt = vs[i].n_cs;

94b2b13d   Pedro Roque   PHACT source
297
				v_idx = i;
4d26a735   Pedro Roque   Increased recogni...
298
299
			}
		}
94b2b13d   Pedro Roque   PHACT source
300
301
	}
#endif
4d26a735   Pedro Roque   Increased recogni...
302
303
304
305
306

	*prop_v_id = convert_ushort(v_idx);
}

#elif CL_LABEL_M == CL_MAX_REGRET
94b2b13d   Pedro Roque   PHACT source
307
308
/*
 * Return the next variable to label by selecting the one on vs_id_to_prop_ vector that has the largest difference between the two smallest values, or CL_N_VS if none
4d26a735   Pedro Roque   Increased recogni...
309
310
311
312
313
314
315
316
317
318
319
320
 * vs_prop_ - vector with all CSP variables
 * vs - vector with all CSP variables common to all work-items
 */
CUDA_FUNC void v_get_id_to_label(CL_MEMORY VARS_PROP* vs_prop_, CL_VS_MEM VARS* vs, unsigned int* prop_v_id TTL_CTR) {
	int diff_aux;
	int diff = 0;
	int v_idx = CL_N_VS;
	int i;

	for (i = 0; i < CL_N_VS; i++) {
		CHECK_TTL(ttl_ctr, 31)

94b2b13d   Pedro Roque   PHACT source
321
322
323
#if CL_CHECK_ERRORS
		bool empty;
		cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
4d26a735   Pedro Roque   Increased recogni...
324
		if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
94b2b13d   Pedro Roque   PHACT source
325
			printf((__constant char *)"\n###error 40\n");
4d26a735   Pedro Roque   Increased recogni...
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
351
		}
#endif

		if (vs[i].to_label && V_N_VALS(vs_prop_[i]) > 1) {

			v_get_max_regret_diff_m(&diff_aux, &vs_prop_[i]);

			if (diff_aux > diff) {

				diff = diff_aux;
				v_idx = i;
			}
		}
	}

#if CL_TO_LABEL_THRESHOLD > 0
	if (v_idx == CL_N_VS) {
		for (i = 0; i < CL_N_VS; i++) {
			CHECK_TTL(ttl_ctr, 31)

#if CL_CHECK_ERRORS
			bool empty;
			cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
			if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
				printf((__constant char *)"\n###error 41\n");
			}
94b2b13d   Pedro Roque   PHACT source
352
353
#endif

4d26a735   Pedro Roque   Increased recogni...
354
355
356
357
358
			if (!vs[i].to_label && V_N_VALS(vs_prop_[i]) > 1) {

				v_get_max_regret_diff_m(&diff_aux, &vs_prop_[i]);

				if (diff_aux > diff) {
94b2b13d   Pedro Roque   PHACT source
359
360

					diff = diff_aux;
4d26a735   Pedro Roque   Increased recogni...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
					v_idx = i;
				}
			}
		}
	}
#endif

	*prop_v_id = v_idx;
}
#endif

#if CL_LABEL_M == CL_SMALLEST
/*
 * Return the next variable to label by selecting the one not labeled yet and with the smallest value on its domain, or CL_N_VS if none
94b2b13d   Pedro Roque   PHACT source
375
376
 * vs_prop_ - vector with all CSP variables
 * vs - vector with all CSP variables common to all work-items
4d26a735   Pedro Roque   Increased recogni...
377
 */
94b2b13d   Pedro Roque   PHACT source
378
CUDA_FUNC void v_get_id_to_label(CL_MEMORY VARS_PROP* vs_prop_, CL_VS_MEM VARS* vs, unsigned int* prop_v_id TTL_CTR) {
4d26a735   Pedro Roque   Increased recogni...
379
	int v_idx = CL_N_VS;
94b2b13d   Pedro Roque   PHACT source
380
	int min = CL_D_MAX;
4d26a735   Pedro Roque   Increased recogni...
381
	int i;
94b2b13d   Pedro Roque   PHACT source
382

4d26a735   Pedro Roque   Increased recogni...
383
	for (i = 0; i < CL_N_VS; i++) {
94b2b13d   Pedro Roque   PHACT source
384
		CHECK_TTL(ttl_ctr, 3)
4d26a735   Pedro Roque   Increased recogni...
385
386
387
388

#if CL_CHECK_ERRORS
		bool empty;
		cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
94b2b13d   Pedro Roque   PHACT source
389
		if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
4d26a735   Pedro Roque   Increased recogni...
390
			printf((__constant char *)"\n###error 42\n");
94b2b13d   Pedro Roque   PHACT source
391
		}
4d26a735   Pedro Roque   Increased recogni...
392
393
394
395
396
397
398
399
400
401
402
#endif

		if (vs[i].to_label && V_N_VALS(vs_prop_[i]) > 1 && V_MIN(vs_prop_[i]) < min) {

			v_idx = i;
			min = V_MIN(vs_prop_[i]);

			// If the minimum value is CL_D_MIN, stop searching
			if (min == CL_D_MIN) {
				i = CL_N_VS;
			}
94b2b13d   Pedro Roque   PHACT source
403
		}
4d26a735   Pedro Roque   Increased recogni...
404
405
	}

94b2b13d   Pedro Roque   PHACT source
406
407
#if CL_TO_LABEL_THRESHOLD > 0
	if (v_idx == CL_N_VS) {
4d26a735   Pedro Roque   Increased recogni...
408
409
410
411
412
		for (i = 0; i < CL_N_VS; i++) {
			CHECK_TTL(ttl_ctr, 3)

#if CL_CHECK_ERRORS
			bool empty;
94b2b13d   Pedro Roque   PHACT source
413
414
			cl_d_is_empty_m(&empty, &vs_prop_->prop_d TTL_CTR_V);
			if (empty || vs_prop_[i].n_vals == 0 || vs_prop_[i].n_vals > vs_prop_[i].max + 1 || vs_prop_[i].min > vs_prop_[i].max || vs_prop_[i].max > CL_D_MAX) {
4d26a735   Pedro Roque   Increased recogni...
415
				printf((__constant char *)"\n###error 43\n");
94b2b13d   Pedro Roque   PHACT source
416
			}
4d26a735   Pedro Roque   Increased recogni...
417
418
419
420
421
422
423
424
425
426
427
#endif

			if (!vs[i].to_label && V_N_VALS(vs_prop_[i]) > 1 && V_MIN(vs_prop_[i]) < min) {

				v_idx = i;
				min = V_MIN(vs_prop_[i]);

				// If the minimum value is 0, stop searching
				if (min == 0) {
					break;
				}
94b2b13d   Pedro Roque   PHACT source
428
			}
4d26a735   Pedro Roque   Increased recogni...
429
		}
94b2b13d   Pedro Roque   PHACT source
430
	}
4d26a735   Pedro Roque   Increased recogni...
431
432
433
#endif

	*prop_v_id = v_idx;
94b2b13d   Pedro Roque   PHACT source
434
}
4d26a735   Pedro Roque   Increased recogni...
435
436
437
438
439
440
441
442
443
444
445
#endif

#if CL_ASSIGN_M == CL_MIN_VAL
/*
 * Assign the minimum value
 * v - variable to be assigned
 * hist - place on backtracking history where the remaining values of the variable should be stored
 * hist_labeleds_n_vals - place where the number of values remaining on the assigned variable should be stored
 */
CUDA_FUNC void v_assign(CL_MEMORY VARS_PROP* v, __global DOMAIN_* hist, __global int* hist_labeleds_n_vals TTL_CTR) {

94b2b13d   Pedro Roque   PHACT source
446
#if CL_CHECK_ERRORS
4d26a735   Pedro Roque   Increased recogni...
447
448
449
450
		bool empty;
		cl_d_is_empty_m(&empty, &v->prop_d TTL_CTR_V);
		if (empty || v->n_vals == 0 || v->n_vals > v->max + 1 || v->min > v->max || v->max > CL_D_MAX) {
			printf((__constant char *)"\n###error 44\n");
94b2b13d   Pedro Roque   PHACT source
451
		}
4d26a735   Pedro Roque   Increased recogni...
452
453
#endif

94b2b13d   Pedro Roque   PHACT source
454
	(*hist_labeleds_n_vals) = V_N_VALS(*v) - 1;
4d26a735   Pedro Roque   Increased recogni...
455
456
457
458
459

	// If the labeled variable has more than one value on its domain remove the labeled value from its backtracking domain
	if ((*hist_labeleds_n_vals) > 0) {

		int min = V_MIN(*v);
94b2b13d   Pedro Roque   PHACT source
460
461
462
		bool changed;
		cl_v_del_all_except_val_m(&changed, v, min TTL_CTR_V);
		cl_d_del_val_no_tests_g(hist, min);
4d26a735   Pedro Roque   Increased recogni...
463
464
465
466
467
468
469
470
471
472

	} else {
		cl_d_clear_g(hist TTL_CTR_V);
	}
}

#elif CL_ASSIGN_M == CL_MAX_VAL
/*
 * Assign the maximum value
 * v - variable to be assigned
94b2b13d   Pedro Roque   PHACT source
473
474
 * hist - place on backtracking history where the remaining values of the variable should be stored
 * hist_labeleds_n_vals - place where tje number of values remaining on the assigned variable should be stored
4d26a735   Pedro Roque   Increased recogni...
475
476
477
478
479
 */
CUDA_FUNC void v_assign(CL_MEMORY VARS_PROP* v, __global DOMAIN_* hist, __global int* hist_labeleds_n_vals) {

#if CL_CHECK_ERRORS
		bool empty;
94b2b13d   Pedro Roque   PHACT source
480
		cl_d_is_empty_m(&empty, &v->prop_d TTL_CTR_V);
4d26a735   Pedro Roque   Increased recogni...
481
482
		if (empty || v->n_vals == 0 || v->n_vals > v->max + 1 || v->min > v->max || v->max > CL_D_MAX) {
			printf((__constant char *)"\n###error 45\n");
94b2b13d   Pedro Roque   PHACT source
483
484
		}
#endif
4d26a735   Pedro Roque   Increased recogni...
485

94b2b13d   Pedro Roque   PHACT source
486
	(*hist_labeleds_n_vals) = V_N_VALS(*v) - 1;
4d26a735   Pedro Roque   Increased recogni...
487
488
489
490
491
492
493
494
495

	// If the labeled variable had more than one value on its domain remove the labeled value from its backtracking domain
	if ((*hist_labeleds_n_vals) > 0) {
		int max = V_MAX(*v);
		bool changed;
		cl_v_del_all_except_val_m(&changed, v, max TTL_CTR_V);
		cl_d_del_val_no_tests_g(hist, max);

	} else {
94b2b13d   Pedro Roque   PHACT source
496
		cl_d_clear_g(hist);
4d26a735   Pedro Roque   Increased recogni...
497
498
	}
}
94b2b13d   Pedro Roque   PHACT source
499
500

#elif CL_ASSIGN_M == CL_SPLIT_VALS
4d26a735   Pedro Roque   Increased recogni...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 * Split the domain in half between the minimum and the maximum, and keeps the first half
 * v - variable to be assigned
 * hist - place on backtracking history where the remaining values of the variable should be stored
 * hist_labeleds_n_vals - place where tje number of values remaining on the assigned variable should be stored
 */
CUDA_FUNC void v_assign(CL_MEMORY VARS_PROP* v, __global DOMAIN_* hist, __global int* hist_labeleds_n_vals) {

#if CL_CHECK_ERRORS
		bool empty;
		cl_d_is_empty_m(&empty, &v->prop_d TTL_CTR_V);
		if (empty || v->n_vals == 0 || v->n_vals > v->max + 1 || v->min > v->max || v->max > CL_D_MAX) {
			printf((__constant char *)"\n###error 46\n");
		}
#endif

	int n_vals = V_N_VALS(*v);

	if (n_vals > 1) {
		int min = V_MIN(*v);
94b2b13d   Pedro Roque   PHACT source
521

4d26a735   Pedro Roque   Increased recogni...
522
523
524
525
526
		int split_val = (V_MAX(*v) - min) / 2 + min;
		cl_v_del_gt_no_tests_m(v, split_val);
		cl_d_del_le_no_tests_g(hist, split_val);

		*hist_labeleds_n_vals = n_vals - V_N_VALS(*v);
94b2b13d   Pedro Roque   PHACT source
527
528
	} else {
		*hist_labeleds_n_vals = 0;
4d26a735   Pedro Roque   Increased recogni...
529
530
531
532
533
534
535
536
537
538
539
540
541
542
		cl_d_clear_g(hist);
	}
}
#endif

#if CL_WORK == CL_OPT

/*
 * Update the domain of the variable to optimize according to the global value to optimize on the received backtracking store. Global memory
 * Return 1 if domain changed and 0 if not
 * hist - backtracking data
 * val_to_opt - global value to optimize
 */
CUDA_FUNC void upd_opt_var_hist_g(__global DOMAIN_* hist, __global unsigned int* val_to_opt TTL_CTR) {
94b2b13d   Pedro Roque   PHACT source
543

4d26a735   Pedro Roque   Increased recogni...
544
545
#if CL_CHECK_ERRORS
		bool empty;
94b2b13d   Pedro Roque   PHACT source
546
		cl_d_is_empty_g(&empty, hist TTL_CTR_V);
4d26a735   Pedro Roque   Increased recogni...
547
548
		if (empty || *val_to_opt > CL_D_MAX) {
			printf((__constant char *)"\n###error 47\n");
94b2b13d   Pedro Roque   PHACT source
549
		}
4d26a735   Pedro Roque   Increased recogni...
550
551
#endif

94b2b13d   Pedro Roque   PHACT source
552
553
554
	bool changed;
	int val_to_opt_aux = convert_int(*val_to_opt);

4d26a735   Pedro Roque   Increased recogni...
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#if CL_OPT_M == CL_DECREASE
	cl_d_del_gt_g(&changed, hist, val_to_opt_aux TTL_CTR_V);
#else
	cl_d_del_lt_g(&changed, hist, val_to_opt_aux TTL_CTR_V);
#endif
}
#endif

#if CL_N_SHARED_SS > 0
/*
 * Pick a new store from the shared stores and prepares all the structures to begin its exploration.
 * prop_v_id will get CL_N_VS if no store is available.
 * shared_ss - All the shared stores
 * shared_ss_flags - flags for each shared store state
 * vs - CSP variables
 * vs_id_to_prop_ - List with the ID of the variables to propagate
 * vs_prop_ - variables on the current state
 * hist - backtracking history
 * hist_tree_level - level of the backtracking
 * hist_labeleds_id - list of the ID of the variables that were labeled for each backtracking history level
 * hist_labeleds_n_vals - number of values that remain on the variables that were labeled for each backtracking history level
 * prop_v_id - ID of the variable that must be propagated
 */
CUDA_FUNC void get_shared_store(
		__global DOMAIN_* shared_ss, __global int* shared_ss_flags, CL_MEMORY unsigned short* vs_id_to_prop_,
		CL_MEMORY VARS_PROP* vs_prop_, __global DOMAIN_* hist, int* hist_tree_level, __global int* hist_labeleds_id,
		__global int* hist_labeleds_n_vals, unsigned int* prop_v_id TTL_CTR) {

	int ss_to_get;

	// flags for signaling the state of each work-sharing store
	// 0 - next shared SS to be picked
	// 1 - next shared SS to be filled
	// 2...number of SS already filled
	// 3..3+CL_N_SHARED_SS - V_ID that was labeled to generate this SS
	if ((ss_to_get = atomic_inc(&shared_ss_flags[0])) < atomic_add(&shared_ss_flags[2], 0)) {

		__global DOMAIN_* new_ss;
		*prop_v_id = convert_uint(shared_ss_flags[3 + ss_to_get]);
		int i;

		// copy the new store
		new_ss = &shared_ss[ss_to_get * CL_N_VS];
		for (i = 0; i < CL_N_VS; i++) {
			CHECK_TTL(ttl_ctr, 35)

			cl_d_copy_mg(&vs_prop_[i].prop_d, &new_ss[i] TTL_CTR_V);
			cl_d_copy_g(&hist[i], &new_ss[i] TTL_CTR_V);

			vs_prop_[i].to_prop = 0;
			cl_v_calc_min_val_m(&vs_prop_[i] TTL_CTR_V);
			cl_v_calc_max_val_m(&vs_prop_[i] TTL_CTR_V);
			cl_v_cnt_vals_m(&vs_prop_[i] TTL_CTR_V);
		}
94b2b13d   Pedro Roque   PHACT source
609
610

		cl_d_clear_g(&hist[*prop_v_id] TTL_CTR_V);
4d26a735   Pedro Roque   Increased recogni...
611
612
613
614
615

		// reset the indexes of the array that contains the IDs of the variables to propagate
		vs_id_to_prop_[0] = 2;
		vs_id_to_prop_[1] = 2;

94b2b13d   Pedro Roque   PHACT source
616
617
		(*hist_tree_level) = 1;
		hist_labeleds_id[0] = convert_int(*prop_v_id);
4d26a735   Pedro Roque   Increased recogni...
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
		(*hist_labeleds_n_vals) = 0;
	}
}

/*
 * Set a new store on the shared stores
 * shared_ss - All the shared stores
 * shared_ss_flags - flags for each shared store state
 * vs_prop_ - variables on the current state
 * v_id - ID of the variable whose domain is to be pruned to create a new shared store
 * hist - backtracking history
 * hist_labeleds_n_vals - number of values that remain on the variables that were labeled for each backtracking history level
 */
CUDA_FUNC void set_shared_store(
		__global DOMAIN_* shared_ss, __global int* shared_ss_flags,
		CL_MEMORY VARS_PROP* vs_prop_, int v_id,
		__global DOMAIN_* hist, __global int* hist_labeleds_n_vals TTL_CTR) {

	int ss_to_fill;

94b2b13d   Pedro Roque   PHACT source
638
	// flags for signaling the state of each work-sharing store
4d26a735   Pedro Roque   Increased recogni...
639
	// 0 - next shared SS to be picked
94b2b13d   Pedro Roque   PHACT source
640
	// 1 - next shared SS to be filled
4d26a735   Pedro Roque   Increased recogni...
641
642
643
	// 2...number of SS already filled
	// 3..3+CL_N_SHARED_SS - V_ID that was labeled to generate this SS
	if ((ss_to_fill = atomic_inc(&shared_ss_flags[1])) < CL_N_SHARED_SS) {
94b2b13d   Pedro Roque   PHACT source
644

4d26a735   Pedro Roque   Increased recogni...
645
646
647
		__global DOMAIN_* new_ss;
		int min;
		int i;
94b2b13d   Pedro Roque   PHACT source
648
649
650

		// copy the new store
		new_ss = &shared_ss[ss_to_fill * CL_N_VS];
4d26a735   Pedro Roque   Increased recogni...
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
		for (i = 0; i < CL_N_VS; i++) {
			CHECK_TTL(ttl_ctr, 33)

			cl_d_copy_gm(&new_ss[i], &vs_prop_[i].prop_d TTL_CTR_V);
		}

		cl_d_calc_min_val_g(&min, hist TTL_CTR_V);

		// remove value from backtracking history
		cl_d_del_val_no_tests_g(hist, min);
		(*hist_labeleds_n_vals)--;

		cl_d_new_vals_gp(&new_ss[v_id], &min, 1 TTL_CTR_V);

		// save the ID of the variable that was labeled to generate the new SS
		shared_ss_flags[3 + ss_to_fill] = v_id;

		atomic_inc(&shared_ss_flags[2]);
	}
}
#endif

#if CL_CS_IGNORE
CUDA_FUNC void clear_cs_ignore(__global char* cs_ignore) {
94b2b13d   Pedro Roque   PHACT source
675
	int i;
94b2b13d   Pedro Roque   PHACT source
676

4d26a735   Pedro Roque   Increased recogni...
677
678
679
680
	for (i = 0; i < CL_N_CS; i++) {
		cs_ignore[i] = 0;
	}
}
94b2b13d   Pedro Roque   PHACT source
681
682
683
#endif

#if CL_PRINT_CSP
4d26a735   Pedro Roque   Increased recogni...
684

94b2b13d   Pedro Roque   PHACT source
685
/*
4d26a735   Pedro Roque   Increased recogni...
686
687
688
689
 * Print the name of the constraint
 * cs -constraint to print the name
 */
CUDA_FUNC void cs_print_type_device(CL_CS_MEM cl_constr* cs) {
94b2b13d   Pedro Roque   PHACT source
690
	switch (cs->kind) {
4d26a735   Pedro Roque   Increased recogni...
691
692
693
694
695
696
		case ALL_DIFFERENT:
			printf((__constant char *)"ALL_DIFFERENT");
			break;
		case AT_LEAST:
			printf((__constant char *)"AT_LEAST");
			break;
94b2b13d   Pedro Roque   PHACT source
697
698
		case AT_MOST:
			printf((__constant char *)"AT_MOST");
4d26a735   Pedro Roque   Increased recogni...
699
700
701
702
703
			break;
		case AT_MOST_ONE:
			printf((__constant char *)"AT_MOST_ONE");
			break;
		case ELEMENT:
94b2b13d   Pedro Roque   PHACT source
704
705
			printf((__constant char *)"ELEMENT");
			break;
4d26a735   Pedro Roque   Increased recogni...
706
		case ELEMENT_INT_VAR:
94b2b13d   Pedro Roque   PHACT source
707
			printf((__constant char *)"ELEMENT_INT_VAR");
4d26a735   Pedro Roque   Increased recogni...
708
709
710
711
712
713
			break;
		case ELEMENT_VAR:
			printf((__constant char *)"ELEMENT_VAR");
			break;
		case EQ:
			printf((__constant char *)"EQ");
94b2b13d   Pedro Roque   PHACT source
714
			break;
4d26a735   Pedro Roque   Increased recogni...
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
		case EQ_VAR:
			printf((__constant char *)"EQ_VAR");
			break;
		case EXACTLY:
			printf((__constant char *)"EXACTLY");
			break;
		case EXACTLY_VAR:
			printf((__constant char *)"EXACTLY_VAR");
			break;
		case LE:
			printf((__constant char *)"LE");
			break;
		case LINEAR:
			printf((__constant char *)"LINEAR");
			break;
		case LINEAR_VAR:
			printf((__constant char *)"LINEAR_VAR");
			break;
		case LT:
			printf((__constant char *)"LT");
			break;
		case MAX:
			printf((__constant char *)"MAX");
			break;
		case MAXIMIZE:
94b2b13d   Pedro Roque   PHACT source
740
			printf((__constant char *)"MAXIMIZE");
4d26a735   Pedro Roque   Increased recogni...
741
			break;
94b2b13d   Pedro Roque   PHACT source
742
		case MIN:
4d26a735   Pedro Roque   Increased recogni...
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
			printf((__constant char *)"MIN");
			break;
		case MINIMIZE:
			printf((__constant char *)"MINIMIZE");
			break;
		case MINUS_EQ:
			printf((__constant char *)"MINUS_EQ");
			break;
		case MINUS_NE:
			printf((__constant char *)"MINUS_NE");
			break;
		case NE:
			printf((__constant char *)"NE");
			break;
		case SUM:
			printf((__constant char *)"SUM");
			break;
		case SUM_PROD:
			printf((__constant char *)"SUM_PROD");
			break;
		case SUM_VAR:
			printf((__constant char *)"SUM_VAR");
			break;
		case VAR_EQ_MINUS:
			printf((__constant char *)"VAR_EQ_MINUS");
			break;
		case VAR_EQ_PLUS:
			printf((__constant char *)"VAR_EQ_PLUS");
			break;
		case VAR_EQ_TIMES:
			printf((__constant char *)"VAR_EQ_TIMES");
			break;
		case VAR_EQ_MINUS_ABS:
94b2b13d   Pedro Roque   PHACT source
776
777
			printf((__constant char *)"VAR_EQ_MINUS_ABS");
			break;
4d26a735   Pedro Roque   Increased recogni...
778
779
780
781
782
		case LINEAR_NE:
			printf((__constant char *)"LINEAR_NE");
			break;
		case LINEAR_LT:
			printf((__constant char *)"LINEAR_LT");
94b2b13d   Pedro Roque   PHACT source
783
784
			break;
		case BOOL_OR:
4d26a735   Pedro Roque   Increased recogni...
785
786
			printf((__constant char *)"BOOL_OR");
			break;
94b2b13d   Pedro Roque   PHACT source
787
		case BOOL_AND:
4d26a735   Pedro Roque   Increased recogni...
788
789
790
791
792
793
			printf((__constant char *)"BOOL_AND");
			break;
		case BOOL_CLAUSE:
			printf((__constant char *)"BOOL_CLAUSE");
			break;
		case BOOL2INT:
94b2b13d   Pedro Roque   PHACT source
794
			printf((__constant char *)"BOOL2INT");
4d26a735   Pedro Roque   Increased recogni...
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
			break;
		default:
			printf((__constant char *)"NOT_RECOGNIZED");
			break;
	}

	if (cs->reified) {
		printf((__constant char *)"_REIF");
	}
}

/*
 * Print all the CSP variables and constraints, including the variables values and ID and
 * the constraints ID and the ID of the variables that they constrain
 * vs - CSP variables
 * cs - CSP constraints
 * cs_per_v_idx - vector with all the constraints ID that constrain each variable, per variable ID order
 * vs_per_c_idx - vector with all constrained variables ID per constraint, per constraint ID order
 * c_consts - vector with all the constant values of a CSP
 */
CUDA_FUNC void print_CSP_device(CL_VS_MEM VARS* vs, CL_CS_MEM cl_constr* cs, CL_INTS_MEM int* cs_per_v_idx, CL_INTS_MEM int* vs_per_c_idx
94b2b13d   Pedro Roque   PHACT source
816
817
#if CS_AT_LEAST == 1 || CS_AT_MOST == 1 || CS_AT_MOST_ONE == 1 || CS_EXACTLY == 1 || CS_LINEAR == 1 || CS_LINEAR_LT == 1 || CS_LINEAR_NE == 1 || CS_LINEAR_VAR == 1 || CS_ELEMENT_INT_VAR == 1
		, CL_INTS_MEM int* c_consts
94b2b13d   Pedro Roque   PHACT source
818
#endif
4d26a735   Pedro Roque   Increased recogni...
819
820
821
822
823
824
825
#if CL_D_TYPE == CL_BITMAP
		, CL_B_DS_MEM cl_bitmap* b_ds
#endif
		) {

	unsigned int prev_val;
	unsigned int new_val;
94b2b13d   Pedro Roque   PHACT source
826
827
828
	unsigned int cntr;
	int d_vals[CL_D_MAX + 1];
	int n_vals, max;
4d26a735   Pedro Roque   Increased recogni...
829
	CL_INTS_MEM int* vs_per_c_idx_;
94b2b13d   Pedro Roque   PHACT source
830
831
832
833
834
835
	CL_INTS_MEM int* cs_per_v_idx_;
	DOMAIN_ d;
	unsigned int i;
	int j;

#if CS_AT_LEAST == 1 || CS_AT_MOST == 1 || CS_AT_MOST_ONE == 1 || CS_EXACTLY == 1 || CS_LINEAR == 1 || CS_LINEAR_LT == 1 || CS_LINEAR_NE == 1 || CS_LINEAR_VAR == 1 || CS_ELEMENT_INT_VAR == 1
4d26a735   Pedro Roque   Increased recogni...
836
837
838
839
840
841
	CL_INTS_MEM int* c_consts_;
#endif

	printf((__constant char *)"\n\n--------------------------------\n");
	printf((__constant char *)"CSP as received by the device:\n");

94b2b13d   Pedro Roque   PHACT source
842
843
	printf((__constant char *)"\nVariables:\n");
	for (i = 0; i < CL_N_VS; i++) {
4d26a735   Pedro Roque   Increased recogni...
844
845
846
847
848
		printf((__constant char *)"   ID=%u:  Values={", i);

#if CL_D_TYPE == CL_BITMAP
#if CL_N_WORDS == 1
		d = b_ds[i];
94b2b13d   Pedro Roque   PHACT source
849
850
#else
		for (j = 0; j < CL_N_WORDS; j++) {
4d26a735   Pedro Roque   Increased recogni...
851
			CHECK_TTL(ttl_ctr, 117)
94b2b13d   Pedro Roque   PHACT source
852
			d[j] = b_ds[i][j];
4d26a735   Pedro Roque   Increased recogni...
853
854
		}
#endif
94b2b13d   Pedro Roque   PHACT source
855

4d26a735   Pedro Roque   Increased recogni...
856
857
858
859
		cl_d_cnt_vals_n(&d, &n_vals);
		cl_d_calc_max_val_n(&max, &d);
		cl_d_get_nth_vals_n(&d, 1, n_vals, d_vals TTL_CTR_V);

94b2b13d   Pedro Roque   PHACT source
860
		j = 0;
4d26a735   Pedro Roque   Increased recogni...
861
862
863

		prev_val = convert_uint(d_vals[j++]);
		new_val = prev_val;
94b2b13d   Pedro Roque   PHACT source
864
		printf((__constant char *)"%u", prev_val);
4d26a735   Pedro Roque   Increased recogni...
865
		while (new_val < (uint)max) {
94b2b13d   Pedro Roque   PHACT source
866

4d26a735   Pedro Roque   Increased recogni...
867
			cntr = 0;
94b2b13d   Pedro Roque   PHACT source
868
			while ((new_val = convert_uint(d_vals[j++])) == prev_val + 1 && new_val < (uint)max) {
4d26a735   Pedro Roque   Increased recogni...
869
				prev_val = new_val;
94b2b13d   Pedro Roque   PHACT source
870
871
				cntr++;
			}
4d26a735   Pedro Roque   Increased recogni...
872

94b2b13d   Pedro Roque   PHACT source
873
			if (cntr == 0) {
4d26a735   Pedro Roque   Increased recogni...
874
875
876
877
878
879
				printf((__constant char *)",%u", new_val);
			} else {
				if (new_val == (uint)max && new_val == prev_val + 1) {
					printf((__constant char *)"-%u", new_val);
				} else if (cntr == 1) {
					printf((__constant char *)",%u,%u", prev_val, new_val);
94b2b13d   Pedro Roque   PHACT source
880
881
				} else {
					printf((__constant char *)"-%u,%u", prev_val, new_val);
4d26a735   Pedro Roque   Increased recogni...
882
883
884
885
886
				}
			}
			prev_val = new_val;
		}

94b2b13d   Pedro Roque   PHACT source
887
888
#else
		printf((__constant char *)"%u,%u", vs[i].domain.s0, vs[i].domain.s1);
4d26a735   Pedro Roque   Increased recogni...
889
#endif
94b2b13d   Pedro Roque   PHACT source
890

4d26a735   Pedro Roque   Increased recogni...
891
892
		if (vs[i].to_label) {
			printf((__constant char *)"}  Label=true");
94b2b13d   Pedro Roque   PHACT source
893
		} else {
4d26a735   Pedro Roque   Increased recogni...
894
895
896
897
898
899
900
901
902
			printf((__constant char *)"}  Label=false");
		}

		if (vs[i].expanded) {
			printf((__constant char *)"  expanded");
		}

		if (vs[i].n_cs > 0) {
			printf((__constant char *)"  Constraints={");
94b2b13d   Pedro Roque   PHACT source
903

4d26a735   Pedro Roque   Increased recogni...
904
			cs_per_v_idx_ = &cs_per_v_idx[vs[i].c_idx];
94b2b13d   Pedro Roque   PHACT source
905

4d26a735   Pedro Roque   Increased recogni...
906
			for (j = 0; j < vs[i].n_cs - 1; j++) {
94b2b13d   Pedro Roque   PHACT source
907
908
909
910
911
912
				printf((__constant char *)"%u,", cs_per_v_idx_[j]);
			}
			if (vs[i].n_cs > 0) {
				printf((__constant char *)"%u}\n", cs_per_v_idx_[j]);
			} else {
				printf((__constant char *)"\n");
4d26a735   Pedro Roque   Increased recogni...
913
914
915
916
917
918
			}
		} else {
			printf((__constant char *)"\n");
		}
	}

94b2b13d   Pedro Roque   PHACT source
919
920
	printf((__constant char *)"Constraints:\n");
	for (i = 0; i < CL_N_CS; i++) {
4d26a735   Pedro Roque   Increased recogni...
921
922
923
924
925

		vs_per_c_idx_ = &vs_per_c_idx[cs[i].v_idx];

#if CS_AT_LEAST == 1 || CS_AT_MOST == 1 || CS_AT_MOST_ONE == 1 || CS_EXACTLY == 1 || CS_LINEAR == 1 || CS_LINEAR_LT == 1 || CS_LINEAR_NE == 1 || CS_LINEAR_VAR == 1 || CS_ELEMENT_INT_VAR == 1
		c_consts_ = &c_consts[cs[i].const_idx];
94b2b13d   Pedro Roque   PHACT source
926
927
#endif

4d26a735   Pedro Roque   Increased recogni...
928
		printf((__constant char *)"   ID=%u:  Type=", i);
94b2b13d   Pedro Roque   PHACT source
929
		cs_print_type_device(&cs[i]);
4d26a735   Pedro Roque   Increased recogni...
930
931
		printf((__constant char *)"  Variables={");
		for (j = 0; j < cs[i].n_c_vs - 1; j++) {
94b2b13d   Pedro Roque   PHACT source
932
			printf((__constant char *)"%u,", vs_per_c_idx_[j]);
4d26a735   Pedro Roque   Increased recogni...
933
934
935
		}
		printf((__constant char *)"%u}", vs_per_c_idx_[j]);

94b2b13d   Pedro Roque   PHACT source
936
		if (cs[i].n_c_consts > 0) {
4d26a735   Pedro Roque   Increased recogni...
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982

#if CS_AT_LEAST == 1 || CS_AT_MOST == 1 || CS_AT_MOST_ONE == 1 || CS_EXACTLY == 1 || CS_LINEAR == 1 || CS_LINEAR_LT == 1 || CS_LINEAR_NE == 1 || CS_LINEAR_VAR == 1 || CS_ELEMENT_INT_VAR == 1
			printf((__constant char *)"  Constants={");
			for (j = 0; j < cs[i].n_c_consts - 1; j++) {
				printf((__constant char *)"%d,", c_consts_[j]);
			}

			if (cs[i].kind == LINEAR || cs[i].kind == LINEAR_NE || cs[i].kind == LINEAR_LT) {

				printf((__constant char *)"%d, %d}", c_consts_[j], cs[i].constant_val);
			} else {
				printf((__constant char *)"%d}", c_consts_[j]);
			}
#endif

		} else if (cs[i].kind == ELEMENT || cs[i].kind == EXACTLY_VAR || cs[i].kind == LINEAR || cs[i].kind == LINEAR_NE || cs[i].kind == MINUS_EQ
				|| cs[i].kind == MINUS_NE || cs[i].kind == SUM_PROD || cs[i].kind == SUM || cs[i].kind == LINEAR_LT || cs[i].kind == ELEMENT_INT_VAR
				|| cs[i].kind == EQ) {
			printf((__constant char *)"  Constants={%d}", cs[i].constant_val);
		}

		if (cs[i].boolean) {
			printf("  boolean");
		}

		if (cs[i].reified) {
			printf((__constant char *)"  reif_var_ID=%u\n", cs[i].reif_var_id);
		} else {
			printf((__constant char *)"\n");
		}
	}
	printf((__constant char *)"\n");
	printf((__constant char *)"--------------------------------\n\n");
}
#endif

// strs:
// 0...CL_N_VS - strs
// repeat n_strs times
//
// hists:
// 0 - labeled variable for the next level
// 1 - remaining number of values on the labeled variable
// 2...CL_N_VS - level domains
// 2+CL_N_VS - labeled variable for the next level
// 3+CL_N_VS...3+CL_N_VS+CL_N_VS - level domains
94b2b13d   Pedro Roque   PHACT source
983
984
985
986
987
988
989
990
991
992
993
// ...
/*
 * place the next unexplored sub-search space available on hist and return 1 if succeeded or 0 if not
 * strs - vector with all stores
 * str_first - index of the next store to pick
 * vs - all CSP variables with the original domains
 * hist - current work-item history vector
 * n_ss - number of sub-search spaces (stores) in strs
 * depth - main search space exploration depth needed to fill all stores
 */
CUDA_FUNC void get_new_str( __global unsigned int* atoms,
4d26a735   Pedro Roque   Increased recogni...
994
995
996
997
998
999
		CL_VS_MEM VARS* vs,
		__global DOMAIN_* hist,
		CL_MEMORY VARS_PROP* vs_prop_, CL_MEMORY unsigned short* vs_id_to_prop_, int* hist_tree_level, __global int* hist_labeleds_id, __global int* hist_labeleds_n_vals,
		unsigned int* prop_v_id
#if CL_D_TYPE == CL_BITMAP
		, CL_B_DS_MEM cl_bitmap* b_ds
94b2b13d   Pedro Roque   PHACT source
1000
1001
#endif
#if (CS_MAXIMIZE == 1 || CS_MINIMIZE == 1) && CL_WORK == CL_OPT
4d26a735   Pedro Roque   Increased recogni...
1002
1003
1004
1005
1006
		, __global unsigned int* val_to_opt_g
#endif
		,__global int* ss_aux_mem TTL_CTR) {

	// atoms
94b2b13d   Pedro Roque   PHACT source
1007
1008
	__global unsigned int* str_to_expl = atoms;	// first available store index
	unsigned int str_last = atoms[1];	// last available store index
4d26a735   Pedro Roque   Increased recogni...
1009
1010
	unsigned int n_ss = atoms[2];	// total number of generated sub-search spaces (n_ss)
	unsigned int depth = atoms[3];	// level of tree expansion needed to create the required number of sub-search spaces
94b2b13d   Pedro Roque   PHACT source
1011
1012
	__global unsigned int* exp_values = atoms + 5;	// each variable number of values expanded

4d26a735   Pedro Roque   Increased recogni...
1013
	unsigned int prev_repeat = 0;
94b2b13d   Pedro Roque   PHACT source
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
	unsigned int curr_repeat = 0;
	unsigned int rel_store_to_synt;
	unsigned int store_to_synt = atomic_inc(str_to_expl);	// increase the next store to pick index
	unsigned int join_n_vals;
	unsigned int join_more_times;
	int first_nth;
	int last_nth;
	bool first_expand = true;
	unsigned int i;

	*prop_v_id = CL_N_VS;

94b2b13d   Pedro Roque   PHACT source
1026
1027
1028
1029
1030
1031
	if (store_to_synt < str_last) {

		for (i = 0; i < depth; i++) {
			CHECK_TTL(ttl_ctr, 4)

				if (exp_values[i] == 0) {
4d26a735   Pedro Roque   Increased recogni...
1032
#if CL_D_TYPE == CL_BITMAP
94b2b13d   Pedro Roque   PHACT source
1033
					cl_d_copy_mbd(&vs_prop_[i].prop_d, &b_ds[i] TTL_CTR_V);
4d26a735   Pedro Roque   Increased recogni...
1034
#else
94b2b13d   Pedro Roque   PHACT source
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
					cl_d_copy_mvs(&vs_prop_[i].prop_d, &vs[i].domain TTL_CTR_V);
#endif

				} else {

					if (first_expand) {
						prev_repeat = n_ss;
						rel_store_to_synt = store_to_synt;
						first_expand = false;
					} else {
						rel_store_to_synt = store_to_synt % prev_repeat;
					}
					curr_repeat = prev_repeat / exp_values[i];

					join_n_vals = vs[i].n_vals / exp_values[i];
					if (join_n_vals == 0) {
						join_n_vals = 1;
					}
					join_more_times = (vs[i].n_vals % exp_values[i]) * curr_repeat;

					first_nth = convert_int(1 + (rel_store_to_synt / curr_repeat) * join_n_vals);
4d26a735   Pedro Roque   Increased recogni...
1056

94b2b13d   Pedro Roque   PHACT source
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
					if (join_more_times > 0) {
						if (rel_store_to_synt > join_more_times) {
							first_nth += convert_int(join_more_times / curr_repeat);
						} else {
							first_nth += convert_int(rel_store_to_synt / curr_repeat);
						}
					}

					last_nth = first_nth + convert_int(join_n_vals);
					if (rel_store_to_synt < join_more_times) {
						last_nth++;
					}

					prev_repeat = curr_repeat;

#if CL_D_TYPE == CL_BITMAP
					cl_d_get_nth_vals_bd(&b_ds[i], first_nth, last_nth - 1, ss_aux_mem TTL_CTR_V);
#else
					cl_d_get_nth_vals_vsg(&vs[i].domain, first_nth, last_nth - 1, ss_aux_mem TTL_CTR_V);
#endif
					cl_d_new_vals_mg(&vs_prop_[i].prop_d, ss_aux_mem, last_nth - first_nth TTL_CTR_V);
				}
		}

		for (i = depth; i < CL_N_VS; i++) {
			CHECK_TTL(ttl_ctr, 6)

#if CL_D_TYPE == CL_BITMAP
			cl_d_copy_mbd(&vs_prop_[i].prop_d, &b_ds[i] TTL_CTR_V);
4d26a735   Pedro Roque   Increased recogni...
1086
#else
94b2b13d   Pedro Roque   PHACT source
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
			cl_d_copy_mvs(&vs_prop_[i].prop_d, &vs[i].domain TTL_CTR_V);
#endif
		}

#if CL_WORK == CL_OPT
		unsigned int val_to_opt_aux = atomic_add(val_to_opt_g, 0);
		bool changed = 0;

		vs_prop_[CL_VAR_ID_TO_OPT].to_prop = 0;
#if CL_BOOLEAN_VS
		vs_prop_[CL_VAR_ID_TO_OPT].boolean = vs[CL_VAR_ID_TO_OPT].boolean;
#endif
		cl_v_calc_min_val_m(&vs_prop_[CL_VAR_ID_TO_OPT] TTL_CTR_V);
		cl_v_calc_max_val_m(&vs_prop_[CL_VAR_ID_TO_OPT] TTL_CTR_V);
		cl_v_cnt_vals_m(&vs_prop_[CL_VAR_ID_TO_OPT] TTL_CTR_V);

#if CS_MINIMIZE == 1
		cl_v_del_gt_m(&changed, &vs_prop_[CL_VAR_ID_TO_OPT], convert_int(val_to_opt_aux) TTL_CTR_V);
#else
		cl_v_del_lt_m(&changed, &vs_prop_[CL_VAR_ID_TO_OPT], convert_int(val_to_opt_aux) TTL_CTR_V);
4d26a735   Pedro Roque   Increased recogni...
1107
#endif
94b2b13d   Pedro Roque   PHACT source
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
#endif

		for (i = 0; i < CL_N_VS; i++) {
			CHECK_TTL(ttl_ctr, 7)

		    cl_d_copy_gm(&hist[i], &vs_prop_[i].prop_d TTL_CTR_V);

			vs_prop_[i].to_prop = 0;
#if CL_BOOLEAN_VS
			vs_prop_[i].boolean = vs[i].boolean;
#endif
			cl_v_calc_min_val_m(&vs_prop_[i] TTL_CTR_V);
			cl_v_calc_max_val_m(&vs_prop_[i] TTL_CTR_V);
			cl_v_cnt_vals_m(&vs_prop_[i] TTL_CTR_V);
		}

		*prop_v_id = 0;
		for (i = 0; i < depth; i++) {
4d26a735   Pedro Roque   Increased recogni...
1126
1127
1128
1129
			CHECK_TTL(ttl_ctr, 8)

			if (vs[i].to_label || vs[i].expanded) {
				*prop_v_id = i;
94b2b13d   Pedro Roque   PHACT source
1130
				i = depth;
4d26a735   Pedro Roque   Increased recogni...
1131
1132
			}
		}
94b2b13d   Pedro Roque   PHACT source
1133

4d26a735   Pedro Roque   Increased recogni...
1134
1135
1136
		hist_labeleds_id[0] = convert_int(*prop_v_id);
		hist_labeleds_n_vals[0] = 0;
		(*hist_tree_level) = 1; // reset hist current level
94b2b13d   Pedro Roque   PHACT source
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147

		// reset the indexes of the array that contains the IDs of the variables to propagate
		vs_id_to_prop_[0] = 2;
		vs_id_to_prop_[1] = 2;

		// if optimizing set variable to optimize for being propagated to update its domain
#if CL_WORK == CL_OPT
		v_add_to_prop(vs_id_to_prop_, vs_prop_, convert_int(*prop_v_id));
		*prop_v_id = CL_VAR_ID_TO_OPT;
#endif

4d26a735   Pedro Roque   Increased recogni...
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
		// Mark variables already expanded to be propagated to check if this sub-search space is already inconsistent
		for (i = 0; i < CL_N_VS; i++) {
			CHECK_TTL(ttl_ctr, 9)

#if CL_FILTERING == 0
			if (i != *prop_v_id && (vs[i].expanded || (i < depth && exp_values[i] > 0))) {
#endif
				v_add_to_prop(vs_id_to_prop_, vs_prop_, convert_int(i));

#if CL_FILTERING == 0
			}
#endif
		}

#if CL_CHECK_ERRORS
		int l;
		for (l = 0; l < CL_N_VS; l++) {

			bool empty;
#if CL_N_WORDS == 1
			empty = (vs_prop_[l].prop_d == 0);
#else
			int m;
			empty = 1;
			for (m = 0; m < CL_N_WORDS; m++) {
				CHECK_TTL(ttl_ctr, 115)
				if (vs_prop_[l].prop_d[m] != 0) {
					empty = 0;
					m = CL_N_WORDS;
				}
			}
#endif
			if (empty || vs_prop_[l].n_vals > vs_prop_[l].max + 1 || vs_prop_[l].min > vs_prop_[l].max || vs_prop_[l].max > CL_D_MAX) {
				printf((__constant char *)"\n###error 74\n");
				printf((__constant char *)"v_id=%d, empty=%d, vs_prop_[l].n_vals=%u, vs_prop_[l].min=%u, vs_prop_[l].max=%u\n",
						l, empty, vs_prop_[l].n_vals, vs_prop_[l].min, vs_prop_[l].max);
			}
		}
#endif
	}
}
94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source