Experiments with LUT mapping for small functions.

This commit is contained in:
Alan Mishchenko 2021-08-01 12:13:27 -07:00
parent e162a26197
commit 4cf906d2fc
4 changed files with 255 additions and 32 deletions

View File

@ -566,6 +566,44 @@ word * Gia_ManCountFraction( Gia_Man_t * p, Vec_Wrd_t * vSimI, Vec_Int_t * vSupp
*pCare = nGood;
return pRes;
}
void Gia_ManPermuteSupp_rec( Gia_Man_t * p, int iObj, Vec_Int_t * vLevels, Vec_Int_t * vCounts )
{
Gia_Obj_t * pObj; int n;
if ( !iObj || Gia_ObjIsTravIdCurrentId(p, iObj) )
return;
Gia_ObjSetTravIdCurrentId(p, iObj);
pObj = Gia_ManObj( p, iObj );
if ( Gia_ObjIsCi(pObj) )
return;
assert( Gia_ObjIsAnd(pObj) );
Gia_ManPermuteSupp_rec( p, Gia_ObjFaninId0(pObj, iObj), vLevels, vCounts );
Gia_ManPermuteSupp_rec( p, Gia_ObjFaninId1(pObj, iObj), vLevels, vCounts );
for ( n = 0; n < 2; n++ )
{
Gia_Obj_t * pFanin = n ? Gia_ObjFanin1(pObj) : Gia_ObjFanin0(pObj);
if ( !Gia_ObjIsCi(pFanin) )
continue;
Vec_IntAddToEntry( vLevels, Gia_ObjCioId(pFanin), Gia_ObjLevel(p, pObj) );
Vec_IntAddToEntry( vCounts, Gia_ObjCioId(pFanin), 1 );
}
}
void Gia_ManPermuteSupp( Gia_Man_t * p, int iOut, int nOuts, Vec_Int_t * vSupp )
{
Vec_Int_t * vLevels = Vec_IntStart( Gia_ManCiNum(p) );
Vec_Int_t * vCounts = Vec_IntStart( Gia_ManCiNum(p) );
int i, * pCost = ABC_CALLOC( int, Gia_ManCiNum(p) );
Gia_Obj_t * pObj;
Gia_ManIncrementTravId( p );
for ( i = 0; i < nOuts; i++ )
Gia_ManPermuteSupp_rec( p, Gia_ObjFaninId0p(p, Gia_ManCo(p, iOut+i)), vLevels, vCounts );
Gia_ManForEachObjVec( vSupp, p, pObj, i )
pCost[i] = 10000 * Vec_IntEntry(vLevels, Gia_ObjCioId(pObj)) / Vec_IntEntry(vCounts, Gia_ObjCioId(pObj));
Vec_IntFree( vCounts );
Vec_IntFree( vLevels );
Vec_IntSelectSortCost2( Vec_IntArray(vSupp), Vec_IntSize(vSupp), pCost );
assert( Vec_IntSize(vSupp) < 2 || pCost[0] <= pCost[1] );
ABC_FREE( pCost );
}
void Gia_ManCollectSupp_rec( Gia_Man_t * p, int iObj, Vec_Int_t * vSupp )
{
Gia_Obj_t * pObj;
@ -591,6 +629,12 @@ Vec_Int_t * Gia_ManCollectSupp( Gia_Man_t * p, int iOut, int nOuts )
Gia_ManCollectSupp_rec( p, Gia_ObjFaninId0p(p, Gia_ManCo(p, iOut+i)), vSupp );
return vSupp;
}
Vec_Int_t * Gia_ManCollectSuppNew( Gia_Man_t * p, int iOut, int nOuts )
{
Vec_Int_t * vRes = Gia_ManCollectSupp( p, iOut, nOuts );
Gia_ManPermuteSupp( p, iOut, nOuts, vRes );
return vRes;
}
int Gia_ManPerformLNetOpt_rec( Gia_Man_t * pNew, Gia_Man_t * p, Gia_Obj_t * pObj )
{
if ( ~pObj->Value )
@ -602,7 +646,6 @@ int Gia_ManPerformLNetOpt_rec( Gia_Man_t * pNew, Gia_Man_t * p, Gia_Obj_t * pObj
}
Gia_Man_t * Gia_ManPerformLNetOpt( Gia_Man_t * p, int fTryNew, char * pFileName, int nIns, int nOuts, int Thresh, int nRounds, int fVerbose )
{
extern int Gia_ManDupOrderDfs_rec( Gia_Man_t * pNew, Gia_Man_t * p, Gia_Obj_t * pObj );
extern Gia_Man_t * Gia_TryPermOpt( word * pTruths, int nIns, int nOuts, int nWords, int nRounds, int fVerbose );
extern Gia_Man_t * Gia_TryPermOptCare( word * pTruths, int nIns, int nOuts, int nWords, int nRounds, int fVerbose );
extern int Kit_TruthToGia2( Gia_Man_t * p, unsigned * pTruth0, unsigned * pTruth1, int nVars, Vec_Int_t * vMemory, Vec_Int_t * vLeaves, int fHash );
@ -620,6 +663,7 @@ Gia_Man_t * Gia_ManPerformLNetOpt( Gia_Man_t * p, int fTryNew, char * pFileName,
printf( "Density of input patterns %8.4f.\n", (float)Abc_TtCountOnesVec(Vec_WrdArray(vSimI), Vec_WrdSize(vSimI))/(64*Vec_WrdSize(vSimI)) );
printf( "Using patterns with count %d and higher as cares.\n", Thresh );
}
Gia_ManLevelNum( p );
Gia_ManFillValue( p );
pNew = Gia_ManStart( Gia_ManObjNum(p) );
pNew->pName = Abc_UtilStrsav( p->pName );
@ -631,7 +675,7 @@ Gia_Man_t * Gia_ManPerformLNetOpt( Gia_Man_t * p, int fTryNew, char * pFileName,
Gia_ManHashStart( pNew );
for ( g = 0; g < Gia_ManCoNum(p); g += nOuts )
{
Vec_Int_t * vSupp = Gia_ManCollectSupp( p, g, nOuts );
Vec_Int_t * vSupp = Gia_ManCollectSuppNew( p, g, nOuts );
int Care = 1 << Vec_IntSize(vSupp), Temp = fVerbose ? printf( "Group %3d / %3d / %3d : Supp = %3d %s", g, nOuts, Gia_ManCoNum(p), Vec_IntSize(vSupp), vSimI ? "":"\n" ) : 0;
word * pCare = vSimI ? Gia_ManCountFraction( p, vSimI, vSupp, Thresh, fVerbose, &Care ) : ABC_FALLOC( word, Abc_Truth6WordNum(Vec_IntSize(vSupp)) );
int nWords = Abc_Truth6WordNum( Vec_IntSize(vSupp) );
@ -706,6 +750,84 @@ Gia_Man_t * Gia_ManPerformLNetOpt( Gia_Man_t * p, int fTryNew, char * pFileName,
ABC_FREE( pTruthsTry );
return pNew;
}
Gia_Man_t * Gia_ManPerformLNetOptNew( Gia_Man_t * p, char * pFileName, int nIns, int nOuts, int Thresh, int nRounds, int fVerbose )
{
extern Gia_Man_t * Gia_TryPermOptNew( word * pTruths, int nIns, int nOuts, int nWords, int nRounds, int fVerbose );
abctime clk = Abc_Clock();
Gia_Man_t * pNew, * pMin; Gia_Obj_t * pObj;
Vec_Int_t * vLeaves = Vec_IntAlloc( nIns );
Vec_Wrd_t * vSimI = pFileName ? Vec_WrdReadBin( pFileName, fVerbose ) : NULL;
word * pTruthsTry = ABC_CALLOC( word, (nOuts+1)*Abc_Truth6WordNum(nIns) );
int k, g; float CareAve = 0;
if ( vSimI && fVerbose )
{
//int nPats = 64*Vec_WrdSize(vSimI)/Gia_ManCiNum(p);
printf( "Density of input patterns %8.4f.\n", (float)Abc_TtCountOnesVec(Vec_WrdArray(vSimI), Vec_WrdSize(vSimI))/(64*Vec_WrdSize(vSimI)) );
printf( "Using patterns with count %d and higher as cares.\n", Thresh );
}
Gia_ManLevelNum( p );
Gia_ManFillValue( p );
pNew = Gia_ManStart( Gia_ManObjNum(p) );
pNew->pName = Abc_UtilStrsav( p->pName );
pNew->pSpec = Abc_UtilStrsav( p->pSpec );
Gia_ManConst0(p)->Value = 0;
Gia_ManForEachCi( p, pObj, k )
pObj->Value = Gia_ManAppendCi(pNew);
Gia_ObjComputeTruthTableStart( p, nIns );
Gia_ManHashStart( pNew );
for ( g = 0; g < Gia_ManCoNum(p); g += nOuts )
{
Vec_Int_t * vSupp = Gia_ManCollectSuppNew( p, g, nOuts );
int Care = 1 << Vec_IntSize(vSupp), Temp = fVerbose ? printf( "Group %3d / %3d / %3d : Supp = %3d %s", g, nOuts, Gia_ManCoNum(p), Vec_IntSize(vSupp), vSimI ? "":"\n" ) : 0;
word * pCare = vSimI ? Gia_ManCountFraction( p, vSimI, vSupp, Thresh, fVerbose, &Care ) : ABC_FALLOC( word, Abc_Truth6WordNum(Vec_IntSize(vSupp)) );
int nWords = Abc_Truth6WordNum( Vec_IntSize(vSupp) );
CareAve += 100.0*Care/(1 << Vec_IntSize(vSupp));
assert( Vec_IntSize(vSupp) <= nIns );
Vec_IntClear( vLeaves );
Gia_ManForEachObjVec( vSupp, p, pObj, k )
Vec_IntPush( vLeaves, pObj->Value );
for ( k = 0; k < nOuts; k++ )
{
Gia_Obj_t * pObj = Gia_ManCo( p, g+k );
word * pTruth = Gia_ObjComputeTruthTableCut( p, Gia_ObjFanin0(pObj), vSupp );
Abc_TtCopy( pTruthsTry + k*nWords, pTruth, nWords, Gia_ObjFaninC0(pObj) );
}
Abc_TtCopy( pTruthsTry + nOuts*nWords, pCare, nWords, 0 );
ABC_FREE( pCare );
pMin = Gia_TryPermOptNew( pTruthsTry, Vec_IntSize(vSupp), nOuts, nWords, nRounds, fVerbose );
Gia_ManFillValue( pMin );
Gia_ManConst0(pMin)->Value = 0;
Gia_ManForEachCi( pMin, pObj, k )
pObj->Value = Vec_IntEntry( vLeaves, k );
Gia_ManForEachCo( pMin, pObj, k )
{
Gia_Obj_t * pObj0 = Gia_ManCo( p, g+k );
pObj0->Value = Gia_ManPerformLNetOpt_rec( pNew, pMin, Gia_ObjFanin0(pObj) );
pObj0->Value ^= Gia_ObjFaninC0(pObj);
}
Gia_ManStop( pMin );
Vec_IntFree( vSupp );
Temp = 0;
}
CareAve /= Gia_ManCoNum(p)/nOuts;
Gia_ManHashStop( pNew );
Gia_ManForEachCo( p, pObj, k )
pObj->Value = Gia_ManAppendCo( pNew, pObj->Value );
Gia_ObjComputeTruthTableStop( p );
Vec_IntFree( vLeaves );
Vec_WrdFreeP( &vSimI );
Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
printf( "Using patterns with count %d and higher as cares. Average care set is %8.4f %%. ", Thresh, CareAve );
Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
if ( 0 )
{
FILE * pTable = fopen( "stats.txt", "a+" );
fprintf( pTable, "%0.2f ", CareAve );
fclose( pTable );
}
ABC_FREE( pTruthsTry );
return pNew;
}
#ifdef ABC_USE_CUDD

View File

@ -520,7 +520,7 @@ static inline word Abc_TtGia6Min_rec( Gia_Man_t * p, word uF, word uR, int nVars
word uF0, uF1, uR0, uR1, uRes0, uRes1, uRes2; int i, Var, iLit0, iLit1;
assert( nVars <= 6 );
assert( (uF & uR) == 0 );
*piLit = -1;
*piLit = 0;
if ( !uF && !uR )
return TT_UNDEF;
if ( !uF && !~uR )
@ -595,7 +595,7 @@ word * Abc_TtGiaMin_rec( Gia_Man_t * p, word * pF, word * pR, int nVars, Vec_Wrd
{
int i, Entry, Var, iLit0, iLit1, nWords = Abc_TtWordNum(nVars);
word * pRes0, * pRes1, * pRes2 = Vec_WrdFetch( vMemory, nWords );
*piLit = -1;
*piLit = 0;
if ( nVars <= 6 )
{
pRes2[0] = Abc_TtGia6Min_rec( p, pF[0], pR[0], nVars, vNodes, piLit, pPerm );
@ -694,7 +694,7 @@ word * Abc_TtGiaMin_rec( Gia_Man_t * p, word * pF, word * pR, int nVars, Vec_Wrd
}
return pRes2;
}
Gia_Man_t * Abc_TtGiaMinArray( word * p, int nOuts, int nVars, int * pnNodes, int fVerbose, int * pIPerm )
Gia_Man_t * Abc_TtGiaMinArray( word * p, int nVars, int nOuts, int * pnNodes, int fVerbose, int * pIPerm )
{
Gia_Man_t * pNew, * pTemp;
int o, i, iLit, nWords = Abc_TtWordNum(nVars);
@ -743,6 +743,50 @@ Gia_Man_t * Abc_TtGiaMinArray( word * p, int nOuts, int nVars, int * pnNodes, in
Gia_ManStop( pTemp );
return pNew;
}
Gia_Man_t * Abc_TtGiaMinArrayNew( word * p, int nVars, int nOuts, int * pnNodes, int fVerbose, int * pIPerm )
{
Gia_Man_t * pNew, * pTemp;
int o, i, iLit, nWords = Abc_TtWordNum(nVars);
word * pF = ABC_ALLOC( word, nWords );
word * pR = ABC_ALLOC( word, nWords );
Vec_Wrd_t * vMemory = Vec_WrdAlloc( 100 );
Vec_Wrd_t * vNodes = Vec_WrdAlloc( 100 );
Vec_Wec_t * vNodes2 = Vec_WecStart( nVars+1 );
Vec_WrdGrow( vMemory, 1 << 20 );
pNew = Gia_ManStart( 1000 );
pNew->pName = Abc_UtilStrsav( "muxes" );
for ( i = 0; i < nVars; i++ )
Gia_ManAppendCi(pNew);
Gia_ManHashAlloc( pNew );
for ( o = 0; o < nOuts; o++ )
{
word * pCare = p + nOuts*nWords;
word * pTruth = p + o*nWords;
Abc_TtAnd( pF, pCare, pTruth, nWords, 0 );
Abc_TtSharp( pR, pCare, pTruth, nWords );
for ( i = nVars; i < 6; i++ )
assert( !Abc_Tt6HasVar(pF[0], i) && !Abc_Tt6HasVar(pR[0], i) );
Abc_TtGiaMin_rec( pNew, pF, pR, nVars, vMemory, vNodes, vNodes2, &iLit, pIPerm );
Gia_ManAppendCo( pNew, iLit );
}
if ( fVerbose )
printf( "Nodes = %5d. Nodes2 = %5d. Total = %5d. ",
Vec_WrdSize(vNodes), Vec_WecSizeSize(vNodes2), Vec_WrdSize(vNodes) + Vec_WecSizeSize(vNodes2) );
//printf( "Memory %d (Truth table %d)\n", Vec_WrdSize(vMemory), nWords );
if ( pnNodes )
*pnNodes = Vec_WrdSize(vNodes) + Vec_WecSizeSize(vNodes2);
Vec_WrdFree( vMemory );
Vec_WrdFree( vNodes );
Vec_WecFree( vNodes2 );
ABC_FREE( pF );
ABC_FREE( pR );
Gia_ManHashStop( pNew );
pNew = Gia_ManCleanup( pTemp = pNew );
Gia_ManStop( pTemp );
return pNew;
}
/**Function*************************************************************
@ -950,7 +994,7 @@ Gia_Man_t * Gia_TryPermOpt( word * pTruths, int nIns, int nOuts, int nWords, int
for ( r = 0; r < nRounds; r++ )
{
int nNodesAll = Gia_ManPermuteTreeOne( pTruthDup, nIns, nOuts, nWords, r>0, pIPerm, 0, fVerbose );
Gia_Man_t * pTemp = Abc_TtGiaMinArray( pTruthDup, nOuts, nIns, NULL, 0, pIPerm );
Gia_Man_t * pTemp = Abc_TtGiaMinArray( pTruthDup, nIns, nOuts, NULL, 0, pIPerm );
nNodes2 = Gia_ManAndNum(pTemp);
if ( nNodesBest > nNodes2 )
{
@ -975,6 +1019,45 @@ Gia_Man_t * Gia_TryPermOpt( word * pTruths, int nIns, int nOuts, int nWords, int
Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
return pBest;
}
Gia_Man_t * Gia_TryPermOptNew( word * pTruths, int nIns, int nOuts, int nWords, int nRounds, int fVerbose )
{
abctime clk = Abc_Clock();
Gia_Man_t * pTemp, * pBest = NULL;
word * pTruthDup = Abc_TtDup( pTruths, (nOuts+1)*nWords, 0 );
int pIPermBest[TREE_MAX_VARS] = {0};
int pIPerm[TREE_MAX_VARS] = {0};
int r, rBest = -1, nNodes2 = -1, nNodesBest = ABC_INFINITY;
//srand( time(NULL) );
Gia_ManRandom(1);
for ( r = 0; r < nRounds; r++ )
{
int nNodesAll = Gia_ManPermuteTreeOne( pTruthDup, nIns, nOuts, nWords, r>0, pIPerm, 0, fVerbose );
Abc_TtPermute( pTruthDup + nOuts*nWords, pIPerm, nIns );
pTemp = Abc_TtGiaMinArrayNew( pTruthDup, nIns, nOuts, NULL, 0, pIPerm );
nNodes2 = Gia_ManAndNum(pTemp);
if ( nNodesBest > nNodes2 )
{
nNodesBest = nNodes2;
memcpy( pIPermBest, pIPerm, sizeof(int)*nIns );
rBest = r;
Gia_ManStopP( &pBest );
pBest = pTemp;
pTemp = NULL;
}
Gia_ManStopP( &pTemp );
Abc_TtCopy( pTruthDup, pTruths, (nOuts+1)*nWords, 0 );
if ( fVerbose )
printf( "Permuted = %5d. AIG = %5d.\n", nNodesAll, nNodes2 );
nNodesAll = 0;
}
if ( fVerbose )
printf( "Best round %3d. Best nodes %5d. ", rBest, nNodesBest );
ABC_FREE( pTruthDup );
if ( fVerbose )
Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
return pBest;
}
/**Function*************************************************************

View File

@ -41461,6 +41461,7 @@ usage:
int Abc_CommandAbc9LNetOpt( Abc_Frame_t * pAbc, int argc, char ** argv )
{
extern Gia_Man_t * Gia_ManPerformLNetOpt( Gia_Man_t * p, int fTryNew, char * pFileName, int nIns, int nOuts, int Thresh, int nRounds, int fVerbose );
extern Gia_Man_t * Gia_ManPerformLNetOptNew( Gia_Man_t * p, char * pFileName, int nIns, int nOuts, int Thresh, int nRounds, int fVerbose );
Gia_Man_t * pTemp;
char * pFileName = NULL;
int c, fTryNew = 1, nIns = 6, nOuts = 2, Limit = 0, nRounds = 100, fVerbose = 0;
@ -41536,7 +41537,7 @@ int Abc_CommandAbc9LNetOpt( Abc_Frame_t * pAbc, int argc, char ** argv )
fclose( pFile );
pFileName = argv[globalUtilOptind];
}
pTemp = Gia_ManPerformLNetOpt( pAbc->pGia, fTryNew, pFileName, nIns, nOuts, Limit, nRounds, fVerbose );
pTemp = Gia_ManPerformLNetOptNew( pAbc->pGia, pFileName, nIns, nOuts, Limit, nRounds, fVerbose );
Abc_FrameUpdateGia( pAbc, pTemp );
return 0;

View File

@ -1663,31 +1663,6 @@ static inline void Abc_TtFlip( word * pTruth, int nWords, int iVar )
}
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline word Abc_Tt6Permute_rec( word t, int * pPerm, int nVars )
{
word uRes0, uRes1; int Var;
if ( t == 0 ) return 0;
if ( ~t == 0 ) return ~(word)0;
for ( Var = nVars-1; Var >= 0; Var-- )
if ( Abc_Tt6HasVar( t, Var ) )
break;
assert( Var >= 0 );
uRes0 = Abc_Tt6Permute_rec( Abc_Tt6Cofactor0(t, Var), pPerm, Var );
uRes1 = Abc_Tt6Permute_rec( Abc_Tt6Cofactor1(t, Var), pPerm, Var );
return (uRes0 & s_Truths6Neg[pPerm[Var]]) | (uRes1 & s_Truths6[pPerm[Var]]);
}
/**Function*************************************************************
Synopsis []
@ -1803,6 +1778,48 @@ static inline word Abc_Tt6RemoveVar( word t, int iVar )
return t;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline word Abc_Tt6Permute_rec( word t, int * pPerm, int nVars )
{
word uRes0, uRes1; int Var;
if ( t == 0 ) return 0;
if ( ~t == 0 ) return ~(word)0;
for ( Var = nVars-1; Var >= 0; Var-- )
if ( Abc_Tt6HasVar( t, Var ) )
break;
assert( Var >= 0 );
uRes0 = Abc_Tt6Permute_rec( Abc_Tt6Cofactor0(t, Var), pPerm, Var );
uRes1 = Abc_Tt6Permute_rec( Abc_Tt6Cofactor1(t, Var), pPerm, Var );
return (uRes0 & s_Truths6Neg[pPerm[Var]]) | (uRes1 & s_Truths6[pPerm[Var]]);
}
static inline void Abc_TtPermute( word * p, int * pPerm, int nVars )
{
int v, nWords = Abc_TtWordNum(nVars), Perm[16];
assert( nVars <= 16 );
for ( v = 0; v < nVars; v++ )
Perm[v] = pPerm[v];
for ( v = nVars-1; v >= 0; v-- )
{
while ( v != Perm[v] )
{
int vCur = Perm[v];
Abc_TtSwapVars( p, nVars, v, vCur );
Perm[v] = Perm[vCur];
Perm[vCur]= vCur;
}
}
}
/**Function*************************************************************
Synopsis [Support minimization.]