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.

395 lines
11 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions to manipulate the variable interaction matrix.
  5. @details The interaction matrix tells whether two variables are both
  6. in the support of some function of the %DD. The main use of the
  7. interaction matrix is in the in-place swapping. Indeed, if two
  8. variables do not interact, there is no arc connecting the two
  9. layers; therefore, the swap can be performed in constant time,
  10. without scanning the subtables. Another use of the interaction
  11. matrix is in the computation of the lower bounds for
  12. sifting. Finally, the interaction matrix can be used to speed up
  13. aggregation checks in symmetric and group sifting.<p>
  14. The computation of the interaction matrix is done with a series of
  15. depth-first searches. The searches start from those nodes that have
  16. only external references. The matrix is stored as a packed array of
  17. bits; since it is symmetric, only the upper triangle is kept in
  18. memory. As a final remark, we note that there may be variables that
  19. do interact, but that for a given variable order have no arc
  20. connecting their layers when they are adjacent. For instance, in
  21. ite(a,b,c) with the order a<b<c, b and c interact, but are not
  22. connected.
  23. @author Fabio Somenzi
  24. @copyright@parblock
  25. Copyright (c) 1995-2015, Regents of the University of Colorado
  26. All rights reserved.
  27. Redistribution and use in source and binary forms, with or without
  28. modification, are permitted provided that the following conditions
  29. are met:
  30. Redistributions of source code must retain the above copyright
  31. notice, this list of conditions and the following disclaimer.
  32. Redistributions in binary form must reproduce the above copyright
  33. notice, this list of conditions and the following disclaimer in the
  34. documentation and/or other materials provided with the distribution.
  35. Neither the name of the University of Colorado nor the names of its
  36. contributors may be used to endorse or promote products derived from
  37. this software without specific prior written permission.
  38. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  41. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  42. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  43. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  44. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  48. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  49. POSSIBILITY OF SUCH DAMAGE.
  50. @endparblock
  51. */
  52. #include "util.h"
  53. #include "cuddInt.h"
  54. /*---------------------------------------------------------------------------*/
  55. /* Constant declarations */
  56. /*---------------------------------------------------------------------------*/
  57. #if SIZEOF_VOID_P == 8
  58. #define BPL 64
  59. #define LOGBPL 6
  60. #else
  61. #define BPL 32
  62. #define LOGBPL 5
  63. #endif
  64. /*---------------------------------------------------------------------------*/
  65. /* Stucture declarations */
  66. /*---------------------------------------------------------------------------*/
  67. /*---------------------------------------------------------------------------*/
  68. /* Type declarations */
  69. /*---------------------------------------------------------------------------*/
  70. /*---------------------------------------------------------------------------*/
  71. /* Variable declarations */
  72. /*---------------------------------------------------------------------------*/
  73. /*---------------------------------------------------------------------------*/
  74. /* Macro declarations */
  75. /*---------------------------------------------------------------------------*/
  76. /** \cond */
  77. /*---------------------------------------------------------------------------*/
  78. /* Static function prototypes */
  79. /*---------------------------------------------------------------------------*/
  80. static void ddSuppInteract (DdNode *f, char *support);
  81. static void ddClearLocal (DdNode *f);
  82. static void ddUpdateInteract (DdManager *table, char *support);
  83. static void ddClearGlobal (DdManager *table);
  84. /** \endcond */
  85. /*---------------------------------------------------------------------------*/
  86. /* Definition of exported functions */
  87. /*---------------------------------------------------------------------------*/
  88. /*---------------------------------------------------------------------------*/
  89. /* Definition of internal functions */
  90. /*---------------------------------------------------------------------------*/
  91. /**
  92. @brief Set interaction matrix entries.
  93. @details Given a pair of variables 0 <= x < y < table->size,
  94. sets the corresponding bit of the interaction matrix to 1.
  95. @sideeffect None
  96. */
  97. void
  98. cuddSetInteract(
  99. DdManager * table,
  100. int x,
  101. int y)
  102. {
  103. ptruint posn, word, bit;
  104. #ifdef DD_DEBUG
  105. assert(x < y);
  106. assert(y < table->size);
  107. assert(x >= 0);
  108. #endif
  109. posn = (((((ptruint)table->size << 1) - x - 3) * x) >> 1) + y - 1;
  110. word = posn >> LOGBPL;
  111. bit = posn & (BPL-1);
  112. table->interact[word] |= ((ptruint) 1) << bit;
  113. } /* end of cuddSetInteract */
  114. /**
  115. @brief Test interaction matrix entries.
  116. @details Given a pair of variables 0 <= x < y < table->size,
  117. tests whether the corresponding bit of the interaction matrix is 1.
  118. Returns the value of the bit.
  119. @sideeffect None
  120. */
  121. int
  122. cuddTestInteract(
  123. DdManager * table,
  124. int x,
  125. int y)
  126. {
  127. ptruint posn, word, bit;
  128. int result;
  129. if (x > y) {
  130. int tmp = x;
  131. x = y;
  132. y = tmp;
  133. }
  134. #ifdef DD_DEBUG
  135. assert(x < y);
  136. assert(y < table->size);
  137. assert(x >= 0);
  138. #endif
  139. posn = (((((ptruint)table->size << 1) - x - 3) * x) >> 1) + y - 1;
  140. word = posn >> LOGBPL;
  141. bit = posn & (BPL-1);
  142. result = (table->interact[word] >> bit) & (ptruint) 1;
  143. return(result);
  144. } /* end of cuddTestInteract */
  145. /**
  146. @brief Initializes the interaction matrix.
  147. @details The interaction matrix is implemented as a bit vector
  148. storing the upper triangle of the symmetric interaction matrix. The
  149. bit vector is kept in an array of ptruints. The computation is based
  150. on a series of depth-first searches, one for each root of the
  151. DAG. Two flags are needed: The local visited flag uses the LSB of
  152. the then pointer. The global visited flag uses the LSB of the next
  153. pointer.
  154. @return 1 if successful; 0 otherwise.
  155. @sideeffect None
  156. */
  157. int
  158. cuddInitInteract(
  159. DdManager * table)
  160. {
  161. unsigned int i;
  162. int j;
  163. ptruint words;
  164. ptruint *interact;
  165. char *support;
  166. DdNode *f;
  167. DdNode *sentinel = &(table->sentinel);
  168. DdNodePtr *nodelist;
  169. int slots;
  170. ptruint n = (ptruint) table->size;
  171. words = ((n * (n-1)) >> (1 + LOGBPL)) + 1;
  172. table->interact = interact = ALLOC(ptruint,words);
  173. if (interact == NULL) {
  174. table->errorCode = CUDD_MEMORY_OUT;
  175. return(0);
  176. }
  177. for (i = 0; i < words; i++) {
  178. interact[i] = 0;
  179. }
  180. support = ALLOC(char,n);
  181. if (support == NULL) {
  182. table->errorCode = CUDD_MEMORY_OUT;
  183. FREE(interact);
  184. return(0);
  185. }
  186. for (i = 0; i < n; i++) {
  187. support[i] = 0;
  188. }
  189. for (i = 0; i < n; i++) {
  190. nodelist = table->subtables[i].nodelist;
  191. slots = table->subtables[i].slots;
  192. for (j = 0; j < slots; j++) {
  193. f = nodelist[j];
  194. while (f != sentinel) {
  195. /* A node is a root of the DAG if it cannot be
  196. ** reached by nodes above it. If a node was never
  197. ** reached during the previous depth-first searches,
  198. ** then it is a root, and we start a new depth-first
  199. ** search from it.
  200. */
  201. if (!Cudd_IsComplement(f->next)) {
  202. ddSuppInteract(f,support);
  203. ddClearLocal(f);
  204. ddUpdateInteract(table,support);
  205. }
  206. f = Cudd_Regular(f->next);
  207. }
  208. }
  209. }
  210. ddClearGlobal(table);
  211. FREE(support);
  212. return(1);
  213. } /* end of cuddInitInteract */
  214. /*---------------------------------------------------------------------------*/
  215. /* Definition of static functions */
  216. /*---------------------------------------------------------------------------*/
  217. /**
  218. @brief Find the support of f.
  219. @details Performs a DFS from f. Uses the LSB of the then pointer
  220. as visited flag.
  221. @sideeffect Accumulates in support the variables on which f depends.
  222. */
  223. static void
  224. ddSuppInteract(
  225. DdNode * f,
  226. char * support)
  227. {
  228. if (cuddIsConstant(f) || Cudd_IsComplement(cuddT(f))) {
  229. return;
  230. }
  231. support[f->index] = 1;
  232. ddSuppInteract(cuddT(f),support);
  233. ddSuppInteract(Cudd_Regular(cuddE(f)),support);
  234. /* mark as visited */
  235. cuddT(f) = Cudd_Complement(cuddT(f));
  236. f->next = Cudd_Complement(f->next);
  237. return;
  238. } /* end of ddSuppInteract */
  239. /**
  240. @brief Performs a DFS from f, clearing the LSB of the then pointers.
  241. @sideeffect None
  242. */
  243. static void
  244. ddClearLocal(
  245. DdNode * f)
  246. {
  247. if (cuddIsConstant(f) || !Cudd_IsComplement(cuddT(f))) {
  248. return;
  249. }
  250. /* clear visited flag */
  251. cuddT(f) = Cudd_Regular(cuddT(f));
  252. ddClearLocal(cuddT(f));
  253. ddClearLocal(Cudd_Regular(cuddE(f)));
  254. return;
  255. } /* end of ddClearLocal */
  256. /**
  257. @brief Marks as interacting all pairs of variables that appear in
  258. support.
  259. @details If support[i == support[j] == 1, sets the (i,j) entry
  260. of the interaction matrix to 1.]
  261. @sideeffect Clears support.
  262. */
  263. static void
  264. ddUpdateInteract(
  265. DdManager * table,
  266. char * support)
  267. {
  268. int i,j;
  269. int n = table->size;
  270. for (i = 0; i < n-1; i++) {
  271. if (support[i] == 1) {
  272. support[i] = 0;
  273. for (j = i+1; j < n; j++) {
  274. if (support[j] == 1) {
  275. cuddSetInteract(table,i,j);
  276. }
  277. }
  278. }
  279. }
  280. support[n-1] = 0;
  281. } /* end of ddUpdateInteract */
  282. /**
  283. @brief Scans the %DD and clears the LSB of the next pointers.
  284. @details The LSB of the next pointers are used as markers to tell
  285. whether a node was reached by at least one DFS. Once the interaction
  286. matrix is built, these flags are reset.
  287. @sideeffect None
  288. */
  289. static void
  290. ddClearGlobal(
  291. DdManager * table)
  292. {
  293. int i,j;
  294. DdNode *f;
  295. DdNode *sentinel = &(table->sentinel);
  296. DdNodePtr *nodelist;
  297. int slots;
  298. for (i = 0; i < table->size; i++) {
  299. nodelist = table->subtables[i].nodelist;
  300. slots = table->subtables[i].slots;
  301. for (j = 0; j < slots; j++) {
  302. f = nodelist[j];
  303. while (f != sentinel) {
  304. f->next = Cudd_Regular(f->next);
  305. f = f->next;
  306. }
  307. }
  308. }
  309. } /* end of ddClearGlobal */