testcudd.c 31.3 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**CFile***********************************************************************

  FileName    [testcudd.c]

  PackageName [cudd]

  Synopsis    [Sanity check tests for some CUDD functions.]

  Description [testcudd reads a matrix with real coefficients and
  transforms it into an ADD. It then performs various operations on
  the ADD and on the BDD corresponding to the ADD pattern. Finally,
  testcudd tests functions relate to Walsh matrices and matrix
  multiplication.]

  SeeAlso     []

  Author      [Fabio Somenzi]

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
50 51 52

******************************************************************************/

53
#include "util.h"
Alan Mishchenko committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include "cuddInt.h"


/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

#define TESTCUDD_VERSION    "TestCudd Version #1.0, Release date 3/17/01"

/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
68
static char rcsid[] DD_UNUSED = "$Id: testcudd.c,v 1.20 2009/03/08 02:49:02 fabio Exp $";
Alan Mishchenko committed
69 70
#endif

71
static const char *onames[] = { "C", "M" }; /* names of functions to be dumped */
Alan Mishchenko committed
72 73 74 75 76 77 78

/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

79 80 81 82 83 84
static void usage (char * prog);
static FILE *open_file (char *filename, const char *mode);
static int testIterators (DdManager *dd, DdNode *M, DdNode *C, int pr);
static int testXor (DdManager *dd, DdNode *f, int pr, int nvars);
static int testHamming (DdManager *dd, DdNode *f, int pr);
static int testWalsh (DdManager *dd, int N, int cmu, int approach, int pr);
Alan Mishchenko committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

/**AutomaticEnd***************************************************************/


/**Function********************************************************************

  Synopsis    [Main function for testcudd.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
main(int argc, char **argv)
{
    FILE *fp;           /* pointer to input file */
104
    char *file = (char *) "";    /* input file name */
Alan Mishchenko committed
105 106 107 108
    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 */
109
    DdNode *one;    /* fast access to constant function */
Alan Mishchenko committed
110 111 112
    DdNode *M;
    DdNode **x;        /* pointers to variables */
    DdNode **y;        /* pointers to variables */
113 114
    DdNode **xn;    /* complements of row variables */
    DdNode **yn_;    /* complements of column variables */
Alan Mishchenko committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    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. */
176
    while ((c = util_getopt(argc, argv, (char *) "CDHMPS:a:bcd:g:hkmn:p:v:x:X:"))
Alan Mishchenko committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
       != 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) {
243
    file = (char *) "-";
Alan Mishchenko committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    } 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);
    one = DD_ONE(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_ONE(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. */
361
    retval = testHamming(dd,C,pr);
Alan Mishchenko committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    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);
    }
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

    /* Test inequality generator. */
    {
        int Nmin = ddMin(nx,ny);
        int q;
        DdGen *gen;
        int *cube;
        DdNode *f = Cudd_Inequality(dd,Nmin,2,xvars,yvars);
        if (f == NULL) exit(2);
        Cudd_Ref(f);
        if (pr>0) {
        (void) printf(":4: ineq");
        Cudd_PrintDebug(dd,f,nx+ny,pr);
        if (pr>1) {
            Cudd_ForeachPrime(dd,Cudd_Not(f),Cudd_Not(f),gen,cube) {
            for (q = 0; q < dd->size; q++) {
                switch (cube[q]) {
                case 0:
                (void) printf("1");
                break;
                case 1:
                (void) printf("0");
                break;
                case 2:
                (void) printf("-");
                break;
                default:
                (void) printf("?");
                }
            }
            (void) printf(" 1\n");
            }
            (void) printf("\n");
        }
        }
        Cudd_IterDerefBdd(dd, f);
    }
