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.

1020 lines
28 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddExact.c]
  3. PackageName [cudd]
  4. Synopsis [Functions for exact variable reordering.]
  5. Description [External procedures included in this file:
  6. <ul>
  7. </ul>
  8. Internal procedures included in this module:
  9. <ul>
  10. <li> cuddExact()
  11. </ul>
  12. Static procedures included in this module:
  13. <ul>
  14. <li> getMaxBinomial()
  15. <li> gcd()
  16. <li> getMatrix()
  17. <li> freeMatrix()
  18. <li> getLevelKeys()
  19. <li> ddShuffle()
  20. <li> ddSiftUp()
  21. <li> updateUB()
  22. <li> ddCountRoots()
  23. <li> ddClearGlobal()
  24. <li> computeLB()
  25. <li> updateEntry()
  26. <li> pushDown()
  27. <li> initSymmInfo()
  28. </ul>]
  29. Author [Cheng Hua, Fabio Somenzi]
  30. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  31. All rights reserved.
  32. Redistribution and use in source and binary forms, with or without
  33. modification, are permitted provided that the following conditions
  34. are met:
  35. Redistributions of source code must retain the above copyright
  36. notice, this list of conditions and the following disclaimer.
  37. Redistributions in binary form must reproduce the above copyright
  38. notice, this list of conditions and the following disclaimer in the
  39. documentation and/or other materials provided with the distribution.
  40. Neither the name of the University of Colorado nor the names of its
  41. contributors may be used to endorse or promote products derived from
  42. this software without specific prior written permission.
  43. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  46. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  47. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  48. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  49. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  50. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  53. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54. POSSIBILITY OF SUCH DAMAGE.]
  55. ******************************************************************************/
  56. #include "util.h"
  57. #include "cuddInt.h"
  58. /*---------------------------------------------------------------------------*/
  59. /* Constant declarations */
  60. /*---------------------------------------------------------------------------*/
  61. /*---------------------------------------------------------------------------*/
  62. /* Stucture declarations */
  63. /*---------------------------------------------------------------------------*/
  64. /*---------------------------------------------------------------------------*/
  65. /* Type declarations */
  66. /*---------------------------------------------------------------------------*/
  67. /*---------------------------------------------------------------------------*/
  68. /* Variable declarations */
  69. /*---------------------------------------------------------------------------*/
  70. #ifndef lint
  71. static char rcsid[] DD_UNUSED = "$Id: cuddExact.c,v 1.30 2012/02/05 01:07:18 fabio Exp $";
  72. #endif
  73. #ifdef DD_STATS
  74. static int ddTotalShuffles;
  75. #endif
  76. /*---------------------------------------------------------------------------*/
  77. /* Macro declarations */
  78. /*---------------------------------------------------------------------------*/
  79. /**AutomaticStart*************************************************************/
  80. /*---------------------------------------------------------------------------*/
  81. /* Static function prototypes */
  82. /*---------------------------------------------------------------------------*/
  83. static int getMaxBinomial (int n);
  84. static DdHalfWord ** getMatrix (int rows, int cols);
  85. static void freeMatrix (DdHalfWord **matrix);
  86. static int getLevelKeys (DdManager *table, int l);
  87. static int ddShuffle (DdManager *table, DdHalfWord *permutation, int lower, int upper);
  88. static int ddSiftUp (DdManager *table, int x, int xLow);
  89. static int updateUB (DdManager *table, int oldBound, DdHalfWord *bestOrder, int lower, int upper);
  90. static int ddCountRoots (DdManager *table, int lower, int upper);
  91. static void ddClearGlobal (DdManager *table, int lower, int maxlevel);
  92. static int computeLB (DdManager *table, DdHalfWord *order, int roots, int cost, int lower, int upper, int level);
  93. static int updateEntry (DdManager *table, DdHalfWord *order, int level, int cost, DdHalfWord **orders, int *costs, int subsets, char *mask, int lower, int upper);
  94. static void pushDown (DdHalfWord *order, int j, int level);
  95. static DdHalfWord * initSymmInfo (DdManager *table, int lower, int upper);
  96. static int checkSymmInfo (DdManager *table, DdHalfWord *symmInfo, int index, int level);
  97. /**AutomaticEnd***************************************************************/
  98. /*---------------------------------------------------------------------------*/
  99. /* Definition of exported functions */
  100. /*---------------------------------------------------------------------------*/
  101. /*---------------------------------------------------------------------------*/
  102. /* Definition of internal functions */
  103. /*---------------------------------------------------------------------------*/
  104. /**Function********************************************************************
  105. Synopsis [Exact variable ordering algorithm.]
  106. Description [Exact variable ordering algorithm. Finds an optimum
  107. order for the variables between lower and upper. Returns 1 if
  108. successful; 0 otherwise.]
  109. SideEffects [None]
  110. SeeAlso []
  111. ******************************************************************************/
  112. int
  113. cuddExact(
  114. DdManager * table,
  115. int lower,
  116. int upper)
  117. {
  118. int k, i, j;
  119. int maxBinomial, oldSubsets, newSubsets;
  120. int subsetCost;
  121. int size; /* number of variables to be reordered */
  122. int unused, nvars, level, result;
  123. int upperBound, lowerBound, cost;
  124. int roots;
  125. char *mask = NULL;
  126. DdHalfWord *symmInfo = NULL;
  127. DdHalfWord **newOrder = NULL;
  128. DdHalfWord **oldOrder = NULL;
  129. int *newCost = NULL;
  130. int *oldCost = NULL;
  131. DdHalfWord **tmpOrder;
  132. int *tmpCost;
  133. DdHalfWord *bestOrder = NULL;
  134. DdHalfWord *order;
  135. #ifdef DD_STATS
  136. int ddTotalSubsets;
  137. #endif
  138. /* Restrict the range to be reordered by excluding unused variables
  139. ** at the two ends. */
  140. while (table->subtables[lower].keys == 1 &&
  141. table->vars[table->invperm[lower]]->ref == 1 &&
  142. lower < upper)
  143. lower++;
  144. while (table->subtables[upper].keys == 1 &&
  145. table->vars[table->invperm[upper]]->ref == 1 &&
  146. lower < upper)
  147. upper--;
  148. if (lower == upper) return(1); /* trivial problem */
  149. /* Apply symmetric sifting to get a good upper bound and to extract
  150. ** symmetry information. */
  151. result = cuddSymmSiftingConv(table,lower,upper);
  152. if (result == 0) goto cuddExactOutOfMem;
  153. #ifdef DD_STATS
  154. (void) fprintf(table->out,"\n");
  155. ddTotalShuffles = 0;
  156. ddTotalSubsets = 0;
  157. #endif
  158. /* Initialization. */
  159. nvars = table->size;
  160. size = upper - lower + 1;
  161. /* Count unused variable among those to be reordered. This is only
  162. ** used to compute maxBinomial. */
  163. unused = 0;
  164. for (i = lower + 1; i < upper; i++) {
  165. if (table->subtables[i].keys == 1 &&
  166. table->vars[table->invperm[i]]->ref == 1)
  167. unused++;
  168. }
  169. /* Find the maximum number of subsets we may have to store. */
  170. maxBinomial = getMaxBinomial(size - unused);
  171. if (maxBinomial == -1) goto cuddExactOutOfMem;
  172. newOrder = getMatrix(maxBinomial, size);
  173. if (newOrder == NULL) goto cuddExactOutOfMem;
  174. newCost = ALLOC(int, maxBinomial);
  175. if (newCost == NULL) goto cuddExactOutOfMem;
  176. oldOrder = getMatrix(maxBinomial, size);
  177. if (oldOrder == NULL) goto cuddExactOutOfMem;
  178. oldCost = ALLOC(int, maxBinomial);
  179. if (oldCost == NULL) goto cuddExactOutOfMem;
  180. bestOrder = ALLOC(DdHalfWord, size);
  181. if (bestOrder == NULL) goto cuddExactOutOfMem;
  182. mask = ALLOC(char, nvars);
  183. if (mask == NULL) goto cuddExactOutOfMem;
  184. symmInfo = initSymmInfo(table, lower, upper);
  185. if (symmInfo == NULL) goto cuddExactOutOfMem;
  186. roots = ddCountRoots(table, lower, upper);
  187. /* Initialize the old order matrix for the empty subset and the best
  188. ** order to the current order. The cost for the empty subset includes
  189. ** the cost of the levels between upper and the constants. These levels
  190. ** are not going to change. Hence, we count them only once.
  191. */
  192. oldSubsets = 1;
  193. for (i = 0; i < size; i++) {
  194. oldOrder[0][i] = bestOrder[i] = (DdHalfWord) table->invperm[i+lower];
  195. }
  196. subsetCost = table->constants.keys;
  197. for (i = upper + 1; i < nvars; i++)
  198. subsetCost += getLevelKeys(table,i);
  199. oldCost[0] = subsetCost;
  200. /* The upper bound is initialized to the current size of the BDDs. */
  201. upperBound = table->keys - table->isolated;
  202. /* Now consider subsets of increasing size. */
  203. for (k = 1; k <= size; k++) {
  204. #ifdef DD_STATS
  205. (void) fprintf(table->out,"Processing subsets of size %d\n", k);
  206. fflush(table->out);
  207. #endif
  208. newSubsets = 0;
  209. level = size - k; /* offset of first bottom variable */
  210. for (i = 0; i < oldSubsets; i++) { /* for each subset of size k-1 */
  211. order = oldOrder[i];
  212. cost = oldCost[i];
  213. lowerBound = computeLB(table, order, roots, cost, lower, upper,
  214. level);
  215. if (lowerBound >= upperBound)
  216. continue;
  217. /* Impose new order. */
  218. result = ddShuffle(table, order, lower, upper);
  219. if (result == 0) goto cuddExactOutOfMem;
  220. upperBound = updateUB(table,upperBound,bestOrder,lower,upper);
  221. /* For each top bottom variable. */
  222. for (j = level; j >= 0; j--) {
  223. /* Skip unused variables. */
  224. if (table->subtables[j+lower-1].keys == 1 &&
  225. table->vars[table->invperm[j+lower-1]]->ref == 1) continue;
  226. /* Find cost under this order. */
  227. subsetCost = cost + getLevelKeys(table, lower + level);
  228. newSubsets = updateEntry(table, order, level, subsetCost,
  229. newOrder, newCost, newSubsets, mask,
  230. lower, upper);
  231. if (j == 0)
  232. break;
  233. if (checkSymmInfo(table, symmInfo, order[j-1], level) == 0)
  234. continue;
  235. pushDown(order,j-1,level);
  236. /* Impose new order. */
  237. result = ddShuffle(table, order, lower, upper);
  238. if (result == 0) goto cuddExactOutOfMem;
  239. upperBound = updateUB(table,upperBound,bestOrder,lower,upper);
  240. } /* for each bottom variable */
  241. } /* for each subset of size k */
  242. /* New orders become old orders in preparation for next iteration. */
  243. tmpOrder = oldOrder; tmpCost = oldCost;
  244. oldOrder = newOrder; oldCost = newCost;
  245. newOrder = tmpOrder; newCost = tmpCost;
  246. #ifdef DD_STATS
  247. ddTotalSubsets += newSubsets;
  248. #endif
  249. oldSubsets = newSubsets;
  250. }
  251. result = ddShuffle(table, bestOrder, lower, upper);
  252. if (result == 0) goto cuddExactOutOfMem;
  253. #ifdef DD_STATS
  254. #ifdef DD_VERBOSE
  255. (void) fprintf(table->out,"\n");
  256. #endif
  257. (void) fprintf(table->out,"#:S_EXACT %8d: total subsets\n",
  258. ddTotalSubsets);
  259. (void) fprintf(table->out,"#:H_EXACT %8d: total shuffles",
  260. ddTotalShuffles);
  261. #endif
  262. freeMatrix(newOrder);
  263. freeMatrix(oldOrder);
  264. FREE(bestOrder);
  265. FREE(oldCost);
  266. FREE(newCost);
  267. FREE(symmInfo);
  268. FREE(mask);
  269. return(1);
  270. cuddExactOutOfMem:
  271. if (newOrder != NULL) freeMatrix(newOrder);
  272. if (oldOrder != NULL) freeMatrix(oldOrder);
  273. if (bestOrder != NULL) FREE(bestOrder);
  274. if (oldCost != NULL) FREE(oldCost);
  275. if (newCost != NULL) FREE(newCost);
  276. if (symmInfo != NULL) FREE(symmInfo);
  277. if (mask != NULL) FREE(mask);
  278. table->errorCode = CUDD_MEMORY_OUT;
  279. return(0);
  280. } /* end of cuddExact */
  281. /**Function********************************************************************
  282. Synopsis [Returns the maximum value of (n choose k) for a given n.]
  283. Description [Computes the maximum value of (n choose k) for a given
  284. n. The maximum value occurs for k = n/2 when n is even, or k =
  285. (n-1)/2 when n is odd. The algorithm used in this procedure avoids
  286. intermediate overflow problems. It is based on the identity
  287. <pre>
  288. binomial(n,k) = n/k * binomial(n-1,k-1).
  289. </pre>
  290. Returns the computed value if successful; -1 if out of range.]
  291. SideEffects [None]
  292. SeeAlso []
  293. ******************************************************************************/
  294. static int
  295. getMaxBinomial(
  296. int n)
  297. {
  298. double i, j, result;
  299. if (n < 0 || n > 33) return(-1); /* error */
  300. if (n < 2) return(1);
  301. for (result = (double)((n+3)/2), i = result+1, j=2; i <= n; i++, j++) {
  302. result *= i;
  303. result /= j;
  304. }
  305. return((int)result);
  306. } /* end of getMaxBinomial */
  307. #if 0
  308. /**Function********************************************************************
  309. Synopsis [Returns the gcd of two integers.]
  310. Description [Returns the gcd of two integers. Uses the binary GCD
  311. algorithm described in Cormen, Leiserson, and Rivest.]
  312. SideEffects [None]
  313. SeeAlso []
  314. ******************************************************************************/
  315. static int
  316. gcd(
  317. int x,
  318. int y)
  319. {
  320. int a;
  321. int b;
  322. int lsbMask;
  323. /* GCD(n,0) = n. */
  324. if (x == 0) return(y);
  325. if (y == 0) return(x);
  326. a = x; b = y; lsbMask = 1;
  327. /* Here both a and b are != 0. The iteration maintains this invariant.
  328. ** Hence, we only need to check for when they become equal.
  329. */
  330. while (a != b) {
  331. if (a & lsbMask) {
  332. if (b & lsbMask) { /* both odd */
  333. if (a < b) {
  334. b = (b - a) >> 1;
  335. } else {
  336. a = (a - b) >> 1;
  337. }
  338. } else { /* a odd, b even */
  339. b >>= 1;
  340. }
  341. } else {
  342. if (b & lsbMask) { /* a even, b odd */
  343. a >>= 1;
  344. } else { /* both even */
  345. lsbMask <<= 1;
  346. }
  347. }
  348. }
  349. return(a);
  350. } /* end of gcd */
  351. #endif
  352. /**Function********************************************************************
  353. Synopsis [Allocates a two-dimensional matrix of ints.]
  354. Description [Allocates a two-dimensional matrix of ints.
  355. Returns the pointer to the matrix if successful; NULL otherwise.]
  356. SideEffects [None]
  357. SeeAlso [freeMatrix]
  358. ******************************************************************************/
  359. static DdHalfWord **
  360. getMatrix(
  361. int rows /* number of rows */,
  362. int cols /* number of columns */)
  363. {
  364. DdHalfWord **matrix;
  365. int i;
  366. if (cols*rows == 0) return(NULL);
  367. matrix = ALLOC(DdHalfWord *, rows);
  368. if (matrix == NULL) return(NULL);
  369. matrix[0] = ALLOC(DdHalfWord, cols*rows);
  370. if (matrix[0] == NULL) {
  371. FREE(matrix);
  372. return(NULL);
  373. }
  374. for (i = 1; i < rows; i++) {
  375. matrix[i] = matrix[i-1] + cols;
  376. }
  377. return(matrix);
  378. } /* end of getMatrix */
  379. /**Function********************************************************************
  380. Synopsis [Frees a two-dimensional matrix allocated by getMatrix.]
  381. Description []
  382. SideEffects [None]
  383. SeeAlso [getMatrix]
  384. ******************************************************************************/
  385. static void
  386. freeMatrix(
  387. DdHalfWord ** matrix)
  388. {
  389. FREE(matrix[0]);
  390. FREE(matrix);
  391. return;
  392. } /* end of freeMatrix */
  393. /**Function********************************************************************
  394. Synopsis [Returns the number of nodes at one level of a unique table.]
  395. Description [Returns the number of nodes at one level of a unique table.
  396. The projection function, if isolated, is not counted.]
  397. SideEffects [None]
  398. SeeAlso []
  399. ******************************************************************************/
  400. static int
  401. getLevelKeys(
  402. DdManager * table,
  403. int l)
  404. {
  405. int isolated;
  406. int x; /* x is an index */
  407. x = table->invperm[l];
  408. isolated = table->vars[x]->ref == 1;
  409. return(table->subtables[l].keys - isolated);
  410. } /* end of getLevelKeys */
  411. /**Function********************************************************************
  412. Synopsis [Reorders variables according to a given permutation.]
  413. Description [Reorders variables according to a given permutation.
  414. The i-th permutation array contains the index of the variable that
  415. should be brought to the i-th level. ddShuffle assumes that no
  416. dead nodes are present and that the interaction matrix is properly
  417. initialized. The reordering is achieved by a series of upward sifts.
  418. Returns 1 if successful; 0 otherwise.]
  419. SideEffects [None]
  420. SeeAlso []
  421. ******************************************************************************/
  422. static int
  423. ddShuffle(
  424. DdManager * table,
  425. DdHalfWord * permutation,
  426. int lower,
  427. int upper)
  428. {
  429. DdHalfWord index;
  430. int level;
  431. int position;
  432. #if 0
  433. int numvars;
  434. #endif
  435. int result;
  436. #ifdef DD_STATS
  437. unsigned long localTime;
  438. int initialSize;
  439. #ifdef DD_VERBOSE
  440. int finalSize;
  441. #endif
  442. int previousSize;
  443. #endif
  444. #ifdef DD_STATS
  445. localTime = util_cpu_time();
  446. initialSize = table->keys - table->isolated;
  447. #endif
  448. #if 0
  449. numvars = table->size;
  450. (void) fprintf(table->out,"%d:", ddTotalShuffles);
  451. for (level = 0; level < numvars; level++) {
  452. (void) fprintf(table->out," %d", table->invperm[level]);
  453. }
  454. (void) fprintf(table->out,"\n");
  455. #endif
  456. for (level = 0; level <= upper - lower; level++) {
  457. index = permutation[level];
  458. position = table->perm[index];
  459. #ifdef DD_STATS
  460. previousSize = table->keys - table->isolated;
  461. #endif
  462. result = ddSiftUp(table,position,level+lower);
  463. if (!result) return(0);
  464. }
  465. #ifdef DD_STATS
  466. ddTotalShuffles++;
  467. #ifdef DD_VERBOSE
  468. finalSize = table->keys - table->isolated;
  469. if (finalSize < initialSize) {
  470. (void) fprintf(table->out,"-");
  471. } else if (finalSize > initialSize) {
  472. (void) fprintf(table->out,"+");
  473. } else {
  474. (void) fprintf(table->out,"=");
  475. }
  476. if ((ddTotalShuffles & 63) == 0) (void) fprintf(table->out,"\n");
  477. fflush(table->out);
  478. #endif
  479. #endif
  480. return(1);
  481. } /* end of ddShuffle */
  482. /**Function********************************************************************
  483. Synopsis [Moves one variable up.]
  484. Description [Takes a variable from position x and sifts it up to
  485. position xLow; xLow should be less than or equal to x.
  486. Returns 1 if successful; 0 otherwise]
  487. SideEffects [None]
  488. SeeAlso []
  489. ******************************************************************************/
  490. static int
  491. ddSiftUp(
  492. DdManager * table,
  493. int x,
  494. int xLow)
  495. {
  496. int y;
  497. int size;
  498. y = cuddNextLow(table,x);
  499. while (y >= xLow) {
  500. size = cuddSwapInPlace(table,y,x);
  501. if (size == 0) {
  502. return(0);
  503. }
  504. x = y;
  505. y = cuddNextLow(table,x);
  506. }
  507. return(1);
  508. } /* end of ddSiftUp */
  509. /**Function********************************************************************
  510. Synopsis [Updates the upper bound and saves the best order seen so far.]
  511. Description [Updates the upper bound and saves the best order seen so far.
  512. Returns the current value of the upper bound.]
  513. SideEffects [None]
  514. SeeAlso []
  515. ******************************************************************************/
  516. static int
  517. updateUB(
  518. DdManager * table,
  519. int oldBound,
  520. DdHalfWord * bestOrder,
  521. int lower,
  522. int upper)
  523. {
  524. int i;
  525. int newBound = table->keys - table->isolated;
  526. if (newBound < oldBound) {
  527. #ifdef DD_STATS
  528. (void) fprintf(table->out,"New upper bound = %d\n", newBound);
  529. fflush(table->out);
  530. #endif
  531. for (i = lower; i <= upper; i++)
  532. bestOrder[i-lower] = (DdHalfWord) table->invperm[i];
  533. return(newBound);
  534. } else {
  535. return(oldBound);
  536. }
  537. } /* end of updateUB */
  538. /**Function********************************************************************
  539. Synopsis [Counts the number of roots.]
  540. Description [Counts the number of roots at the levels between lower and
  541. upper. The computation is based on breadth-first search.
  542. A node is a root if it is not reachable from any previously visited node.
  543. (All the nodes at level lower are therefore considered roots.)
  544. The visited flag uses the LSB of the next pointer. Returns the root
  545. count. The roots that are constant nodes are always ignored.]
  546. SideEffects [None]
  547. SeeAlso [ddClearGlobal]
  548. ******************************************************************************/
  549. static int
  550. ddCountRoots(
  551. DdManager * table,
  552. int lower,
  553. int upper)
  554. {
  555. int i,j;
  556. DdNode *f;
  557. DdNodePtr *nodelist;
  558. DdNode *sentinel = &(table->sentinel);
  559. int slots;
  560. int roots = 0;
  561. int maxlevel = lower;
  562. for (i = lower; i <= upper; i++) {
  563. nodelist = table->subtables[i].nodelist;
  564. slots = table->subtables[i].slots;
  565. for (j = 0; j < slots; j++) {
  566. f = nodelist[j];
  567. while (f != sentinel) {
  568. /* A node is a root of the DAG if it cannot be
  569. ** reached by nodes above it. If a node was never
  570. ** reached during the previous depth-first searches,
  571. ** then it is a root, and we start a new depth-first
  572. ** search from it.
  573. */
  574. if (!Cudd_IsComplement(f->next)) {
  575. if (f != table->vars[f->index]) {
  576. roots++;
  577. }
  578. }
  579. if (!Cudd_IsConstant(cuddT(f))) {
  580. cuddT(f)->next = Cudd_Complement(cuddT(f)->next);
  581. if (table->perm[cuddT(f)->index] > maxlevel)
  582. maxlevel = table->perm[cuddT(f)->index];
  583. }
  584. if (!Cudd_IsConstant(cuddE(f))) {
  585. Cudd_Regular(cuddE(f))->next =
  586. Cudd_Complement(Cudd_Regular(cuddE(f))->next);
  587. if (table->perm[Cudd_Regular(cuddE(f))->index] > maxlevel)
  588. maxlevel = table->perm[Cudd_Regular(cuddE(f))->index];
  589. }
  590. f = Cudd_Regular(f->next);
  591. }
  592. }
  593. }
  594. ddClearGlobal(table, lower, maxlevel);
  595. return(roots);
  596. } /* end of ddCountRoots */
  597. /**Function********************************************************************
  598. Synopsis [Scans the DD and clears the LSB of the next pointers.]
  599. Description [Scans the DD and clears the LSB of the next pointers.
  600. The LSB of the next pointers are used as markers to tell whether a
  601. node was reached. Once the roots are counted, these flags are
  602. reset.]
  603. SideEffects [None]
  604. SeeAlso [ddCountRoots]
  605. ******************************************************************************/
  606. static void
  607. ddClearGlobal(
  608. DdManager * table,
  609. int lower,
  610. int maxlevel)
  611. {
  612. int i,j;
  613. DdNode *f;
  614. DdNodePtr *nodelist;
  615. DdNode *sentinel = &(table->sentinel);
  616. int slots;
  617. for (i = lower; i <= maxlevel; i++) {
  618. nodelist = table->subtables[i].nodelist;
  619. slots = table->subtables[i].slots;
  620. for (j = 0; j < slots; j++) {
  621. f = nodelist[j];
  622. while (f != sentinel) {
  623. f->next = Cudd_Regular(f->next);
  624. f = f->next;
  625. }
  626. }
  627. }
  628. } /* end of ddClearGlobal */
  629. /**Function********************************************************************
  630. Synopsis [Computes a lower bound on the size of a BDD.]
  631. Description [Computes a lower bound on the size of a BDD from the
  632. following factors:
  633. <ul>
  634. <li> size of the lower part of it;
  635. <li> size of the part of the upper part not subjected to reordering;
  636. <li> number of roots in the part of the BDD subjected to reordering;
  637. <li> variable in the support of the roots in the upper part of the
  638. BDD subjected to reordering.
  639. <ul/>]
  640. SideEffects [None]
  641. SeeAlso []
  642. ******************************************************************************/
  643. static int
  644. computeLB(
  645. DdManager * table /* manager */,
  646. DdHalfWord * order /* optimal order for the subset */,
  647. int roots /* roots between lower and upper */,
  648. int cost /* minimum cost for the subset */,
  649. int lower /* lower level to be reordered */,
  650. int upper /* upper level to be reordered */,
  651. int level /* offset for the current top bottom var */
  652. )
  653. {
  654. int i;
  655. int lb = cost;
  656. int lb1 = 0;
  657. int lb2;
  658. int support;
  659. DdHalfWord ref;
  660. /* The levels not involved in reordering are not going to change.
  661. ** Add their sizes to the lower bound.
  662. */
  663. for (i = 0; i < lower; i++) {
  664. lb += getLevelKeys(table,i);
  665. }
  666. /* If a variable is in the support, then there is going
  667. ** to be at least one node labeled by that variable.
  668. */
  669. for (i = lower; i <= lower+level; i++) {
  670. support = table->subtables[i].keys > 1 ||
  671. table->vars[order[i-lower]]->ref > 1;
  672. lb1 += support;
  673. }
  674. /* Estimate the number of nodes required to connect the roots to
  675. ** the nodes in the bottom part. */
  676. if (lower+level+1 < table->size) {
  677. if (lower+level < upper)
  678. ref = table->vars[order[level+1]]->ref;
  679. else
  680. ref = table->vars[table->invperm[upper+1]]->ref;
  681. lb2 = table->subtables[lower+level+1].keys -
  682. (ref > (DdHalfWord) 1) - roots;
  683. } else {
  684. lb2 = 0;
  685. }
  686. lb += lb1 > lb2 ? lb1 : lb2;
  687. return(lb);
  688. } /* end of computeLB */
  689. /**Function********************************************************************
  690. Synopsis [Updates entry for a subset.]
  691. Description [Updates entry for a subset. Finds the subset, if it exists.
  692. If the new order for the subset has lower cost, or if the subset did not
  693. exist, it stores the new order and cost. Returns the number of subsets
  694. currently in the table.]
  695. SideEffects [None]
  696. SeeAlso []
  697. ******************************************************************************/
  698. static int
  699. updateEntry(
  700. DdManager * table,
  701. DdHalfWord * order,
  702. int level,
  703. int cost,
  704. DdHalfWord ** orders,
  705. int * costs,
  706. int subsets,
  707. char * mask,
  708. int lower,
  709. int upper)
  710. {
  711. int i, j;
  712. int size = upper - lower + 1;
  713. /* Build a mask that says what variables are in this subset. */
  714. for (i = lower; i <= upper; i++)
  715. mask[table->invperm[i]] = 0;
  716. for (i = level; i < size; i++)
  717. mask[order[i]] = 1;
  718. /* Check each subset until a match is found or all subsets are examined. */
  719. for (i = 0; i < subsets; i++) {
  720. DdHalfWord *subset = orders[i];
  721. for (j = level; j < size; j++) {
  722. if (mask[subset[j]] == 0)
  723. break;
  724. }
  725. if (j == size) /* no mismatches: success */
  726. break;
  727. }
  728. if (i == subsets || cost < costs[i]) { /* add or replace */
  729. for (j = 0; j < size; j++)
  730. orders[i][j] = order[j];
  731. costs[i] = cost;
  732. subsets += (i == subsets);
  733. }
  734. return(subsets);
  735. } /* end of updateEntry */
  736. /**Function********************************************************************
  737. Synopsis [Pushes a variable in the order down to position "level."]
  738. Description []
  739. SideEffects [None]
  740. SeeAlso []
  741. ******************************************************************************/
  742. static void
  743. pushDown(
  744. DdHalfWord * order,
  745. int j,
  746. int level)
  747. {
  748. int i;
  749. DdHalfWord tmp;
  750. tmp = order[j];
  751. for (i = j; i < level; i++) {
  752. order[i] = order[i+1];
  753. }
  754. order[level] = tmp;
  755. return;
  756. } /* end of pushDown */
  757. /**Function********************************************************************
  758. Synopsis [Gathers symmetry information.]
  759. Description [Translates the symmetry information stored in the next
  760. field of each subtable from level to indices. This procedure is called
  761. immediately after symmetric sifting, so that the next fields are correct.
  762. By translating this informaton in terms of indices, we make it independent
  763. of subsequent reorderings. The format used is that of the next fields:
  764. a circular list where each variable points to the next variable in the
  765. same symmetry group. Only the entries between lower and upper are
  766. considered. The procedure returns a pointer to an array
  767. holding the symmetry information if successful; NULL otherwise.]
  768. SideEffects [None]
  769. SeeAlso [checkSymmInfo]
  770. ******************************************************************************/
  771. static DdHalfWord *
  772. initSymmInfo(
  773. DdManager * table,
  774. int lower,
  775. int upper)
  776. {
  777. int level, index, next, nextindex;
  778. DdHalfWord *symmInfo;
  779. symmInfo = ALLOC(DdHalfWord, table->size);
  780. if (symmInfo == NULL) return(NULL);
  781. for (level = lower; level <= upper; level++) {
  782. index = table->invperm[level];
  783. next = table->subtables[level].next;
  784. nextindex = table->invperm[next];
  785. symmInfo[index] = nextindex;
  786. }
  787. return(symmInfo);
  788. } /* end of initSymmInfo */
  789. /**Function********************************************************************
  790. Synopsis [Check symmetry condition.]
  791. Description [Returns 1 if a variable is the one with the highest index
  792. among those belonging to a symmetry group that are in the top part of
  793. the BDD. The top part is given by level.]
  794. SideEffects [None]
  795. SeeAlso [initSymmInfo]
  796. ******************************************************************************/
  797. static int
  798. checkSymmInfo(
  799. DdManager * table,
  800. DdHalfWord * symmInfo,
  801. int index,
  802. int level)
  803. {
  804. int i;
  805. i = symmInfo[index];
  806. while (i != index) {
  807. if (index < i && table->perm[i] <= level)
  808. return(0);
  809. i = symmInfo[i];
  810. }
  811. return(1);
  812. } /* end of checkSymmInfo */