Example #1
0
void todot(DdManager *manager, DdNode *f, const char * fname) {
  FILE *out = fopen(fname,"w");
  DdNode *outputs[] = {f};
  char *names[] = {"f"};
  Cudd_DumpDot(manager,1,outputs,NULL,names,out);
  fclose(out);
}
/**Function*************************************************************

  Synopsis    [Visualizes BDD of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodeShowBdd( Abc_Obj_t * pNode )
{
    FILE * pFile;
    Vec_Ptr_t * vNamesIn;
    char FileNameDot[200];
    char * pNameOut;

    assert( Abc_NtkIsBddLogic(pNode->pNtk) );
    // create the file name
    Abc_ShowGetFileName( Abc_ObjName(pNode), FileNameDot );
    // check that the file can be opened
    if ( (pFile = fopen( FileNameDot, "w" )) == NULL )
    {
        fprintf( stdout, "Cannot open the intermediate file \"%s\".\n", FileNameDot );
        return;
    }

    // set the node names 
    vNamesIn = Abc_NodeGetFaninNames( pNode );
    pNameOut = Abc_ObjName(pNode);
    Cudd_DumpDot( pNode->pNtk->pManFunc, 1, (DdNode **)&pNode->pData, (char **)vNamesIn->pArray, &pNameOut, pFile );
    Abc_NodeFreeNames( vNamesIn );
    Abc_NtkCleanCopy( pNode->pNtk );
    fclose( pFile );

    // visualize the file 
    Abc_ShowFile( FileNameDot );
}
Example #3
0
 void BDD::dot(const char* fname) const {
   FILE *out = fopen(fname,"w");
   DdNode *outputs[] = {node};
   char *names[] = {"f"};
   Cudd_DumpDot(Cudd::dd,1,outputs,NULL,names,out);
   fclose(out);
 }
Example #4
0
static YAP_Bool create_dot(void) {
  const char *onames[] = {"Out"};
  char **inames;
  DdNode *array[1];
  YAP_Term arg1, arg2;
  int i, b, index;
  variable v;
  char numberVar[10], numberBit[10], filename[1000];
  FILE *file;

  arg1 = YAP_ARG1;
  arg2 = YAP_ARG2;

  YAP_StringToBuffer(arg2, filename, 1000);
  inames = (char **)malloc(sizeof(char *) * (boolVars_ex[ex]));
  index = 0;
  for (i = 0; i < nVars_ex[ex]; i++) {
    v = vars_ex[ex][i];
    for (b = 0; b < v.nVal - 1; b++) {
      inames[b + index] = (char *)malloc(sizeof(char) * 20);
      strcpy(inames[b + index], "X");
      sprintf(numberVar, "%d", i);
      strcat(inames[b + index], numberVar);
      strcat(inames[b + index], "_");
      sprintf(numberBit, "%d", b);
      strcat(inames[b + index], numberBit);
    }
    index = index + v.nVal - 1;
  }
  array[0] = (DdNode *)YAP_IntOfTerm(arg1);
  file = open_file(filename, "w");
  Cudd_DumpDot(mgr_ex[ex], 1, array, (const char **)inames, onames, file);
  fclose(file);
  index = 0;
  for (i = 0; i < nVars_ex[ex]; i++) {
    v = vars_ex[ex][i];
    for (b = 0; b < v.nVal - 1; b++) {
      free(inames[b + index]);
    }
    index = index + v.nVal - 1;
  }
  free(inames);
  return 1;
}
Example #5
0
void create_dotc(int node,prolog_term filenameatom)
{
    char * onames[]= {"Out"};
    char ** inames;
    DdNode * array[1];
    int i,b,index;
    variable v;
    char numberVar[10],numberBit[10];
    FILE * file;
    char * filename;

    filename=p2c_string(filenameatom);
    inames= (char **) malloc(sizeof(char *)*boolVars);
    index=0;
    for (i=0; i<nVars; i++)
    {
        v=vars[i];
        for (b=0; b<v.nVal-1; b++)
        {
            inames[b+index]=(char *) malloc(sizeof(char)*20);
            strcpy(inames[b+index],"X");
            sprintf(numberVar,"%d",i);
            strcat(inames[b+index],numberVar);
            strcat(inames[b+index],"_");
            sprintf(numberBit,"%d",b);
            strcat(inames[b+index],numberBit);
        }
        index=index+v.nVal-1;
    }
    array[0]=(DdNode *)node;
    file = open_file(filename, "w");

    Cudd_DumpDot(mgr,1,array,inames,onames,file);
    fclose(file);
    index=0;
    for (i=0; i<nVars; i++)
    {
        v=vars[i];
        for (b=0; b<v.nVal-1; b++)
            free(inames[b+index]);
        index=index+v.nVal-1;
    }
    free(inames);
}
Example #6
0
void BddBuilder::dotDump(){
	char filename[128];
	DdNode * dumpdd[1];
	char * dumpname[1];
	int res;
	for(int i=0 ; i<__outputWireCnt ; ++i){
		sprintf(filename, "./dotdump/dump_%s.dot", __ppOutputNodesNames[i]);
		printf("DOT dump for %s ", __ppOutputNodesNames[i]);

		FILE * fp = fopen(filename, "w");
		dumpdd[0] = __pddOutputNodes[i];
		dumpname[0] = __ppOutputNodesNames[i];

		res = Cudd_DumpDot(__pddManager, 1, dumpdd, __ppInputNodesNames, dumpname, fp);
		if(res == 1)
			printf("completed.\n");
		else
			printf("failed.\n");
		fclose(fp);
	}
	//cout<<Cudd_ReadNodeCount(__pddManager)<<endl;
}
Example #7
0
/**Function********************************************************************

  Synopsis    [Main function for testcudd.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
main(int argc, char **argv)
{
    FILE *fp;           /* pointer to input file */
    char *file = (char *) "";	/* input file name */
    FILE *dfp = NULL;	/* pointer to dump file */
    char *dfile;	/* file for DD dump */
    DdNode *dfunc[2];	/* addresses of the functions to be dumped */
    DdManager *dd;	/* pointer to DD manager */
    DdNode *_true;	/* fast access to constant function */
    DdNode *M;
    DdNode **x;		/* pointers to variables */
    DdNode **y;		/* pointers to variables */
    DdNode **xn;       	/* complements of row variables */
    DdNode **yn_;      	/* complements of column variables */
    DdNode **xvars;
    DdNode **yvars;
    DdNode *C;		/* result of converting from ADD to BDD */
    DdNode *ess;	/* cube of essential variables */
    DdNode *shortP;	/* BDD cube of shortest path */
    DdNode *largest;	/* BDD of largest cube */
    DdNode *shortA;	/* ADD cube of shortest path */
    DdNode *constN;	/* value returned by evaluation of ADD */
    DdNode *ycube;	/* cube of the negated y vars for c-proj */
    DdNode *CP;		/* C-Projection of C */
    DdNode *CPr;	/* C-Selection of C */
    int    length;	/* length of the shortest path */
    int    nx;			/* number of variables */
    int    ny;
    int    maxnx;
    int    maxny;
    int    m;
    int    n;
    int    N;
    int    cmu;			/* use CMU multiplication */
    int    pr;			/* verbose printout level */
    int    harwell;
    int    multiple;		/* read multiple matrices */
    int    ok;
    int    c;			/* variable to read in options */
    int    approach;		/* reordering approach */
    int    autodyn;		/* automatic reordering */
    int    groupcheck;		/* option for group sifting */
    int    profile;		/* print heap profile if != 0 */
    int    keepperm;		/* keep track of permutation */
    int    clearcache;		/* clear the cache after each matrix */
    int    blifOrDot;		/* dump format: 0 -> dot, 1 -> blif, ... */
    int    retval;		/* return value */
    int    i;			/* loop index */
    long   startTime;		/* initial time */
    long   lapTime;
    int    size;
    unsigned int cacheSize, maxMemory;
    unsigned int nvars,nslots;

    startTime = util_cpu_time();

    approach = CUDD_REORDER_NONE;
    autodyn = 0;
    pr = 0;
    harwell = 0;
    multiple = 0;
    profile = 0;
    keepperm = 0;
    cmu = 0;
    N = 4;
    nvars = 4;
    cacheSize = 127;
    maxMemory = 0;
    nslots = CUDD_UNIQUE_SLOTS;
    clearcache = 0;
    groupcheck = CUDD_GROUP_CHECK7;
    dfile = NULL;
    blifOrDot = 0; /* dot format */

    /* Parse command line. */
    while ((c = util_getopt(argc, argv, (char *) "CDHMPS:a:bcd:g:hkmn:p:v:x:X:"))
	   != EOF) {
	switch(c) {
	case 'C':
	    cmu = 1;
	    break;
	case 'D':
	    autodyn = 1;
	    break;
	case 'H':
	    harwell = 1;
	    break;
	case 'M':
#ifdef MNEMOSYNE
	    (void) mnem_setrecording(0);
#endif
	    break;
	case 'P':
	    profile = 1;
	    break;
	case 'S':
	    nslots = atoi(util_optarg);
	    break;
	case 'X':
	    maxMemory = atoi(util_optarg);
	    break;
	case 'a':
	    approach = atoi(util_optarg);
	    break;
	case 'b':
	    blifOrDot = 1; /* blif format */
	    break;
	case 'c':
	    clearcache = 1;
	    break;
	case 'd':
	    dfile = util_optarg;
	    break;
	case 'g':
	    groupcheck = atoi(util_optarg);
	    break;
	case 'k':
	    keepperm = 1;
	    break;
	case 'm':
	    multiple = 1;
	    break;
	case 'n':
	    N = atoi(util_optarg);
	    break;
	case 'p':
	    pr = atoi(util_optarg);
	    break;
	case 'v':
	    nvars = atoi(util_optarg);
	    break;
	case 'x':
	    cacheSize = atoi(util_optarg);
	    break;
	case 'h':
	default:
	    usage(argv[0]);
	    break;
	}
    }

    if (argc - util_optind == 0) {
	file = (char *) "-";
    } else if (argc - util_optind == 1) {
	file = argv[util_optind];
    } else {
	usage(argv[0]);
    }
    if ((approach<0) || (approach>17)) {
	(void) fprintf(stderr,"Invalid approach: %d \n",approach);
	usage(argv[0]);
    }

    if (pr >= 0) {
	(void) printf("# %s\n", TESTCUDD_VERSION);
	/* Echo command line and arguments. */
	(void) printf("#");
	for (i = 0; i < argc; i++) {
	    (void) printf(" %s", argv[i]);
	}
	(void) printf("\n");
	(void) fflush(stdout);
    }

    /* Initialize manager and provide easy reference to terminals. */
    dd = Cudd_Init(nvars,0,nslots,cacheSize,maxMemory);
    _true = DD_TRUE(dd);
    dd->groupcheck = (Cudd_AggregationType) groupcheck;
    if (autodyn) Cudd_AutodynEnable(dd,CUDD_REORDER_SAME);

    /* Open input file. */
    fp = open_file(file, "r");

    /* Open dump file if requested */
    if (dfile != NULL) {
	dfp = open_file(dfile, "w");
    }

    x = y = xn = yn_ = NULL;
    do {
	/* We want to start anew for every matrix. */
	maxnx = maxny = 0;
	nx = maxnx; ny = maxny;
	if (pr>0) lapTime = util_cpu_time();
	if (harwell) {
	    if (pr >= 0) (void) printf(":name: ");
	    ok = Cudd_addHarwell(fp, dd, &M, &x, &y, &xn, &yn_, &nx, &ny,
	    &m, &n, 0, 2, 1, 2, pr);
	} else {
	    ok = Cudd_addRead(fp, dd, &M, &x, &y, &xn, &yn_, &nx, &ny,
	    &m, &n, 0, 2, 1, 2);
	    if (pr >= 0)
		(void) printf(":name: %s: %d rows %d columns\n", file, m, n);
	}
	if (!ok) {
	    (void) fprintf(stderr, "Error reading matrix\n");
	    exit(1);
	}

	if (nx > maxnx) maxnx = nx;
	if (ny > maxny) maxny = ny;

	/* Build cube of negated y's. */
	ycube = DD_TRUE(dd);
	Cudd_Ref(ycube);
	for (i = maxny - 1; i >= 0; i--) {
	    DdNode *tmpp;
	    tmpp = Cudd_bddAnd(dd,Cudd_Not(dd->vars[y[i]->index]),ycube);
	    if (tmpp == NULL) exit(2);
	    Cudd_Ref(tmpp);
	    Cudd_RecursiveDeref(dd,ycube);
	    ycube = tmpp;
	}
	/* Initialize vectors of BDD variables used by priority func. */
	xvars = ALLOC(DdNode *, nx);
	if (xvars == NULL) exit(2);
	for (i = 0; i < nx; i++) {
	    xvars[i] = dd->vars[x[i]->index];
	}
	yvars = ALLOC(DdNode *, ny);
	if (yvars == NULL) exit(2);
	for (i = 0; i < ny; i++) {
	    yvars[i] = dd->vars[y[i]->index];
	}

	/* Clean up */
	for (i=0; i < maxnx; i++) {
	    Cudd_RecursiveDeref(dd, x[i]);
	    Cudd_RecursiveDeref(dd, xn[i]);
	}
	FREE(x);
	FREE(xn);
	for (i=0; i < maxny; i++) {
	    Cudd_RecursiveDeref(dd, y[i]);
	    Cudd_RecursiveDeref(dd, yn_[i]);
	}
	FREE(y);
	FREE(yn_);

	if (pr>0) {(void) printf(":1: M"); Cudd_PrintDebug(dd,M,nx+ny,pr);}

	if (pr>0) (void) printf(":2: time to read the matrix = %s\n",
		    util_print_time(util_cpu_time() - lapTime));

	C = Cudd_addBddPattern(dd, M);
	if (C == 0) exit(2);
	Cudd_Ref(C);
	if (pr>0) {(void) printf(":3: C"); Cudd_PrintDebug(dd,C,nx+ny,pr);}

	/* Test iterators. */
	retval = testIterators(dd,M,C,pr);
	if (retval == 0) exit(2);

	cuddCacheProfile(dd,stdout);

	/* Test XOR */
	retval = testXor(dd,C,pr,nx+ny);
	if (retval == 0) exit(2);

	/* Test Hamming distance functions. */
	retval = testHamming(dd,C,pr);
	if (retval == 0) exit(2);

	/* Test selection functions. */
	CP = Cudd_CProjection(dd,C,ycube);
	if (CP == NULL) exit(2);
	Cudd_Ref(CP);
	if (pr>0) {(void) printf("ycube"); Cudd_PrintDebug(dd,ycube,nx+ny,pr);}
	if (pr>0) {(void) printf("CP"); Cudd_PrintDebug(dd,CP,nx+ny,pr);}

	if (nx == ny) {
	    CPr = Cudd_PrioritySelect(dd,C,xvars,yvars,(DdNode **)NULL,
		(DdNode *)NULL,ny,Cudd_Xgty);
	    if (CPr == NULL) exit(2);
	    Cudd_Ref(CPr);
	    if (pr>0) {(void) printf(":4: CPr"); Cudd_PrintDebug(dd,CPr,nx+ny,pr);}
	    if (CP != CPr) {
		(void) printf("CP != CPr!\n");
	    }
	    Cudd_RecursiveDeref(dd, CPr);
	}
	FREE(xvars); FREE(yvars);

	Cudd_RecursiveDeref(dd, CP);
	Cudd_RecursiveDeref(dd, ycube);

	/* Test functions for essential variables. */
	ess = Cudd_FindEssential(dd,C);
	if (ess == NULL) exit(2);
	Cudd_Ref(ess);
	if (pr>0) {(void) printf(":4: ess"); Cudd_PrintDebug(dd,ess,nx+ny,pr);}
	Cudd_RecursiveDeref(dd, ess);

	/* Test functions for shortest paths. */
	shortP = Cudd_ShortestPath(dd, M, NULL, NULL, &length);
	if (shortP == NULL) exit(2);
	Cudd_Ref(shortP);
	if (pr>0) {
	    (void) printf(":5: shortP"); Cudd_PrintDebug(dd,shortP,nx+ny,pr);
	}
	/* Test functions for largest cubes. */
	largest = Cudd_LargestCube(dd, Cudd_Not(C), &length);
	if (largest == NULL) exit(2);
	Cudd_Ref(largest);
	if (pr>0) {
	    (void) printf(":5b: largest");
	    Cudd_PrintDebug(dd,largest,nx+ny,pr);
	}
	Cudd_RecursiveDeref(dd, largest);

	/* Test Cudd_addEvalConst and Cudd_addIteConstant. */
	shortA = Cudd_BddToAdd(dd,shortP);
	if (shortA == NULL) exit(2);
	Cudd_Ref(shortA);
	Cudd_RecursiveDeref(dd, shortP);
	constN = Cudd_addEvalConst(dd,shortA,M);
	if (constN == DD_NON_CONSTANT) exit(2);
	if (Cudd_addIteConstant(dd,shortA,M,constN) != constN) exit(2);
	if (pr>0) {(void) printf("The value of M along the chosen shortest path is %g\n", cuddV(constN));}
	Cudd_RecursiveDeref(dd, shortA);

	shortP = Cudd_ShortestPath(dd, C, NULL, NULL, &length);
	if (shortP == NULL) exit(2);
	Cudd_Ref(shortP);
	if (pr>0) {
	    (void) printf(":6: shortP"); Cudd_PrintDebug(dd,shortP,nx+ny,pr);
	}

	/* Test Cudd_bddIteConstant and Cudd_bddLeq. */
	if (!Cudd_bddLeq(dd,shortP,C)) exit(2);
	if (Cudd_bddIteConstant(dd,Cudd_Not(shortP),_true,C) != _true) exit(2);
	Cudd_RecursiveDeref(dd, shortP);

	if (profile) {
	    retval = cuddHeapProfile(dd);
	}

	size = dd->size;

	if (pr>0) {
	    (void) printf("Average distance: %g\n", Cudd_AverageDistance(dd));
	}

	/* Reorder if so requested. */
        if (approach != CUDD_REORDER_NONE) {
#ifndef DD_STATS
	    retval = Cudd_EnableReorderingReporting(dd);
	    if (retval == 0) {
		(void) fprintf(stderr,"Error reported by Cudd_EnableReorderingReporting\n");
		exit(3);
	    }
#endif
#ifdef DD_DEBUG
	    retval = Cudd_DebugCheck(dd);
	    if (retval != 0) {
		(void) fprintf(stderr,"Error reported by Cudd_DebugCheck\n");
		exit(3);
	    }
	    retval = Cudd_CheckKeys(dd);
	    if (retval != 0) {
		(void) fprintf(stderr,"Error reported by Cudd_CheckKeys\n");
		exit(3);
	    }
#endif
	    retval = Cudd_ReduceHeap(dd,(Cudd_ReorderingType)approach,5);
	    if (retval == 0) {
		(void) fprintf(stderr,"Error reported by Cudd_ReduceHeap\n");
		exit(3);
	    }
#ifndef DD_STATS
	    retval = Cudd_DisableReorderingReporting(dd);
	    if (retval == 0) {
		(void) fprintf(stderr,"Error reported by Cudd_DisableReorderingReporting\n");
		exit(3);
	    }
#endif
#ifdef DD_DEBUG
	    retval = Cudd_DebugCheck(dd);
	    if (retval != 0) {
		(void) fprintf(stderr,"Error reported by Cudd_DebugCheck\n");
		exit(3);
	    }
	    retval = Cudd_CheckKeys(dd);
	    if (retval != 0) {
		(void) fprintf(stderr,"Error reported by Cudd_CheckKeys\n");
		exit(3);
	    }
#endif
	    if (approach == CUDD_REORDER_SYMM_SIFT ||
	    approach == CUDD_REORDER_SYMM_SIFT_CONV) {
		Cudd_SymmProfile(dd,0,dd->size-1);
	    }

	    if (pr>0) {
		(void) printf("Average distance: %g\n", Cudd_AverageDistance(dd));
	    }

	    if (keepperm) {
		/* Print variable permutation. */
		(void) printf("Variable Permutation:");
		for (i=0; i<size; i++) {
		    if (i%20 == 0) (void) printf("\n");
		    (void) printf("%d ", dd->invperm[i]);
		}
		(void) printf("\n");
		(void) printf("Inverse Permutation:");
		for (i=0; i<size; i++) {
		    if (i%20 == 0) (void) printf("\n");
		    (void) printf("%d ", dd->perm[i]);
		}
		(void) printf("\n");
	    }

	    if (pr>0) {(void) printf("M"); Cudd_PrintDebug(dd,M,nx+ny,pr);}

	    if (profile) {
		retval = cuddHeapProfile(dd);
	    }

	}

	/* Dump DDs of C and M if so requested. */
	if (dfile != NULL) {
	    dfunc[0] = C;
	    dfunc[1] = M;
	    if (blifOrDot == 1) {
		/* Only dump C because blif cannot handle ADDs */
		retval = Cudd_DumpBlif(dd,1,dfunc,NULL,(char **)onames,
				       NULL,dfp);
	    } else {
		retval = Cudd_DumpDot(dd,2,dfunc,NULL,(char **)onames,dfp);
	    }
	    if (retval != 1) {
		(void) fprintf(stderr,"abnormal termination\n");
		exit(2);
	    }
	}

	Cudd_RecursiveDeref(dd, C);
	Cudd_RecursiveDeref(dd, M);

	if (clearcache) {
	    if (pr>0) {(void) printf("Clearing the cache... ");}
	    for (i = dd->cacheSlots - 1; i>=0; i--) {
		dd->cache[i].data = NIL(DdNode);
	    }
	    if (pr>0) {(void) printf("done\n");}
	}
	if (pr>0) {
	    (void) printf("Number of variables = %6d\t",dd->size);
	    (void) printf("Number of slots     = %6d\n",dd->slots);
	    (void) printf("Number of keys      = %6d\t",dd->keys);
	    (void) printf("Number of min dead  = %6d\n",dd->minDead);
	}

    } while (multiple && !feof(fp));

    fclose(fp);
    if (dfile != NULL) {
	fclose(dfp);
    }

    /* Second phase: experiment with Walsh matrices. */
    if (!testWalsh(dd,N,cmu,approach,pr)) {
	exit(2);
    }

    /* Check variable destruction. */
    assert(cuddDestroySubtables(dd,3));
    assert(Cudd_DebugCheck(dd) == 0);
    assert(Cudd_CheckKeys(dd) == 0);

    retval = Cudd_CheckZeroRef(dd);
    ok = retval != 0;  /* ok == 0 means O.K. */
    if (retval != 0) {
	(void) fprintf(stderr,
	    "%d non-zero DD reference counts after dereferencing\n", retval);
    }

    if (pr >= 0) {
	(void) Cudd_PrintInfo(dd,stdout);
    }

    Cudd_Quit(dd);