Alan Mishchenko committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    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),one,C) != one) 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. */
482
    if (approach != CUDD_REORDER_NONE) {
Alan Mishchenko committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
#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 */
565 566
        retval = Cudd_DumpBlif(dd,1,dfunc,NULL,(char **)onames,
                       NULL,dfp,0);
Alan Mishchenko committed
567
        } else {
568
        retval = Cudd_DumpDot(dd,2,dfunc,NULL,(char **)onames,dfp);
Alan Mishchenko committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
        }
        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);
588 589 590
        (void) printf("Number of slots     = %6u\n",dd->slots);
        (void) printf("Number of keys      = %6u\t",dd->keys);
        (void) printf("Number of min dead  = %6u\n",dd->minDead);
Alan Mishchenko committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    }

    } 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 */


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Prints usage info for testcudd.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static void
usage(char *prog)
{
    (void) fprintf(stderr, "usage: %s [options] [file]\n", prog);
    (void) fprintf(stderr, "   -C\t\tuse CMU multiplication algorithm\n");
    (void) fprintf(stderr, "   -D\t\tenable automatic dynamic reordering\n");
    (void) fprintf(stderr, "   -H\t\tread matrix in Harwell format\n");
    (void) fprintf(stderr, "   -M\t\tturns off memory allocation recording\n");
    (void) fprintf(stderr, "   -P\t\tprint BDD heap profile\n");
    (void) fprintf(stderr, "   -S n\t\tnumber of slots for each subtable\n");
    (void) fprintf(stderr, "   -X n\t\ttarget maximum memory in bytes\n");
    (void) fprintf(stderr, "   -a n\t\tchoose reordering approach (0-13)\n");
    (void) fprintf(stderr, "   \t\t\t0: same as autoMethod\n");
    (void) fprintf(stderr, "   \t\t\t1: no reordering (default)\n");
    (void) fprintf(stderr, "   \t\t\t2: random\n");
    (void) fprintf(stderr, "   \t\t\t3: pivot\n");
    (void) fprintf(stderr, "   \t\t\t4: sifting\n");
    (void) fprintf(stderr, "   \t\t\t5: sifting to convergence\n");
    (void) fprintf(stderr, "   \t\t\t6: symmetric sifting\n");
    (void) fprintf(stderr, "   \t\t\t7: symmetric sifting to convergence\n");
    (void) fprintf(stderr, "   \t\t\t8-10: window of size 2-4\n");
    (void) fprintf(stderr, "   \t\t\t11-13: window of size 2-4 to conv.\n");
    (void) fprintf(stderr, "   \t\t\t14: group sifting\n");
    (void) fprintf(stderr, "   \t\t\t15: group sifting to convergence\n");
    (void) fprintf(stderr, "   \t\t\t16: simulated annealing\n");
    (void) fprintf(stderr, "   \t\t\t17: genetic algorithm\n");
    (void) fprintf(stderr, "   -b\t\tuse blif as format for dumps\n");
    (void) fprintf(stderr, "   -c\t\tclear the cache after each matrix\n");
    (void) fprintf(stderr, "   -d file\tdump DDs to file\n");
    (void) fprintf(stderr, "   -g\t\tselect aggregation criterion (0,5,7)\n");
    (void) fprintf(stderr, "   -h\t\tprints this message\n");
    (void) fprintf(stderr, "   -k\t\tprint the variable permutation\n");
    (void) fprintf(stderr, "   -m\t\tread multiple matrices (only with -H)\n");
    (void) fprintf(stderr, "   -n n\t\tnumber of variables\n");
    (void) fprintf(stderr, "   -p n\t\tcontrol verbosity\n");
    (void) fprintf(stderr, "   -v n\t\tinitial variables in the unique table\n");
    (void) fprintf(stderr, "   -x n\t\tinitial size of the cache\n");
    exit(2);
} /* end of usage */


