sbdSat.c 25.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**CFile****************************************************************

  FileName    [sbd.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SAT-based optimization using internal don't-cares.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: sbd.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "sbdInt.h"
22
#include "misc/util/utilTruth.h"
23 24 25 26 27 28 29 30

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

31 32
#define MAX_M  8 // max inputs
#define MAX_N 30 // max nodes
33
#define MAX_K  6 // max lutsize
34
#define MAX_D  8 // max delays
35 36 37 38 39

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 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 176 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
// new AIG manager
typedef struct Sbd_Pro_t_ Sbd_Pro_t;
struct Sbd_Pro_t_
{
    int             nLuts;  // LUT count
    int             nSize;  // LUT size
    int             nDivs;  // divisor count
    int             nVars;  // intermediate variables (nLuts * nSize)
    int             nPars;  // total parameter count (nLuts * (1 << nSize) + nLuts * nSize * nDivs) 
    int             pPars1[SBD_LUTS_MAX][1<<SBD_SIZE_MAX];            // lut parameters
    int             pPars2[SBD_LUTS_MAX][SBD_SIZE_MAX][SBD_DIV_MAX];  // topo parameters
    int             pVars[SBD_LUTS_MAX][SBD_SIZE_MAX+1];              // internal variables
    int             pDivs[SBD_DIV_MAX];                               // divisor variables (external)
};

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Sbd_ProblemSetup( Sbd_Pro_t * p, int nLuts, int nSize, int nDivs )
{
    Vec_Int_t * vCnf = Vec_IntAlloc( 1000 );
    int i, k, d, v, n, iVar = 0;
    assert( nLuts >= 1 && nLuts <= 2 );
    memset( p, 0, sizeof(Sbd_Pro_t) );
    p->nLuts = nLuts;
    p->nSize = nSize;
    p->nDivs = nDivs;
    p->nVars = nLuts * nSize;
    p->nPars = nLuts * (1 << nSize) + nLuts * nSize * nDivs;
    // set parameters
    for ( i = 0; i < nLuts; i++ )
    for ( k = 0; k < (1 << nSize); k++ )
        p->pPars1[i][k] = iVar++;
    for ( i = 0; i < nLuts; i++ )
    for ( k = 0; k < nSize; k++ )
    for ( d = 0; d < nDivs; d++ )
        p->pPars2[i][k][d] = iVar++;
    // set intermediate variables
    for ( i = 0; i < nLuts; i++ )
    for ( k = 0; k < nSize; k++ )
        p->pVars[i][k] = iVar++;
    // set top variables 
    for ( i = 1; i < nLuts; i++ )
        p->pVars[i][nSize] = p->pVars[i-1][0];
    // set divisor variables
    for ( d = 0; d < nDivs; d++ )
        p->pDivs[d] = iVar++;
    assert( iVar == p->nPars + p->nVars + p->nDivs );

    // input compatiblity clauses
    for ( i = 0; i < nLuts; i++ )
    for ( k = (i > 0); k < nSize; k++ )
    for ( d = 0; d < nDivs; d++ )
    for ( n = 0; n < nDivs; n++ )
    {
        if ( n < d )
        {
            Vec_IntPush( vCnf, Abc_Var2Lit(p->pPars2[i][k][d], 0) );
            Vec_IntPush( vCnf, Abc_Var2Lit(p->pPars2[i][k][n], 0) );
            Vec_IntPush( vCnf, -1 );
        }
        else if ( k < nSize-1 )
        {
            Vec_IntPush( vCnf, Abc_Var2Lit(p->pPars2[i][k][d], 0) );
            Vec_IntPush( vCnf, Abc_Var2Lit(p->pPars2[i][k+1][n], 0) );
            Vec_IntPush( vCnf, -1 );
        }
    }

    // create LUT clauses
    for ( i = 0; i < nLuts; i++ )
    for ( k = 0; k < (1 << nSize); k++ )
    for ( n = 0; n < 2; n++ )
    {
        for ( v = 0; v < nSize; v++ )
            Vec_IntPush( vCnf, Abc_Var2Lit(p->pPars1[i][v], (k >> v) & 1) );
        Vec_IntPush( vCnf, Abc_Var2Lit(p->pVars[i][nSize], n) );
        Vec_IntPush( vCnf, Abc_Var2Lit(p->pPars1[i][k], !n) );
        Vec_IntPush( vCnf, -1 );
    }

    // create input clauses
    for ( i = 0; i < nLuts; i++ )
    for ( k = (i > 0); k < nSize; k++ )
    for ( d = 0; d < nDivs; d++ )
    for ( n = 0; n < 2; n++ )
    {
        Vec_IntPush( vCnf, Abc_Var2Lit(p->pPars2[i][k][d], 0) );
        Vec_IntPush( vCnf, Abc_Var2Lit(p->pPars1[i][k], n) );
        Vec_IntPush( vCnf, Abc_Var2Lit(p->pDivs[d], !n) );
        Vec_IntPush( vCnf, -1 );
    }

    return vCnf;
}
// add clauses to the don't-care computation solver
void Sbd_ProblemLoad1( Sbd_Pro_t * p, Vec_Int_t * vCnf, int iStartVar, int * pDivVars, int iTopVar, sat_solver * pSat )
{
    int pLits[8], nLits, i, k, iLit, RetValue;
    int ThisTopVar = p->pVars[0][p->nSize];
    int FirstDivVar = p->nPars + p->nVars;
    // add clauses
    for ( i = 0; i < Vec_IntSize(vCnf); i++ )
    {
        assert( Vec_IntEntry(vCnf, i) != -1 );
        for ( k = i+1; k < Vec_IntSize(vCnf); k++ )
            if ( Vec_IntEntry(vCnf, i) == -1 )
                break;
        nLits = 0;
        Vec_IntForEachEntryStartStop( vCnf, iLit, i, i, k ) {
            if ( Abc_Lit2Var(iLit) == ThisTopVar )
                pLits[nLits++] = Abc_Var2Lit( ThisTopVar, Abc_LitIsCompl(iLit) );
            else if ( Abc_Lit2Var(iLit) >= FirstDivVar )
                pLits[nLits++] = Abc_Var2Lit( pDivVars[Abc_Lit2Var(iLit)-FirstDivVar], Abc_LitIsCompl(iLit) );
            else
                pLits[nLits++] = iLit + 2 * iStartVar;
        }
        assert( nLits <= 8 );
        RetValue = sat_solver_addclause( pSat, pLits, pLits + nLits );
        assert( RetValue );
    }
}
// add clauses to the functionality evaluation solver
void Sbd_ProblemLoad2( Sbd_Pro_t * p, Vec_Wec_t * vCnf, int iStartVar, int * pDivVarValues, int iTopVarValue, sat_solver * pSat )
{
    Vec_Int_t * vLevel;
    int pLits[8], nLits, i, k, iLit, RetValue;
    int ThisTopVar = p->pVars[0][p->nSize];
    int FirstDivVar = p->nPars + p->nVars;
    int FirstIntVar = p->nPars;
    // add clauses
    Vec_WecForEachLevel( vCnf, vLevel, i )
    {
        nLits = 0;
        Vec_IntForEachEntry( vLevel, iLit, k ) {
            if ( Abc_Lit2Var(iLit) == ThisTopVar )
            {
                if ( Abc_LitIsCompl(iLit) == iTopVarValue )
                    break;
                continue;
            }
            else if ( Abc_Lit2Var(iLit) >= FirstDivVar )
            {
                if ( Abc_LitIsCompl(iLit) == pDivVarValues[Abc_Lit2Var(iLit)-FirstDivVar] )
                    break;
                continue;
            }
            else if ( Abc_Lit2Var(iLit) >= FirstIntVar )
                pLits[nLits++] = iLit + 2 * iStartVar;
            else
                pLits[nLits++] = iLit;
        }
        if ( k < Vec_IntSize(vLevel) )
            continue;
        assert( nLits <= 8 );
        RetValue = sat_solver_addclause( pSat, pLits, pLits + nLits );
        assert( RetValue );
    }
}


209 210 211 212 213 214 215 216 217 218 219
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
220
sat_solver * Sbd_SolverTopo( int M, int N, int K, int pVars[MAX_N][MAX_M+MAX_N][MAX_K], int pVars2[MAX_M+MAX_N][MAX_D], int pDelays[], int Req, int * pnVars ) // inputs, nodes, lutsize
221 222 223 224
{
    sat_solver * pSat = NULL;
    Vec_Int_t * vTemp = Vec_IntAlloc(100);
    // assign vars
225 226
    int RetValue, n, i, j, j2, k, k2, d, Count, nVars = 0;
    for ( n = 0; n < N;   n++ )
227
    for ( i = 0; i < M+N; i++ )
228 229 230 231 232
    for ( k = 0; k < K;   k++ )
        pVars[n][i][k] = -1;
    for ( n = 0; n < N;   n++ )
    for ( i = 0; i < M+n; i++ )
    for ( k = 0; k < K;   k++ )
233
        pVars[n][i][k] = nVars++;
234 235
    printf( "Number of topo vars = %d.\n", nVars );
    *pnVars = nVars;
236 237 238 239 240 241 242 243 244
    // add constraints
    pSat = sat_solver_new();
    sat_solver_setnvars( pSat, nVars );
    // each node is used
    for ( i = 0; i < M+N-1; i++ )
    {
        Vec_IntClear( vTemp );
        for ( n = 0; n < N; n++ )
        for ( k = 0; k < K; k++ )
245 246
            if ( pVars[n][i][k] >= 0 )
                Vec_IntPush( vTemp, Abc_Var2Lit(pVars[n][i][k], 0) );
247 248 249
        RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
        assert( RetValue );
    }
250 251 252
    printf( "Added %d node connectivity constraints.\n", i );
    // each fanin of each node is connected exactly once
    Count = 0;
253 254 255
    for ( n = 0; n < N; n++ )
    for ( k = 0; k < K; k++ )
    {
256
        // connected
257 258 259 260 261
        Vec_IntClear( vTemp );
        for ( i = 0; i < M+n; i++ )
            Vec_IntPush( vTemp, Abc_Var2Lit(pVars[n][i][k], 0) );
        RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
        assert( RetValue );
262 263 264 265 266 267 268 269 270
        // exactly once
        for ( i = 0;   i < M+n; i++ )
        for ( j = i+1; j < M+n; j++ )
        {
            Vec_IntFillTwo( vTemp, 2, Abc_Var2Lit(pVars[n][i][k], 1), Abc_Var2Lit(pVars[n][j][k], 1) );
            RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
            assert( RetValue );
            Count++;
        }
271
    }
272 273 274
    printf( "Added %d fanin connectivity constraints.\n", Count );
    // node fanins are unique
    Count = 0;
275
    for ( n = 0; n < N;   n++ )
276
    for ( i = 0; i < M+n; i++ )
277
    for ( k = 0; k < K;   k++ )
278 279 280 281 282 283 284 285 286 287 288 289 290
    for ( j = i; j < M+n; j++ )
    for ( k2 = k+1; k2 < K; k2++ )
    {
        Vec_IntFillTwo( vTemp, 2, Abc_Var2Lit(pVars[n][i][k], 1), Abc_Var2Lit(pVars[n][j][k2], 1) );
        RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
        assert( RetValue );
        Count++;
    }
    printf( "Added %d fanin exclusivity constraints.\n", Count );
    // nodes are ordered
    Count = 0;
    for ( n = 1; n < N;     n++ )
    for ( i = 0; i < M+n-1; i++ )
291
    {
292 293
        // first of n cannot be smaller than first of n-1 (but can be equal)
        for ( j = i+1; j < M+n-1; j++ )
294
        {
295
            Vec_IntFillTwo( vTemp, 2, Abc_Var2Lit(pVars[n][i][0], 1), Abc_Var2Lit(pVars[n-1][j][0], 1) );
296 297
            RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
            assert( RetValue );
298 299 300 301 302 303 304 305 306 307 308 309
            Count++;
        }
        // if first nodes of n and n-1 are equal, second nodes are ordered
        Vec_IntFillTwo( vTemp, 2, Abc_Var2Lit(pVars[n][i][0], 1), Abc_Var2Lit(pVars[n-1][i][0], 1) );
        for ( j = 0;    j < i;  j++ )
        for ( j2 = j+1; j2 < i; j2++ )
        {
            Vec_IntPushTwo( vTemp, Abc_Var2Lit(pVars[n][j][1], 1), Abc_Var2Lit(pVars[n-1][j2][1], 1) );
            RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
            assert( RetValue );
            Vec_IntShrink( vTemp, 2 );
            Count++;
310 311
        }
    }
312
    printf( "Added %d node ordering constraints.\n", Count );
313
    // exclude fanins of two-input nodes
314
    Count = 0;
315
    if ( K == 2 )
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
    for ( n = 1; n < N;   n++ )
    for ( i = M; i < M+n; i++ )
    for ( j = 0; j < i;   j++ )
    for ( k = 0; k < K;   k++ )
    {
        Vec_IntClear( vTemp );
        Vec_IntPush( vTemp, Abc_Var2Lit(pVars[n][i][0], 1) );
        Vec_IntPush( vTemp, Abc_Var2Lit(pVars[n][j][1], 1) );
        Vec_IntPush( vTemp, Abc_Var2Lit(pVars[i-M][j][k], 1) );
        RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
        assert( RetValue );
        Count++;
    }
    printf( "Added %d two-node non-triviality constraints.\n", Count );


    // assign delay vars
    assert( Req < MAX_D-1 );
    for ( i = 0; i < M+N;   i++ )
    for ( d = 0; d < MAX_D; d++ )
        pVars2[i][d] = nVars++;
    printf( "Number of total vars = %d.\n", nVars );
    // set input delays
    for ( i = 0; i < M; i++ )
    {
        assert( pDelays[i] < MAX_D-2 );
        Vec_IntFill( vTemp, 1, Abc_Var2Lit(pVars2[i][pDelays[i]], 0) );
        RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
        assert( RetValue );
    }
    // set output delay
    for ( k = Req; k < MAX_D; k++ )
    {
        Vec_IntFill( vTemp, 1, Abc_Var2Lit(pVars2[M+N-1][Req+1], 1) );
        RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
        assert( RetValue );
    }
    // set internal nodes
354
    for ( n = 0; n < N;   n++ )
355
    for ( i = 0; i < M+n; i++ )
356
    for ( k = 0; k < K;   k++ )
357
    for ( d = 0; d < MAX_D-1; d++ )
358
    {
359 360 361 362 363
        Vec_IntFill( vTemp, 1, Abc_Var2Lit(pVars[n][i][k],   1) );
        Vec_IntPush( vTemp,    Abc_Var2Lit(pVars2[i][d],     1) );
        Vec_IntPush( vTemp,    Abc_Var2Lit(pVars2[M+n][d+1], 0) );
        RetValue = sat_solver_addclause( pSat, Vec_IntArray(vTemp), Vec_IntLimit(vTemp) );
        assert( RetValue );
364
    }
365 366

    
367 368 369 370 371 372 373
    Vec_IntFree( vTemp );
    return pSat;
}
void Sbd_SolverTopoPrint( sat_solver * pSat, int M, int N, int K, int pVars[MAX_N][MAX_M+MAX_N][MAX_K] ) 
{
    int n, i, k;
    printf( "Solution:\n" );
374 375 376 377 378
    printf( "     | " );
    for ( n = 0; n < N; n++ )
        printf( "%2d  ", M+n );
    printf( "\n" );
    for ( i = M+N-2; i >= 0; i-- )
379
    {
380
        printf( "%2d %c | ", i, i < M ? 'i' : ' ' );
381 382
        for ( n = 0; n < N; n++ )
        {
383 384 385 386 387
            for ( k = K-1; k >= 0; k-- )
                if ( pVars[n][i][k] == -1 )
                    printf( " " );
                else                    
                    printf( "%c", sat_solver_var_value(pSat, pVars[n][i][k]) ? '*' : '.' );
388 389 390 391 392 393 394
            printf( "  " );
        }
        printf( "\n" );
    }
}
void Sbd_SolverTopoTest()
{
395 396 397
    int M = 8;  //  6;  // inputs
    int N = 3;  // 16;  // nodes
    int K = 4;  //  2;  // lutsize
398
    int status, v, nVars, nIter, nSols = 0;
399
    int pVars[MAX_N][MAX_M+MAX_N][MAX_K]; // 20 x 32 x 6 = 3840
400 401 402
    int pVars2[MAX_M+MAX_N][MAX_D];       // 20 x 32 x 6 = 3840
    int pDelays[MAX_M] = {1,0,0,0,1};
    abctime clk = Abc_Clock();
403
    Vec_Int_t * vLits = Vec_IntAlloc(100);
404
    sat_solver * pSat = Sbd_SolverTopo( M, N, K, pVars, pVars2, pDelays, 2, &nVars );
405
    for ( nIter = 0; nIter < 1000000; nIter++ )
406 407 408 409 410 411 412 413
    {
        // find onset minterm
        status = sat_solver_solve( pSat, NULL, NULL, 0, 0, 0, 0 );
        if ( status == l_Undef )
            break;
        if ( status == l_False )
            break;
        assert( status == l_True );
414
        nSols++;
415
        // print solution
416 417
        if ( nIter < 5 )
            Sbd_SolverTopoPrint( pSat, M, N, K, pVars );
418 419 420 421 422 423 424
        // remember variable values
        Vec_IntClear( vLits );
        for ( v = 0; v < nVars; v++ )
            if ( sat_solver_var_value(pSat, v) )
                Vec_IntPush( vLits, Abc_Var2Lit(v, 1) );
        // add breaking clause
        status = sat_solver_addclause( pSat, Vec_IntArray(vLits), Vec_IntLimit(vLits) );
425 426
        if ( status == 0 )
            break;
427 428
        //if ( nIter == 5 )
        //    break;
429 430 431
    }
    sat_solver_delete( pSat );
    Vec_IntFree( vLits );
432 433
    printf( "Found %d solutions. ", nSols );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
434 435 436
}


437 438 439

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

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 482 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
  Synopsis    [Synthesize random topology.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sbd_SolverSynth( int M, int N, int K, int pLuts[MAX_N][MAX_K] )
{
    int Used[MAX_M+MAX_N] = {0};
    int nUnused = M;
    int n, iFan0, iFan1;
    srand( time(NULL) );
    for ( n = 0; nUnused < N - n; n++ )
    {
        iFan0 = iFan1 = 0;
        while ( (iFan0 = rand() % (M + n)) == (iFan1 = rand() % (M + n)) )
            ;
        pLuts[n][0] = iFan0;
        pLuts[n][1] = iFan1;
        if ( Used[iFan0] == 0 )
        {
            Used[iFan0] = 1;
            nUnused--;
        }
        if ( Used[iFan1] == 0 )
        {
            Used[iFan1] = 1;
            nUnused--;
        }
        nUnused++;
    }
    if ( nUnused == N - n )
    {
        // undo the first one
        for ( iFan0 = 0; iFan0 < M+n; iFan0++ )
            if ( Used[iFan0] )
            {
                Used[iFan0] = 0;
                nUnused++;
                break;
            }

    }
    assert( nUnused == N - n + 1 );
    for ( ; n < N; n++ )
    {
        for ( iFan0 = 0; iFan0 < M+n; iFan0++ )
            if ( Used[iFan0] == 0 )
            {
                Used[iFan0] = 1;
                break;
            }
        assert( iFan0 < M+n );
        for ( iFan1 = 0; iFan1 < M+n; iFan1++ )
            if ( Used[iFan1] == 0 )
            {
                Used[iFan1] = 1;
                break;
            }
        assert( iFan1 < M+n );
        pLuts[n][0] = iFan0;
        pLuts[n][1] = iFan1;
    }

    printf( "{\n" );
    for ( n = 0; n < N; n++ )
        printf( "    {%d, %d}%s // %d\n", pLuts[n][0], pLuts[n][1], n==N-1 ? "" :",", M+n );
    printf( "};\n" );
}


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

516 517 518 519 520 521 522 523 524 525 526 527
  Synopsis    [Compute truth table for the given parameter settings.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
word Sbd_SolverTruth( int M, int N, int K, int pLuts[MAX_N][MAX_K], int pValues[MAX_N*((1<<MAX_K)-1)] )
{
    int i, k, v, nLutPars = (1 << K) - 1;
528 529
    word Truths[MAX_M+MAX_N];
    assert( M <= 6 && N <= MAX_N );
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    for ( i = 0; i < M; i++ )
        Truths[i] = s_Truths6[i];
    for ( i = 0; i < N; i++ )
    {
        word Truth = 0, Mint;
        for ( k = 1; k <= nLutPars; k++ )
        {            
            if ( !pValues[i*nLutPars+k-1] )
                continue;
            Mint = ~(word)0;
            for ( v = 0; v < K; v++ )
                Mint &= ((k >> v) & 1) ? Truths[pLuts[i][v]] :  ~Truths[pLuts[i][v]];
            Truth |= Mint;
        }
        Truths[M+i] = Truth;
    }
    return Truths[M+N-1];
}
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
word * Sbd_SolverTruthWord( int M, int N, int K, int pLuts[MAX_N][MAX_K], int pValues[MAX_N*((1<<MAX_K)-1)], word * pTruthsElem, int fCompl )
{
    int i, k, v, nLutPars = (1 << K) - 1;
    int nWords = Abc_TtWordNum( M );
    word * pRes = pTruthsElem + (M+N-1)*nWords;
    assert( M <= MAX_M && N <= MAX_N );
    for ( i = 0; i < N; i++ )
    {
        word * pMint, * pTruth = pTruthsElem + (M+i)*nWords;
        Abc_TtClear( pTruth, nWords );
        for ( k = 1; k <= nLutPars; k++ )
        {            
            if ( !pValues[i*nLutPars+k-1] )
                continue;
            pMint = pTruthsElem + (M+N)*nWords;
            Abc_TtFill( pMint, nWords );
            for ( v = 0; v < K; v++ )
            {
                word * pFanin = pTruthsElem + pLuts[i][v]*nWords;
                Abc_TtAndSharp( pMint, pMint, pFanin, nWords, ((k >> v) & 1) == 0 );
            }
            Abc_TtOr( pTruth, pTruth, pMint, nWords );
        }
    }
    if ( fCompl )
        Abc_TtNot( pRes, nWords );
    return pRes;
}

577 578 579 580 581 582 583 584 585 586 587 588

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
589
int Sbd_SolverFunc( int M, int N, int K, int pLuts[MAX_N][MAX_K], word * pTruthInit, int * pValues ) 
590 591
{
    int fVerbose = 0;
592 593
    abctime clk = Abc_Clock();
    abctime clk2, clkOther = 0;
594
    sat_solver * pSat = NULL;
595
    int nWords = Abc_TtWordNum(M);
596 597
    int pLits[MAX_K+2], pLits2[MAX_K+2], nLits;
    int nLutPars = (1 << K) - 1, nVars = N * nLutPars;
598 599 600 601
    int i, k, m, status, iMint, Iter, fCompl = (int)(pTruthInit[0] & 1);
    // create truth tables
    word * pTruthNew, * pTruths = ABC_ALLOC( word, Abc_TtWordNum(MAX_N) * (MAX_M + MAX_N + 1) );
    Abc_TtElemInit2( pTruths, M );
602 603 604
    // create solver
    pSat = sat_solver_new();
    sat_solver_setnvars( pSat, nVars );
605
    printf( "Number of parameters %d x %d = %d.\n", N, nLutPars, nVars );
606
    // start with the last minterm
607 608
//    iMint = (1 << M) - 1;
    iMint = 1;
609 610 611 612 613 614
    for ( Iter = 0; Iter < (1 << M); Iter++ )
    {
        // assign the first intermediate variable
        int nVarStart = sat_solver_nvars(pSat);
        sat_solver_setnvars( pSat, nVarStart + N - 1 );
        // add clauses for nodes
615 616
        //if ( fVerbose )
            printf( "Iter %3d : Mint = %3d. Conflicts =%8d.\n", Iter, iMint, sat_solver_nconflicts(pSat) );
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
        for ( i = 0; i < N; i++ )
        for ( m = 0; m <= nLutPars; m++ )
        {
            if ( fVerbose )
                printf( "i = %d.  m = %d.\n", i, m );
            // selector variables
            nLits = 0;
            for ( k = 0; k < K; k++ ) 
            {
                if ( pLuts[i][k] >= M )
                {
                    assert( pLuts[i][k] - M < N - 1 );
                    pLits[nLits] = pLits2[nLits] = Abc_Var2Lit( nVarStart + pLuts[i][k] - M, (m >> k) & 1 ); 
                    nLits++;
                }
                else if ( ((iMint >> pLuts[i][k]) & 1) != ((m >> k) & 1) )
                    break;
            }
            if ( k < K )
                continue;
            // add parameter
            if ( m )
            {
                pLits[nLits]  = Abc_Var2Lit( i*nLutPars + m-1, 1 );
                pLits2[nLits] = Abc_Var2Lit( i*nLutPars + m-1, 0 );
                nLits++;
            }
            // node variable
            if ( i != N - 1 ) 
            {
                pLits[nLits]  = Abc_Var2Lit( nVarStart + i, 0 );
                pLits2[nLits] = Abc_Var2Lit( nVarStart + i, 1 );
                nLits++;
            }
            // add clauses
652
            if ( i != N - 1 || Abc_TtGetBit(pTruthInit, iMint) != fCompl )
653 654 655 656 657 658 659 660
            {
                status = sat_solver_addclause( pSat, pLits2, pLits2 + nLits );
                if ( status == 0 )
                {
                    fCompl = -1;
                    goto finish;
                }
            }
661
            if ( (i != N - 1 || Abc_TtGetBit(pTruthInit, iMint) == fCompl) && m > 0 )
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
            {
                status = sat_solver_addclause( pSat, pLits, pLits + nLits );
                if ( status == 0 )
                {
                    fCompl = -1;
                    goto finish;
                }
            }
        }

        // run SAT solver
        status = sat_solver_solve( pSat, NULL, NULL, 0, 0, 0, 0 );
        if ( status == l_Undef )
            break;
        if ( status == l_False )
        {
            fCompl = -1;
            goto finish;
        }
        assert( status == l_True );

        // collect values
        for ( i = 0; i < nVars; i++ )
            pValues[i] = sat_solver_var_value(pSat, i);
686 687 688 689 690

        clk2 = Abc_Clock();
        pTruthNew = Sbd_SolverTruthWord( M, N, K, pLuts, pValues, pTruths, fCompl );
        clkOther += Abc_Clock() - clk2;

691 692 693 694 695 696 697 698
        if ( fVerbose )
        {
            for ( i = 0; i < nVars; i++ )
                printf( "%d=%d ", i, pValues[i] );
            printf( "  " );
            for ( i = nVars; i < sat_solver_nvars(pSat); i++ )
                printf( "%d=%d ", i, sat_solver_var_value(pSat, i) );
            printf( "\n" );
699 700
            Extra_PrintBinary( stdout, (unsigned *)pTruthInit, (1 << M) );  printf( "\n" );
            Extra_PrintBinary( stdout, (unsigned *)pTruthNew,  (1 << M) );  printf( "\n" );
701
        }
702
        if ( Abc_TtEqual(pTruthInit, pTruthNew, nWords) )
703 704 705
            break;

        // get new minterm
706
        iMint = Abc_TtFindFirstDiffBit( pTruthInit, pTruthNew, M );
707 708
    }
finish:
709
    printf( "Finished after %d iterations and %d conflicts.  ", Iter, sat_solver_nconflicts(pSat) );
710
    sat_solver_delete( pSat );
711 712 713
    ABC_FREE( pTruths );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
    Abc_PrintTime( 1, "Time", clkOther );
714 715 716 717 718 719 720
    return fCompl;
}
void Sbd_SolverFuncTest() 
{
//    int M = 4;  //  6;  // inputs
//    int N = 3;  // 16;  // nodes
//    int K = 2;  //  2;  // lutsize
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
//    word Truth = ~((word)3 << 8);
//    int pLuts[MAX_N][MAX_K] = { {0,1}, {2,3}, {4,5}, {6,7}, {8,9} };

/*
    int M =  6;  //  6;  // inputs
    int N = 19;  // 16;  // nodes
    int K =  2;  //  2;  // lutsize
    word pTruth[4] = { ABC_CONST(0x9ef7a8d9c7193a0f), 0, 0, 0 };
    int pLuts[MAX_N][MAX_K] = { 
        {3, 5}, {1, 6}, {0, 5}, {8, 2}, {7, 9},
        {0, 1}, {2, 11}, {5, 12}, {3, 13}, {1, 14},
        {10, 15}, {11, 2}, {3, 17}, {9, 18}, {0, 13},
        {20, 7}, {19, 21}, {4, 16}, {23, 22} 
    };
*/

/*
738 739
    int M = 6;  //  6;  // inputs
    int N = 5;  // 16;  // nodes
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
    int K = 4;  //  2;  // lutsize
    word Truth = ABC_CONST(0x9ef7a8d9c7193a0f);
    int pLuts[MAX_N][MAX_K] = { 
        {0, 1, 2, 3}, // 6
        {1, 2, 3, 4}, // 7
        {2, 3, 4, 5}, // 8
        {0, 1, 4, 5}, // 9
        {6, 7, 8, 9}  // 10
    };
*/

/*
    int M =  8;  //  6;  // inputs
    int N =  7;  // 16;  // nodes
    int K =  2;  //  2;  // lutsize
//    word pTruth[4] = { 0, 0, 0, ABC_CONST(0x8000000000000000) };
//    word pTruth[4] = { ABC_CONST(0x0000000000000001), 0, 0, 0 };
    word pTruth[4] = { 0, 0, 0, ABC_CONST(0x0000000000020000) };
    int pLuts[MAX_N][MAX_K] = { {0,1}, {2,3}, {4,5}, {6,7}, {8,9}, {10,11}, {12,13} };
*/

    int M =  8;  //  6;  // inputs
    int N =  7;  // 16;  // nodes
    int K =  2;  //  2;  // lutsize
    word pTruth[4] = { ABC_CONST(0x0000080000020000), ABC_CONST(0x0000000000020000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000020000) };
    int pLuts[MAX_N][MAX_K] = { {0,1}, {2,3}, {4,5}, {6,7}, {8,9}, {10,11}, {12,13} };

767
    int pValues[MAX_N*((1<<MAX_K)-1)];
768 769 770 771 772
    int Res, i, k, nLutPars = (1 << K) - 1;

    //Sbd_SolverSynth( M, N, K, pLuts );

    Res = Sbd_SolverFunc( M, N, K, pLuts, pTruth, pValues );
773 774 775 776 777 778 779 780 781 782 783 784 785 786
    if ( Res == -1 )
    {
        printf( "Solution does not exist.\n" );
        return;
    }
    printf( "Result (compl = %d):\n", Res );
    for ( i = 0; i < N; i++ )
    {
        for ( k = nLutPars-1; k >= 0; k-- )
            printf( "%d", pValues[i*nLutPars+k] );
        printf( "0\n" );
    }
}

787 788 789 790 791 792 793
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END