#ifdef MNEMOSYNE
    mnem_writestats();
#endif

    if (pr>0) (void) printf("total time = %s\n",
		util_print_time(util_cpu_time() - startTime));

    if (pr >= 0) util_print_cpu_stats(stdout);
    exit(ok);
    /* NOTREACHED */

} /* end of main */
Example #8
0
static int compute_prob(void)
/* this is the function that implements the compute_prob predicate used in pp.pl
*/
{
    YAP_Term out,arg1,arg2,arg3,arg4;
    variables  vars;
    expr expression;
    DdNode * function;
    DdManager * mgr;
    int nBVar,i,intBits,create_dot;
    FILE * file;
    DdNode * array[1];
    double prob;
    char * onames[1];
    char inames[1000][20];
    char * names[1000];
    tablerow * nodes;

    arg1=YAP_ARG1;
    arg2=YAP_ARG2;
    arg3=YAP_ARG3;
    arg4=YAP_ARG4;

    mgr=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0);
    create_dot=YAP_IntOfTerm(arg4);
    vars=createVars(arg1,mgr,create_dot,inames);

    //Cudd_PrintInfo(mgr,stderr);

    /* automatic variable reordering, default method CUDD_REORDER_SIFT used */
    //printf("status %d\n",Cudd_ReorderingStatus(mgr,&order));
    //printf("order %d\n",order);

    Cudd_AutodynEnable(mgr,CUDD_REORDER_SAME);
    /*   Cudd_AutodynEnable(mgr, CUDD_REORDER_RANDOM_PIVOT);
      printf("status %d\n",Cudd_ReorderingStatus(mgr,&order));
            printf("order %d\n",order);
      printf("%d",CUDD_REORDER_RANDOM_PIVOT);
    */


    expression=createExpression(arg2);

    function=retFunction(mgr,expression,vars);

    /* the BDD build by retFunction is converted to an ADD (algebraic decision diagram)
    because it is easier to interpret and to print */
    //add=Cudd_BddToAdd(mgr,function);
    //Cudd_PrintInfo(mgr,stderr);

    if (create_dot)
        /* if specified by the user, a dot file for the BDD is written to cpl.dot */
    {
        nBVar=vars.nBVar;
        for(i=0; i<nBVar; i++)
            names[i]=inames[i];
        array[0]=function;
        onames[0]="Out";
        file = open_file("cpl.dot", "w");
        Cudd_DumpDot(mgr,1,array,names,onames,file);
        fclose(file);
    }
    nodes=init_table(vars.nBVar);
    intBits=sizeof(unsigned int)*8;
    prob=Prob(function,vars,nodes);
    out=YAP_MkFloatTerm(prob);
    destroy_table(nodes,vars.nBVar);

    Cudd_Quit(mgr);
    for(i=0; i<vars.nVar; i++)
    {
        free(vars.varar[i].probabilities);
        free(vars.varar[i].booleanVars);
    }
    free(vars.varar);
    free(vars.bVar2mVar);
    for(i=0; i<expression.nTerms; i++)
    {
        free(expression.terms[i].factors);
    }
    free(expression.terms);
    return(YAP_Unify(out,arg3));
}
Example #9
0
void BddBuilder::dotDumpC(DdNode ** ddNodes,int& number_of_diff_output,char** diff_output){
	char filename[128];
	DdNode * dumpdd[1];
	char * dumpname[1];
	int res;
	int * index_of_diff_output;
	int j=0;
	index_of_diff_output = new int[__outputWireCnt];
	for(int i=0 ; i<__outputWireCnt ; ++i){
		if(ddNodes[i] != NULL){
			number_of_diff_output++;
			index_of_diff_output[j++] = i;
			printf("%s\n",__ppOutputNodesNames[i]);
			sprintf(filename, "./dotdump/dumpC_%s.dot", __ppOutputNodesNames[i]);
			FILE * fp = fopen(filename, "w");
			dumpdd[0] = ddNodes[i];
			dumpname[0] = __ppOutputNodesNames[i];
			res = Cudd_DumpDot(__pddManager, 1, dumpdd, __ppInputNodesNames, dumpname, fp);
			fclose(fp);

			sprintf(filename, "./factored/dumpC_factored_%s", __ppOutputNodesNames[i]);
			fp = fopen(filename, "w");
			Cudd_DumpFactoredForm(__pddManager, 1, dumpdd, __ppInputNodesNames, dumpname, fp);
			fclose(fp);
			printf("Factored form(Boolean equation) written : %s\n", filename);

			//Cudd_PrintDebug(__pddManager, dumpdd[0], Cudd_ReadSize(__pddManager), 4);
			sprintf(filename, "./sop/%s.sop", __ppOutputNodesNames[i]);
			FILE * fp_sop = fopen(filename, "w");
			FILE * tmp;
			FILE* debug = fopen("debug.txt","w");

			printf("%s's SOP:\n", __ppOutputNodesNames[i]);
			Cudd_bddPrintCover(__pddManager, dumpdd[0], dumpdd[0]);
			//Cudd_PrintMinterm(__pddManager,dumpdd[0]);
			tmp = Cudd_ReadStdout(__pddManager);
			Cudd_SetStdout(__pddManager, fp_sop);
			Cudd_bddPrintCover(__pddManager, dumpdd[0], dumpdd[0]);
			//Cudd_PrintMinterm(__pddManager,dumpdd[0]);
			//fprintf(fp_sop,"\n");
			Cudd_SetStdout(__pddManager, debug);
			Cudd_PrintDebug(__pddManager,dumpdd[0],Cudd_ReadSize(__pddManager),4);
			Cudd_SetStdout(__pddManager, tmp);
			fseek(fp_sop,-2,SEEK_END);
			//fputc(0,fp_so;
			fclose(fp_sop);
			printf("SOP file written : %s\n\n", filename);
			/*printf("Quine-Mccluskey for %s:\n", __ppOutputNodesNames[i]);
			QuineMccluskey quine(filename);
			printf("Quine-Mccluskey for %s Done.\n", __ppOutputNodesNames[i]);
			*/
			//Cudd_PrintMinterm(__pddManager, dumpdd[0]);
			if(res == 1)
				printf("DOT dump for C's %s completed.\n", __ppOutputNodesNames[i]);
			else
				printf("DOT dump for C's %s failed.\n", __ppOutputNodesNames[i]);
		}
		else
			printf("DOT dump for C's %s not executed: A = B\n", __ppOutputNodesNames[i]);
	}
	for(int i=0;i<number_of_diff_output;i++){
		strcpy(diff_output[i],__ppOutputNodesNames[index_of_diff_output[i]]);
	}
	delete index_of_diff_output;
}
Example #10
0
static int compute_prob(void)
/* this is the function that implements the compute_prob predicate used in pp.pl
*/
{
	YAP_Term out,arg1,arg2,arg3,arg4;
	array_t * variables,* expression, * bVar2mVar;
	DdNode * function, * add;
	DdManager * mgr;
	int nBVar,i,j,intBits,create_dot;
        FILE * file;
        DdNode * array[1];
        char * onames[1];
        char inames[1000][20];
	char * names[1000];
	GHashTable  * nodes; /* hash table that associates nodes with their probability if already 
				computed, it is defined in glib */
	Cudd_ReorderingType order;
	arg1=YAP_ARG1;
	arg2=YAP_ARG2;
	arg3=YAP_ARG3;
	arg4=YAP_ARG4;

  	mgr=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0);
	variables=array_alloc(variable,0);
	bVar2mVar=array_alloc(int,0);
	create_dot=YAP_IntOfTerm(arg4);
	createVars(variables,arg1,mgr,bVar2mVar,create_dot,inames);
        //Cudd_PrintInfo(mgr,stderr);

	/* automatic variable reordering, default method CUDD_REORDER_SIFT used */
	//printf("status %d\n",Cudd_ReorderingStatus(mgr,&order));
	//printf("order %d\n",order);

	Cudd_AutodynEnable(mgr,CUDD_REORDER_SAME); 
