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.

432 lines
13 KiB

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