You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

616 lines
19 KiB

  1. #include <argp.h>
  2. #include <inttypes.h>
  3. #include <locale.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/time.h>
  8. #ifdef HAVE_PROFILER
  9. #include <gperftools/profiler.h>
  10. #endif
  11. #include <sylvan.h>
  12. #include <llmsset.h>
  13. /* Configuration */
  14. static int report_levels = 0; // report states at end of every level
  15. static int report_table = 0; // report table size at end of every level
  16. static int report_nodes = 0; // report number of nodes of BDDs
  17. static int strategy = 1; // set to 1 = use PAR strategy; set to 0 = use BFS strategy
  18. static int check_deadlocks = 0; // set to 1 to check for deadlocks
  19. static int merge_relations = 0; // merge relations to 1 relation
  20. static int print_transition_matrix = 0; // print transition relation matrix
  21. static int workers = 0; // autodetect
  22. static char* model_filename = NULL; // filename of model
  23. #ifdef HAVE_PROFILER
  24. static char* profile_filename = NULL; // filename for profiling
  25. #endif
  26. /* argp configuration */
  27. static struct argp_option options[] =
  28. {
  29. {"workers", 'w', "<workers>", 0, "Number of workers (default=0: autodetect)", 0},
  30. {"strategy", 's', "<bfs|par|sat>", 0, "Strategy for reachability (default=par)", 0},
  31. #ifdef HAVE_PROFILER
  32. {"profiler", 'p', "<filename>", 0, "Filename for profiling", 0},
  33. #endif
  34. {"deadlocks", 3, 0, 0, "Check for deadlocks", 1},
  35. {"count-nodes", 5, 0, 0, "Report #nodes for BDDs", 1},
  36. {"count-states", 1, 0, 0, "Report #states at each level", 1},
  37. {"count-table", 2, 0, 0, "Report table usage at each level", 1},
  38. {"merge-relations", 6, 0, 0, "Merge transition relations into one transition relation", 1},
  39. {"print-matrix", 4, 0, 0, "Print transition matrix", 1},
  40. {0, 0, 0, 0, 0, 0}
  41. };
  42. static error_t
  43. parse_opt(int key, char *arg, struct argp_state *state)
  44. {
  45. switch (key) {
  46. case 'w':
  47. workers = atoi(arg);
  48. break;
  49. case 's':
  50. if (strcmp(arg, "bfs")==0) strategy = 0;
  51. else if (strcmp(arg, "par")==0) strategy = 1;
  52. else if (strcmp(arg, "sat")==0) strategy = 2;
  53. else argp_usage(state);
  54. break;
  55. case 4:
  56. print_transition_matrix = 1;
  57. break;
  58. case 3:
  59. check_deadlocks = 1;
  60. break;
  61. case 1:
  62. report_levels = 1;
  63. break;
  64. case 2:
  65. report_table = 1;
  66. break;
  67. case 6:
  68. merge_relations = 1;
  69. break;
  70. #ifdef HAVE_PROFILER
  71. case 'p':
  72. profile_filename = arg;
  73. break;
  74. #endif
  75. case ARGP_KEY_ARG:
  76. if (state->arg_num >= 1) argp_usage(state);
  77. model_filename = arg;
  78. break;
  79. case ARGP_KEY_END:
  80. if (state->arg_num < 1) argp_usage(state);
  81. break;
  82. default:
  83. return ARGP_ERR_UNKNOWN;
  84. }
  85. return 0;
  86. }
  87. static struct argp argp = { options, parse_opt, "<model>", 0, 0, 0, 0 };
  88. /* Globals */
  89. typedef struct set
  90. {
  91. BDD bdd;
  92. BDD variables; // all variables in the set (used by satcount)
  93. } *set_t;
  94. typedef struct relation
  95. {
  96. BDD bdd;
  97. BDD variables; // all variables in the relation (used by relprod)
  98. } *rel_t;
  99. static int vector_size; // size of vector
  100. static int statebits, actionbits; // number of bits for state, number of bits for action
  101. static int bits_per_integer; // number of bits per integer in the vector
  102. static int next_count; // number of partitions of the transition relation
  103. static rel_t *next; // each partition of the transition relation
  104. /* Obtain current wallclock time */
  105. static double
  106. wctime()
  107. {
  108. struct timeval tv;
  109. gettimeofday(&tv, NULL);
  110. return (tv.tv_sec + 1E-6 * tv.tv_usec);
  111. }
  112. static double t_start;
  113. #define INFO(s, ...) fprintf(stdout, "[% 8.2f] " s, wctime()-t_start, ##__VA_ARGS__)
  114. #define Abort(...) { fprintf(stderr, __VA_ARGS__); exit(-1); }
  115. /* Load a set from file */
  116. #define set_load(f) CALL(set_load, f)
  117. TASK_1(set_t, set_load, FILE*, f)
  118. {
  119. sylvan_serialize_fromfile(f);
  120. size_t set_bdd, set_vector_size, set_state_vars;
  121. if ((fread(&set_bdd, sizeof(size_t), 1, f) != 1) ||
  122. (fread(&set_vector_size, sizeof(size_t), 1, f) != 1) ||
  123. (fread(&set_state_vars, sizeof(size_t), 1, f) != 1)) {
  124. Abort("Invalid input file!\n");
  125. }
  126. set_t set = (set_t)malloc(sizeof(struct set));
  127. set->bdd = sylvan_serialize_get_reversed(set_bdd);
  128. set->variables = sylvan_support(sylvan_serialize_get_reversed(set_state_vars));
  129. sylvan_protect(&set->bdd);
  130. sylvan_protect(&set->variables);
  131. return set;
  132. }
  133. /* Load a relation from file */
  134. #define rel_load(f) CALL(rel_load, f)
  135. TASK_1(rel_t, rel_load, FILE*, f)
  136. {
  137. sylvan_serialize_fromfile(f);
  138. size_t rel_bdd, rel_vars;
  139. if ((fread(&rel_bdd, sizeof(size_t), 1, f) != 1) ||
  140. (fread(&rel_vars, sizeof(size_t), 1, f) != 1)) {
  141. Abort("Invalid input file!\n");
  142. }
  143. rel_t rel = (rel_t)malloc(sizeof(struct relation));
  144. rel->bdd = sylvan_serialize_get_reversed(rel_bdd);
  145. rel->variables = sylvan_support(sylvan_serialize_get_reversed(rel_vars));
  146. sylvan_protect(&rel->bdd);
  147. sylvan_protect(&rel->variables);
  148. return rel;
  149. }
  150. #define print_example(example, variables) CALL(print_example, example, variables)
  151. VOID_TASK_2(print_example, BDD, example, BDDSET, variables)
  152. {
  153. uint8_t str[vector_size * bits_per_integer];
  154. if (example != sylvan_false) {
  155. sylvan_sat_one(example, variables, str);
  156. printf("[");
  157. for (int i=0; i<vector_size; i++) {
  158. uint32_t res = 0;
  159. for (int j=0; j<bits_per_integer; j++) {
  160. if (str[bits_per_integer*i+j] == 1) res++;
  161. res<<=1;
  162. }
  163. if (i>0) printf(",");
  164. printf("%" PRIu32, res);
  165. }
  166. printf("]");
  167. }
  168. }
  169. /* Straight-forward implementation of parallel reduction */
  170. TASK_5(BDD, go_par, BDD, cur, BDD, visited, size_t, from, size_t, len, BDD*, deadlocks)
  171. {
  172. if (len == 1) {
  173. // Calculate NEW successors (not in visited)
  174. BDD succ = sylvan_relnext(cur, next[from]->bdd, next[from]->variables);
  175. bdd_refs_push(succ);
  176. if (deadlocks) {
  177. // check which BDDs in deadlocks do not have a successor in this relation
  178. BDD anc = sylvan_relprev(next[from]->bdd, succ, next[from]->variables);
  179. bdd_refs_push(anc);
  180. *deadlocks = sylvan_diff(*deadlocks, anc);
  181. bdd_refs_pop(1);
  182. }
  183. BDD result = sylvan_diff(succ, visited);
  184. bdd_refs_pop(1);
  185. return result;
  186. } else {
  187. BDD deadlocks_left;
  188. BDD deadlocks_right;
  189. if (deadlocks) {
  190. deadlocks_left = *deadlocks;
  191. deadlocks_right = *deadlocks;
  192. sylvan_protect(&deadlocks_left);
  193. sylvan_protect(&deadlocks_right);
  194. }
  195. // Recursively calculate left+right
  196. bdd_refs_spawn(SPAWN(go_par, cur, visited, from, (len+1)/2, deadlocks ? &deadlocks_left: NULL));
  197. BDD right = bdd_refs_push(CALL(go_par, cur, visited, from+(len+1)/2, len/2, deadlocks ? &deadlocks_right : NULL));
  198. BDD left = bdd_refs_push(bdd_refs_sync(SYNC(go_par)));
  199. // Merge results of left+right
  200. BDD result = sylvan_or(left, right);
  201. bdd_refs_pop(2);
  202. if (deadlocks) {
  203. bdd_refs_push(result);
  204. *deadlocks = sylvan_and(deadlocks_left, deadlocks_right);
  205. sylvan_unprotect(&deadlocks_left);
  206. sylvan_unprotect(&deadlocks_right);
  207. bdd_refs_pop(1);
  208. }
  209. return result;
  210. }
  211. }
  212. /* PAR strategy, parallel strategy (operations called in parallel *and* parallelized by Sylvan) */
  213. VOID_TASK_1(par, set_t, set)
  214. {
  215. BDD visited = set->bdd;
  216. BDD next_level = visited;
  217. BDD cur_level = sylvan_false;
  218. BDD deadlocks = sylvan_false;
  219. sylvan_protect(&visited);
  220. sylvan_protect(&next_level);
  221. sylvan_protect(&cur_level);
  222. sylvan_protect(&deadlocks);
  223. int iteration = 1;
  224. do {
  225. // calculate successors in parallel
  226. cur_level = next_level;
  227. deadlocks = cur_level;
  228. next_level = CALL(go_par, cur_level, visited, 0, next_count, check_deadlocks ? &deadlocks : NULL);
  229. if (check_deadlocks && deadlocks != sylvan_false) {
  230. INFO("Found %'0.0f deadlock states... ", sylvan_satcount(deadlocks, set->variables));
  231. if (deadlocks != sylvan_false) {
  232. printf("example: ");
  233. print_example(deadlocks, set->variables);
  234. check_deadlocks = 0;
  235. }
  236. printf("\n");
  237. }
  238. // visited = visited + new
  239. visited = sylvan_or(visited, next_level);
  240. if (report_table && report_levels) {
  241. size_t filled, total;
  242. sylvan_table_usage(&filled, &total);
  243. INFO("Level %d done, %'0.0f states explored, table: %0.1f%% full (%'zu nodes)\n",
  244. iteration, sylvan_satcount(visited, set->variables),
  245. 100.0*(double)filled/total, filled);
  246. } else if (report_table) {
  247. size_t filled, total;
  248. sylvan_table_usage(&filled, &total);
  249. INFO("Level %d done, table: %0.1f%% full (%'zu nodes)\n",
  250. iteration,
  251. 100.0*(double)filled/total, filled);
  252. } else if (report_levels) {
  253. INFO("Level %d done, %'0.0f states explored\n", iteration, sylvan_satcount(visited, set->variables));
  254. } else {
  255. INFO("Level %d done\n", iteration);
  256. }
  257. iteration++;
  258. } while (next_level != sylvan_false);
  259. set->bdd = visited;
  260. sylvan_unprotect(&visited);
  261. sylvan_unprotect(&next_level);
  262. sylvan_unprotect(&cur_level);
  263. sylvan_unprotect(&deadlocks);
  264. }
  265. /* Sequential version of merge-reduction */
  266. TASK_5(BDD, go_bfs, BDD, cur, BDD, visited, size_t, from, size_t, len, BDD*, deadlocks)
  267. {
  268. if (len == 1) {
  269. // Calculate NEW successors (not in visited)
  270. BDD succ = sylvan_relnext(cur, next[from]->bdd, next[from]->variables);
  271. bdd_refs_push(succ);
  272. if (deadlocks) {
  273. // check which BDDs in deadlocks do not have a successor in this relation
  274. BDD anc = sylvan_relprev(next[from]->bdd, succ, next[from]->variables);
  275. bdd_refs_push(anc);
  276. *deadlocks = sylvan_diff(*deadlocks, anc);
  277. bdd_refs_pop(1);
  278. }
  279. BDD result = sylvan_diff(succ, visited);
  280. bdd_refs_pop(1);
  281. return result;
  282. } else {
  283. BDD deadlocks_left;
  284. BDD deadlocks_right;
  285. if (deadlocks) {
  286. deadlocks_left = *deadlocks;
  287. deadlocks_right = *deadlocks;
  288. sylvan_protect(&deadlocks_left);
  289. sylvan_protect(&deadlocks_right);
  290. }
  291. // Recursively calculate left+right
  292. BDD left = CALL(go_bfs, cur, visited, from, (len+1)/2, deadlocks ? &deadlocks_left : NULL);
  293. bdd_refs_push(left);
  294. BDD right = CALL(go_bfs, cur, visited, from+(len+1)/2, len/2, deadlocks ? &deadlocks_right : NULL);
  295. bdd_refs_push(right);
  296. // Merge results of left+right
  297. BDD result = sylvan_or(left, right);
  298. bdd_refs_pop(2);
  299. if (deadlocks) {
  300. bdd_refs_push(result);
  301. *deadlocks = sylvan_and(deadlocks_left, deadlocks_right);
  302. sylvan_unprotect(&deadlocks_left);
  303. sylvan_unprotect(&deadlocks_right);
  304. bdd_refs_pop(1);
  305. }
  306. return result;
  307. }
  308. }
  309. /* BFS strategy, sequential strategy (but operations are parallelized by Sylvan) */
  310. VOID_TASK_1(bfs, set_t, set)
  311. {
  312. BDD visited = set->bdd;
  313. BDD next_level = visited;
  314. BDD cur_level = sylvan_false;
  315. BDD deadlocks = sylvan_false;
  316. sylvan_protect(&visited);
  317. sylvan_protect(&next_level);
  318. sylvan_protect(&cur_level);
  319. sylvan_protect(&deadlocks);
  320. int iteration = 1;
  321. do {
  322. // calculate successors in parallel
  323. cur_level = next_level;
  324. deadlocks = cur_level;
  325. next_level = CALL(go_bfs, cur_level, visited, 0, next_count, check_deadlocks ? &deadlocks : NULL);
  326. if (check_deadlocks && deadlocks != sylvan_false) {
  327. INFO("Found %'0.0f deadlock states... ", sylvan_satcount(deadlocks, set->variables));
  328. if (deadlocks != sylvan_false) {
  329. printf("example: ");
  330. print_example(deadlocks, set->variables);
  331. check_deadlocks = 0;
  332. }
  333. printf("\n");
  334. }
  335. // visited = visited + new
  336. visited = sylvan_or(visited, next_level);
  337. if (report_table && report_levels) {
  338. size_t filled, total;
  339. sylvan_table_usage(&filled, &total);
  340. INFO("Level %d done, %'0.0f states explored, table: %0.1f%% full (%'zu nodes)\n",
  341. iteration, sylvan_satcount(visited, set->variables),
  342. 100.0*(double)filled/total, filled);
  343. } else if (report_table) {
  344. size_t filled, total;
  345. sylvan_table_usage(&filled, &total);
  346. INFO("Level %d done, table: %0.1f%% full (%'zu nodes)\n",
  347. iteration,
  348. 100.0*(double)filled/total, filled);
  349. } else if (report_levels) {
  350. INFO("Level %d done, %'0.0f states explored\n", iteration, sylvan_satcount(visited, set->variables));
  351. } else {
  352. INFO("Level %d done\n", iteration);
  353. }
  354. iteration++;
  355. } while (next_level != sylvan_false);
  356. set->bdd = visited;
  357. sylvan_unprotect(&visited);
  358. sylvan_unprotect(&next_level);
  359. sylvan_unprotect(&cur_level);
  360. sylvan_unprotect(&deadlocks);
  361. }
  362. /**
  363. * Extend a transition relation to a larger domain (using s=s')
  364. */
  365. #define extend_relation(rel, vars) CALL(extend_relation, rel, vars)
  366. TASK_2(BDD, extend_relation, BDD, relation, BDDSET, variables)
  367. {
  368. /* first determine which state BDD variables are in rel */
  369. int has[statebits];
  370. for (int i=0; i<statebits; i++) has[i] = 0;
  371. BDDSET s = variables;
  372. while (!sylvan_set_isempty(s)) {
  373. BDDVAR v = sylvan_set_var(s);
  374. if (v/2 >= (unsigned)statebits) break; // action labels
  375. has[v/2] = 1;
  376. s = sylvan_set_next(s);
  377. }
  378. /* create "s=s'" for all variables not in rel */
  379. BDD eq = sylvan_true;
  380. for (int i=statebits-1; i>=0; i--) {
  381. if (has[i]) continue;
  382. BDD low = sylvan_makenode(2*i+1, eq, sylvan_false);
  383. bdd_refs_push(low);
  384. BDD high = sylvan_makenode(2*i+1, sylvan_false, eq);
  385. bdd_refs_pop(1);
  386. eq = sylvan_makenode(2*i, low, high);
  387. }
  388. bdd_refs_push(eq);
  389. BDD result = sylvan_and(relation, eq);
  390. bdd_refs_pop(1);
  391. return result;
  392. }
  393. /**
  394. * Compute \BigUnion ( sets[i] )
  395. */
  396. #define big_union(first, count) CALL(big_union, first, count)
  397. TASK_2(BDD, big_union, int, first, int, count)
  398. {
  399. if (count == 1) return next[first]->bdd;
  400. bdd_refs_spawn(SPAWN(big_union, first, count/2));
  401. BDD right = bdd_refs_push(CALL(big_union, first+count/2, count-count/2));
  402. BDD left = bdd_refs_push(bdd_refs_sync(SYNC(big_union)));
  403. BDD result = sylvan_or(left, right);
  404. bdd_refs_pop(2);
  405. return result;
  406. }
  407. static void
  408. print_matrix(BDD vars)
  409. {
  410. for (int i=0; i<vector_size; i++) {
  411. if (sylvan_set_isempty(vars)) {
  412. fprintf(stdout, "-");
  413. } else {
  414. BDDVAR next_s = 2*((i+1)*bits_per_integer);
  415. if (sylvan_set_var(vars) < next_s) {
  416. fprintf(stdout, "+");
  417. for (;;) {
  418. vars = sylvan_set_next(vars);
  419. if (sylvan_set_isempty(vars)) break;
  420. if (sylvan_set_var(vars) >= next_s) break;
  421. }
  422. } else {
  423. fprintf(stdout, "-");
  424. }
  425. }
  426. }
  427. }
  428. VOID_TASK_0(gc_start)
  429. {
  430. INFO("(GC) Starting garbage collection...\n");
  431. }
  432. VOID_TASK_0(gc_end)
  433. {
  434. INFO("(GC) Garbage collection done.\n");
  435. }
  436. int
  437. main(int argc, char **argv)
  438. {
  439. argp_parse(&argp, argc, argv, 0, 0, 0);
  440. setlocale(LC_NUMERIC, "en_US.utf-8");
  441. t_start = wctime();
  442. FILE *f = fopen(model_filename, "r");
  443. if (f == NULL) {
  444. fprintf(stderr, "Cannot open file '%s'!\n", model_filename);
  445. return -1;
  446. }
  447. // Init Lace
  448. lace_init(workers, 1000000); // auto-detect number of workers, use a 1,000,000 size task queue
  449. lace_startup(0, NULL, NULL); // auto-detect program stack, do not use a callback for startup
  450. LACE_ME;
  451. // Init Sylvan
  452. // Nodes table size: 24 bytes * 2**N_nodes
  453. // Cache table size: 36 bytes * 2**N_cache
  454. // With: N_nodes=25, N_cache=24: 1.3 GB memory
  455. sylvan_init_package(1LL<<21, 1LL<<27, 1LL<<20, 1LL<<26);
  456. sylvan_init_bdd(6); // granularity 6 is decent default value - 1 means "use cache for every operation"
  457. sylvan_gc_add_mark(0, TASK(gc_start));
  458. sylvan_gc_add_mark(40, TASK(gc_end));
  459. /* Load domain information */
  460. if ((fread(&vector_size, sizeof(int), 1, f) != 1) ||
  461. (fread(&statebits, sizeof(int), 1, f) != 1) ||
  462. (fread(&actionbits, sizeof(int), 1, f) != 1)) {
  463. Abort("Invalid input file!\n");
  464. }
  465. bits_per_integer = statebits;
  466. statebits *= vector_size;
  467. // Read initial state
  468. set_t states = set_load(f);
  469. // Read transitions
  470. if (fread(&next_count, sizeof(int), 1, f) != 1) Abort("Invalid input file!\n");
  471. next = (rel_t*)malloc(sizeof(rel_t) * next_count);
  472. int i;
  473. for (i=0; i<next_count; i++) {
  474. next[i] = rel_load(f);
  475. }
  476. /* Done */
  477. fclose(f);
  478. if (print_transition_matrix) {
  479. for (i=0; i<next_count; i++) {
  480. INFO("");
  481. print_matrix(next[i]->variables);
  482. fprintf(stdout, "\n");
  483. }
  484. }
  485. // Report statistics
  486. INFO("Read file '%s'\n", model_filename);
  487. INFO("%d integers per state, %d bits per integer, %d transition groups\n", vector_size, bits_per_integer, next_count);
  488. if (merge_relations) {
  489. BDD prime_variables = sylvan_set_empty();
  490. for (int i=statebits-1; i>=0; i--) {
  491. bdd_refs_push(prime_variables);
  492. prime_variables = sylvan_set_add(prime_variables, i*2+1);
  493. bdd_refs_pop(1);
  494. }
  495. bdd_refs_push(prime_variables);
  496. INFO("Extending transition relations to full domain.\n");
  497. for (int i=0; i<next_count; i++) {
  498. next[i]->bdd = extend_relation(next[i]->bdd, next[i]->variables);
  499. next[i]->variables = prime_variables;
  500. }
  501. INFO("Taking union of all transition relations.\n");
  502. next[0]->bdd = big_union(0, next_count);
  503. next_count = 1;
  504. }
  505. if (report_nodes) {
  506. INFO("BDD nodes:\n");
  507. INFO("Initial states: %zu BDD nodes\n", sylvan_nodecount(states->bdd));
  508. for (i=0; i<next_count; i++) {
  509. INFO("Transition %d: %zu BDD nodes\n", i, sylvan_nodecount(next[i]->bdd));
  510. }
  511. }
  512. #ifdef HAVE_PROFILER
  513. if (profile_filename != NULL) ProfilerStart(profile_filename);
  514. #endif
  515. if (strategy == 1) {
  516. double t1 = wctime();
  517. CALL(par, states);
  518. double t2 = wctime();
  519. INFO("PAR Time: %f\n", t2-t1);
  520. } else {
  521. double t1 = wctime();
  522. CALL(bfs, states);
  523. double t2 = wctime();
  524. INFO("BFS Time: %f\n", t2-t1);
  525. }
  526. #ifdef HAVE_PROFILER
  527. if (profile_filename != NULL) ProfilerStop();
  528. #endif
  529. // Now we just have states
  530. INFO("Final states: %'0.0f states\n", sylvan_satcount(states->bdd, states->variables));
  531. if (report_nodes) {
  532. INFO("Final states: %'zu BDD nodes\n", sylvan_nodecount(states->bdd));
  533. }
  534. sylvan_stats_report(stdout, 1);
  535. return 0;
  536. }