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.

122 lines
4.1 KiB

2 months ago
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <sylvan.h>
  5. #include <sylvan_obj.hpp>
  6. using namespace sylvan;
  7. VOID_TASK_0(simple_cxx)
  8. {
  9. Bdd one = Bdd::bddOne(); // the True terminal
  10. Bdd zero = Bdd::bddZero(); // the False terminal
  11. // check if they really are the True/False terminal
  12. assert(one.GetBDD() == sylvan_true);
  13. assert(zero.GetBDD() == sylvan_false);
  14. Bdd a = Bdd::bddVar(0); // create a BDD variable x_0
  15. Bdd b = Bdd::bddVar(1); // create a BDD variable x_1
  16. // check if a really is the Boolean formula "x_0"
  17. assert(!a.isConstant());
  18. assert(a.TopVar() == 0);
  19. assert(a.Then() == one);
  20. assert(a.Else() == zero);
  21. // check if b really is the Boolean formula "x_1"
  22. assert(!b.isConstant());
  23. assert(b.TopVar() == 1);
  24. assert(b.Then() == one);
  25. assert(b.Else() == zero);
  26. // compute !a
  27. Bdd not_a = !a;
  28. // check if !!a is really a
  29. assert((!not_a) == a);
  30. // compute a * b and !(!a + !b) and check if they are equivalent
  31. Bdd a_and_b = a * b;
  32. Bdd not_not_a_or_not_b = !(!a + !b);
  33. assert(a_and_b == not_not_a_or_not_b);
  34. // perform some simple quantification and check the results
  35. Bdd ex = a_and_b.ExistAbstract(a); // \exists a . a * b
  36. assert(ex == b);
  37. Bdd andabs = a.AndAbstract(b, a); // \exists a . a * b using AndAbstract
  38. assert(ex == andabs);
  39. Bdd univ = a_and_b.UnivAbstract(a); // \forall a . a * b
  40. assert(univ == zero);
  41. // alternative method to get the cube "ab" using bddCube
  42. BddSet variables = a * b;
  43. std::vector<unsigned char> vec = {1, 1};
  44. assert(a_and_b == Bdd::bddCube(variables, vec));
  45. // test the bddCube method for all combinations
  46. assert((!a * !b) == Bdd::bddCube(variables, std::vector<uint8_t>({0, 0})));
  47. assert((!a * b) == Bdd::bddCube(variables, std::vector<uint8_t>({0, 1})));
  48. assert((!a) == Bdd::bddCube(variables, std::vector<uint8_t>({0, 2})));
  49. assert((a * !b) == Bdd::bddCube(variables, std::vector<uint8_t>({1, 0})));
  50. assert((a * b) == Bdd::bddCube(variables, std::vector<uint8_t>({1, 1})));
  51. assert((a) == Bdd::bddCube(variables, std::vector<uint8_t>({1, 2})));
  52. assert((!b) == Bdd::bddCube(variables, std::vector<uint8_t>({2, 0})));
  53. assert((b) == Bdd::bddCube(variables, std::vector<uint8_t>({2, 1})));
  54. assert(one == Bdd::bddCube(variables, std::vector<uint8_t>({2, 2})));
  55. }
  56. VOID_TASK_1(_main, void*, arg)
  57. {
  58. // Initialize Sylvan
  59. // With starting size of the nodes table 1 << 21, and maximum size 1 << 27.
  60. // With starting size of the cache table 1 << 20, and maximum size 1 << 20.
  61. // Memory usage: 24 bytes per node, and 36 bytes per cache bucket
  62. // - 1<<24 nodes: 384 MB
  63. // - 1<<25 nodes: 768 MB
  64. // - 1<<26 nodes: 1536 MB
  65. // - 1<<27 nodes: 3072 MB
  66. // - 1<<24 cache: 576 MB
  67. // - 1<<25 cache: 1152 MB
  68. // - 1<<26 cache: 2304 MB
  69. // - 1<<27 cache: 4608 MB
  70. sylvan_set_sizes(1LL<<22, 1LL<<26, 1LL<<22, 1LL<<26);
  71. sylvan_init_package();
  72. // Initialize the BDD module with granularity 1 (cache every operation)
  73. // A higher granularity (e.g. 6) often results in better performance in practice
  74. sylvan_init_bdd();
  75. // Now we can do some simple stuff using the C++ objects.
  76. CALL(simple_cxx);
  77. // Report statistics (if SYLVAN_STATS is 1 in the configuration)
  78. sylvan_stats_report(stdout);
  79. // And quit, freeing memory
  80. sylvan_quit();
  81. // We didn't use arg
  82. (void)arg;
  83. }
  84. int
  85. main (int argc, char *argv[])
  86. {
  87. int n_workers = 0; // automatically detect number of workers
  88. size_t deque_size = 0; // default value for the size of task deques for the workers
  89. size_t program_stack_size = 0; // default value for the program stack of each pthread
  90. // Initialize the Lace framework for <n_workers> workers.
  91. lace_init(n_workers, deque_size);
  92. // Spawn and start all worker pthreads; suspends current thread until done.
  93. lace_startup(program_stack_size, TASK(_main), NULL);
  94. // The lace_startup command also exits Lace after _main is completed.
  95. return 0;
  96. (void)argc; // unused variable
  97. (void)argv; // unused variable
  98. }