VIS

src/puresat/puresatUtil.c

Go to the documentation of this file.
00001 
00049 #include "puresatInt.h"
00050 #include "sat.h"
00051 
00052 /*---------------------------------------------------------------------------*/
00053 /* Constant declarations                                                     */
00054 /*---------------------------------------------------------------------------*/
00055 
00056 /*---------------------------------------------------------------------------*/
00057 /* Structure declarations                                                    */
00058 /*---------------------------------------------------------------------------*/
00059 
00060 /*---------------------------------------------------------------------------*/
00061 /* Type declarations                                                         */
00062 /*---------------------------------------------------------------------------*/
00063 
00064 /*---------------------------------------------------------------------------*/
00065 /* Variable declarations                                                     */
00066 /*---------------------------------------------------------------------------*/
00067 static array_t *RCArray;
00068 
00069 /*---------------------------------------------------------------------------*/
00070 /* Macro declarations                                                        */
00071 /*---------------------------------------------------------------------------*/
00072 
00075 /*---------------------------------------------------------------------------*/
00076 /* Static function prototypes                                                */
00077 /*---------------------------------------------------------------------------*/
00078 
00079 
00082 /*---------------------------------------------------------------------------*/
00083 /* Definition of exported functions                                          */
00084 /*---------------------------------------------------------------------------*/
00085 
00086 
00087 /*---------------------------------------------------------------------------*/
00088 /* Definition of internal functions                                          */
00089 /*---------------------------------------------------------------------------*/
00090 
00091 
00104 static int DfsLevel = 0;
00105 static array_t *NumInCone;
00106 static array_t *NumInAbs;
00107 static array_t *DfsArray;
00108 /*static array_t *RCArray;*/
00109 
00110 
00123 PureSat_Manager_t * PureSatManagerAlloc(void)
00124 {
00125   PureSat_Manager_t * pm = ALLOC(PureSat_Manager_t,1);
00126   memset(pm,0,sizeof(PureSat_Manager_t));
00127   pm->incre = TRUE;
00128   pm->ltlFileName = ALLOC(char,200);
00129   pm->timeOutPeriod = 10000000;
00130   pm->Arosat = TRUE;;
00131   pm->CoreRefMin = TRUE;
00132   pm->RefPredict = TRUE;
00133   pm->Switch = TRUE;
00134   return pm;
00135 }
00136 
00148 void
00149 PureSatManagerFree(PureSat_Manager_t * pm){
00150   if(pm->latchToTableBaigNodes)
00151     st_free_table(pm->latchToTableBaigNodes);
00152   if(pm->ClsidxToLatchTable)
00153     st_free_table(pm->ClsidxToLatchTable);
00154   if(pm->CoiTable)
00155     st_free_table(pm->CoiTable);
00156   if(pm->AbsTable)
00157     st_free_table(pm->AbsTable);
00158   if(pm->vertexTable)
00159     st_free_table(pm->vertexTable);
00160   if(pm->SufAbsTable)
00161     st_free_table(pm->SufAbsTable);;
00162   if(pm->supportTable)
00163     st_free_table(pm->supportTable);
00164   if(pm->node2dfsTable)
00165     st_free_table(pm->node2dfsTable);
00166   if(pm->result!=NIL(array_t))
00167     array_free(pm->result);
00168   FREE(pm->ltlFileName);
00169   FREE(pm);
00170 
00171 }
00172 
00184 PureSat_IncreSATManager_t * PureSatIncreSATManagerAlloc(PureSat_Manager_t * pm)
00185 {
00186   PureSat_IncreSATManager_t * pism = ALLOC(PureSat_IncreSATManager_t,1);
00187   pism->beginPosition = 0;
00188   pism->Length = 0;
00189   pism->oldLength = 0;
00190   pism->propertyPos = 0;
00191   memset(pism,0,sizeof(PureSat_IncreSATManager_t));
00192   if(pm->sss){
00193     pism->cnfClauses = BmcCnfClausesAlloc();
00194     pism->cm = sat_InitManager(0);
00195     pism->cm->comment = ALLOC(char, 2);
00196     pism->cm->comment[0] = ' ';
00197     pism->cm->comment[1] = '\0';
00198 
00199     pism->cm->maxNodesArraySize = 1;
00200     pism->cm->nodesArray = ALLOC(long, pism->cm->maxNodesArraySize );
00201     pism->cm->nodesArraySize = satNodeSize;
00202     pism->cm->stdErr = vis_stderr;
00203     pism->cm->stdOut = vis_stdout;
00204     pism->cm->unitLits = sat_ArrayAlloc(16);
00205     pism->cm->pureLits = sat_ArrayAlloc(16);
00206     pism->cm->each = sat_InitStatistics();
00207     pism->cm->option = sat_InitOption();
00208     pism->cm->option->ForceFinish = 1;
00209     pism->cm->option->incTraceObjective = 1;
00210     pism->cm->option->includeLevelZeroLiteral = 1;
00211     pism->cm->option->minimizeConflictClause = 0;
00212     pism->cm->option->SSS = 1;
00213   }
00214   return pism;
00215 }
00216 
00229 void
00230 PureSatIncreSATManagerFree(PureSat_Manager_t *pm,
00231                            PureSat_IncreSATManager_t * pism)
00232 {
00233   if(pm->sss){
00234     BmcCnfClausesFree(pism->cnfClauses);
00235     FREE(pism->cm->nodesArray);
00236     sat_ArrayFree(pism->cm->unitLits);
00237     sat_ArrayFree(pism->cm->pureLits);
00238     sat_FreeStatistics(pism->cm->each);
00239     sat_FreeOption(pism->cm->option);
00240     FREE(pism->cm);
00241   }
00242   FREE(pism);
00243 }
00244 
00257 void
00258 PureSatBmcGetCoiForNtkNode(
00259   Ntk_Node_t  *node,
00260   st_table    *CoiTable,
00261   st_table    *visited)
00262 {
00263   int        i, j;
00264   Ntk_Node_t *faninNode;
00265 
00266   if(node == NIL(Ntk_Node_t)){
00267     return;
00268   }
00269   if(Ntk_NodeTestIsLatch(node)){
00270     DfsLevel++;
00271     if (st_lookup_int(CoiTable, node, &j)&&j<=DfsLevel){
00272       DfsLevel--;
00273       return;
00274     }
00275   }
00276 
00277   if (st_lookup_int(visited,  node, &j)&&j<=DfsLevel){
00278     if(Ntk_NodeTestIsLatch(node))
00279       DfsLevel--;
00280     return;
00281   }
00282   st_insert(visited, node, (char *)(long)DfsLevel);
00283   if (Ntk_NodeTestIsLatch(node)){
00284     st_insert(CoiTable,  node, (char *)(long)DfsLevel);
00285   }
00286 
00287   if(Ntk_NodeTestIsInput(node))
00288     {
00289       return;
00290     }
00291 
00292   Ntk_NodeForEachFanin(node, i, faninNode) {
00293     PureSatBmcGetCoiForNtkNode(faninNode, CoiTable, visited);
00294   }
00295   if(Ntk_NodeTestIsLatch(node))
00296     DfsLevel--;
00297   return;
00298 }
00299 
00312 void
00313 PureSatBmcGetCoiForLtlFormulaRecursive(
00314   Ntk_Network_t   *network,
00315   Ctlsp_Formula_t *formula,
00316   st_table        *ltlCoiTable,
00317   st_table        *visited)
00318 {
00319   if (formula == NIL(Ctlsp_Formula_t)) {
00320     return;
00321   }
00322   /* leaf node */
00323   if (formula->type == Ctlsp_ID_c){
00324     char       *name = Ctlsp_FormulaReadVariableName(formula);
00325     Ntk_Node_t *node = Ntk_NetworkFindNodeByName(network, name);
00326     int        tmp;
00327 
00328     if (st_lookup_int(visited, node, &tmp)){
00329       /* Node already visited */
00330       return;
00331     }
00332     PureSatBmcGetCoiForNtkNode(node, ltlCoiTable, visited);
00333     return;
00334   }
00335   PureSatBmcGetCoiForLtlFormulaRecursive(network, formula->left,  ltlCoiTable, visited);
00336   PureSatBmcGetCoiForLtlFormulaRecursive(network, formula->right, ltlCoiTable, visited);
00337 
00338   return;
00339 }
00340 
00352 DfsColor
00353 PureSatNodeReadColor(
00354   Ntk_Node_t * node)
00355 {
00356   return ((DfsColor) (long) Ntk_NodeReadUndef(node));
00357 }
00358 
00370 void
00371 PureSatNodeSetColor(
00372   Ntk_Node_t * node,
00373   DfsColor  color)
00374 {
00375   Ntk_NodeSetUndef(node, (void *) (long) color);
00376 }
00377 
00389 void
00390 PureSatNodeRecursivelyComputeTransitiveFaninNodes_AllNodes(
00391   Ntk_Node_t *node,
00392   int * NumofSupports,
00393   boolean stopAtLatches)
00394 {
00395   int i;
00396   Ntk_Node_t * fanin;
00397 
00398   /* test if we have already started processing this node */
00399   if ( PureSatNodeReadColor( node ) == dfsBlack_c ) {
00400         return;
00401   }
00402   PureSatNodeSetColor( node, dfsBlack_c );
00403 
00404   /* if ( Ntk_NodeTestIsCombInput( node ) ) {
00405   st_insert( resultTable, (char *) node, (char *) 0 );
00406          }*/
00407 
00408   (*NumofSupports)++;
00409 
00410   if ( Ntk_NodeTestIsInput(node) || ((stopAtLatches == TRUE)&&(Ntk_NodeTestIsLatch(node))) ) {
00411         return;
00412   }
00413 
00414   Ntk_NodeForEachFanin( node, i, fanin ) {
00415         PureSatNodeRecursivelyComputeTransitiveFaninNodes_AllNodes( fanin, NumofSupports, stopAtLatches );
00416   }
00417 }
00418 
00431 int
00432 PureSatNodeComputeCombinationalSupport_AllNodes(
00433   Ntk_Network_t *network,
00434   array_t * nodeArray,
00435   boolean stopAtLatches )
00436 {
00437   lsGen gen;
00438   int i, NumofSupports=0;
00439   Ntk_Node_t *node;
00440 
00441   Ntk_NetworkForEachNode( network, gen, node ) {
00442         PureSatNodeSetColor( node, dfsWhite_c );
00443   }
00444 
00445 /* for each lacthes(node) in the nodeArray, compute the number of gates in
00446    the cone of this latch, put the number in NumofSupports*/
00447   arrayForEachItem( Ntk_Node_t *, nodeArray, i, node ) {
00448     NumofSupports = 0;
00449     PureSatNodeRecursivelyComputeTransitiveFaninNodes_AllNodes( node, &NumofSupports, stopAtLatches );
00450   }
00451 
00452   return NumofSupports;
00453 }
00454 
00468 /*build a supportTable, each value associated with a latch is NumofGatesInCone*/
00469 void PureSatGenerateSupportTable(
00470   Ntk_Network_t   *network,
00471   PureSat_Manager_t *pm)
00472 {
00473   array_t    *roots = array_alloc(Ntk_Node_t *,0); /*supports;*/
00474   st_generator *stgen;
00475   Ntk_Node_t *node,*DataNode=(Ntk_Node_t *)0;
00476   int        count, NumofSupports;
00477 
00478   st_foreach_item_int(pm->CoiTable, stgen, &node, &count) {
00479     if (Ntk_NodeTestIsLatch(node))
00480       DataNode = Ntk_LatchReadDataInput(node);
00481     array_insert(Ntk_Node_t *, roots, 0, DataNode);
00482     NumofSupports = PureSatNodeComputeCombinationalSupport_AllNodes(network, roots,FALSE);
00483 
00484     st_insert(pm->supportTable,node, (char *)(long)NumofSupports);
00485   }
00486   array_free(roots);
00487 }
00488 
00500 void
00501 PureSatBmcGetCoiForLtlFormula(
00502   Ntk_Network_t   *network,
00503   Ctlsp_Formula_t *formula,
00504   st_table        *ltlCoiTable)
00505 {
00506   st_table *visited = st_init_table(st_ptrcmp, st_ptrhash);
00507 
00508   PureSatBmcGetCoiForLtlFormulaRecursive(network, formula, ltlCoiTable, visited);
00509   st_free_table(visited);
00510   return;
00511 }
00512 
00513 
00527 void
00528 PureSatGetFormulaNodes(
00529   Ntk_Network_t *network,
00530   Ctlsp_Formula_t *F,
00531   array_t *formulaNodes)
00532 {
00533 
00534   if (F == NIL(Ctlsp_Formula_t))
00535     return;
00536 
00537   if (Ctlsp_FormulaReadType(F) == Ctlsp_ID_c) {
00538     char *nodeNameString = Ctlsp_FormulaReadVariableName(F);
00539     Ntk_Node_t *node = Ntk_NetworkFindNodeByName(network, nodeNameString);
00540     assert(node);
00541     array_insert_last(Ntk_Node_t *, formulaNodes, node);
00542     return;
00543   }
00544 
00545   PureSatGetFormulaNodes(network, Ctlsp_FormulaReadRightChild(F), formulaNodes);
00546   PureSatGetFormulaNodes(network, Ctlsp_FormulaReadLeftChild(F),  formulaNodes);
00547 }
00548 
00549 
00550 
00563 void
00564 PureSatGetFaninLatches(
00565   Ntk_Node_t *node,
00566   st_table *visited,
00567   st_table *vertexTable)
00568 {
00569   if (st_is_member(visited, node))
00570     return;
00571 
00572   st_insert(visited, node, (char *)0);
00573 
00574   if (Ntk_NodeTestIsLatch(node))
00575     st_insert(vertexTable, Ntk_NodeReadName(node), (char *)0);
00576   else {
00577     int i;
00578     Ntk_Node_t *fanin;
00579     Ntk_NodeForEachFanin(node, i, fanin) {
00580       PureSatGetFaninLatches(fanin, visited, vertexTable);
00581     }
00582   }
00583 }
00597 st_table *
00598 PureSatCreateInitialAbstraction(
00599   Ntk_Network_t *network,
00600   Ctlsp_Formula_t *invFormula,
00601   int * Num,
00602   PureSat_Manager_t *pm)
00603 {
00604   array_t *formulaNodes = array_alloc(Ntk_Node_t *, 0);
00605   Ntk_Node_t *node;
00606   int i;
00607 
00608   st_table *vertexTable = st_init_table(strcmp, st_strhash);
00609   st_table *visited = st_init_table(st_ptrcmp, st_ptrhash);
00610   *Num=0;
00611 
00612   PureSatGetFormulaNodes(network, invFormula, formulaNodes);
00613   arrayForEachItem(Ntk_Node_t *, formulaNodes, i, node) {
00614     PureSatGetFaninLatches(node, visited, vertexTable);
00615   }
00616 
00617   st_free_table(visited);
00618   array_free(formulaNodes);
00619 
00620   if (1) {
00621     st_generator *stgen;
00622     char *nodeNameString;
00623 
00624     if(pm->verbosity>=1)
00625       fprintf(vis_stdout, "\n Create_InitialAbstraction() =\n");
00626     st_foreach_item(vertexTable, stgen, &nodeNameString, 0) {
00627       if(pm->verbosity>=1)
00628         fprintf(vis_stdout, "%s\n", nodeNameString);
00629       (*Num)++;
00630     }
00631     if(pm->verbosity>=1){
00632       fprintf(vis_stdout, "\n");
00633       fflush(vis_stdout);
00634     }
00635   }
00636 
00637 
00638   return vertexTable;
00639 }
00640 
00641 
00654 void
00655 PureSatRecursivelyComputeTableForLatch(Ntk_Network_t * network,
00656                                        st_table *Table,
00657                                        Ntk_Node_t * node)
00658 {
00659   int i;
00660   Ntk_Node_t * fanin;
00661 
00662   //want input to be in the Table
00663   if(Ntk_NodeTestIsLatch(node))
00664     return;
00665 
00666   if(!st_lookup(Table,node,NIL(char *)))
00667     st_insert(Table,node, (char *)0);
00668   else
00669     return;
00670 
00671    if ( Ntk_NodeTestIsCombInput(node)) {
00672         return;
00673   }
00674 
00675   Ntk_NodeForEachFanin( node,i,fanin)
00676    PureSatRecursivelyComputeTableForLatch(network,Table,fanin);
00677 }
00678 
00679 
00691 /* put all the gates of Abs model to the Table */
00692 void
00693 PureSatComputeTableForLatch(Ntk_Network_t * network,
00694                             st_table * Table,
00695                             Ntk_Node_t * Latch)
00696 {
00697   Ntk_Node_t * fanin;
00698   int i;
00699 
00700   if(!Ntk_NodeTestIsLatch(Latch))
00701     {
00702       char * name = Ntk_NodeReadName(Latch);
00703       fprintf(vis_stdout,"%s is not a latch!\n",name);
00704       exit (0);
00705     }
00706 
00707   if(!st_lookup(Table,Latch,NIL(char *)))
00708     {
00709       st_insert(Table,Latch, (char *)0);
00710       Ntk_NodeForEachFanin(Latch,i,fanin)
00711         PureSatRecursivelyComputeTableForLatch(network,Table,fanin);
00712     }
00713 }
00714 
00715 
00730 void
00731 PureSatGetCoiForVisibleArray_Ring(
00732   Ntk_Network_t   *network,
00733   array_t *visibleArray,
00734   int             position,
00735   st_table        *ltlCoiTable)
00736 {
00737   int i;
00738   char * name;
00739   Ntk_Node_t * node;
00740 
00741   st_table *visited = st_init_table(st_ptrcmp, st_ptrhash);
00742 
00743 
00744   for(i=position;i<array_n(visibleArray);i++){
00745     name = array_fetch(char *,visibleArray,i);
00746     node = Ntk_NetworkFindNodeByName(network,name);
00747     DfsLevel = 0;
00748     PureSatBmcGetCoiForNtkNode(node,ltlCoiTable, visited);
00749   }
00750   st_free_table(visited);
00751   return;
00752 }
00753 
00754 
00767 int
00768 NumInConeCompare(int *ptrX,
00769                  int *ptrY)
00770 {
00771   int a,b;
00772   int c,d;
00773   double e,f;
00774   a = array_fetch(int,NumInCone,(int)*ptrX);
00775   b = array_fetch(int,NumInCone,(int)*ptrY);
00776   c = array_fetch(int,NumInAbs,(int)*ptrX);
00777   d = array_fetch(int,NumInAbs,(int)*ptrY);
00778   e = (double)c/(double)a;
00779   f = (double)d/(double)b;
00780   if(e > f)
00781     return -1;
00782   if( e < f)
00783     return 1;
00784   return 0;
00785 }
00786 
00787 
00800 int
00801 NumInConeCompare_Ring(int *ptrX,
00802                       int *ptrY)
00803 {
00804   int a,b;
00805   double c,d;
00806   double e,f;
00807   a = array_fetch(int,DfsArray,(int)*ptrX);
00808   b = array_fetch(int,DfsArray,(int)*ptrY);
00809   c = array_fetch(double,RCArray,(int)*ptrX);
00810   d = array_fetch(double,RCArray,(int)*ptrY);
00811   e = (double)((a)*1000000)-c;
00812   f = (double)((b)*1000000)-d;
00813   if(e > f)
00814     return 1;
00815   if( e < f)
00816     return -1;
00817   return 0;
00818 }
00819 
00820 
00841 void PureSatRecursivelyComputeCorrelationforLatch(Ntk_Network_t *network,
00842                                                   st_table *AbsTable,
00843                                                   st_table * visited,
00844                                                   Ntk_Node_t *node,
00845                                                   int *count)
00846 {
00847   Ntk_Node_t * fanin;
00848   int i;
00849 
00850   if(!st_lookup(visited,node,NIL(char *)))
00851   {
00852     st_insert(visited,node,(char *)0);
00853     if(st_lookup(AbsTable,node,NIL(char *)))
00854       {
00855         (*count)++;
00856       }
00857   }
00858   else
00859     return;
00860 
00861   if(Ntk_NodeTestIsCombInput(node))
00862     return;
00863 
00864   Ntk_NodeForEachFanin(node,i,fanin)
00865     PureSatRecursivelyComputeCorrelationforLatch(network,AbsTable,visited,fanin,count);
00866 }
00867 
00868 
00869 
00890 int PureSatComputeCorrelationforLatch(Ntk_Network_t * network, st_table *
00891                               AbsTable, Ntk_Node_t *Latch)
00892 {
00893   int count =0,i;
00894   Ntk_Node_t * fanin;
00895 
00896   st_table *visited = st_init_table(st_ptrcmp,st_ptrhash);
00897   Ntk_NodeForEachFanin(Latch,i,fanin)
00898     PureSatRecursivelyComputeCorrelationforLatch(network,AbsTable,visited,fanin,&count);
00899   st_free_table(visited);
00900   return count*10000;
00901 }
00902 
00925 array_t * PureSatGenerateRingFromAbs(Ntk_Network_t *network,
00926                                     PureSat_Manager_t *pm,
00927                                     array_t *invisibleArray,
00928                                     int * NumInSecondLevel)
00929 {
00930   st_table *CoiTable = pm->CoiTable;
00931   st_table *supportTable = pm->supportTable;
00932   st_table *AbsTable= pm->AbsTable;
00933 
00934   array_t * arrayRC = array_alloc(char *,array_n(invisibleArray)),* NumInConeArray = array_alloc(int,0),* NumInAbsArray = array_alloc(int,0);
00935   array_t * tmpNumInCone = array_alloc(int,0),* tmpNumInAbs = array_alloc(int,0);
00936   array_t * tmpDfsArray = array_alloc(int,0), *tmpRCArray = array_alloc(double,0);
00937   array_t * tmpDfsArray1 = array_alloc(int,0), *tmpRCArray1 = array_alloc(double,0);
00938   int numincone,i,j,dfs;
00939   char *name;
00940   double d1;
00941 
00942   (*NumInSecondLevel) = 0;
00943 
00944   for(i=0;i<array_n(invisibleArray);i++)
00945    {
00946      int tmp;
00947      Ntk_Node_t *tmpnode;
00948      name = array_fetch(char *,invisibleArray,i);
00949      tmpnode = Ntk_NetworkFindNodeByName(network,name);
00950      tmp = PureSatComputeCorrelationforLatch(network,AbsTable,tmpnode);
00951      tmp = tmp/10000;
00952 
00953      if(!st_lookup_int(CoiTable,tmpnode,&dfs)){
00954        fprintf(vis_stderr,"not in Coitable,  Wrong");
00955        exit(0);
00956      }
00957      if(!st_lookup_int(supportTable,tmpnode, &numincone)){
00958        fprintf(vis_stderr,"not in supporttable,  Wrong");
00959        exit(0);
00960      }
00961      d1 = (double)tmp/(double)numincone;
00962      array_insert_last(double,tmpRCArray,d1);
00963      array_insert_last(int,tmpDfsArray,dfs);
00964 
00965    }
00966 
00967 
00968   DfsArray = tmpDfsArray;
00969   RCArray = tmpRCArray;
00970    {
00971      int tmpvalue;
00972      double dvalue;
00973      int nn = array_n(invisibleArray);
00974      int *tay=ALLOC(int,nn);
00975      for(j=0;j<nn;j++)
00976        tay[j]=j;
00977      qsort((void *)tay, nn, sizeof(int),
00978            (int (*)(const void *, const void *))NumInConeCompare_Ring);
00979      for(i=0;i<nn;i++)
00980        {
00981          char *str=ALLOC(char,100);
00982          name = array_fetch(char *,invisibleArray,tay[i]);
00983          strcpy(str,name);
00984          array_insert(char *,arrayRC,i,str);
00985          tmpvalue = array_fetch(int,tmpDfsArray,tay[i]);
00986          if(tmpvalue == 2)
00987            (*NumInSecondLevel)++;
00988          array_insert(int,tmpDfsArray1,i,tmpvalue);
00989          dvalue = array_fetch(double,tmpRCArray,tay[i]);
00990          array_insert(double,tmpRCArray1,i,dvalue);
00991          if(pm->verbosity>=2){
00992            int i1;
00993            double d1,d2;
00994            i1 = array_fetch(int,tmpDfsArray1,i)*1000000;
00995            d1 = array_fetch(double,tmpRCArray1,i);
00996            d2 = (double)array_fetch(int,tmpDfsArray1,i)*1000000 - d1;
00997            if(pm->verbosity>=1)
00998              fprintf(vis_stdout,"arrayRC %s: %d-%f = %f\n",name,i1,d1,d2);
00999            /* fprintf(vis_stdout,"arrayRC %s: %d-%f = %f\n",name,array_fetch(int,tmpDfsArray1,i)*1000000,array_fetch(double,tmpRCArray1,i),
01000               (double)array_fetch(int,tmpDfsArray1,i)*1000000-(double)array_fetch(double,tmpRCArray1,i));*/
01001          }
01002        }
01003      FREE(tay);
01004    }
01005    array_free(NumInConeArray);
01006    array_free(NumInAbsArray);
01007    array_free(tmpNumInCone);
01008    array_free(tmpNumInAbs);
01009    array_free(tmpDfsArray);
01010    array_free(tmpRCArray);
01011    array_free(tmpDfsArray1);
01012    array_free(tmpRCArray1);
01013    return arrayRC;
01014 }
01015 
01028 void PureSatCleanSat(satManager_t * cm)
01029 {
01030   int i;
01031   satVariable_t *var;
01032   satLevel_t *d;
01033 
01034   cm->nodesArraySize = satNodeSize;
01035   sat_ArrayFree(cm->unitLits);
01036   sat_ArrayFree(cm->pureLits);
01037   cm->unitLits = sat_ArrayAlloc(16);
01038   cm->pureLits = sat_ArrayAlloc(16);
01039   memset(cm->each,0,sizeof(satStatistics_t));
01040   cm->literals->last = cm->literals->begin+1;
01041   cm->literals->initialSize = cm->literals->last;
01042   cm->status = 0;
01043   cm->currentVarPos = 0;
01044   cm->currentTopConflict = 0;
01045   cm->lowestBacktrackLevel = 0;
01046   cm->implicatedSoFar = 0;
01047   /*cm->decisionHead = ALLOC(satLevel_t, cm->decisionHeadSize);*/
01048   memset(cm->decisionHead, 0, sizeof(satLevel_t) * cm->decisionHeadSize);
01049   /* FREE(cm->variableArray);
01050   cm->variableArray = 0;*/
01051   if(cm->variableArray) {
01052     for(i=0; i<=cm->initNumVariables; i++) {
01053       var = &(cm->variableArray[i]);
01054       if(var->wl[0]) {
01055         sat_ArrayFree(var->wl[0]);
01056         var->wl[0] = 0;
01057       }
01058       if(var->wl[1]) {
01059         sat_ArrayFree(var->wl[1]);
01060         var->wl[1] = 0;
01061       }
01062     }
01063     free(cm->variableArray);
01064     cm->variableArray = 0;
01065   }
01066 
01067   if(cm->decisionHead) {
01068     for(i=0; i<cm->decisionHeadSize; i++) {
01069       d = &(cm->decisionHead[i]);
01070       if(d->implied)
01071         sat_ArrayFree(d->implied);
01072       if(d->satisfied)
01073         sat_ArrayFree(d->satisfied);
01074     }
01075     free(cm->decisionHead);
01076     cm->decisionHead = 0;
01077     cm->decisionHeadSize = 0;
01078   }
01079 
01080   sat_FreeQueue(cm->queue);
01081   cm->queue = 0;
01082   sat_FreeQueue(cm->BDDQueue);
01083   cm->BDDQueue = 0;
01084   sat_FreeQueue(cm->unusedAigQueue);
01085   cm->unusedAigQueue = 0;
01086   sat_ArrayFree(cm->auxArray);
01087   sat_ArrayFree(cm->nonobjUnitLitArray);
01088   sat_ArrayFree(cm->objUnitLitArray);
01089 
01090 }
01091 
01106 void PureSatReadClauses(PureSat_IncreSATManager_t * pism,
01107                         PureSat_Manager_t * pm)
01108 {
01109   int i,node,nVar = pism->cnfClauses->cnfGlobalIndex - 1;
01110   satManager_t * cm = pism->cm;
01111   satArray_t * clauseArray;
01112 
01113   cm->option->verbose = pm->verbosity;
01114   cm->initNumVariables = nVar;
01115   cm->variableArray = ALLOC(satVariable_t, cm->initNumVariables+1);
01116   memset(cm->variableArray, 0, sizeof(satVariable_t) *
01117          (cm->initNumVariables+1));
01118   if(cm->maxNodesArraySize <= (cm->initNumVariables+2)*satNodeSize){
01119     cm->nodesArray = ALLOC(long, (cm->initNumVariables+2)*satNodeSize);
01120     cm->maxNodesArraySize = (cm->initNumVariables+2)*satNodeSize;
01121   }
01122 
01123 
01124   if(pism->cm->option->coreGeneration){
01125     pism->cm->dependenceTable = st_init_table(st_ptrcmp, st_ptrhash);
01126 
01127     /*the tables stroes entry of (var_node_idx,cls_idx);*/
01128     pism->cm->anteTable = st_init_table(st_numcmp, st_numhash);
01129 
01130   }
01131   sat_AllocLiteralsDB(cm);
01132 
01133   for(i=1;i<=nVar;i++){
01134     node = sat_CreateNode(cm,2,2);
01135     SATflags(node) |= CoiMask;
01136     SATvalue(node) = 2;
01137     /*SATfanout(node) = 0;*/
01138     cm->nodesArray[node+satFanout] = 0;
01139     SATnFanout(node) = 0;
01140   }
01141 
01142   clauseArray = sat_ArrayAlloc(4096);
01143   for(i=0; i<pism->cnfClauses->nextIndex;i++){
01144     int k, cls_idx,v;
01145     k = array_fetch(int,pism->cnfClauses->clauseArray,i);
01146     if(k!=0){
01147       int sign = 1;
01148       if(k<0){
01149         sign = 0;
01150         k = -k;
01151       }
01152       v = k*satNodeSize + sign;
01153       sat_ArrayInsert(clauseArray, v);
01154     }
01155     else{
01156       if(clauseArray->num == 1) {
01157         v = clauseArray->space[0];
01158         sat_ArrayInsert(cm->unitLits, SATnot(v));
01159         cls_idx = sat_AddUnitClause(cm, clauseArray);
01160         if(pism->cm->option->coreGeneration){
01161           v = SATnormalNode(v);
01162           st_insert(pism->cm->anteTable,(char *)(long)v,(char *)(long)cls_idx);
01163         }
01164       }
01165       else{
01166         cls_idx = sat_AddClause(cm, clauseArray);
01167       }
01168       cm->initNumClauses++;
01169       cm->initNumLiterals += clauseArray->num;
01170       if(i>pism->propertyPos)
01171         SATflags(cls_idx) |= ObjMask;
01172       clauseArray->num = 0;
01173     }
01174   }
01175   sat_ArrayFree(clauseArray);
01176 
01177 }
01178 
01191 array_t * PureSatGetImmediateSupportLatches(
01192   Ntk_Network_t *network,
01193   int            beginPosition,
01194   array_t       *latchNameArray)
01195 {
01196   /* old support, form 0 to 'beginPosition'-1*/
01197   array_t    *dataInputs, *supports;
01198   /* new support, from 'beginPosition' to array_n(latchNameArray)-1*/
01199   array_t    *dataInputs2, *supports2, *supportLatches2;
01200   /* The result should be {new support}-{old support}*/
01201   st_table   *oldSupportTable;
01202   Ntk_Node_t *latch, *dataInput;
01203   int        i;
01204   char       *latchName;
01205 
01206   dataInputs = array_alloc(Ntk_Node_t *, 0);
01207   dataInputs2 = array_alloc(Ntk_Node_t *, 0);
01208   arrayForEachItem(char *, latchNameArray, i, latchName) {
01209     latch = Ntk_NetworkFindNodeByName(network, latchName);
01210     assert(latch);
01211     assert(Ntk_NodeTestIsLatch(latch));
01212     dataInput = Ntk_LatchReadDataInput(latch);
01213     if (i < beginPosition)
01214       array_insert_last(Ntk_Node_t *, dataInputs, dataInput);
01215     else
01216       array_insert_last(Ntk_Node_t *, dataInputs2, dataInput);
01217   }
01218 
01219   supports = Ntk_NodeComputeCombinationalSupport(network, dataInputs, 1);
01220   supports2 = Ntk_NodeComputeCombinationalSupport(network, dataInputs2, 1);
01221   array_free(dataInputs);
01222   array_free(dataInputs2);
01223 
01224   oldSupportTable = st_init_table(st_ptrcmp, st_ptrhash);
01225   arrayForEachItem(Ntk_Node_t *, supports, i, latch) {
01226     st_insert(oldSupportTable, latch, NIL(char));
01227   }
01228   array_free(supports);
01229 
01230   supportLatches2 = array_alloc(char *, 0);
01231   arrayForEachItem(Ntk_Node_t *,  supports2, i, latch) {
01232     if (Ntk_NodeTestIsLatch(latch) &&
01233         !st_is_member(oldSupportTable,latch))
01234       array_insert_last(char *, supportLatches2, Ntk_NodeReadName(latch));
01235   }
01236   array_free(supports2);
01237 
01238   arrayForEachItem(char *, latchNameArray, i, latchName) {
01239     if (i < beginPosition) continue;
01240     latch = Ntk_NetworkFindNodeByName(network, latchName);
01241     if (!st_is_member(oldSupportTable, latch))
01242       array_insert_last(char *, supportLatches2, latchName);
01243   }
01244   st_free_table(oldSupportTable);
01245 
01246   return supportLatches2;
01247 }
01248 
01249 
01261 void PureSatWriteClausesToFile(PureSat_IncreSATManager_t * pism,
01262                                mAig_Manager_t  *maigManager,
01263                                char *coreFile)
01264 {
01265 
01266   BmcCnfClauses_t *cnfClauses = pism->cnfClauses;
01267   st_generator *stGen;
01268   char         *name;
01269   int          cnfIndex, i, k;
01270   FILE *cnfFile;
01271 
01272   coreFile = BmcCreateTmpFile();
01273   cnfFile = fopen(coreFile,"w");
01274 
01275   fprintf(vis_stdout, "coreFile is %s\n", coreFile);
01276   fprintf(vis_stdout,
01277           "Number of Variables = %d Number of Clauses = %d\n",
01278           cnfClauses->cnfGlobalIndex-1,  cnfClauses->noOfClauses);
01279 
01280 
01281   st_foreach_item_int(cnfClauses->cnfIndexTable, stGen, &name, &cnfIndex) {
01282     fprintf(cnfFile, "c %s %d\n",name, cnfIndex);
01283   }
01284   (void) fprintf(cnfFile, "p cnf %d %d\n", cnfClauses->cnfGlobalIndex-1,
01285                  cnfClauses->noOfClauses);
01286   if (cnfClauses->clauseArray != NIL(array_t)) {
01287     for (i = 0; i < cnfClauses->nextIndex; i++) {
01288       k = array_fetch(int, cnfClauses->clauseArray, i);
01289       (void) fprintf(cnfFile, "%d%c", k, (k == 0) ? '\n' : ' ');
01290     }
01291   }
01292   fclose(cnfFile);
01293   FREE(coreFile);
01294   return;
01295 }
01296 
01297 
01310 void PureSatWriteAllClausesToFile(PureSat_IncreSATManager_t * pism,
01311                                   char *coreFile)
01312 {
01313   satManager_t *cm = pism->cm;
01314   char *tmpallcnfFile = BmcCreateTmpFile();
01315   FILE *fp1 = fopen(coreFile,"r");
01316   FILE *fp2 = fopen(tmpallcnfFile,"w");
01317   char buffer[1024], buffer1[1024];
01318   long *space;
01319   int i, numOfConCls=0, numOfVar, sign,var;
01320   satArray_t *saved = cm->savedConflictClauses;
01321 
01322   for(i=0, space=saved->space; i<saved->num; i++,space++){
01323     if(*space<0)
01324       numOfConCls++;
01325   }
01326   if(numOfConCls%2!=0){
01327     fprintf(vis_stdout,"should be times of 2\n");
01328     exit(0);
01329   }
01330   numOfConCls/=2;
01331 
01332   numOfConCls += cm->initNumClauses;
01333   numOfVar = cm->initNumVariables;
01334   i=0;
01335   /*sprintf(buffer,"p cnf %d %d 0\n", numOfConCls, numOfVar);
01336     fputs(buffer, fp2);*/
01337   while(fgets(buffer,1024,fp1)){
01338     if(strncmp(buffer,"c ",2)!=0){
01339       if(strncmp(buffer,"p cnf ",6)==0){
01340         sprintf(buffer1,"p cnf %d %d 0\n", numOfVar, numOfConCls);
01341         fputs(buffer1, fp2);
01342       }
01343       else
01344         fputs(buffer, fp2);
01345     }
01346   }
01347 
01348   saved = cm->savedConflictClauses;
01349 
01350   for(i=0, space=saved->space; i<saved->num; i++){
01351     if(*space<0){
01352       space++;
01353       i++;
01354       while(*space>0){
01355         sign = !SATisInverted(*space);
01356         var = SATnormalNode(*space);
01357         if(sign == 0)
01358           var = -var;
01359         fprintf(fp2,"%d ",var);
01360         space++;
01361         i++;
01362       }
01363       fprintf(fp2,"0\n");
01364       space++;
01365       i++;
01366     }
01367   }
01368   fclose(fp1);
01369   fclose(fp2);
01370   FREE(tmpallcnfFile);
01371 }
01372 
01373 
01374 
01387 array_t * PureSatGetLatchFromTable(Ntk_Network_t *network,
01388                                   PureSat_Manager_t * pm,
01389                                   char *coreFile)
01390 {
01391   FILE *fp;
01392   char buffer[1024],*nodeName;
01393   int idx;
01394   st_table * localTable = st_init_table(strcmp,st_strhash);
01395   array_t * refinement = array_alloc(char *,0);
01396   st_table *ClsidxToLatchTable = pm->ClsidxToLatchTable;
01397   st_table *vertexTable = pm->vertexTable;
01398 
01399   fp = fopen(coreFile,"r");
01400   while(fgets(buffer,1024,fp)){
01401     if(strncmp(buffer,"#clause index:",14)==0){
01402       sscanf(buffer,"#clause index:%d\n",&idx);
01403       if(st_lookup(ClsidxToLatchTable,(char *)(long)idx,&nodeName))
01404         if(!st_lookup(vertexTable,nodeName,NIL(char *))&&!st_lookup(localTable,nodeName,NIL(char *)))
01405           {
01406             st_insert(localTable,nodeName,(char *)0);
01407             array_insert_last(char *,refinement,nodeName);
01408           }
01409     }
01410   }
01411   st_free_table(localTable);
01412   return refinement;
01413 }
01414 
01415 
01416 
01430 array_t * PureSatRemove_char(array_t * array1, int i)
01431 {
01432   int j;
01433   char * str;
01434   array_t * array2=array_alloc(char *,0);
01435   assert(i<array_n(array1));
01436   arrayForEachItem(char *,array1,j,str)
01437     {
01438       if(j!=i)
01439         {
01440 
01441           array_insert_last(char *,array2,str);
01442         }
01443     }
01444       return array2;
01445 }
01446 
01447 
01460 void PureSatComputeNumGatesInAbsForNode(Ntk_Network_t * network,
01461                                         PureSat_Manager_t * pm,
01462                                         Ntk_Node_t * node,
01463                                         st_table * visited,
01464                                         int  * ct,
01465                                         int  * ct1)
01466 {
01467   int i;
01468   Ntk_Node_t *fanin;
01469 
01470 
01471   st_insert(visited,node,(char *)0);
01472 
01473   (*ct1)++;
01474   if(st_lookup(pm->AbsTable,node,NIL(char*)))
01475     (*ct)++;
01476 
01477   if(Ntk_NodeTestIsCombInput(node))
01478     return;
01479 
01480   Ntk_NodeForEachFanin(node,i,fanin){
01481     if(!st_lookup(visited,(char *)fanin,NIL(char *))&&
01482         !Ntk_NodeTestIsLatch(fanin))
01483       PureSatComputeNumGatesInAbsForNode(network,pm,fanin,visited,ct,ct1);
01484   }
01485   return;
01486 
01487 }
01488 
01489 
01503 void PureSatComputeNumGatesInAbs(Ntk_Network_t *network,
01504                                 PureSat_Manager_t *pm,
01505                                 array_t * invisibleArray)
01506 {
01507   int i,ct,ct1,j;
01508   char * name;
01509   Ntk_Node_t * node,*fanin;
01510   st_table * visited;
01511 
01512 
01513   arrayForEachItem(char*,invisibleArray,i,name){
01514     node = Ntk_NetworkFindNodeByName(network,name);
01515     ct = 0;
01516     ct1 = 0;
01517     visited = st_init_table(st_ptrcmp,st_ptrhash);
01518     st_insert(visited,node,(char *)0);
01519 
01520     ct1++;
01521 
01522     Ntk_NodeForEachFanin(node,j,fanin){
01523       if(!st_lookup(visited,(char *)fanin,NIL(char *))&&
01524          !Ntk_NodeTestIsLatch(fanin))
01525         PureSatComputeNumGatesInAbsForNode(network,pm,
01526                                       fanin,visited,&ct,&ct1);
01527     }
01528     st_insert(pm->niaTable,name,(char*)(long)ct);
01529 
01530     st_free_table(visited);
01531   }
01532   return;
01533 
01534 }
01535 
01536 
01549 void PureSatComputeNumGatesInConeForNode(Ntk_Network_t * network,
01550                                        PureSat_Manager_t * pm,
01551                                        Ntk_Node_t * node,
01552                                        st_table * visited,
01553                                        int  * ct)
01554 {
01555   int i;
01556   Ntk_Node_t *fanin;
01557 
01558 
01559   st_insert(visited,node,(char *)0);
01560 
01561   (*ct)++;
01562 
01563   if(Ntk_NodeTestIsCombInput(node))
01564     return;
01565 
01566   Ntk_NodeForEachFanin(node,i,fanin){
01567     if(!st_lookup(visited,(char *)fanin,NIL(char *))&&
01568        !Ntk_NodeTestIsLatch(fanin))
01569       PureSatComputeNumGatesInConeForNode(network,pm,fanin,visited,ct);
01570   }
01571   return;
01572 
01573 }
01574 
01575 
01588 void PureSatComputeNumGatesInCone(Ntk_Network_t *network,
01589                                 PureSat_Manager_t *pm,
01590                                 array_t * latchArray)
01591 {
01592   int i,ct,j;
01593   char * name;
01594   Ntk_Node_t * node,* fanin;
01595   st_table * visited;
01596 
01597 
01598   arrayForEachItem(char*,latchArray,i,name){
01599     node = Ntk_NetworkFindNodeByName(network,name);
01600     ct = 0;
01601     visited = st_init_table(st_ptrcmp,st_ptrhash);
01602     st_insert(visited,node,(char *)0);
01603     ct++;
01604 
01605     Ntk_NodeForEachFanin(node,j,fanin){
01606       if(!st_lookup(visited,(char *)fanin,NIL(char *))&&
01607          !Ntk_NodeTestIsLatch(fanin))
01608         PureSatComputeNumGatesInConeForNode(network,pm
01609                                             ,fanin,visited,&ct);
01610     }
01611     st_insert(pm->nicTable,name,(char*)(long)ct);
01612 
01613     st_free_table(visited);
01614   }
01615   return;
01616 
01617 }
01618 
01619 
01632 array_t * PureSatGenerateRCArray_2(Ntk_Network_t *network,
01633                                    PureSat_Manager_t *pm,
01634                                    array_t *invisibleArray,
01635                                    int *NumInSecondLevel)
01636 {
01637   array_t * tmpRCArray = array_alloc(double,0);
01638   array_t * tmpDfsArray = array_alloc(int,0);
01639   array_t * arrayRC = array_alloc(char*,array_n(invisibleArray));
01640   int i,j;
01641   char * name;
01642 
01643   if(pm->RefPredict)
01644     pm->latchArray = array_alloc(char *,0);
01645 
01646   PureSatComputeNumGatesInAbs(network,pm,invisibleArray);
01647 
01648   for(i=0;i<array_n(invisibleArray);i++){
01649     int nic,nia,dfs;
01650     Ntk_Node_t *tmpnode;
01651     int retVal;
01652     name = array_fetch(char *,invisibleArray,i);
01653     tmpnode = Ntk_NetworkFindNodeByName(network,name);
01654     retVal = st_lookup(pm->node2dfsTable,name,&dfs);
01655     assert(retVal);
01656     array_insert_last(int,tmpDfsArray,dfs);
01657     retVal = st_lookup(pm->nicTable,name,&nic);
01658     assert(retVal);
01659     retVal = st_lookup(pm->niaTable,name,&nia);
01660     assert(retVal);
01661     array_insert_last(double,tmpRCArray,
01662                       (double)nia/(double)nic);
01663   }
01664 
01665 
01666   DfsArray = tmpDfsArray;
01667   RCArray = tmpRCArray;
01668   *NumInSecondLevel = 0;
01669 
01670    {
01671      int tmpvalue;
01672      double rcvalue;
01673      int nn = array_n(invisibleArray);
01674      int *tay=ALLOC(int,nn);
01675      for(j=0;j<nn;j++)
01676        tay[j]=j;
01677      qsort((void *)tay, nn, sizeof(int),
01678            (int (*)(const void *, const void *))NumInConeCompare_Ring);
01679      for(i=0;i<nn;i++)
01680        {
01681          name = array_fetch(char *,invisibleArray,tay[i]);
01682          array_insert(char *,arrayRC,i,name);
01683          tmpvalue = array_fetch(int,tmpDfsArray,tay[i]);
01684          if(tmpvalue == 2)
01685            (*NumInSecondLevel)++;
01686          rcvalue = array_fetch(double,tmpRCArray,tay[i]);
01687          if(pm->verbosity>=1)
01688            fprintf(vis_stdout,"arrayRC %s: %f-%f = %f\n",name,
01689                    (double)tmpvalue*1000000,rcvalue,(double)tmpvalue*1000000-rcvalue);
01690 
01691          if(pm->RefPredict){
01692            if(rcvalue>=0.87&&tmpvalue==2){
01693              array_insert_last(char*,pm->latchArray,name);
01694              if(pm->verbosity>=1)
01695                fprintf(vis_stdout,"add %s into pm->latchArray\n",name);
01696            }
01697          }
01698 
01699        }
01700      FREE(tay);
01701    }
01702 
01703    array_free(DfsArray);
01704    array_free(RCArray);
01705 
01706    return arrayRC;
01707 }
01708 
01709 /*---------------------------------------------------------------------------*/
01710 /* Definition of static functions                                            */
01711 /*---------------------------------------------------------------------------*/