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.

121 lines
4.1 KiB

  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_init_package(1LL<<22, 1LL<<26, 1LL<<22, 1LL<<26);
  71. // Initialize the BDD module with granularity 1 (cache every operation)
  72. // A higher granularity (e.g. 6) often results in better performance in practice
  73. sylvan_init_bdd(1);
  74. // Now we can do some simple stuff using the C++ objects.
  75. CALL(simple_cxx);
  76. // Report statistics (if SYLVAN_STATS is 1 in the configuration)
  77. sylvan_stats_report(stdout, 1);
  78. // And quit, freeing memory
  79. sylvan_quit();
  80. // We didn't use arg
  81. (void)arg;
  82. }
  83. int
  84. main (int argc, char *argv[])
  85. {
  86. int n_workers = 0; // automatically detect number of workers
  87. size_t deque_size = 0; // default value for the size of task deques for the workers
  88. size_t program_stack_size = 0; // default value for the program stack of each pthread
  89. // Initialize the Lace framework for <n_workers> workers.
  90. lace_init(n_workers, deque_size);
  91. // Spawn and start all worker pthreads; suspends current thread until done.
  92. lace_startup(program_stack_size, TASK(_main), NULL);
  93. // The lace_startup command also exits Lace after _main is completed.
  94. return 0;
  95. (void)argc; // unused variable
  96. (void)argv; // unused variable
  97. }