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

  FileName    [cuddInit.c]

  PackageName [cudd]

  Synopsis    [Functions to initialize and shut down the DD manager.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19
                <ul>
                <li> Cudd_Init()
                <li> Cudd_Quit()
                </ul>
               Internal procedures included in this module:
                <ul>
                <li> cuddZddInitUniv()
                <li> cuddZddFreeUniv()
                </ul>
              ]
Alan Mishchenko committed
20 21 22 23 24

  SeeAlso     []

  Author      [Fabio Somenzi]

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 50 51 52 53 54 55
  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
56 57 58

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

59
#include "misc/util/util_hack.h"
60
#include "cuddInt.h"
61 62 63

ABC_NAMESPACE_IMPL_START

64

Alan Mishchenko committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

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


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


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


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

#ifndef lint
86
static char rcsid[] DD_UNUSED = "$Id: cuddInit.c,v 1.33 2007/07/01 05:10:50 fabio Exp $";
Alan Mishchenko committed
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
#endif

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


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

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


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


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

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

  Synopsis    [Creates a new DD manager.]

  Description [Creates a new DD manager, initializes the table, the
  basic constants and the projection functions. If maxMemory is 0,
  Cudd_Init decides suitable values for the maximum size of the cache
  and for the limit for fast unique table growth based on the available
  memory. Returns a pointer to the manager if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_Quit]

******************************************************************************/
DdManager *
Cudd_Init(
  unsigned int numVars /* initial number of BDD variables (i.e., subtables) */,
  unsigned int numVarsZ /* initial number of ZDD variables (i.e., subtables) */,
  unsigned int numSlots /* initial size of the unique tables */,
  unsigned int cacheSize /* initial size of the cache */,
  unsigned long maxMemory /* target maximum memory occupation */)
{
    DdManager *unique;
    int i,result;
    DdNode *one, *zero;
    unsigned int maxCacheSize;
    unsigned int looseUpTo;
137 138
    extern DD_OOMFP MMoutOfMemory;
    DD_OOMFP saveHandler;
Alan Mishchenko committed
139 140

    if (maxMemory == 0) {
141
        maxMemory = getSoftDataLimit();
Alan Mishchenko committed
142 143
    }
    looseUpTo = (unsigned int) ((maxMemory / sizeof(DdNode)) /
144
                                DD_MAX_LOOSE_FRACTION);
Alan Mishchenko committed
145 146
    unique = cuddInitTable(numVars,numVarsZ,numSlots,looseUpTo);
    if (unique == NULL) return(NULL);
147
    unique->maxmem = (unsigned long) maxMemory / 10 * 9;
Alan Mishchenko committed
148
    maxCacheSize = (unsigned int) ((maxMemory / sizeof(DdCache)) /
149
                                   DD_MAX_CACHE_FRACTION);
Alan Mishchenko committed
150 151 152 153 154
    result = cuddInitCache(unique,cacheSize,maxCacheSize);
    if (result == 0) return(NULL);

    saveHandler = MMoutOfMemory;
    MMoutOfMemory = Cudd_OutOfMem;
Alan Mishchenko committed
155
    unique->stash = ABC_ALLOC(char,(maxMemory / DD_STASH_FRACTION) + 4);
Alan Mishchenko committed
156 157
    MMoutOfMemory = saveHandler;
    if (unique->stash == NULL) {
158
        (void) fprintf(unique->err,"Unable to set aside memory\n");
Alan Mishchenko committed
159 160 161 162 163 164 165 166 167 168 169
    }

    /* Initialize constants. */
    unique->one = cuddUniqueConst(unique,1.0);
    if (unique->one == NULL) return(0);
    cuddRef(unique->one);
    unique->zero = cuddUniqueConst(unique,0.0);
    if (unique->zero == NULL) return(0);
    cuddRef(unique->zero);
#ifdef HAVE_IEEE_754
    if (DD_PLUS_INF_VAL != DD_PLUS_INF_VAL * 3 ||
170 171 172
        DD_PLUS_INF_VAL != DD_PLUS_INF_VAL / 3) {
        (void) fprintf(unique->err,"Warning: Crippled infinite values\n");
        (void) fprintf(unique->err,"Recompile without -DHAVE_IEEE_754\n");
Alan Mishchenko committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186
    }
#endif
    unique->plusinfinity = cuddUniqueConst(unique,DD_PLUS_INF_VAL);
    if (unique->plusinfinity == NULL) return(0);
    cuddRef(unique->plusinfinity);
    unique->minusinfinity = cuddUniqueConst(unique,DD_MINUS_INF_VAL);
    if (unique->minusinfinity == NULL) return(0);
    cuddRef(unique->minusinfinity);
    unique->background = unique->zero;

    /* The logical zero is different from the CUDD_VALUE_TYPE zero! */
    one = unique->one;
    zero = Cudd_Not(one);
    /* Create the projection functions. */
Alan Mishchenko committed
187
    unique->vars = ABC_ALLOC(DdNodePtr,unique->maxSize);
Alan Mishchenko committed
188
    if (unique->vars == NULL) {
189 190
        unique->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
191 192
    }
    for (i = 0; i < unique->size; i++) {
193 194 195
        unique->vars[i] = cuddUniqueInter(unique,i,one,zero);
        if (unique->vars[i] == NULL) return(0);
        cuddRef(unique->vars[i]);
Alan Mishchenko committed
196 197 198
    }

    if (unique->sizeZ)
199
        cuddZddInitUniv(unique);
Alan Mishchenko committed
200 201 202

    unique->memused += sizeof(DdNode *) * unique->maxSize;

203 204
    unique->bFunc = NULL;
    unique->bFunc2 = NULL;
205
    unique->TimeStop = 0;
Alan Mishchenko committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    return(unique);

} /* end of Cudd_Init */


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

  Synopsis    [Deletes resources associated with a DD manager.]

  Description [Deletes resources associated with a DD manager and
  resets the global statistical counters. (Otherwise, another manaqger
  subsequently created would inherit the stats of this one.)]

  SideEffects [None]

  SeeAlso     [Cudd_Init]