/**Function********************************************************************

  Synopsis    [Opens a file.]

  Description [Opens a file, or fails with an error message and exits.
  Allows '-' as a synonym for standard input.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static FILE *
707
open_file(char *filename, const char *mode)
Alan Mishchenko committed
708 709 710 711
{
    FILE *fp;

    if (strcmp(filename, "-") == 0) {
712
    return mode[0] == 'r' ? stdin : stdout;
Alan Mishchenko committed
713
    } else if ((fp = fopen(filename, mode)) == NULL) {
714 715
    perror(filename);
    exit(1);
Alan Mishchenko committed
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
    }
    return fp;

} /* end of open_file */


/**Function********************************************************************

  Synopsis    [Tests Walsh matrix multiplication.]

  Description [Tests Walsh matrix multiplication.  Return 1 if successful;
  0 otherwise.]

  SideEffects [May create new variables in the manager.]

  SeeAlso     []

******************************************************************************/
static int
testWalsh(
  DdManager *dd /* manager */,
  int N /* number of variables */,
  int cmu /* use CMU approach to matrix multiplication */,
  int approach /* reordering approach */,
  int pr /* verbosity level */)
{
    DdNode *walsh1, *walsh2, *wtw;
    DdNode **x, **v, **z;
    int i, retval;
    DdNode *one = DD_ONE(dd);
    DdNode *zero = DD_ZERO(dd);

    if (N > 3) {
    x = ALLOC(DdNode *,N);
    v = ALLOC(DdNode *,N);
    z = ALLOC(DdNode *,N);

    for (i = N-1; i >= 0; i--) {
        Cudd_Ref(x[i]=cuddUniqueInter(dd,3*i,one,zero));
        Cudd_Ref(v[i]=cuddUniqueInter(dd,3*i+1,one,zero));
        Cudd_Ref(z[i]=cuddUniqueInter(dd,3*i+2,one,zero));
    }
    Cudd_Ref(walsh1 = Cudd_addWalsh(dd,v,z,N));
    if (pr>0) {(void) printf("walsh1"); Cudd_PrintDebug(dd,walsh1,2*N,pr);}
    Cudd_Ref(walsh2 = Cudd_addWalsh(dd,x,v,N));
    if (cmu) {
        Cudd_Ref(wtw = Cudd_addTimesPlus(dd,walsh2,walsh1,v,N));
    } else {
        Cudd_Ref(wtw = Cudd_addMatrixMultiply(dd,walsh2,walsh1,v,N));
    }
    if (pr>0) {(void) printf("wtw"); Cudd_PrintDebug(dd,wtw,2*N,pr);}

    if (approach != CUDD_REORDER_NONE) {
#ifdef DD_DEBUG
        retval = Cudd_DebugCheck(dd);
        if (retval != 0) {
        (void) fprintf(stderr,"Error reported by Cudd_DebugCheck\n");
        return(0);
        }
#endif
        retval = Cudd_ReduceHeap(dd,(Cudd_ReorderingType)approach,5);
        if (retval == 0) {
        (void) fprintf(stderr,"Error reported by Cudd_ReduceHeap\n");
        return(0);
        }
#ifdef DD_DEBUG
        retval = Cudd_DebugCheck(dd);
        if (retval != 0) {
        (void) fprintf(stderr,"Error reported by Cudd_DebugCheck\n");
        return(0);
        }
#endif
        if (approach == CUDD_REORDER_SYMM_SIFT ||
        approach == CUDD_REORDER_SYMM_SIFT_CONV) {
        Cudd_SymmProfile(dd,0,dd->size-1);
        }
    }
    /* Clean up. */
    Cudd_RecursiveDeref(dd, wtw);
    Cudd_RecursiveDeref(dd, walsh1);
    Cudd_RecursiveDeref(dd, walsh2);
    for (i=0; i < N; i++) {
        Cudd_RecursiveDeref(dd, x[i]);
        Cudd_RecursiveDeref(dd, v[i]);
        Cudd_RecursiveDeref(dd, z[i]);
    }
    FREE(x);
    FREE(v);
    FREE(z);
    }
    return(1);

} /* end of testWalsh */