/*	 Cudd_AutodynEnable(mgr, CUDD_REORDER_RANDOM_PIVOT);
	printf("status %d\n",Cudd_ReorderingStatus(mgr,&order));
        printf("order %d\n",order);
	printf("%d",CUDD_REORDER_RANDOM_PIVOT);
*/


	expression=array_alloc(array_t *,0);
	createExpression(expression,arg2);	

	function=retFunction(mgr,expression,variables);
	/* the BDD build by retFunction is converted to an ADD (algebraic decision diagram)
	because it is easier to interpret and to print */
	add=Cudd_BddToAdd(mgr,function);
	//Cudd_PrintInfo(mgr,stderr);

	if (create_dot)
	/* if specified by the user, a dot file for the BDD is written to cpl.dot */
	{	
		nBVar=array_n(bVar2mVar);
		for(i=0;i<nBVar;i++)
		   names[i]=inames[i];
	  	array[0]=add;
		onames[0]="Out";
		file = open_file("cpl.dot", "w");
		Cudd_DumpDot(mgr,1,array,names,onames,file);
  		fclose(file);
	}
	
	nodes=g_hash_table_new(my_hash,my_equal);
	intBits=sizeof(unsigned int)*8;
	/* dividend is a global variable used by my_hash 
	   it is equal to an unsigned int with binary representation 11..1 */ 
	dividend=1;
	for(j=1;j<intBits;j++)
	{
		dividend=(dividend<<1)+1;
	}
	out=YAP_MkFloatTerm(Prob(add,variables,bVar2mVar,nodes));
	g_hash_table_foreach (nodes,dealloc,NULL);
	g_hash_table_destroy(nodes);
	Cudd_Quit(mgr);
	array_free(variables);
 	array_free(bVar2mVar);
	array_free(expression);
    	return(YAP_Unify(out,arg3));
}