******************************************************************************/
void
Cudd_Quit(
  DdManager * unique)
{
Alan Mishchenko committed
228
    if (unique->stash != NULL) ABC_FREE(unique->stash);
Alan Mishchenko committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    cuddFreeTable(unique);

} /* end of Cudd_Quit */


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


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

  Synopsis    [Initializes the ZDD universe.]

  Description [Initializes the ZDD universe. Returns 1 if successful; 0
  otherwise.]

  SideEffects [None]

  SeeAlso     [cuddZddFreeUniv]

******************************************************************************/
int
cuddZddInitUniv(
  DdManager * zdd)
{
255 256
    DdNode      *p, *res;
    int         i;
Alan Mishchenko committed
257

Alan Mishchenko committed
258
    zdd->univ = ABC_ALLOC(DdNodePtr, zdd->sizeZ);
Alan Mishchenko committed
259
    if (zdd->univ == NULL) {
260 261
        zdd->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
262 263 264 265 266
    }

    res = DD_ONE(zdd);
    cuddRef(res);
    for (i = zdd->sizeZ - 1; i >= 0; i--) {
267 268 269 270 271 272 273 274 275 276 277
        unsigned int index = zdd->invpermZ[i];
        p = res;
        res = cuddUniqueInterZdd(zdd, index, p, p);
        if (res == NULL) {
            Cudd_RecursiveDerefZdd(zdd,p);
            ABC_FREE(zdd->univ);
            return(0);
        }
        cuddRef(res);
        cuddDeref(p);
        zdd->univ[i] = res;
Alan Mishchenko committed
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
    }

#ifdef DD_VERBOSE
    cuddZddP(zdd, zdd->univ[0]);
#endif

    return(1);

} /* end of cuddZddInitUniv */


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

  Synopsis    [Frees the ZDD universe.]

  Description [Frees the ZDD universe.]

  SideEffects [None]

  SeeAlso     [cuddZddInitUniv]

******************************************************************************/
void
cuddZddFreeUniv(
  DdManager * zdd)
{
    if (zdd->univ) {
305 306
        Cudd_RecursiveDerefZdd(zdd, zdd->univ[0]);
        ABC_FREE(zdd->univ);
Alan Mishchenko committed
307 308 309 310 311 312 313 314 315
    }

} /* end of cuddZddFreeUniv */


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

316

317 318
ABC_NAMESPACE_IMPL_END

319