📄 cuddzddreord.c
字号:
/**CFile*********************************************************************** FileName [cuddZddReord.c] PackageName [cudd] Synopsis [Procedures for dynamic variable ordering of ZDDs.] Description [External procedures included in this module: <ul> <li> Cudd_zddReduceHeap() <li> Cudd_zddShuffleHeap() </ul> Internal procedures included in this module: <ul> <li> cuddZddAlignToBdd() <li> cuddZddNextHigh() <li> cuddZddNextLow() <li> cuddZddUniqueCompare() <li> cuddZddSwapInPlace() <li> cuddZddSwapping() <li> cuddZddSifting() </ul> Static procedures included in this module: <ul> <li> zddSwapAny() <li> cuddZddSiftingAux() <li> cuddZddSiftingUp() <li> cuddZddSiftingDown() <li> cuddZddSiftingBackward() <li> zddReorderPreprocess() <li> zddReorderPostprocess() <li> zddShuffle() <li> zddSiftUp() </ul> ] SeeAlso [] Author [Hyong-Kyoon Shin, In-Ho Moon] Copyright [ This file was created at the University of Colorado at Boulder. The University of Colorado at Boulder makes no warranty about the suitability of this software for any purpose. It is presented on an AS IS basis.]******************************************************************************/#include "util.h"#include "cuddInt.h"/*---------------------------------------------------------------------------*//* Constant declarations *//*---------------------------------------------------------------------------*/#define DD_MAX_SUBTABLE_SPARSITY 8#define DD_SHRINK_FACTOR 2/*---------------------------------------------------------------------------*//* Stucture declarations *//*---------------------------------------------------------------------------*//*---------------------------------------------------------------------------*//* Type declarations *//*---------------------------------------------------------------------------*//*---------------------------------------------------------------------------*//* Variable declarations *//*---------------------------------------------------------------------------*/#ifndef lintstatic char rcsid[] DD_UNUSED = "$Id: cuddZddReord.c,v 1.1.1.1 2003/02/24 22:23:54 wjiang Exp $";#endifint *zdd_entry;int zddTotalNumberSwapping;static DdNode *empty;/*---------------------------------------------------------------------------*//* Macro declarations *//*---------------------------------------------------------------------------*//**AutomaticStart*************************************************************//*---------------------------------------------------------------------------*//* Static function prototypes *//*---------------------------------------------------------------------------*/static Move * zddSwapAny ARGS((DdManager *table, int x, int y));static int cuddZddSiftingAux ARGS((DdManager *table, int x, int x_low, int x_high));static Move * cuddZddSiftingUp ARGS((DdManager *table, int x, int x_low, int initial_size));static Move * cuddZddSiftingDown ARGS((DdManager *table, int x, int x_high, int initial_size));static int cuddZddSiftingBackward ARGS((DdManager *table, Move *moves, int size));static void zddReorderPreprocess ARGS((DdManager *table));static int zddReorderPostprocess ARGS((DdManager *table));static int zddShuffle ARGS((DdManager *table, int *permutation));static int zddSiftUp ARGS((DdManager *table, int x, int xLow));static void zddFixTree ARGS((DdManager *table, MtrNode *treenode));/**AutomaticEnd***************************************************************//*---------------------------------------------------------------------------*//* Definition of exported functions *//*---------------------------------------------------------------------------*//**Function******************************************************************** Synopsis [Main dynamic reordering routine for ZDDs.] Description [Main dynamic reordering routine for ZDDs. Calls one of the possible reordering procedures: <ul> <li>Swapping <li>Sifting <li>Symmetric Sifting </ul> For sifting and symmetric sifting it is possible to request reordering to convergence.<p> The core of all methods is the reordering procedure cuddZddSwapInPlace() which swaps two adjacent variables. Returns 1 in case of success; 0 otherwise. In the case of symmetric sifting (with and without convergence) returns 1 plus the number of symmetric variables, in case of success.] SideEffects [Changes the variable order for all ZDDs and clears the cache.]******************************************************************************/intCudd_zddReduceHeap( DdManager * table /* DD manager */, Cudd_ReorderingType heuristic /* method used for reordering */, int minsize /* bound below which no reordering occurs */){ DdHook *hook; int result; unsigned int nextDyn;#ifdef DD_STATS unsigned int initialSize; unsigned int finalSize;#endif long localTime; /* Don't reorder if there are too many dead nodes. */ if (table->keysZ - table->deadZ < (unsigned) minsize) return(1); if (heuristic == CUDD_REORDER_SAME) { heuristic = table->autoMethodZ; } if (heuristic == CUDD_REORDER_NONE) { return(1); } /* This call to Cudd_zddReduceHeap does initiate reordering. Therefore ** we count it. */ table->reorderings++; empty = table->zero; localTime = util_cpu_time(); /* Run the hook functions. */ hook = table->preReorderingHook; while (hook != NULL) { int res = (hook->f)(table, "ZDD", (void *)heuristic); if (res == 0) return(0); hook = hook->next; } /* Clear the cache and collect garbage. */ zddReorderPreprocess(table); zddTotalNumberSwapping = 0;#ifdef DD_STATS initialSize = table->keysZ; switch(heuristic) { case CUDD_REORDER_RANDOM: case CUDD_REORDER_RANDOM_PIVOT: (void) fprintf(table->out,"#:I_RANDOM "); break; case CUDD_REORDER_SIFT: case CUDD_REORDER_SIFT_CONVERGE: case CUDD_REORDER_SYMM_SIFT: case CUDD_REORDER_SYMM_SIFT_CONV: (void) fprintf(table->out,"#:I_SIFTING "); break; case CUDD_REORDER_LINEAR: case CUDD_REORDER_LINEAR_CONVERGE: (void) fprintf(table->out,"#:I_LINSIFT "); break; default: (void) fprintf(table->err,"Unsupported ZDD reordering method\n"); return(0); } (void) fprintf(table->out,"%8d: initial size",initialSize); #endif result = cuddZddTreeSifting(table,heuristic);#ifdef DD_STATS (void) fprintf(table->out,"\n"); finalSize = table->keysZ; (void) fprintf(table->out,"#:F_REORDER %8d: final size\n",finalSize); (void) fprintf(table->out,"#:T_REORDER %8g: total time (sec)\n", ((double)(util_cpu_time() - localTime)/1000.0)); (void) fprintf(table->out,"#:N_REORDER %8d: total swaps\n", zddTotalNumberSwapping);#endif if (result == 0) return(0); if (!zddReorderPostprocess(table)) return(0); if (table->realignZ) { if (!cuddBddAlignToZdd(table)) return(0); } nextDyn = table->keysZ * DD_DYN_RATIO; if (table->reorderings < 20 || nextDyn > table->nextDyn) table->nextDyn = nextDyn; else table->nextDyn += 20; table->reordered = 1; /* Run hook functions. */ hook = table->postReorderingHook; while (hook != NULL) { int res = (hook->f)(table, "ZDD", (void *)localTime); if (res == 0) return(0); hook = hook->next; } /* Update cumulative reordering time. */ table->reordTime += util_cpu_time() - localTime; return(result);} /* end of Cudd_zddReduceHeap *//**Function******************************************************************** Synopsis [Reorders ZDD variables according to given permutation.] Description [Reorders ZDD variables according to given permutation. The i-th entry of the permutation array contains the index of the variable that should be brought to the i-th level. The size of the array should be equal or greater to the number of variables currently in use. Returns 1 in case of success; 0 otherwise.] SideEffects [Changes the ZDD variable order for all diagrams and clears the cache.] SeeAlso [Cudd_zddReduceHeap]******************************************************************************/intCudd_zddShuffleHeap( DdManager * table /* DD manager */, int * permutation /* required variable permutation */){ int result; empty = table->zero; zddReorderPreprocess(table); result = zddShuffle(table,permutation); if (!zddReorderPostprocess(table)) return(0); return(result);} /* end of Cudd_zddShuffleHeap *//*---------------------------------------------------------------------------*//* Definition of internal functions *//*---------------------------------------------------------------------------*//**Function******************************************************************** Synopsis [Reorders ZDD variables according to the order of the BDD variables.] Description [Reorders ZDD variables according to the order of the BDD variables. This function can be called at the end of BDD reordering to insure that the order of the ZDD variables is consistent with the order of the BDD variables. The number of ZDD variables must be a multiple of the number of BDD variables. Let <code>M</code> be the ratio of the two numbers. cuddZddAlignToBdd then considers the ZDD variables from <code>M*i</code> to <code>(M+1)*i-1</code> as corresponding to BDD variable <code>i</code>. This function should be normally called from Cudd_ReduceHeap, which clears the cache. Returns 1 in case of success; 0 otherwise.] SideEffects [Changes the ZDD variable order for all diagrams and performs garbage collection of the ZDD unique table.] SeeAlso [Cudd_zddShuffleHeap Cudd_ReduceHeap]******************************************************************************/intcuddZddAlignToBdd( DdManager * table /* DD manager */){ int *invpermZ; /* permutation array */ int M; /* ratio of ZDD variables to BDD variables */ int i,j; /* loop indices */ int result; /* return value */ /* We assume that a ratio of 0 is OK. */ if (table->sizeZ == 0) return(1); empty = table->zero; M = table->sizeZ / table->size; /* Check whether the number of ZDD variables is a multiple of the ** number of BDD variables. */ if (M * table->size != table->sizeZ) return(0); /* Create and initialize the inverse permutation array. */ invpermZ = ALLOC(int,table->sizeZ); if (invpermZ == NULL) { table->errorCode = CUDD_MEMORY_OUT; return(0); } for (i = 0; i < table->size; i++) { int index = table->invperm[i]; int indexZ = index * M; int levelZ = table->permZ[indexZ]; levelZ = (levelZ / M) * M; for (j = 0; j < M; j++) { invpermZ[M * i + j] = table->invpermZ[levelZ + j]; } } /* Eliminate dead nodes. Do not scan the cache again, because we ** assume that Cudd_ReduceHeap has already cleared it. */ cuddGarbageCollectZdd(table,0); result = zddShuffle(table, invpermZ); FREE(invpermZ); /* Fix the ZDD variable group tree. */ zddFixTree(table,table->treeZ); return(result); } /* end of cuddZddAlignToBdd *//**Function******************************************************************** Synopsis [Finds the next subtable with a larger index.] Description [Finds the next subtable with a larger index. Returns the index.] SideEffects [None] SeeAlso []******************************************************************************/intcuddZddNextHigh( DdManager * table, int x){ return(x + 1);} /* end of cuddZddNextHigh *//**Function******************************************************************** Synopsis [Finds the next subtable with a smaller index.] Description [Finds the next subtable with a smaller index. Returns the index.] SideEffects [None] SeeAlso []******************************************************************************/intcuddZddNextLow( DdManager * table, int x){ return(x - 1);} /* end of cuddZddNextLow *//**Function******************************************************************** Synopsis [Comparison function used by qsort.] Description [Comparison function used by qsort to order the variables according to the number of keys in the subtables. Returns the difference in number of keys between the two variables being compared.] SideEffects [None] SeeAlso []******************************************************************************/intcuddZddUniqueCompare( int * ptr_x, int * ptr_y){ return(zdd_entry[*ptr_y] - zdd_entry[*ptr_x]);} /* end of cuddZddUniqueCompare *//**Function******************************************************************** Synopsis [Swaps two adjacent variables.] Description [Swaps two adjacent variables. It assumes that no dead nodes are present on entry to this procedure. The procedure then guarantees that no dead nodes will be present when it terminates. cuddZddSwapInPlace assumes that x < y. Returns the number of keys in the table if successful; 0 otherwise.] SideEffects [None] SeeAlso []******************************************************************************/intcuddZddSwapInPlace( DdManager * table, int x, int y){ DdNodePtr *xlist, *ylist; int xindex, yindex; int xslots, yslots; int xshift, yshift; int oldxkeys, oldykeys; int newxkeys, newykeys; int i; int posn; DdNode *f, *f1, *f0, *f11, *f10, *f01, *f00; DdNode *newf1, *newf0, *next; DdNodePtr g, *lastP, *previousP;#ifdef DD_DEBUG assert(x < y); assert(cuddZddNextHigh(table,x) == y); assert(table->subtableZ[x].keys != 0); assert(table->subtableZ[y].keys != 0); assert(table->subtableZ[x].dead == 0); assert(table->subtableZ[y].dead == 0);#endif zddTotalNumberSwapping++; /* Get parameters of x subtable. */ xindex = table->invpermZ[x]; xlist = table->subtableZ[x].nodelist; oldxkeys = table->subtableZ[x].keys; xslots = table->subtableZ[x].slots; xshift = table->subtableZ[x].shift; newxkeys = 0; yindex = table->invpermZ[y]; ylist = table->subtableZ[y].nodelist; oldykeys = table->subtableZ[y].keys; yslots = table->subtableZ[y].slots; yshift = table->subtableZ[y].shift; newykeys = oldykeys; /* The nodes in the x layer that don't depend on y directly ** will stay there; the others are put in a chain. ** The chain is handled as a FIFO; g points to the beginning and ** last points to the end. */ g = NULL; lastP = &g; for (i = 0; i < xslots; i++) { previousP = &(xlist[i]); f = *previousP; while (f != NULL) { next = f->next; f1 = cuddT(f); f0 = cuddE(f); if ((f1->index != (DdHalfWord) yindex) && (f0->index != (DdHalfWord) yindex)) { /* stays */ newxkeys++; *previousP = f; previousP = &(f->next); } else { f->index = yindex; *lastP = f; lastP = &(f->next); } f = next; } /* while there are elements in the collision chain */ *previousP = NULL; } /* for each slot of the x subtable */ *lastP = NULL;#ifdef DD_COUNT table->swapSteps += oldxkeys - newxkeys;#endif /* Take care of the x nodes that must be re-expressed. ** They form a linked list pointed by g. Their index has been ** changed to yindex already. */ f = g; while (f != NULL) { next = f->next; /* Find f1, f0, f11, f10, f01, f00. */ f1 = cuddT(f); if ((int) f1->index == yindex) { f11 = cuddT(f1); f10 = cuddE(f1); } else { f11 = empty; f10 = f1; } f0 = cuddE(f); if ((int) f0->index == yindex) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -