VIS

src/puresat/puresatRefine.c

Go to the documentation of this file.
00001 
00048 #include "puresatInt.h"
00049 
00050 /*---------------------------------------------------------------------------*/
00051 /* Constant declarations                                                     */
00052 /*---------------------------------------------------------------------------*/
00053 
00054 /*---------------------------------------------------------------------------*/
00055 /* Structure declarations                                                    */
00056 /*---------------------------------------------------------------------------*/
00057 
00058 /*---------------------------------------------------------------------------*/
00059 /* Type declarations                                                         */
00060 /*---------------------------------------------------------------------------*/
00061 
00062 /*---------------------------------------------------------------------------*/
00063 /* Variable declarations                                                     */
00064 /*---------------------------------------------------------------------------*/
00065 
00066 /*---------------------------------------------------------------------------*/
00067 /* Macro declarations                                                        */
00068 /*---------------------------------------------------------------------------*/
00069 
00072 /*---------------------------------------------------------------------------*/
00073 /* Static function prototypes                                                */
00074 /*---------------------------------------------------------------------------*/
00075 
00076 
00079 /*---------------------------------------------------------------------------*/
00080 /* Definition of exported functions                                          */
00081 /*---------------------------------------------------------------------------*/
00082 
00083 
00084 /*---------------------------------------------------------------------------*/
00085 /* Definition of internal functions                                          */
00086 /*---------------------------------------------------------------------------*/
00087 
00101 array_t * PureSatRefineOnAbs(Ntk_Network_t *network,
00102                       PureSat_Manager_t *pm,
00103                       bAigEdge_t  property,
00104                       int latchThreshHold)
00105 {
00106   mAig_Manager_t    *maigManager       = Ntk_NetworkReadMAigManager(network);
00107   lsGen             gen;
00108   Ntk_Node_t        *latch;
00109   FILE              *fp, *fp1;
00110   BmcOption_t       *options,*option2;
00111   int        i,j,k,Length,beginPosition=0;
00112   int        NumInSecondLevel=0;
00113   array_t    * tmpRefinement,*tmpArray1,*tmpArray2;
00114   array_t    *tmpModel,*tmpRefinement1;
00115   /*st_table   * RefinementTable, *CandidateTable;*/
00116   array_t    *Pclause, *tmpRefinement2, *tmpRefinement3, *oriSufArray;
00117   char       buffer[1024],str[100];
00118   char       *name, *coreFile, *tmpCoreFile, *coreFile1=(char *)0;
00119   int         oldLength=0,oriCoreSize, CoreSize,weight;
00120   st_table          *nodeToMvfAigTable;
00121   BmcCnfStates_t    *cnfstate;
00122   int             oldNumOfLatchInCore;
00123   int             newNumOfLatchInCore,NumOfLatchInModel,round;
00124   boolean     VarInCoreIsEnough = FALSE, LatchFromCore = FALSE;
00125   boolean     firstTime = TRUE;
00126   long t1,t2,t3,t4;
00127   st_table * localSufAbsTable;
00128   PureSat_IncreSATManager_t * pism = PureSatIncreSATManagerAlloc(pm);
00129   satManager_t * cm = pism->cm;
00130   BmcCnfClauses_t   * cnfClauses = pism->cnfClauses;
00131   st_table *vertexTable = pm->vertexTable;
00132   st_table *SufAbsTable = pm->SufAbsTable;
00133   /*st_table *CoiTable = pm->CoiTable;*/
00134   /*st_table *supportTable = pm->supportTable;*/
00135   /* st_table *AbsTable = pm->AbsTable;*/
00136     
00137   t1 = util_cpu_ctime();
00138 
00139   cm->option->clauseDeletionHeuristic = 0;
00140   cm->option->incTraceObjective = 0;
00141   pism->Length = pm->Length;
00142   Length =  pm->Length;
00143   coreFile = BmcCreateTmpFile();
00144   tmpCoreFile = BmcCreateTmpFile();
00145   strcpy(str,"coreFile: ");
00146   strcat(str,coreFile);
00147   strcat(str,", tmpCoreFile: ");
00148   strcat(str,tmpCoreFile);
00149   strcat(str,"\n");
00150   if(pm->verbosity>=2)
00151     fprintf(vis_stdout,"%s",str);
00152   nodeToMvfAigTable = (st_table *) Ntk_NetworkReadApplInfo(network, MVFAIG_NETWORK_APPL_KEY);
00153   if (nodeToMvfAigTable == NIL(st_table)){
00154     (void) fprintf(vis_stderr, "** bmc error: please run buid_partiton_maigs first\n");
00155     exit (0);
00156   }
00157   
00158   option2 = BmcOptionAlloc();
00159   option2->satInFile = BmcCreateTmpFile();
00160   option2->satOutFile = BmcCreateTmpFile();
00161   options = BmcOptionAlloc();
00162   options->satInFile = BmcCreateTmpFile();
00163   options->satOutFile = BmcCreateTmpFile();
00164 
00165   newNumOfLatchInCore = 0;
00166   NumOfLatchInModel = 0;
00167   tmpModel = array_alloc(char *,0);
00168   oriSufArray = array_alloc(char *,0);
00169   Ntk_NetworkForEachLatch(network, gen, latch){
00170     name = Ntk_NodeReadName(latch);
00171     if(st_lookup(vertexTable,name,NIL(char *))){
00172       array_insert_last(char *,tmpModel,name);
00173       NumOfLatchInModel++;
00174     }
00175     else
00176       if(st_lookup(SufAbsTable,latch,NIL(char *))){
00177         newNumOfLatchInCore++;
00178         array_insert_last(char *,oriSufArray,name);
00179       }
00180   }
00181   
00182   localSufAbsTable = st_copy(SufAbsTable);
00183   //tmpRefinement2 = array_alloc(char *,0);
00184   tmpRefinement3 = array_alloc(char *,0);
00185   //tmpArray1 = array_alloc(char *,0);
00186   //tmpArray2 = array_alloc(char *,0);
00187   weight = (NumOfLatchInModel+newNumOfLatchInCore+1)*10000;
00188   round=0;
00189   
00190   round++;
00191   if(pm->verbosity>=2)
00192     fprintf(vis_stdout,"round: %d\n",round);
00193   oldNumOfLatchInCore = newNumOfLatchInCore;
00194   pm->ClsidxToLatchTable = st_init_table(st_numcmp,st_numhash);
00195   PureSatGenerateClausesForPath_EnhanceInit(network,0,Length,pism,pm,nodeToMvfAigTable,localSufAbsTable);
00196   Pclause = array_alloc(int,0);
00197   array_insert_last(int, Pclause, BmcGenerateCnfFormulaForAigFunction(maigManager,property,
00198                                                                       Length, cnfClauses));
00199   BmcCnfInsertClause(cnfClauses, Pclause);
00200   array_free(Pclause);
00201   CoreSize=cnfClauses->noOfClauses;
00202   
00203   oriCoreSize = CoreSize;
00204   cm->option->coreGeneration = 1;
00205   cm->fp = fopen(tmpCoreFile, "w");
00206   if(pm->verbosity>=2)
00207     PureSatWriteClausesToFile(pism,maigManager,coreFile1);
00208   t1 = util_cpu_ctime();
00209   PureSatReadClauses(pism,pm);
00210   sat_Main(cm);
00211   t2 = util_cpu_ctime();
00212   if(pm->verbosity>=2)
00213     fprintf(vis_stdout,"time for SAT Solver(satMain): %g\n",(double)((t2-t1)/1000.0));
00214   
00215   fclose(cm->fp);
00216   if(cm->status == SAT_SAT){
00217     fprintf(vis_stderr,"This instance is supposed to be UNSAT, wrong and exit\n");
00218     exit(0);
00219   }
00220   cm->stdOut = vis_stdout; 
00221   cm->option->coreGeneration = 0;
00222   
00223   t2 = util_cpu_ctime();
00224   if(pm->verbosity>=2)
00225     fprintf(vis_stdout,"time for satMain: %g\n",(double)((t2-t1)/1000.0));
00226   CoreSize = cm->numOfClsInCore;
00227   if(pm->verbosity>=2)
00228     fprintf(vis_stdout,"CoreSize:%d/OriCoreSize:%d\n", CoreSize,oriCoreSize);
00229   
00230   fp1 = fopen(coreFile, "w");
00231   fp = fopen(tmpCoreFile, "r");
00232   sprintf(buffer,"p cnf %d %d 0\n", cm->initNumVariables, cm->numOfClsInCore);
00233   fputs(buffer, fp1);
00234   while(fgets(buffer,1024,fp)){
00235     fputs(buffer, fp1);
00236   }
00237   fclose(fp);
00238   fclose(fp1);
00239 
00240   //tmpRefinement1 = array_alloc(char *,0);
00241   /* generate sufficient refinement */
00242   st_free_table(localSufAbsTable);
00243   localSufAbsTable = st_init_table(st_ptrcmp,st_ptrhash);  
00244   t3 = util_cpu_ctime();
00245   tmpRefinement1 = PureSatGetLatchFromTable(network,pm,coreFile);
00246   t4 = util_cpu_ctime();
00247   if(pm->verbosity>=2)
00248     fprintf(vis_stdout,"time for PureSatGetLatchFromTable: %g\n",(double)((t4-t3)/1000.0));
00249   
00250   if(array_n(tmpRefinement1)>array_n(oriSufArray)){
00251     array_free(tmpRefinement1);
00252     tmpRefinement1 = array_dup(oriSufArray);
00253   }
00254   else{
00255     array_free(oriSufArray);
00256     oriSufArray = array_dup(tmpRefinement1);
00257   }
00258   if(pm->verbosity>=2)
00259     fprintf(vis_stdout,"All latches picked from UNSAT Proof:\n");
00260   arrayForEachItem(char *,tmpRefinement1,i,name){
00261     if(pm->verbosity>=2)
00262       fprintf(vis_stdout," %s  ",name);
00263     latch = Ntk_NetworkFindNodeByName(network,name);
00264     st_insert(localSufAbsTable,latch,(char *)0);
00265   }
00266   if(pm->verbosity>=2){
00267     fprintf(vis_stdout,"\n");
00268     fprintf(vis_stdout,"tmpModel: \n");
00269   }
00270   arrayForEachItem(char *,tmpModel,i,name){
00271     if(pm->verbosity>=2)
00272       fprintf(vis_stdout," %s  ",name);
00273     latch = Ntk_NetworkFindNodeByName(network,name);
00274     st_insert(localSufAbsTable,latch,(char *)0);
00275   }
00276   if(pm->verbosity>=2)
00277     fprintf(vis_stdout,"\n");
00278   newNumOfLatchInCore = array_n(tmpRefinement1);
00279   BmcCnfClausesFree(pism->cnfClauses);
00280   
00281   if(pm->verbosity>=2)
00282     fprintf(vis_stdout,"newNumOfLatchInCore/oldNumOfLatchInCore=%f\n",(double)newNumOfLatchInCore/(double)oldNumOfLatchInCore);
00283   
00284   /* Add the refinement to vertexTable*/
00285   arrayForEachItem(char *,tmpRefinement1,i,name){
00286     st_insert(vertexTable, name,(char*)0);
00287   }
00288   
00289   pism->cnfClauses = BmcCnfClausesAlloc();
00290   cnfClauses = pism->cnfClauses;
00291   if(array_n(tmpRefinement1)) 
00292     {
00293       tmpRefinement = PureSatGenerateRingFromAbs(network,pm,tmpRefinement1,&NumInSecondLevel);
00294       /* latchThreshHold = (latchThreshHold <= NumInSecondLevel) ? latchThreshHold:NumInSecondLevel;*/
00295       array_free(tmpRefinement1);
00296       LatchFromCore = TRUE;
00297       for(i=0;i<array_n(tmpRefinement);i=i+latchThreshHold)
00298         {
00299           for(j=0;j<latchThreshHold;j++)
00300             {
00301               if((i+j)<array_n(tmpRefinement))
00302                 {
00303                   name = array_fetch(char *,tmpRefinement,i+j);
00304                   array_insert_last(char *,tmpRefinement3,name);
00305                   if(pm->verbosity>=1)
00306                     fprintf(vis_stdout, "picking %s\n",name);
00307                 }
00308               else
00309                 break;
00310             }
00311           tmpRefinement2=array_dup(tmpModel);
00312           array_append(tmpRefinement2,tmpRefinement3);
00313           if(!PureSatExistCE(network,pism,option2,tmpRefinement2,property,pm,0)){
00314             VarInCoreIsEnough = TRUE;
00315             array_free(tmpRefinement2);
00316             for(k=i+j;k<array_n(tmpRefinement);k++)
00317               {
00318                 name = array_fetch(char *,tmpRefinement,k);
00319                 if(st_lookup(vertexTable,name,NIL(char *))){
00320                   st_delete(vertexTable,&name,NIL(char *));
00321                 }
00322               }
00323             break;
00324           }
00325           firstTime = FALSE;
00326           beginPosition = array_n(tmpRefinement2);
00327           array_free(tmpRefinement2);
00328           oldLength = Length;
00329         } 
00330       array_free(tmpRefinement);
00331       tmpRefinement = array_dup(tmpRefinement3); /* now tmpRefinement1 contains the
00332                                                    latches in Core*/
00333       array_free(tmpRefinement3);
00334       oldLength=0;
00335       beginPosition=0;
00336       BmcCnfClausesFree(pism->cnfClauses);
00337       pism->cnfClauses = BmcCnfClausesAlloc();
00338     }
00339   
00340 #if 1
00341   if(!VarInCoreIsEnough){
00342     fprintf(vis_stderr,"this should never happen, wrong\n");
00343     exit(0);
00344   }
00345 #endif
00346   
00347   //tmpRefinement = array_dup(tmpRefinement1);
00348   //array_free(tmpRefinement1);
00349   
00350 # if 1
00351   /*Refinement Minimization
00352     try all the candidates*/
00353   for(i=array_n(tmpRefinement)-1;i>=0;i--)
00354     {
00355       name = array_fetch(char *,tmpRefinement,i);
00356       if(pm->verbosity>=1)
00357         fprintf(vis_stdout,"RefMin: testing %s\n",name);
00358       tmpArray2 = PureSatRemove_char(tmpRefinement,i);
00359       tmpArray1 = array_dup(tmpModel);
00360       array_append(tmpArray1,tmpArray2);
00361       cnfstate = BmcCnfClausesFreeze(pism->cnfClauses);
00362       if(PureSatExistCE(network,pism,option2,tmpArray1,property,pm,0))
00363         array_free(tmpArray2);
00364       else /*delete it*/
00365         {
00366           /*   i--;*/
00367           name = array_fetch(char *, tmpRefinement,i);
00368           if(st_lookup(vertexTable, name,NIL(char *)))
00369             st_delete(vertexTable, &name, NIL(char *));
00370           else
00371             fprintf(vis_stderr," %s should be in vertexTable, Wrong!!!\n",name);
00372           array_free(tmpRefinement);
00373           tmpRefinement = tmpArray2;
00374         }
00375       array_free(tmpArray1);
00376       BmcCnfClausesRestore(pism->cnfClauses, cnfstate);
00377       FREE(cnfstate);
00378     }
00379   //BmcCnfClausesFree(pism->cnfClauses);
00380 #endif
00381   
00382   if(pm->verbosity>=1){
00383     fprintf(vis_stdout,"\n sufficient refinement candidates from CA\n");
00384     arrayForEachItem(char *,tmpRefinement,i,name)
00385       fprintf(vis_stdout,"%s\n",name);
00386   }
00387   BmcOptionFree(option2);
00388   BmcOptionFree(options);
00389   unlink(coreFile);
00390   unlink(tmpCoreFile);
00391   FREE(coreFile);
00392   FREE(tmpCoreFile);
00393   array_free(oriSufArray);
00394   st_free_table(pm->ClsidxToLatchTable);
00395   pm->ClsidxToLatchTable = NIL(st_table);
00396   PureSatIncreSATManagerFree(pm,pism);
00397   return tmpRefinement;
00398 }
00399 
00400 
00401 /*---------------------------------------------------------------------------*/
00402 /* Definition of static functions                                            */
00403 /*---------------------------------------------------------------------------*/