testmulti.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. @file
  3. @ingroup cplusplus
  4. @brief Test program for multiple managers (one per thread).
  5. @details This program tests the ability to run different CUDD managers
  6. in different threads. Each thread builds the hidden weight bit function
  7. for a certain number of variables and then reorders the variables.
  8. @author Fabio Somenzi
  9. @copyright@parblock
  10. Copyright (c) 1995-2015, Regents of the University of Colorado
  11. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions
  14. are met:
  15. Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. Redistributions in binary form must reproduce the above copyright
  18. notice, this list of conditions and the following disclaimer in the
  19. documentation and/or other materials provided with the distribution.
  20. Neither the name of the University of Colorado nor the names of its
  21. contributors may be used to endorse or promote products derived from
  22. this software without specific prior written permission.
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. POSSIBILITY OF SUCH DAMAGE.
  35. @endparblock
  36. */
  37. #include "config.h"
  38. #include "cuddObj.hh"
  39. #include <cstdlib>
  40. #include <iostream>
  41. #include <sstream>
  42. #if HAVE_WORKING_THREAD == 1
  43. #include <thread>
  44. /**
  45. * @brief Taks performed by each thread.
  46. */
  47. class Task {
  48. public:
  49. /** Constructor. */
  50. Task(int n, std::ostringstream & os) : n(n), os(os) {}
  51. /** Builds the hidden weight bit function and reorders the variables. */
  52. void operator()(void) {
  53. Cudd mgr;
  54. mgr.AutodynEnable();
  55. int nvars = n + 32;
  56. std::vector<BDD> vars;
  57. for (int i = 0; i != nvars; ++i) {
  58. vars.push_back(mgr.bddVar());
  59. }
  60. os << "Report from thread " << n << " with " << nvars << " variables: ";
  61. // The hidden weight bit function is built from a tally circuit and
  62. // a multiplexer. First the tally circuit...
  63. std::vector<BDD> oldt;
  64. oldt.push_back(mgr.bddOne());
  65. std::vector<BDD> t;
  66. for (int i = 1; i != nvars + 1; ++i) {
  67. t.clear();
  68. t.push_back(oldt.at(0) & !vars.at(i-1));
  69. for (int j = 1; j != i; ++j) {
  70. t.push_back(vars.at(i-1).Ite(oldt.at(j-1), oldt.at(j)));
  71. }
  72. t.push_back(oldt.at(i-1) & vars.at(i-1));
  73. oldt = t;
  74. }
  75. #if 0
  76. // Diagnostic print.
  77. for (int i = 0; i != nvars + 1; ++i) {
  78. os << "t(" << i << ") = " << t.at(i).FactoredFormString()
  79. << std::endl;
  80. }
  81. #endif
  82. // ...then the multiplexer.
  83. BDD hwb = mgr.bddZero();
  84. for (int i = 0; i != nvars; ++i) {
  85. hwb |= t.at(i+1) & vars.at(i);
  86. }
  87. mgr.ReduceHeap(CUDD_REORDER_SIFT_CONVERGE);
  88. int nodes = hwb.nodeCount();
  89. os << nodes << " nodes and ";
  90. int digits;
  91. DdApaNumber apa_minterms = hwb.ApaCountMinterm(nvars, &digits);
  92. os << mgr.ApaStringDecimal(digits, apa_minterms) << " minterms\n";
  93. free(apa_minterms);
  94. os << "Variable order: " << mgr.OrderString() << "\n";
  95. mgr.Srandom(n+11);
  96. os << "A random number from our generator: " << mgr.Random() << "\n";
  97. #if 0
  98. // Diagnostic prints.
  99. hwb.summary(nvars);
  100. os << hwb.FactoredFormString() << std::endl;
  101. #endif
  102. }
  103. private:
  104. int n;
  105. std::ostringstream & os;
  106. };
  107. /**
  108. * @brief Class to join threads in RAII fashion.
  109. */
  110. class joinThreads {
  111. public:
  112. explicit joinThreads(std::vector<std::thread>& t) : threads_(t) {}
  113. /** It completes once all threads have been joined. */
  114. ~joinThreads() {
  115. for (std::vector<std::thread>::iterator it = threads_.begin();
  116. it != threads_.end(); ++it)
  117. if (it->joinable())
  118. it->join();
  119. }
  120. joinThreads(joinThreads const &) = delete;
  121. joinThreads& operator=(joinThreads const &) = delete;
  122. private:
  123. std::vector<std::thread>& threads_; /**< vector of threads to be joined. */
  124. };
  125. #endif
  126. /**
  127. @brief Main program for testmulti.
  128. */
  129. int main(int argc, char **argv)
  130. {
  131. int nthreads = 4; // default value
  132. // If there's an argument, it's the number of threads.
  133. if (argc == 2) {
  134. int nread;
  135. int retval = sscanf(argv[1], "%d%n", &nthreads, &nread);
  136. if (retval != 1 || argv[1][nread]) {
  137. std::cerr << "The argument should be an integer." << std::endl;
  138. return 1;
  139. }
  140. } else if (argc != 1) {
  141. std::cerr << "Either no arguments or one argument." << std::endl;
  142. return 1;
  143. }
  144. #if HAVE_WORKING_THREAD == 1
  145. // Each thread has its own output stream, so that main can print thread
  146. // reports without interleaving. We can't use an std::vector here because
  147. // old versions of g++ don't support move semantics (and streams can't
  148. // be copied). We can't use a variable-length array either because it's
  149. // a g++ extension and clang++ rejects it. Hence we use new/delete.
  150. std::ostringstream *oss = new std::ostringstream[nthreads];
  151. // Multi-threaded code in this block.
  152. {
  153. std::vector<std::thread> t;
  154. joinThreads joiner(t); // threads are joined by its destructor
  155. for (int n = 0; n != nthreads; ++n) {
  156. t.push_back(std::thread(Task(n, oss[n])));
  157. }
  158. }
  159. // Print the reports.
  160. for (int n = 0; n != nthreads; ++n) {
  161. std::cout << oss[n].str();
  162. }
  163. delete [] oss;
  164. return 0;
  165. #else
  166. return 77; // test skipped
  167. #endif
  168. }