/**Function********************************************************************

  Synopsis    [Tests iterators.]

  Description [Tests iterators on cubes and nodes.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
testIterators(
  DdManager *dd,
  DdNode *M,
  DdNode *C,
  int pr)
{
    int *cube;
    CUDD_VALUE_TYPE value;
    DdGen *gen;
    int q;

    /* Test iterator for cubes. */
    if (pr>1) {
    (void) printf("Testing iterator on cubes:\n");
    Cudd_ForeachCube(dd,M,gen,cube,value) {
        for (q = 0; q < dd->size; q++) {
        switch (cube[q]) {
        case 0:
            (void) printf("0");
            break;
        case 1:
            (void) printf("1");
            break;
        case 2:
            (void) printf("-");
            break;
        default:
            (void) printf("?");
        }
        }
        (void) printf(" %g\n",value);
    }
    (void) printf("\n");
    }

    if (pr>1) {
    (void) printf("Testing prime expansion of cubes:\n");
    if (!Cudd_bddPrintCover(dd,C,C)) return(0);
    }

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
    if (pr>1) {
    (void) printf("Testing iterator on primes (CNF):\n");
    Cudd_ForeachPrime(dd,Cudd_Not(C),Cudd_Not(C),gen,cube) {
        for (q = 0; q < dd->size; q++) {
        switch (cube[q]) {
        case 0:
            (void) printf("1");
            break;
        case 1:
            (void) printf("0");
            break;
        case 2:
            (void) printf("-");
            break;
        default:
            (void) printf("?");
        }
        }
        (void) printf(" 1\n");
    }
    (void) printf("\n");
    }

Alan Mishchenko committed
885 886 887 888 889 890 891 892
    /* Test iterator on nodes. */
    if (pr>2) {
    DdNode *node;
    (void) printf("Testing iterator on nodes:\n");
    Cudd_ForeachNode(dd,M,gen,node) {
        if (Cudd_IsConstant(node)) {
#if SIZEOF_VOID_P == 8
        (void) printf("ID = 0x%lx\tvalue = %-9g\n",
893 894
                  (ptruint) node /
                  (ptruint) sizeof(DdNode),
Alan Mishchenko committed
895 896 897
                  Cudd_V(node));
#else
        (void) printf("ID = 0x%x\tvalue = %-9g\n",
898 899
                  (ptruint) node /
                  (ptruint) sizeof(DdNode),
Alan Mishchenko committed
900 901 902 903
                  Cudd_V(node));
#endif
        } else {
#if SIZEOF_VOID_P == 8
904 905 906
        (void) printf("ID = 0x%lx\tindex = %u\tr = %u\n",
                  (ptruint) node /
                  (ptruint) sizeof(DdNode),
Alan Mishchenko committed
907 908
                  node->index, node->ref);
#else
909 910 911
        (void) printf("ID = 0x%x\tindex = %u\tr = %u\n",
                  (ptruint) node /
                  (ptruint) sizeof(DdNode),
Alan Mishchenko committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
                  node->index, node->ref);
#endif
        }
    }
    (void) printf("\n");
    }
    return(1);

} /* end of testIterators */


