82 #define DD_MAX_SUBTABLE_SPARSITY 8 83 #define DD_SHRINK_FACTOR 2 98 static char rcsid[]
DD_UNUSED =
"$Id: cuddReorder.c,v 1.71 2012/02/05 01:07:19 fabio Exp $";
179 unsigned int nextDyn;
181 unsigned int initialSize;
182 unsigned int finalSize;
184 unsigned long localTime;
187 if (table->
keys - table->
dead < (
unsigned) minsize)
206 while (hook != NULL) {
207 int res = (hook->
f)(table,
"BDD", (
void *)heuristic);
208 if (res == 0)
return(0);
225 (void) fprintf(table->
out,
"#:I_RANDOM ");
233 (void) fprintf(table->
out,
"#:I_SIFTING ");
241 (void) fprintf(table->
out,
"#:I_WINDOW ");
244 (void) fprintf(table->
out,
"#:I_ANNEAL ");
247 (void) fprintf(table->
out,
"#:I_GENETIC ");
251 (void) fprintf(table->
out,
"#:I_LINSIFT ");
254 (void) fprintf(table->
out,
"#:I_EXACT ");
259 (void) fprintf(table->
out,
"%8d: initial size",initialSize);
273 (void) fprintf(table->
out,
"\n");
275 (void) fprintf(table->
out,
"#:F_REORDER %8d: final size\n",finalSize);
276 (void) fprintf(table->
out,
"#:T_REORDER %8g: total time (sec)\n",
278 (void) fprintf(table->
out,
"#:N_REORDER %8d: total swaps\n",
280 (void) fprintf(table->
out,
"#:M_REORDER %8d: NI swaps\n",ddTotalNISwaps);
307 while (hook != NULL) {
308 int res = (hook->
f)(table,
"BDD", (
void *)localTime);
309 if (res == 0)
return(0);
348 for (i = 0; i < table->
size; i++) {
349 if (permutation[i] != table->
invperm[i]) {
363 for (i = 0; i < table->
size; i++)
364 perm[permutation[i]] = i;
419 MMoutOfMemory = saveHandler;
420 if (mem == NULL && table->
stash != NULL) {
426 for (i = 0; i < table->
size; i++) {
439 (void) fprintf(table->
err,
440 "cuddDynamicAllocNode: out of memory");
441 (void) fprintf(table->
err,
"Memory in use = %lu\n",
446 unsigned long offset;
455 offset = (
unsigned long) mem & (
sizeof(
DdNode) - 1);
458 assert(((
unsigned long) mem & (
sizeof(
DdNode) - 1)) == 0);
465 list[i - 1].
next = &list[i];
523 goto cuddSiftingOutOfMem;
525 var =
ALLOC(
int,size);
528 goto cuddSiftingOutOfMem;
531 for (i = 0; i < size; i++) {
548 x = table->
perm[var[i]];
556 if (!result)
goto cuddSiftingOutOfMem;
558 if (table->
keys < (
unsigned) previousSize + table->
isolated) {
559 (void) fprintf(table->
out,
"-");
560 }
else if (table->
keys > (
unsigned) previousSize + table->
isolated) {
561 (void) fprintf(table->
out,
"+");
562 (void) fprintf(table->
err,
"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->
keys - table->
isolated, var[i]);
564 (void) fprintf(table->
out,
"=");
578 if (var != NULL)
FREE(var);
623 assert(lower >= 0 && upper < table->size && lower <= upper);
626 nvars = upper - lower + 1;
629 for (i = 0; i < iterate; i++) {
634 for (j = lower; j <= upper; j++) {
641 modulo = upper - pivot;
648 modulo = pivot - lower - 1;
664 if (moves == NULL)
goto cuddSwappingOutOfMem;
666 if (!result)
goto cuddSwappingOutOfMem;
667 while (moves != NULL) {
673 if (table->
keys < (
unsigned) previousSize + table->
isolated) {
674 (void) fprintf(table->
out,
"-");
675 }
else if (table->
keys > (
unsigned) previousSize + table->
isolated) {
676 (void) fprintf(table->
out,
"+");
678 (void) fprintf(table->
out,
"=");
683 (void) fprintf(table->
out,
"#:t_SWAPPING %8d: tmp size\n",
690 cuddSwappingOutOfMem:
691 while (moves != NULL) {
769 int oldxkeys, oldykeys;
770 int newxkeys, newykeys;
771 int comple, newcomplement;
777 DdNode *f,*f0,*f1,*f01,*f00,*f11,*f10,*newf1,*newf0;
830 isolated = - ((table->
vars[xindex]->
ref == 1) +
831 (table->
vars[yindex]->
ref == 1));
838 if ((oldxkeys >= xslots || (
unsigned) xslots == table->
initSlots) &&
840 for (i = 0; i < xslots; i++) {
841 previousP = &(xlist[i]);
843 while (f != sentinel) {
851 previousP = &(f->
next);
859 *previousP = sentinel;
864 unsigned int newxslots;
868 for (i = 0; i < xslots; i++) {
870 while (f != sentinel) {
894 while ((
unsigned) oldxkeys < newxslots &&
903 MMoutOfMemory = saveHandler;
904 if (newxlist == NULL) {
905 (void) fprintf(table->
err,
"Unable to resize subtable %d for lack of memory\n", i);
910 table->
slots += ((int) newxslots - xslots);
917 ((
int) newxslots - xslots) *
sizeof(
DdNodePtr);
924 for (i = 0; i < xslots; i++) {
934 posn =
ddHash(f1, f0, xshift);
936 previousP = &(xlist[posn]);
938 while (f1 <
cuddT(tmp)) {
939 previousP = &(tmp->
next);
943 previousP = &(tmp->
next);
946 f->
next = *previousP;
953 table->swapSteps += oldxkeys - newxkeys;
967 if ((
int) f1->
index == yindex) {
978 if ((
int) f0->
index == yindex) {
995 posn =
ddHash(f11, f01, xshift);
997 previousP = &(xlist[posn]);
999 while (f11 <
cuddT(newf1)) {
1000 previousP = &(newf1->
next);
1003 while (f11 ==
cuddT(newf1) && f01 <
cuddE(newf1)) {
1004 previousP = &(newf1->
next);
1007 if (
cuddT(newf1) == f11 &&
cuddE(newf1) == f01) {
1012 goto cuddSwapOutOfMem;
1013 newf1->
index = xindex; newf1->
ref = 1;
1020 newf1->
next = *previousP;
1044 if (newcomplement) {
1049 posn =
ddHash(f10, f00, xshift);
1051 previousP = &(xlist[posn]);
1053 while (f10 <
cuddT(newf0)) {
1054 previousP = &(newf0->
next);
1057 while (f10 ==
cuddT(newf0) && f00 <
cuddE(newf0)) {
1058 previousP = &(newf0->
next);
1061 if (
cuddT(newf0) == f10 &&
cuddE(newf0) == f00) {
1066 goto cuddSwapOutOfMem;
1067 newf0->
index = xindex; newf0->
ref = 1;
1074 newf0->
next = *previousP;
1080 if (newcomplement) {
1090 posn =
ddHash(newf1, newf0, yshift);
1092 previousP = &(ylist[posn]);
1094 while (newf1 <
cuddT(tmp)) {
1095 previousP = &(tmp->
next);
1098 while (newf1 ==
cuddT(tmp) && newf0 <
cuddE(tmp)) {
1099 previousP = &(tmp->
next);
1102 f->
next = *previousP;
1110 for (i = 0; i < yslots; i++) {
1111 previousP = &(ylist[i]);
1113 while (f != sentinel) {
1124 previousP = &(f->
next);
1128 *previousP = sentinel;
1133 (void) fprintf(table->
out,
"Swapping %d and %d\n",x,y);
1137 for (i = 0; i < yslots; i++) {
1139 while (f != sentinel) {
1146 if (count != newykeys) {
1147 (void) fprintf(table->
out,
1148 "Error in finding newykeys\toldykeys = %d\tnewykeys = %d\tactual = %d\n",
1149 oldykeys,newykeys,count);
1152 (void) fprintf(table->
out,
1153 "Error in id's of ylist\twrong id's = %d\n",
1157 for (i = 0; i < xslots; i++) {
1159 while (f != sentinel) {
1166 if (count != newxkeys) {
1167 (void) fprintf(table->
out,
1168 "Error in finding newxkeys\toldxkeys = %d \tnewxkeys = %d \tactual = %d\n",
1169 oldxkeys,newxkeys,count);
1172 (void) fprintf(table->
out,
1173 "Error in id's of xlist\twrong id's = %d\n",
1177 isolated += (table->
vars[xindex]->
ref == 1) +
1178 (table->
vars[yindex]->
ref == 1);
1211 table->
perm[xindex] = y; table->
perm[yindex] = x;
1214 table->
keys += newxkeys + newykeys - oldxkeys - oldykeys;
1219 (void) fprintf(table->
err,
"Error: cuddSwapInPlace out of memory\n");
1259 if (table->
size == 0)
1270 if (invperm == NULL) {
1274 for (i = 0; i < table->
sizeZ; i += M) {
1276 int index = indexZ / M;
1277 invperm[i / M] = index;
1286 for (i = 0; i < table->
size; i++) {
1292 if (result == 0)
return(0);
1328 return((*ptrX) - (*ptrY));
1359 tmp = x; x = y; y = tmp;
1370 if ( xNext == yNext) {
1372 if (size == 0)
goto ddSwapAnyOutOfMem;
1374 if (move == NULL)
goto ddSwapAnyOutOfMem;
1382 if (size == 0)
goto ddSwapAnyOutOfMem;
1384 if (move == NULL)
goto ddSwapAnyOutOfMem;
1392 if (size == 0)
goto ddSwapAnyOutOfMem;
1394 if (move == NULL)
goto ddSwapAnyOutOfMem;
1401 tmp = x; x = y; y = tmp;
1403 }
else if (x == yNext) {
1406 if (size == 0)
goto ddSwapAnyOutOfMem;
1408 if (move == NULL)
goto ddSwapAnyOutOfMem;
1415 tmp = x; x = y; y = tmp;
1419 if (size == 0)
goto ddSwapAnyOutOfMem;
1421 if (move == NULL)
goto ddSwapAnyOutOfMem;
1429 if (size == 0)
goto ddSwapAnyOutOfMem;
1431 if (move == NULL)
goto ddSwapAnyOutOfMem;
1444 if (xNext > yRef)
break;
1446 if ((
double) size > table->
maxGrowth * (
double) limitSize)
break;
1447 if (size < limitSize) limitSize = size;
1451 if (size == 0)
goto ddSwapAnyOutOfMem;
1453 if (move == NULL)
goto ddSwapAnyOutOfMem;
1464 while (moves != NULL) {
1511 if (!result)
goto ddSiftingAuxOutOfMem;
1513 }
else if (x == xHigh) {
1519 if (!result)
goto ddSiftingAuxOutOfMem;
1521 }
else if ((x - xLow) > (xHigh - x)) {
1525 if (moveDown != NULL) {
1529 if (moveUp == (
Move *) CUDD_OUT_OF_MEM)
goto ddSiftingAuxOutOfMem;
1532 if (!result)
goto ddSiftingAuxOutOfMem;
1538 if (moveUp != NULL) {
1542 if (moveDown == (
Move *) CUDD_OUT_OF_MEM)
goto ddSiftingAuxOutOfMem;
1545 if (!result)
goto ddSiftingAuxOutOfMem;
1548 while (moveDown != NULL) {
1549 move = moveDown->
next;
1553 while (moveUp != NULL) {
1554 move = moveUp->
next;
1561 ddSiftingAuxOutOfMem:
1563 while (moveDown != NULL) {
1564 move = moveDown->
next;
1569 if (moveUp != (
Move *) CUDD_OUT_OF_MEM) {
1570 while (moveUp != NULL) {
1571 move = moveUp->
next;
1623 for (x = xLow + 1; x < y; x++) {
1626 isolated = table->
vars[xindex]->
ref == 1;
1630 isolated = table->
vars[yindex]->
ref == 1;
1634 while (x >= xLow && L <= limitSize) {
1638 for (z = xLow + 1; z < y; z++) {
1641 isolated = table->
vars[zindex]->
ref == 1;
1645 isolated = table->
vars[yindex]->
ref == 1;
1650 if (size == 0)
goto ddSiftingUpOutOfMem;
1653 isolated = table->
vars[xindex]->
ref == 1;
1657 if (move == NULL)
goto ddSiftingUpOutOfMem;
1663 if ((
double) size > (double) limitSize * table->
maxGrowth)
break;
1664 if (size < limitSize) limitSize = size;
1670 ddSiftingUpOutOfMem:
1671 while (moves != NULL) {
1718 for (y = xHigh; y > x; y--) {
1721 isolated = table->
vars[yindex]->
ref == 1;
1727 while (y <= xHigh && size - R < limitSize) {
1730 for (z = xHigh; z > x; z--) {
1733 isolated = table->
vars[zindex]->
ref == 1;
1742 isolated = table->
vars[yindex]->
ref == 1;
1746 if (size == 0)
goto ddSiftingDownOutOfMem;
1748 if (move == NULL)
goto ddSiftingDownOutOfMem;
1754 if ((
double) size > (double) limitSize * table->
maxGrowth)
break;
1755 if (size < limitSize) limitSize = size;
1761 ddSiftingDownOutOfMem:
1762 while (moves != NULL) {
1794 for (move = moves; move != NULL; move = move->
next) {
1795 if (move->
size < size) {
1800 for (move = moves; move != NULL; move = move->
next) {
1801 if (move->
size == size)
return(1);
1803 if (!res)
return(0);
1840 for (i = 0; i < table->
size; i++) {
1846 if (res == 0)
return(0);
1868 (void) fflush(table->
out);
1906 unsigned long localTime;
1916 (void) fprintf(table->
out,
"#:I_SHUFFLE %8d: initial size\n",
1921 numvars = table->
size;
1923 for (level = 0; level <
numvars; level++) {
1924 index = permutation[level];
1925 position = table->
perm[index];
1929 result =
ddSiftUp(table,position,level);
1930 if (!result)
return(0);
1932 if (table->
keys < (
unsigned) previousSize + table->
isolated) {
1933 (void) fprintf(table->
out,
"-");
1934 }
else if (table->
keys > (
unsigned) previousSize + table->
isolated) {
1935 (void) fprintf(table->
out,
"+");
1937 (void) fprintf(table->
out,
"=");
1944 (void) fprintf(table->
out,
"\n");
1946 (void) fprintf(table->
out,
"#:F_SHUFFLE %8d: final size\n",finalSize);
1947 (void) fprintf(table->
out,
"#:T_SHUFFLE %8g: total time (sec)\n",
1949 (void) fprintf(table->
out,
"#:N_SHUFFLE %8d: total swaps\n",
1951 (void) fprintf(table->
out,
"#:M_SHUFFLE %8d: NI swaps\n",ddTotalNISwaps);
2013 if (treenode == NULL)
return;
2014 treenode->
low = ((int) treenode->
index < table->
size) ?
2016 if (treenode->
child != NULL) {
2019 if (treenode->
younger != NULL)
2049 unsigned int i, size;
2050 int index, level, minLevel, maxLevel, minIndex;
2052 if (treenode == NULL)
return(1);
2058 for (i = treenode->
low; i < treenode->low + treenode->
size; i++) {
2060 level = perm[index];
2061 if (level < minLevel) {
2065 if (level > maxLevel)
2068 size = maxLevel - minLevel + 1;
2069 if (minIndex == -1)
return(0);
2070 if (size == treenode->
size) {
2071 treenode->
low = minLevel;
2072 treenode->
index = minIndex;
2077 if (treenode->
child != NULL) {
2081 if (treenode->
younger != NULL) {
2108 unsigned int i, size;
2109 int index, level, minLevel, maxLevel;
2111 if (treenode == NULL)
return(1);
2113 minLevel = table->
size;
2116 for (i = treenode->
low; i < treenode->low + treenode->
size; i++) {
2118 level = perm[index];
2119 if (level < minLevel)
2121 if (level > maxLevel)
2124 size = maxLevel - minLevel + 1;
2125 if (size != treenode->
size)
2128 if (treenode->
child != NULL) {
2132 if (treenode->
younger != NULL) {
static int ddCheckPermuation(DdManager *table, MtrNode *treenode, int *perm, int *invperm)
unsigned short DdHalfWord
static Move * ddSiftingDown(DdManager *table, int x, int xHigh)
void Cudd_OutOfMem(long size)
#define cuddDeallocMove(unique, node)
Cudd_ReorderingType autoMethod
unsigned int peakLiveNodes
int cuddSwapping(DdManager *table, int lower, int upper, Cudd_ReorderingType heuristic)
int(* DD_QSFP)(const void *, const void *)
static Move * ddSwapAny(DdManager *table, int x, int y)
DdNode * cuddDynamicAllocNode(DdManager *table)
DdHook * preReorderingHook
static char rcsid [] DD_UNUSED
unsigned int maxCacheHard
int cuddInitInteract(DdManager *table)
#define Cudd_Regular(node)
static int ddSiftingAux(DdManager *table, int x, int xLow, int xHigh)
static int ddSiftUp(DdManager *table, int x, int xLow)
int cuddSifting(DdManager *table, int lower, int upper)
static void bddFixTree(DdManager *table, MtrNode *treenode)
int cuddTreeSifting(DdManager *table, Cudd_ReorderingType method)
#define DD_MAX_SUBTABLE_DENSITY
int Cudd_ShuffleHeap(DdManager *table, int *permutation)
int ddTotalNumberSwapping
int cuddGarbageCollect(DdManager *unique, int clearCache)
unsigned int randomizeOrder
Cudd_VariableType varType
#define Cudd_IsComplement(node)
void cuddLocalCacheClearAll(DdManager *manager)
DdHook * postReorderingHook
int cuddNextLow(DdManager *table, int x)
int cuddNextHigh(DdManager *table, int x)
static int ddReorderPostprocess(DdManager *table)
static int ddUniqueCompare(int *ptrX, int *ptrY)
Cudd_LazyGroupType varToBeGrouped
static int ddUpdateMtrTree(DdManager *table, MtrNode *treenode, int *perm, int *invperm)
#define DD_MAX_CACHE_TO_SLOTS_RATIO
void(* MMoutOfMemory)(long)
#define cuddDeallocNode(unique, node)
int cuddBddAlignToZdd(DdManager *table)
int Cudd_ReduceHeap(DdManager *table, Cudd_ReorderingType heuristic, int minsize)
static int ddReorderPreprocess(DdManager *table)
int cuddSwapInPlace(DdManager *table, int x, int y)
static int ddSiftingBackward(DdManager *table, int size, Move *moves)
void cuddCacheFlush(DdManager *table)
static int ddShuffle(DdManager *table, int *permutation)
int cuddZddAlignToBdd(DdManager *table)
int cuddTestInteract(DdManager *table, int x, int y)
static Move * ddSiftingUp(DdManager *table, int y, int xLow)