cuddHarwell.c 18.1 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile***********************************************************************

  FileName    [cuddHarwell.c]

  PackageName [cudd]

  Synopsis    [Function to read a matrix in Harwell format.]

  Description [External procedures included in this module:
10 11 12 13
                <ul>
                <li> Cudd_addHarwell()
                </ul>
        ]
Alan Mishchenko committed
14 15 16

  Author      [Fabio Somenzi]

17 18 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
  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
48 49 50

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

51
#include "misc/util/util_hack.h"
Alan Mishchenko committed
52 53
#include "cuddInt.h"

54 55 56
ABC_NAMESPACE_IMPL_START


57

Alan Mishchenko committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/


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

#ifndef lint
78
static char rcsid[] DD_UNUSED = "$Id: cuddHarwell.c,v 1.9 2004/08/13 18:04:49 fabio Exp $";
Alan Mishchenko committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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
#endif

/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/


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

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


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


/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/

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

  Synopsis    [Reads in a matrix in the format of the Harwell-Boeing
  benchmark suite.]

  Description [Reads in a matrix in the format of the Harwell-Boeing
  benchmark suite. The variables are ordered as follows:
  <blockquote>
  x\[0\] y\[0\] x\[1\] y\[1\] ...
  </blockquote>
  0 is the most significant bit.  On input, nx and ny hold the numbers
  of row and column variables already in existence. On output, they
  hold the numbers of row and column variables actually used by the
  matrix.  m and n are set to the numbers of rows and columns of the
  matrix.  Their values on input are immaterial.  Returns 1 on
  success; 0 otherwise. The ADD for the sparse matrix is returned in
  E, and its reference count is > 0.]

  SideEffects [None]

  SeeAlso     [Cudd_addRead Cudd_bddRead]