/**Function********************************************************************

  Synopsis    [Tests the functions related to the exclusive OR.]

  Description [Tests the functions related to the exclusive OR. It
  builds the boolean difference of the given function in three
  different ways and checks that the results is the same. Returns 1 if
  successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
testXor(DdManager *dd, DdNode *f, int pr, int nvars)
{
    DdNode *f1, *f0, *res1, *res2;
    int x;

    /* Extract cofactors w.r.t. mid variable. */
    x = nvars / 2;
    f1 = Cudd_Cofactor(dd,f,dd->vars[x]);
    if (f1 == NULL) return(0);
    Cudd_Ref(f1);

    f0 = Cudd_Cofactor(dd,f,Cudd_Not(dd->vars[x]));
    if (f0 == NULL) {
    Cudd_RecursiveDeref(dd,f1);
    return(0);
    }
    Cudd_Ref(f0);

    /* Compute XOR of cofactors with ITE. */
    res1 = Cudd_bddIte(dd,f1,Cudd_Not(f0),f0);
    if (res1 == NULL) return(0);
    Cudd_Ref(res1);

    if (pr>0) {(void) printf("xor1"); Cudd_PrintDebug(dd,res1,nvars,pr);}

    /* Compute XOR of cofactors with XOR. */
    res2 = Cudd_bddXor(dd,f1,f0);
    if (res2 == NULL) {
    Cudd_RecursiveDeref(dd,res1);
    return(0);
    }
    Cudd_Ref(res2);

    if (res1 != res2) {
    if (pr>0) {(void) printf("xor2"); Cudd_PrintDebug(dd,res2,nvars,pr);}
    Cudd_RecursiveDeref(dd,res1);
    Cudd_RecursiveDeref(dd,res2);
    return(0);
    }
    Cudd_RecursiveDeref(dd,res1);
    Cudd_RecursiveDeref(dd,f1);
    Cudd_RecursiveDeref(dd,f0);

    /* Compute boolean difference directly. */
    res1 = Cudd_bddBooleanDiff(dd,f,x);
    if (res1 == NULL) {
    Cudd_RecursiveDeref(dd,res2);
    return(0);
    }
    Cudd_Ref(res1);

    if (res1 != res2) {
    if (pr>0) {(void) printf("xor3"); Cudd_PrintDebug(dd,res1,nvars,pr);}
    Cudd_RecursiveDeref(dd,res1);
    Cudd_RecursiveDeref(dd,res2);
    return(0);
    }
    Cudd_RecursiveDeref(dd,res1);
    Cudd_RecursiveDeref(dd,res2);
    return(1);

} /* end of testXor */


/**Function********************************************************************

  Synopsis    [Tests the Hamming distance functions.]

  Description [Tests the Hammming distance functions. Returns
  1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
testHamming(
  DdManager *dd,
  DdNode *f,
1018
  int pr)
Alan Mishchenko committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
{
    DdNode **vars, *minBdd, *zero, *scan;
    int i;
    int d;
    int *minterm;
    int size = Cudd_ReadSize(dd);

    vars = ALLOC(DdNode *, size);
    if (vars == NULL) return(0);
    for (i = 0; i < size; i++) {
    vars[i] = Cudd_bddIthVar(dd,i);
    }

    minBdd = Cudd_bddPickOneMinterm(dd,Cudd_Not(f),vars,size);
    Cudd_Ref(minBdd);
    if (pr > 0) {
    (void) printf("Chosen minterm for Hamming distance test: ");
    Cudd_PrintDebug(dd,minBdd,size,pr);
    }

    minterm = ALLOC(int,size);
    if (minterm == NULL) {
    FREE(vars);
    Cudd_RecursiveDeref(dd,minBdd);
    return(0);
    }
    scan = minBdd;
    zero = Cudd_Not(DD_ONE(dd));
    while (!Cudd_IsConstant(scan)) {
    DdNode *R = Cudd_Regular(scan);
    DdNode *T = Cudd_T(R);
    DdNode *E = Cudd_E(R);
    if (R != scan) {
        T = Cudd_Not(T);
        E = Cudd_Not(E);
    }
    if (T == zero) {
        minterm[R->index] = 0;
        scan = E;
    } else {
        minterm[R->index] = 1;
        scan = T;
    }
    }
    Cudd_RecursiveDeref(dd,minBdd);

    d = Cudd_MinHammingDist(dd,f,minterm,size);

    (void) printf("Minimum Hamming distance = %d\n", d);

    FREE(vars);
    FREE(minterm);
    return(1);

} /* end of testHamming */