config.c
25.8 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
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
740
741
742
/*
* config.c
*
* Created on: 03/11/2014
* Author: pedro
*/
#include "config.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <stdint.h>
#define X_OK 1 /* execute permission - unsupported in windows*/
#else
#include <unistd.h>
#endif
#include "bitmaps.h"
#include "devices.h"
#include "variables.h"
#include "kernels/cl_constraints.h"
label_heur LABEL_MODE_D = INPUT_ORDER; // argument indicating the default mode for selecting the variable to label
assign_heur ASSIGN_MODE_D = MIN_VAL; // argument indicating the default mode for selecting the value to assign to the variable for labeling
work WORK_D = ONE; // argument indicating if one solution, all solutions or optimization is default
// to be used when modeling the CSP for printing results
bool OPTIMIZING = false; // true if optimizing
bool FINDING_ONE_SOLUTION = false; // true if finding one solution
bool COUNTING_SOLUTIONS = false; // true if counting all the solutions
bool PRINT_STATS = false; // True if statistic data should be collected and printed
statistics STATS; // statistics data on host
label_heur LABEL_MODE = DEFAULT_L; // argument indicating the mode for selecting the variable to label
assign_heur ASSIGN_MODE = DEFAULT_A; // argument indicating the mode for selecting the value to assign to the variable for labeling
label_heur LABEL_MODE_COM = DEFAULT_L; // argument indicating the mode for selecting the variable to label inserted in command line
assign_heur ASSIGN_MODE_COM = DEFAULT_A; // argument indicating the mode for selecting the value to assign to the variable for labeling inserted in command line
work WORK = DEFAULT_W; // argument indicating if one solution, all solutions or optimization is wanted
opt OPT_MODE = NONE; // if optimization is to reduce a value, or to increase it
bool ALL_DEVS = false; // argument if all devices should be used
bool VERBOSE = false; // argument indicating if the Solver must print info
bool QUIET = false; // argument indicating if the Solver must print only the number of solutions that were found, or the solution or the best solution
char* CSP = NULL; // argument indicating the CSP problem to solve
char* FZN_FILE_NAME = NULL; // argument indicating the name of the flatzinc file
char* MZN_FILE_NAME = NULL; // argument indicating the name of the minizinc file
char* DZN_FILE_NAME = NULL; // argument indicating the name of the minizinc file containing the CSP dimensions
unsigned int D_MAX = 0; // maximum domain value on CSP variables (start at 0)
unsigned int D_MIN = UINT32_MAX; // minimum domain value on variables CSP
unsigned int CL_BITS_ = 32; // number of bits to use on devices, calculated after creating all the CSP variables in init_csp_and_d_bits() in config.c
unsigned int CL_WORD_ = 0; // number of bits in one word of the bitmap use on devices, calculated after creating all the CSP variables in init_csp_and_d_bits() in config.c
unsigned int CL_N_WORDS_; // number of words that compose one bitmap on the devices, calculated after creating all the CSP variables in init_csp_and_d_bits() in config.c
unsigned int N_DEVS = 0; // number of devices to use
unsigned int N_GPUs = 0; // requested number of GPUs to use
unsigned int N_CPUs = 0; // requested number of CPUs to use
unsigned int N_ACCs = 0; // requested number of ACCs to use
unsigned int N_SS = 0; // optional number of sub-search spaces to create
cl_uint VAL_TO_OPT; // current minimum or maximum value to optimize (not yet found)
unsigned int VAR_ID_TO_OPT = 0; // ID of the variable to optimize
bool PRINT_SOLUTIONS = false; // set to 1 to print all solutions (the value of each CSP variable) when using only one work-item
bool PRINT_CSP = false; // set to 1 to print all CSP variables with their domains, and all the constraints with the respective variables identified
bool MZN2FZN_ONLY = false; // if the MZN must be converted to FZN, but without solving the CSP
bool REV = 0; // set to 1 to propagate the last labeled variable with all the remaining values before backtracking it, or 0 to not
int N_VS_TO_LABEL = 0; // Number of variables marked for labeling
bool BOOLEAN_VS = 0; // 1 if the CSP use boolean variables
unsigned int N_VS_ORIGINAL = 0; // number of CSP variables in the model (before filtering)
unsigned int N_CS_ORIGINAL = 0; // number of CSP constraints in the model (before filtering)
bool CAN_USE_INTERVALS = 1; // true if variable domains are contiguous
bool CS_IGNORE = USE_CS_IGNORE; // 1 to when constraint is fixed or its propagator is unable to propagate more, ignore it in the following propagations
// only for bitmap domains (not for intervals)
d_type DOMAIN_TYPE = BITMAP_; // domain representation to use
size_t DOMAIN_SIZE; // size of the domain type used on this device
__time_t init_sec; // Seconds when the Solver started
__suseconds_t init_usec; // Microseconds when the Solver started
bool USE_CS[N_C_TYPES]; // flags for compiling each constraint type in kernel
bool USE_CS_REIFI[N_C_TYPES]; // flags for compiling reification for the constraint types that use it
bool USE_NON_CS_REIFI[N_C_TYPES]; // flags for printing the stats of used constraints
int* CONST_VS_ID; // to store the id of the variables that have only one value on the domain
unsigned int N_VS = 256; // number of CSP variables
unsigned int N_CS = 256; // number of CSP constraints
unsigned int* EXP_VALUES; // Number of values expanded to achieve the required number of sub-search spaces
var* VS; // Vector with all the CSP variables
var* VS_LOCK; // Vector with temporary CSP variables when optimizing
var* VS_LOCK_BEST; // Vector with the current best solution when optimizing
var* VS_AUX; // Vector with all the CSP variables for increasing size and sorting
var* VS_AUX2; // Vector with all the CSP variables for sorting
constr* CS; // Vector with all the CSP constraints
constr* CS_AUX; // Vector with all the CSP constraints for increasing size
unsigned int V_ID_CNTR = 0; // Unique identifier of each CSP variable
unsigned int C_ID_CNTR = 0; // Unique identifier of each CSP constraint
device_info DEVICES_INFO[MAX_DEVS]; // Information of the devices to use
device_args DEVICES_ARGS[MAX_DEVS]; // Device arguments (buffers, etc.)
/*
* Parse execution arguments to initialize extern variables
*/
void parse_args(int *argc, char *argv[], int* csp_dims, char* csp_inst_name) {
int args = *argc;
int i, j;
int k = 0;
int csp_dim_arg_ctr = 0;
int params_leng;
for (i = 0; i < MAX_DEVS; i++) {
DEVICES_INFO[i].n_ss_mult = 1;
}
for (i = 1; i < args; i++) {
if (!strcmp(argv[i], "-COUNT")) {
COUNTING_SOLUTIONS = true;
WORK = CNT;
} else if (!strcmp(argv[i], "-ONE")) {
FINDING_ONE_SOLUTION = true;
WORK = ONE;
} else if (!strcmp(argv[i], "-OPT")) {
OPTIMIZING = true;
WORK = OPT;
} else if (isdigit(*argv[i])) {
if (csp_dim_arg_ctr + 1 == MAX_CSP_DIM_ARGS) {
fprintf(stderr, "\nToo many dimension arguments. Please increase \"MAX_CSP_DIM_ARGS\" value.'\n");
exit(-1);
}
csp_dims[csp_dim_arg_ctr++] = atoi(argv[i]);
} else if (isdigit(*argv[i])) {
if (csp_dim_arg_ctr + 1 == MAX_CSP_DIM_ARGS) {
fprintf(stderr, "\nToo many dimension arguments. Please increase \"MAX_CSP_DIM_ARGS\" value.'\n");
exit(-1);
}
csp_dims[csp_dim_arg_ctr++] = atoi(argv[i]);
} else if (!strcmp(argv[i], "-N-SS")) {
N_SS = (unsigned int)atoi(argv[++i]);
} else if (!strcmp(argv[i], "-E")) {
CSP = malloc((strlen(argv[++i]) + 1) * sizeof(char));
strcpy(CSP, argv[i]);
} else if (!strcmp(argv[i], "-V")) {
VERBOSE = true;
} else if (!strcmp(argv[i], "-Q")) {
QUIET = true;
} else if (!strcmp(argv[i], "-INPUT-ORDER")) {
LABEL_MODE_COM = INPUT_ORDER;
} else if (!strcmp(argv[i], "-OCCURRENCE")) {
LABEL_MODE_COM = OCCURRENCE;
} else if (!strcmp(argv[i], "-FIRST-FAIL")) {
LABEL_MODE_COM = FIRST_FAIL;
} /*else if (!strcmp(argv[i], "-MAX-REGRET")) {
LABEL_MODE_COM = MAX_REGRET;
} else if (!strcmp(argv[i], "-SMALLEST")) {
LABEL_MODE_COM = SMALLEST;
}*/ else if (!strcmp(argv[i], "-MIN-VALUE")) {
ASSIGN_MODE_COM = MIN_VAL;
} else if (!strcmp(argv[i], "-MAX-VALUE")) {
ASSIGN_MODE_COM = MAX_VAL;
} else if (!strcmp(argv[i], "-SPLIT-VALUES")) {
ASSIGN_MODE_COM = SPLIT_VALS;
} else if (!strcmp(argv[i], "-SMALLEST")) {
LABEL_MODE_COM = SMALLEST;
} else if (!strcmp(argv[i], "-STATS")) {
PRINT_STATS = true;
} else if (!strcmp(argv[i], "-INTERVALS")) {
DOMAIN_TYPE = INTERVAL;
} else if (!strcmp(argv[i], "-PRINT-SOLUTIONS")) {
PRINT_SOLUTIONS = true;
} else if (!strcmp(argv[i], "-PRINT-CSP")) {
PRINT_CSP = true;
} else if (!strcmp(argv[i], "-MZN2FZN-ONLY")) {
MZN2FZN_ONLY = true;
} else if (!strcmp(argv[i], "-D")) {
N_DEVS++;
i++;
params_leng = (int)strlen(argv[i]);
char* params = malloc(((unsigned long)params_leng + 1) * sizeof(char));
strcpy(params, argv[i]);
if (params[0] == 'G') {
DEVICES_INFO[k].type = CL_DEVICE_TYPE_GPU;
N_GPUs++;
} else if (params[0] == 'C') {
DEVICES_INFO[k].type = CL_DEVICE_TYPE_CPU;
N_CPUs++;
} else if (params[0] == 'A') {
DEVICES_INFO[k].type = CL_DEVICE_TYPE_ACCELERATOR;
N_ACCs++;
} else {
fprintf(stderr, "\nInvalid argument '%s'\n", params);
print_help();
free(params);
exit(-1);
}
DEVICES_INFO[k].dev_type_n = 0;
DEVICES_INFO[k].n_wg = 0;
DEVICES_INFO[k].n_wi_wg = 0;
if (params_leng > 3) {
j = 3;
// if : get number of device of this type to use (1...)
if (j < params_leng && params[j] == ':') {
DEVICES_INFO[k].dev_type_n = atoi(params + j + 1);
j += 1 + get_int_len(DEVICES_INFO[k].dev_type_n);
}
// if / use all the devices of this type
if (j < params_leng && params[j] == '/') {
// if / use the default number of work-groups for this device
if (params[j + 1] == '/') {
j++;
DEVICES_INFO[k].n_wg = 0;
// if something else capture the number of work-items per work-group to use
if (j < params_leng) {
DEVICES_INFO[k].n_wi_wg = (unsigned long)atoi(params + j + 1);
}
// if nothing use the default number of work-items
else {
DEVICES_INFO[k].n_wi_wg = 0;
}
} else {
// capture number of work-groups to use
DEVICES_INFO[k].n_wg = (unsigned long)atoi(params + j + 1);
j += 1 + get_int_len((int)DEVICES_INFO[k].n_wg);
// if something else capture the number of work-items per work-group to use
if (j + 1 < params_leng) {
DEVICES_INFO[k].n_wi_wg = (unsigned long)atoi(params + j + 1);
}
// if nothing use the default number of work-items
else {
DEVICES_INFO[k].n_wi_wg = 0;
}
}
}
// if nothing use all the devices of this type with default number of work-groups and work-items
else if (j == params_leng) {
DEVICES_INFO[k].n_wg = 0;
DEVICES_INFO[k].n_wi_wg = 0;
} else {
fprintf(stderr, "\nInvalid argument '%s'\n", params);
print_help();
free(params);
exit(-1);
}
}
k++;
free(params);
} else if (!strcmp(argv[i], "-H")) {
print_help();
exit(-1);
} else if (!strcmp(argv[i], "-FZN")) {
FZN_FILE_NAME = malloc((strlen(argv[++i]) + 1) * sizeof(char));
strcpy(FZN_FILE_NAME, argv[i]);
} else if (!strcmp(argv[i], "-MZN")) {
MZN_FILE_NAME = malloc((strlen(argv[++i]) + 1) * sizeof(char));
strcpy(MZN_FILE_NAME, argv[i]);
int len = (int)strlen(argv[++i]);
if (len > 4) {
const char *last_four = &argv[i][len - 4];
if (strcmp(last_four, ".dzn") == 0) {
DZN_FILE_NAME = malloc((strlen(argv[i]) + 1) * sizeof(char));
strcpy(DZN_FILE_NAME, argv[i]);
} else {
i--;
}
} else {
i--;
}
} else if (CSP != NULL && strcmp(csp_inst_name, "No name given") == 0) {
if ((strlen(argv[i]) + 1) > 20) {
fprintf(stderr, "\nThe name of the CSP instance is too big, please decrease it to a maximum of 20 characters.\n");
exit(-1);
}
strcpy(csp_inst_name, argv[i]);
} else {
fprintf(stderr, "\nInvalid argument '%s'\n", argv[i]);
print_help();
exit(-1);
}
}
if (N_DEVS == 0) {
ALL_DEVS = true;
}
if (csp_dim_arg_ctr == 0 && csp_inst_name == NULL) {
fprintf(stderr, "\n-N must be defined!\n");
print_help();
exit(-1);
}
if (QUIET) {
VERBOSE = false;
}
}
/*
* Prints all accepted arguments with their description
*/
void print_help() {
printf("\nList of accepted arguments:\n"
" -D [GPU|CPU|ACC][:n][/[wg]/[wi]] - Select the device/s to use. Examples:\n"
" -D CPU:1/64/1 -D GPU:2 -D ACC//1 - Use first CPU with 64 work-groups\n"
" and 1 work-item per work-group, second GPU with the default number of\n"
" work-groups and of work-items, and all accelerators with default number\n"
" of work-groups and one work-item per work-group;\n"
" -D CPU:1 -D GPU:1 -INTERVALS - Use first CPU and first GPU with INTERVAL\n"
" domains;\n"
" -D CPU -D GPU - Use all GPUs and all CPUs with default number of\n"
" work-groups and work-items;\n"
" If none -D argument is introduced, all the devices compatible with OpenCL\n"
" will be used.\n\n"
" -E [QUEENS | COSTAS | GOLOMB | SUDOKU | ALL-DIFF | QAP | LANGFORD | STEINER |\n"
" LATIN | ALL-INTERVAL | MARKET-SPLIT | SCHURS] - Select one of the sample\n"
" CSPs implemented through PHACT's interface;\n"
" -FZN /home/user/csp.fzn - Solve the Flatzinc model in the file\n"
" \"/home/user/csp.fzn\". If only the mame of the file is given, it\n"
" will be searched in src/csps/csp.fzn. Flex and Bison programs are\n"
" required;\n"
" -MZN /home/user/csp.mzn /home/user/csp.dzn - Solve the Minizinc model\n"
" in the files \"/home/user/csp.mzn\" and \"/home/user/csp.dzn\".\n"
" If only the mame of the files is given, they will be searched in\n"
" src/csps/csp.Xzn. Mzn2fzn, Flex and Bison programs are required.\n\n"
" -MZN /home/user/csp.mzn [/home/user/csp.dzn] -MZN2FZN-ONLY - Only\n"
" converts the MZN file in \"/home/user/csp.mzn\" and\n"
" \"/home/user/csp.dzn\" to the FZN file \"/home/user/csp.fzn\".\n"
" If only the mame of the file is given, it will be searched in\n"
" src/csps/csp.Xzn. Mzn2fzn program is required.\n\n"
" (int) - CSP dimension. \"(int)\" should be replaced by each dimension of the\n"
" CSP to solve. Not used when solving a Minizinc or Flatzinc model.\n\n"
" [-COUNT|-ONE|-OPT] - Select what must be done with the CSP. When solving\n"
" FlatZinc models, it overrides the model selection:\n"
" -COUNT - Count all the solutions;\n"
" -ONE - Find one solution. Default for CSPs modeled with PHACT C interface;\n"
" -OPT - Do optimization.\n\n"
" [-INTERVALS] - Use interval representation for domains, instead of bitmaps.\n\n"
" [-N-SS n] - Number of sub-search spaces to create. \"n\" should be replaced by\n"
" the number of sub-search spaces to create. If not present, the default\n"
" number of sub-search spaces will be created;\n\n"
" [-INPUT-ORDER | -OCCURRENCE] - Method\n"
" to select the variable to label:\n"
" -FIRST-FAIL - Select the variable to label that have less values in its\n"
" domain.\n"
" -INPUT-ORDER - Select the variable to label by the order on which they were\n"
" created. Default;\n"
" -OCCURRENCE - Select the variable to label that is more constrained;\n\n"
" [-MIN-VALUE | -MAX-VALUE | -SPLIT-VALUES] - Method to select the value to\n"
" assign to the variable for labeling:\n"
" -MIN-VALUE - Select the minimum value to assign. Default;\n"
" -MAX-VALUE - Select the maximum value to assign;\n"
" -SPLIT-VALUES - Splits the domain about half and tries the first half.\n\n"
" -STATS - Print statistics about the solving process;\n"
" -PRINT-SOLUTIONS - Print all the solutions (only available when using only\n"
" one thread per device);\n"
" -PRINT-CSP - Before starting the exploration, prints all the variables with\n"
" their domains, the constraints and the relation between them;\n"
" -V - Print more information and timings about what is being done by each\n"
" device;\n"
" -Q - Print only the best solution, the solution, or the number of solutions,\n"
" depending if optimizing, searching for one solution or counting the\n"
" number of solutions;\n\n"
" -H - Show this information.\n\n"
"\nNotes on compilation:\n"
" To compile PHACT execute one of the following commands on folder\n"
" \"PHACT/Debug\":\n"
" make all - To solve CSPs with variables whose domains have values between 0\n"
" and 1023;\n"
" make all CFLAGS=\"-D BITS=n\" - To solve CSPs with variables whose domains\n"
" have values between 0 and n. When recompiling the Solver to change\n"
" CL_BITS value, please run \"make clean\" before;\n"
" make all CFLAGS=\"-D COMPILE_FZN=0\" - Required when the programs mzn2fzn,\n"
" flex or bison are not available. Minizinc and Flatzinc interpreter will\n"
" not be available.\n"
"\nThe OpenCL drivers implemented by some vendors for their devices will try to\n"
"vectorize the kernel. When using PHACT in some devices, that may result in\n"
"crashing the OpenCL compiler, or in a poor performance of the solver.\n"
"For that motive, it is recommended to disable this OpenCL feature by running\n"
"\"CL_CONFIG_USE_VECTORIZER=false\" before executing PHACT.\n"
"\nExecution examples:\n"
" For counting the number of solutions of the Costas Array 10 problem using all\n"
" the devices compatible with OpenCL on the running machine, execute the\n"
" following command on folder \"PHACT/Debug\":\n"
" ./PHACT -E COSTAS 10 -COUNT\n\n"
" For finding one solution for the n-Queens 30 problem using all the GPUs\n"
" compatible with OpenCL on the running machine, execute the following\n"
" command on folder \"PHACT/Debug\":\n"
" ./PHACT -E QUEENS 30 -D GPU\n\n"
" For finding one solution for a new CSP modeled in the file \"/src/csps/CSP.c\"\n"
" and using the CPU on the running machine, after recompiling PHACT,execute\n"
" the following command on folder \"PHACT/Debug\":\n"
" ./PHACT -D CPU\n\n"
" For solving the CSP modeled in the FlatZinc file\n"
" \"PHACT/Debug/src/csps/CSP.fzn\" file using all the GPUs compatible with\n"
" OpenCL on the running machine, execute the following command on folder\n"
" \"PHACT/Debug\":\n"
" ./PHACT CSP.fzn -D GPU\n\n"
" For solving the CSP modeled in the MiniZinc files\n"
" \"PHACT/Debug/src/csps/CSP.mzn\" and \"PHACT/Debug/src/csps/CSP.dzn\"\n"
" files using all the devices compatible with OpenCL on the running machine,\n"
" execute the following command on folder \"PHACT/Debug\":\n"
" ./PHACT -MZN CSP.mzn CSP.dzn\n\n");
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
printf("\nPress a key to exit\n");
int a = getchar();
#endif
}
/*
* Parse execution arguments
* argc - solver command arguments
* argv - solver command arguments
* csp_dims - will return all the solver arguments of the int type, placed by input order
*/
void load_args(int *argc, char **argv[], int* csp_dims, char* csp_inst_name) {
parse_args(argc, *argv, csp_dims, csp_inst_name);
}
/*
* Initialize number of variables and constraints and bitmap size on devices
* val_to_opt- Max or min value on the domain of the variable to optimize (atomic read and write)
* bitmap_size - to store the bitmap domain size needed on devices
* interval_size - to store the interval domain size needed on devices
*/
void init_csp_and_d_bits() {
if (WORK != OPT && (USE_CS[MAXIMIZE] == 1 || USE_CS[MINIMIZE] == 1)) {
fprintf(stderr, "\nYou are trying to count all the solutions or just finding one solution for a CSP that was modeled as an optimization problem.\n"
"For that purpose please remove the \"c_min\" or \"c_max\" constraints.\n");
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
printf("\nPress a key to exit\n");
int a = getchar();
#endif
exit(-1);
}
if (WORK == OPT && USE_CS[MAXIMIZE] == 0 && USE_CS[MINIMIZE] == 0) {
fprintf(stderr, "\nYou are trying to optimize a CSP that was not modeled as an optimization problem.\n"
"For that purpose please use the \"c_min\" or \"c_max\" constraints.\n");
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
printf("\nPress a key to exit\n");
int a = getchar();
#endif
exit(-1);
}
// set domain size for usage on kernel
if (DOMAIN_TYPE == BITMAP_) {
if (D_MAX < 32) {
CL_BITS_ = 32;
CL_WORD_ = 32;
} else if (D_MAX < 64) {
CL_BITS_ = 64;
CL_WORD_ = 64;
} else {
CL_BITS_ = D_MAX / 64 * 64 + 64;
CL_WORD_ = 64;
}
CL_N_WORDS_ = CL_BITS_ / CL_WORD_;
if (CL_N_WORDS_ == 0) {
CL_N_WORDS_ = 1;
}
DOMAIN_SIZE = CL_N_WORDS_ * CL_WORD_ / 8;
} else if (DOMAIN_TYPE == INTERVAL) {
DOMAIN_SIZE = sizeof(interval);
}
N_VS = V_ID_CNTR;
N_CS = C_ID_CNTR;
}
/*
* 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
*/
void print_CSP() {
unsigned int i;
unsigned int prev_val;
unsigned int new_val;
unsigned int cntr;
int j;
printf("\n\n--------------------------------\n");
printf("CSP as received by the host:\n");
printf("\nVariables:\n");
for (i = 0; i < V_ID_CNTR; i++) {
printf(" ID=%u: Values={", VS[i].v_id);
j = 1;
prev_val = b_get_nth_val(&VS[i].domain_b, (unsigned int)j++);
new_val = prev_val;
printf("%u", prev_val);
while (new_val < VS[i].max) {
cntr = 0;
while ((new_val = (unsigned int)b_get_nth_val(&VS[i].domain_b, (unsigned int)j++)) == prev_val + 1 && new_val < VS[i].max) {
prev_val = new_val;
cntr++;
}
if (cntr == 0) {
printf(",%u", new_val);
} else {
if (new_val == VS[i].max && new_val == prev_val + 1) {
printf("-%u", new_val);
} else if (cntr == 1) {
printf(",%u,%u", prev_val, new_val);
} else {
printf("-%u,%u", prev_val, new_val);
}
}
prev_val = new_val;
}
if (VS[i].to_label) {
printf("} Label=true");
} else {
printf("} Label=false");
}
if (VS[i].n_cs > 0) {
printf(" Constraints={");
for (j = 0; j < VS[i].n_cs - 1; j++) {
printf("%u,", VS[i].cs[j]->c_id);
}
if (VS[i].n_cs > 0) {
printf("%u}\n", VS[i].cs[j]->c_id);
} else {
printf("\n");
}
} else {
printf("\n");
}
}
printf("Constraints:\n");
for (i = 0; i < C_ID_CNTR; i++) {
printf(" ID=%u: Type=", CS[i].c_id);
cs_print_type(&CS[i]);
printf(" Variables={");
for (j = 0; j < CS[i].n_c_vs - 1; j++) {
printf("%u,", CS[i].c_vs[j]->v_id);
}
printf("%u}", CS[i].c_vs[j]->v_id);
if (CS[i].n_c_consts > 0) {
printf(" Constants={");
for (j = 0; j < CS[i].n_c_consts - 1; j++) {
printf("%d,", CS[i].c_consts[j]);
}
if (CS[i].kind == LINEAR || CS[i].kind == LINEAR_NE || CS[i].kind == LINEAR_LT) {
printf("%d, %d}", CS[i].c_consts[j], CS[i].constant_val);
} else {
printf("%d}", CS[i].c_consts[j]);
}
} 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(" Constants={%d}", CS[i].constant_val);
}
if (CS[i].ignore) {
printf(" ignored");
}
if (CS[i].boolean) {
printf(" boolean");
}
if (CS[i].reified) {
printf(" reif_var_ID=%u\n", CS[i].reif_v_id);
} else {
printf("\n");
}
}
printf("\n");
printf("--------------------------------\n\n");
}
/*
* Return the name of the heuristic used for selecting the next variable to label
*/
char* get_label_heur() {
switch (LABEL_MODE) {
case FIRST_FAIL:
return "First fail";
break;
case INPUT_ORDER:
return "Input order";
break;
case OCCURRENCE:
return "Occurrence";
break;
case MAX_REGRET:
return "Maximum Regret";
break;
case SMALLEST:
return "Smallest";
break;
default:
return "UNKNOWN";
break;
}
}
/*
* Return the name of the heuristic used for selecting the next value to assign
*/
char* get_assign_heur() {
switch (ASSIGN_MODE) {
case MIN_VAL:
return "Minimum Value";
break;
case MAX_VAL:
return "Maximum Value";
break;
case SPLIT_VALS:
return "Split Values";
break;
default:
return "UNKNOWN";
break;
}
}
/*
* Clear CSP variables and constraints memory
*/
void clear_csp() {
unsigned int i;
for (i = 0; i < N_DEVS; i++) {
free(DEVICES_INFO[i].dev_name);
}
cs_clear();
vs_clear();
if (WORK == OPT) {
free(VS_LOCK);
free(VS_LOCK_BEST);
}
free(VS);
free(CS);
free(CONST_VS_ID);
free(CSP);
free(EXP_VALUES);
}
/*
* return the number of characters on an integer
*/
int get_int_len(int x) {
if (x >= 1000)
return 4;
if (x >= 100)
return 3;
if (x >= 10)
return 2;
return 1;
}
/*
* Check if program exists on the PATH environment
* cmd - command to execute the program
*/
bool can_run_command(const char *cmd) {
if (strchr(cmd, '/')) {
// if cmd includes a slash, no path search must be performed,
// go straight to checking if it's executable
return access(cmd, X_OK) == 0;
}
const char *path = getenv("PATH");
if (!path)
return false;
char *buf = malloc(strlen(path) + strlen(cmd) + 3);
if (!buf)
return false;
// loop as path contains something
for (; *path; ++path) {
// start from the beginning of the buffer
char *p = buf;
// copy in buf the current path element
for (; *path && *path != ':'; ++path, ++p) {
*p = *path;
}
// empty path entries are treated like "."
if (p == buf)
*p++ = '.';
// slash and command name
if (p[-1] != '/')
*p++ = '/';
strcpy(p, cmd);
// check if it can be executed
if (access(buf, X_OK) == 0) {
free(buf);
return true;
}
// quit at last cycle
if (!*path)
break;
}
// not found
free(buf);
return false;
}