******************************************************************************/
int
Cudd_addHarwell(
  FILE * fp /* pointer to the input file */,
  DdManager * dd /* DD manager */,
  DdNode ** E /* characteristic function of the graph */,
  DdNode *** x /* array of row variables */,
  DdNode *** y /* array of column variables */,
  DdNode *** xn /* array of complemented row variables */,
  DdNode *** yn_ /* array of complemented column variables */,
  int * nx /* number or row variables */,
  int * ny /* number or column variables */,
  int * m /* number of rows */,
  int * n /* number of columns */,
  int  bx /* first index of row variables */,
  int  sx /* step of row variables */,
  int  by /* first index of column variables */,
  int  sy /* step of column variables */,
  int  pr /* verbosity level */)
{
    DdNode *one, *zero;
    DdNode *w;
    DdNode *cubex, *cubey, *minterm1;
    int u, v, err, i, j, nv;
    double val;
147
    DdNode **lx = NULL, **ly = NULL, **lxn = NULL, **lyn = NULL;    /* local copies of x, y, xn, yn_ */
148
    int lnx, lny;                       /* local copies of nx and ny */
Alan Mishchenko committed
149 150 151
    char title[73], key[9], mxtype[4], rhstyp[4];
    int totcrd, ptrcrd, indcrd, valcrd, rhscrd,
        nrow, ncol, nnzero, neltvl,
152
        nrhs, nrhsix;
Alan Mishchenko committed
153 154 155
    int *colptr, *rowind;
#if 0
    int nguess, nexact;
156
    int *rhsptr, *rhsind;
Alan Mishchenko committed
157 158 159 160 161 162 163 164 165 166
#endif

    if (*nx < 0 || *ny < 0) return(0);

    one = DD_ONE(dd);
    zero = DD_ZERO(dd);

    /* Read the header */
    err = fscanf(fp, "%72c %8c", title, key);
    if (err == EOF) {
167
        return(0);
Alan Mishchenko committed
168 169 170 171 172 173 174 175 176
    } else if (err != 2) {
        return(0);
    }
    title[72] = (char) 0;
    key[8] = (char) 0;

    err = fscanf(fp, "%d %d %d %d %d", &totcrd, &ptrcrd, &indcrd,
    &valcrd, &rhscrd);
    if (err == EOF) {
177
        return(0);
Alan Mishchenko committed
178 179 180 181 182 183 184
    } else if (err != 5) {
        return(0);
    }

    err = fscanf(fp, "%3s %d %d %d %d", mxtype, &nrow, &ncol,
    &nnzero, &neltvl);
    if (err == EOF) {
185
        return(0);
Alan Mishchenko committed
186 187 188 189 190 191
    } else if (err != 5) {
        return(0);
    }

    /* Skip FORTRAN formats */
    if (rhscrd == 0) {
192
        err = fscanf(fp, "%*s %*s %*s \n");
Alan Mishchenko committed
193
    } else {
194
        err = fscanf(fp, "%*s %*s %*s %*s \n");
Alan Mishchenko committed
195 196
    }
    if (err == EOF) {
197
        return(0);
Alan Mishchenko committed
198 199 200 201 202 203
    } else if (err != 0) {
        return(0);
    }

    /* Print out some stuff if requested to be verbose */
    if (pr>0) {
204 205 206
        (void) fprintf(dd->out,"%s: type %s, %d rows, %d columns, %d entries\n", key,
        mxtype, nrow, ncol, nnzero);
        if (pr>1) (void) fprintf(dd->out,"%s\n", title);
Alan Mishchenko committed
207 208 209 210
    }

    /* Check matrix type */
    if (mxtype[0] != 'R' || mxtype[1] != 'U' || mxtype[2] != 'A') {
211 212 213
        (void) fprintf(dd->err,"%s: Illegal matrix type: %s\n",
                       key, mxtype);
        return(0);
Alan Mishchenko committed
214 215 216 217 218
    }
    if (neltvl != 0) return(0);

    /* Read optional 5-th line */
    if (rhscrd != 0) {
219 220 221 222 223 224 225 226 227 228 229 230 231
        err = fscanf(fp, "%3c %d %d", rhstyp, &nrhs, &nrhsix);
        if (err == EOF) {
            return(0);
        } else if (err != 3) {
            return(0);
        }
        rhstyp[3] = (char) 0;
        if (rhstyp[0] != 'F') {
            (void) fprintf(dd->err,
            "%s: Sparse right-hand side not yet supported\n", key);
            return(0);
        }
        if (pr>0) (void) fprintf(dd->out,"%d right-hand side(s)\n", nrhs);
Alan Mishchenko committed
232
    } else {
233
        nrhs = 0;
Alan Mishchenko committed
234 235 236 237 238 239 240
    }

    /* Compute the number of variables */

    /* row and column numbers start from 0 */
    u = nrow - 1;
    for (i=0; u > 0; i++) {
241
        u >>= 1;
Alan Mishchenko committed
242 243 244
    }
    lnx = i;
    if (nrhs == 0) {
245
        v = ncol - 1;
Alan Mishchenko committed
246
    } else {
247
        v = 2* (ddMax(ncol, nrhs) - 1);
Alan Mishchenko committed
248 249
    }
    for (i=0; v > 0; i++) {
250
        v >>= 1;
Alan Mishchenko committed
251 252 253 254 255
    }
    lny = i;

    /* Allocate or reallocate arrays for variables as needed */
    if (*nx == 0) {
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        if (lnx > 0) {
            *x = lx = ABC_ALLOC(DdNode *,lnx);
            if (lx == NULL) {
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            *xn = lxn =  ABC_ALLOC(DdNode *,lnx);
            if (lxn == NULL) {
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
        } else {
            *x = *xn = NULL;
        }
    } else if (lnx > *nx) {
        *x = lx = ABC_REALLOC(DdNode *, *x, lnx);
Alan Mishchenko committed
272
        if (lx == NULL) {
273 274
            dd->errorCode = CUDD_MEMORY_OUT;
            return(0);
Alan Mishchenko committed
275
        }
276
        *xn = lxn =  ABC_REALLOC(DdNode *, *xn, lnx);
Alan Mishchenko committed
277
        if (lxn == NULL) {
278 279
            dd->errorCode = CUDD_MEMORY_OUT;
            return(0);
Alan Mishchenko committed
280 281
        }
    } else {
282 283
        lx = *x;
        lxn = *xn;
Alan Mishchenko committed
284 285
    }
    if (*ny == 0) {
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
        if (lny >0) {
            *y = ly = ABC_ALLOC(DdNode *,lny);
            if (ly == NULL) {
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            *yn_ = lyn = ABC_ALLOC(DdNode *,lny);
            if (lyn == NULL) {
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
        } else {
            *y = *yn_ = NULL;
        }
    } else if (lny > *ny) {
        *y = ly = ABC_REALLOC(DdNode *, *y, lny);
Alan Mishchenko committed
302
        if (ly == NULL) {
303 304
            dd->errorCode = CUDD_MEMORY_OUT;
            return(0);
Alan Mishchenko committed
305
        }
306
        *yn_ = lyn = ABC_REALLOC(DdNode *, *yn_, lny);
Alan Mishchenko committed
307
        if (lyn == NULL) {
308 309
            dd->errorCode = CUDD_MEMORY_OUT;
            return(0);
Alan Mishchenko committed
310 311
        }
    } else {
312 313
        ly = *y;
        lyn = *yn_;
Alan Mishchenko committed
314 315 316 317
    }

    /* Create new variables as needed */
    for (i= *nx,nv=bx+(*nx)*sx; i < lnx; i++,nv+=sx) {
318 319 320 321 322
        do {
            dd->reordered = 0;
            lx[i] = cuddUniqueInter(dd, nv, one, zero);
        } while (dd->reordered == 1);
        if (lx[i] == NULL) return(0);
Alan Mishchenko committed
323
        cuddRef(lx[i]);
324 325 326 327 328
        do {
            dd->reordered = 0;
            lxn[i] = cuddUniqueInter(dd, nv, zero, one);
        } while (dd->reordered == 1);
        if (lxn[i] == NULL) return(0);
Alan Mishchenko committed
329 330 331
        cuddRef(lxn[i]);
    }
    for (i= *ny,nv=by+(*ny)*sy; i < lny; i++,nv+=sy) {
332 333 334 335 336 337 338 339 340 341 342 343
        do {
            dd->reordered = 0;
            ly[i] = cuddUniqueInter(dd, nv, one, zero);
        } while (dd->reordered == 1);
        if (ly[i] == NULL) return(0);
        cuddRef(ly[i]);
        do {
            dd->reordered = 0;
            lyn[i] = cuddUniqueInter(dd, nv, zero, one);
        } while (dd->reordered == 1);
        if (lyn[i] == NULL) return(0);
        cuddRef(lyn[i]);
Alan Mishchenko committed
344 345 346 347 348 349 350
    }

    /* Update matrix parameters */
    *nx = lnx;
    *ny = lny;
    *m = nrow;
    if (nrhs == 0) {
351
        *n = ncol;
Alan Mishchenko committed
352
    } else {
353
        *n = (1 << (lny - 1)) + nrhs;
Alan Mishchenko committed
354 355 356
    }
    
    /* Read structure data */
Alan Mishchenko committed
357
    colptr = ABC_ALLOC(int, ncol+1);
Alan Mishchenko committed
358
    if (colptr == NULL) {
359 360
        dd->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
361
    }
Alan Mishchenko committed
362
    rowind = ABC_ALLOC(int, nnzero);
Alan Mishchenko committed
363
    if (rowind == NULL) {
364 365
        dd->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
366 367 368
    }

    for (i=0; i<ncol+1; i++) {
369 370 371 372 373 374 375 376 377 378 379
        err = fscanf(fp, " %d ", &u);
        if (err == EOF){ 
            ABC_FREE(colptr);
            ABC_FREE(rowind);
            return(0);
        } else if (err != 1) {
            ABC_FREE(colptr);
            ABC_FREE(rowind);
            return(0);
        }
        colptr[i] = u - 1;
Alan Mishchenko committed
380 381
    }
    if (colptr[0] != 0) {
382 383
        (void) fprintf(dd->err,"%s: Unexpected colptr[0] (%d)\n",
                       key,colptr[0]);
Alan Mishchenko committed
384 385
        ABC_FREE(colptr);
        ABC_FREE(rowind);
Alan Mishchenko committed
386 387
        return(0);
    }
388 389 390 391 392 393 394 395 396 397 398 399
    for (i=0; i<nnzero; i++) {
        err = fscanf(fp, " %d ", &u);
        if (err == EOF){ 
            ABC_FREE(colptr);
            ABC_FREE(rowind);
            return(0);
        } else if (err != 1) {
            ABC_FREE(colptr);
            ABC_FREE(rowind);
            return(0);
        }
        rowind[i] = u - 1;
Alan Mishchenko committed
400 401 402 403 404
    }

    *E = zero; cuddRef(*E);

    for (j=0; j<ncol; j++) {
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
        v = j;
        cubey = one; cuddRef(cubey);
        for (nv = lny - 1; nv>=0; nv--) {
            if (v & 1) {
                w = Cudd_addApply(dd, Cudd_addTimes, cubey, ly[nv]);
            } else {
                w = Cudd_addApply(dd, Cudd_addTimes, cubey, lyn[nv]);
            }
            if (w == NULL) {
                Cudd_RecursiveDeref(dd, cubey);
                ABC_FREE(colptr);
                ABC_FREE(rowind);
                return(0);
            }
            cuddRef(w);
Alan Mishchenko committed
420
            Cudd_RecursiveDeref(dd, cubey);
421 422
            cubey = w;
            v >>= 1;
Alan Mishchenko committed
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
        for (i=colptr[j]; i<colptr[j+1]; i++) {
            u = rowind[i];
            err = fscanf(fp, " %lf ", &val);
            if (err == EOF || err != 1){ 
                Cudd_RecursiveDeref(dd, cubey);
                ABC_FREE(colptr);
                ABC_FREE(rowind);
                return(0);
            }
            /* Create new Constant node if necessary */
            cubex = cuddUniqueConst(dd, (CUDD_VALUE_TYPE) val);
            if (cubex == NULL) {
                Cudd_RecursiveDeref(dd, cubey);
                ABC_FREE(colptr);
                ABC_FREE(rowind);
                return(0);
            }
            cuddRef(cubex);

            for (nv = lnx - 1; nv>=0; nv--) {
                if (u & 1) {
                    w = Cudd_addApply(dd, Cudd_addTimes, cubex, lx[nv]);
                } else { 
                    w = Cudd_addApply(dd, Cudd_addTimes, cubex, lxn[nv]);
                }
                if (w == NULL) {
                    Cudd_RecursiveDeref(dd, cubey);
                    Cudd_RecursiveDeref(dd, cubex);
                    ABC_FREE(colptr);
                    ABC_FREE(rowind);
                    return(0);
                }
                cuddRef(w);
                Cudd_RecursiveDeref(dd, cubex);
                cubex = w;
                u >>= 1;
            }
            minterm1 = Cudd_addApply(dd, Cudd_addTimes, cubey, cubex);
            if (minterm1 == NULL) {
                Cudd_RecursiveDeref(dd, cubey);
                Cudd_RecursiveDeref(dd, cubex);
                ABC_FREE(colptr);
                ABC_FREE(rowind);
                return(0);
            }
            cuddRef(minterm1);
            Cudd_RecursiveDeref(dd, cubex);
            w = Cudd_addApply(dd, Cudd_addPlus, *E, minterm1);
            if (w == NULL) {
                Cudd_RecursiveDeref(dd, cubey);
                ABC_FREE(colptr);
                ABC_FREE(rowind);
                return(0);
            }
            cuddRef(w);
            Cudd_RecursiveDeref(dd, minterm1);
            Cudd_RecursiveDeref(dd, *E);
            *E = w;
Alan Mishchenko committed
482 483 484
        }
        Cudd_RecursiveDeref(dd, cubey);
    }
Alan Mishchenko committed
485 486
    ABC_FREE(colptr);
    ABC_FREE(rowind);
Alan Mishchenko committed
487 488 489

    /* Read right-hand sides */
    for (j=0; j<nrhs; j++) {
490 491 492 493 494 495 496 497 498 499 500 501 502
        v = j + (1<< (lny-1));
        cubey = one; cuddRef(cubey);
        for (nv = lny - 1; nv>=0; nv--) {
            if (v & 1) {
                w = Cudd_addApply(dd, Cudd_addTimes, cubey, ly[nv]);
            } else {
                w = Cudd_addApply(dd, Cudd_addTimes, cubey, lyn[nv]);
            }
            if (w == NULL) {
                Cudd_RecursiveDeref(dd, cubey);
                return(0);
            }
            cuddRef(w);
Alan Mishchenko committed
503
            Cudd_RecursiveDeref(dd, cubey);
504 505
            cubey = w;
            v >>= 1;
Alan Mishchenko committed
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
        for (i=0; i<nrow; i++) {
            u = i;
            err = fscanf(fp, " %lf ", &val);
            if (err == EOF || err != 1){ 
                Cudd_RecursiveDeref(dd, cubey);
                return(0);
            }
            /* Create new Constant node if necessary */
            if (val == (double) 0.0) continue;
            cubex = cuddUniqueConst(dd, (CUDD_VALUE_TYPE) val);
            if (cubex == NULL) {
                Cudd_RecursiveDeref(dd, cubey);
                return(0);
            }
            cuddRef(cubex);

            for (nv = lnx - 1; nv>=0; nv--) {
                if (u & 1) {
                   w = Cudd_addApply(dd, Cudd_addTimes, cubex, lx[nv]);
                } else { 
                    w = Cudd_addApply(dd, Cudd_addTimes, cubex, lxn[nv]);
                }
                if (w == NULL) {
                    Cudd_RecursiveDeref(dd, cubey);
                    Cudd_RecursiveDeref(dd, cubex);
                    return(0);
                }
                cuddRef(w);
                Cudd_RecursiveDeref(dd, cubex);
                cubex = w;
                u >>= 1;
            }
            minterm1 = Cudd_addApply(dd, Cudd_addTimes, cubey, cubex);
            if (minterm1 == NULL) {
                Cudd_RecursiveDeref(dd, cubey);
                Cudd_RecursiveDeref(dd, cubex);
                return(0);
            }
            cuddRef(minterm1);
            Cudd_RecursiveDeref(dd, cubex);
            w = Cudd_addApply(dd, Cudd_addPlus, *E, minterm1);
            if (w == NULL) {
                Cudd_RecursiveDeref(dd, cubey);
                return(0);
            }
            cuddRef(w);
            Cudd_RecursiveDeref(dd, minterm1);
            Cudd_RecursiveDeref(dd, *E);
            *E = w;
Alan Mishchenko committed
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
        }
        Cudd_RecursiveDeref(dd, cubey);
    }

    return(1);

} /* end of Cudd_addHarwell */


/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/

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

573

574 575
ABC_NAMESPACE_IMPL_END

576