From 938ae9428bcb565b94511995d2fddd49195e899a Mon Sep 17 00:00:00 2001 From: Alan Mishchenko Date: Fri, 28 Mar 2025 18:35:30 -0700 Subject: [PATCH] Extending interface of "permute". --- src/aig/saig/saigIso.c | 2 +- src/base/abc/abc.h | 2 +- src/base/abc/abcNtk.c | 175 +++++++++++++++++++++++++++-------------- src/base/abci/abc.c | 30 +++++-- src/base/io/ioResub.h | 4 +- 5 files changed, 143 insertions(+), 70 deletions(-) diff --git a/src/aig/saig/saigIso.c b/src/aig/saig/saigIso.c index 7ab8486e4..03ec21f3a 100644 --- a/src/aig/saig/saigIso.c +++ b/src/aig/saig/saigIso.c @@ -600,7 +600,7 @@ Aig_Man_t * Iso_ManTest888( Aig_Man_t * pAig1, int fVerbose ) Vec_Int_t * vMap; pNtk = Abc_NtkFromAigPhase( pAig1 ); - Abc_NtkPermute( pNtk, 1, 0, 1, NULL ); + Abc_NtkPermute( pNtk, 1, 0, 1, NULL, NULL, NULL ); pAig2 = Abc_NtkToDar( pNtk, 0, 1 ); Abc_NtkDelete( pNtk ); diff --git a/src/base/abc/abc.h b/src/base/abc/abc.h index ada1f499f..1d4bde4d7 100644 --- a/src/base/abc/abc.h +++ b/src/base/abc/abc.h @@ -792,7 +792,7 @@ extern ABC_DLL Abc_Ntk_t * Abc_NtkCreateWithNodes( Vec_Ptr_t * vSops ); extern ABC_DLL void Abc_NtkDelete( Abc_Ntk_t * pNtk ); extern ABC_DLL void Abc_NtkFixNonDrivenNets( Abc_Ntk_t * pNtk ); extern ABC_DLL void Abc_NtkMakeComb( Abc_Ntk_t * pNtk, int fRemoveLatches ); -extern ABC_DLL void Abc_NtkPermute( Abc_Ntk_t * pNtk, int fInputs, int fOutputs, int fFlops, char * pFlopPermFile ); +extern ABC_DLL void Abc_NtkPermute( Abc_Ntk_t * pNtk, int fInputs, int fOutputs, int fFlops, char * pInPermFile, char * pOutPermFile, char * pFlopPermFile ); extern ABC_DLL void Abc_NtkUnpermute( Abc_Ntk_t * pNtk ); extern ABC_DLL Abc_Ntk_t * Abc_NtkCreateFromSops( char * pName, Vec_Ptr_t * vSops ); extern ABC_DLL Abc_Ntk_t * Abc_NtkCreateFromGias( char * pName, Vec_Ptr_t * vGias, Gia_Man_t * pMulti ); diff --git a/src/base/abc/abcNtk.c b/src/base/abc/abcNtk.c index 5ea38cc12..45a479cf2 100644 --- a/src/base/abc/abcNtk.c +++ b/src/base/abc/abcNtk.c @@ -2059,11 +2059,11 @@ void Abc_NtkRemovePo( Abc_Ntk_t * pNtk, int iOutput, int fRemoveConst0 ) SeeAlso [] ***********************************************************************/ -Vec_Int_t * Abc_NtkReadFlopPerm( char * pFileName, int nFlops ) +Vec_Int_t * Abc_NtkReadSignalPerm( char * pFileName, int nSignals ) { char Buffer[1000]; FILE * pFile; - Vec_Int_t * vFlops; + Vec_Int_t * vSignals; int iFlop = -1; pFile = fopen( pFileName, "rb" ); if ( pFile == NULL ) @@ -2071,29 +2071,29 @@ Vec_Int_t * Abc_NtkReadFlopPerm( char * pFileName, int nFlops ) printf( "Cannot open input file \"%s\".\n", pFileName ); return NULL; } - vFlops = Vec_IntAlloc( nFlops ); + vSignals = Vec_IntAlloc( nSignals ); while ( fgets( Buffer, 1000, pFile ) != NULL ) { if ( Buffer[0] == ' ' || Buffer[0] == '\r' || Buffer[0] == '\n' ) continue; iFlop = atoi( Buffer ); - if ( iFlop < 0 || iFlop >= nFlops ) + if ( iFlop < 0 || iFlop >= nSignals ) { - printf( "Flop ID (%d) is out of range.\n", iFlop ); + printf( "Signal ID (%d) is out of range.\n", iFlop ); fclose( pFile ); - Vec_IntFree( vFlops ); + Vec_IntFree( vSignals ); return NULL; } - Vec_IntPush( vFlops, iFlop ); + Vec_IntPush( vSignals, iFlop ); } fclose( pFile ); - if ( Vec_IntSize(vFlops) != nFlops ) + if ( Vec_IntSize(vSignals) != nSignals ) { - printf( "The number of flops read in from file (%d) is different from the number of flops in the circuit (%d).\n", iFlop, nFlops ); - Vec_IntFree( vFlops ); + printf( "The number of indexes read in from file (%d) is different from the number of signals in the circuit (%d).\n", iFlop, nSignals ); + Vec_IntFree( vSignals ); return NULL; } - return vFlops; + return vSignals; } /**Function************************************************************* @@ -2106,61 +2106,98 @@ Vec_Int_t * Abc_NtkReadFlopPerm( char * pFileName, int nFlops ) SeeAlso [] ***********************************************************************/ -void Abc_NtkPermute( Abc_Ntk_t * pNtk, int fInputs, int fOutputs, int fFlops, char * pFlopPermFile ) +void Abc_NtkPermute( Abc_Ntk_t * pNtk, int fInputs, int fOutputs, int fFlops, char * pInPermFile, char * pOutPermFile, char * pFlopPermFile ) { Abc_Obj_t * pTemp; Vec_Int_t * vInputs, * vOutputs, * vFlops, * vTemp; int i, k, Entry; // start permutation arrays + if ( pInPermFile ) + { + vInputs = Abc_NtkReadSignalPerm( pInPermFile, Abc_NtkPiNum(pNtk) ); + if ( vInputs == NULL ) + return; + fInputs = 1; + } + else + vInputs = Vec_IntStartNatural( Abc_NtkPiNum(pNtk) ); + if ( pOutPermFile ) + { + vOutputs = Abc_NtkReadSignalPerm( pOutPermFile, Abc_NtkPoNum(pNtk) ); + if ( vOutputs == NULL ) + return; + fOutputs = 1; + } + else + vOutputs = Vec_IntStartNatural( Abc_NtkPoNum(pNtk) ); if ( pFlopPermFile ) { - vFlops = Abc_NtkReadFlopPerm( pFlopPermFile, Abc_NtkLatchNum(pNtk) ); + vFlops = Abc_NtkReadSignalPerm( pFlopPermFile, Abc_NtkLatchNum(pNtk) ); if ( vFlops == NULL ) return; - fInputs = 0; - fOutputs = 0; - fFlops = 0; + fFlops = 1; } else vFlops = Vec_IntStartNatural( Abc_NtkLatchNum(pNtk) ); - vInputs = Vec_IntStartNatural( Abc_NtkPiNum(pNtk) ); - vOutputs = Vec_IntStartNatural( Abc_NtkPoNum(pNtk) ); // permute inputs + Vec_Ptr_t * vCis = Vec_PtrDup(pNtk->vCis); + Vec_Ptr_t * vCos = Vec_PtrDup(pNtk->vCos); + Vec_Ptr_t * vFfs = Vec_PtrDup(pNtk->vBoxes); if ( fInputs ) for ( i = 0; i < Abc_NtkPiNum(pNtk); i++ ) { - k = rand() % Abc_NtkPiNum(pNtk); - // swap indexes - Entry = Vec_IntEntry( vInputs, i ); - Vec_IntWriteEntry( vInputs, i, Vec_IntEntry(vInputs, k) ); - Vec_IntWriteEntry( vInputs, k, Entry ); - // swap PIs - pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vPis, i ); - Vec_PtrWriteEntry( pNtk->vPis, i, Vec_PtrEntry(pNtk->vPis, k) ); - Vec_PtrWriteEntry( pNtk->vPis, k, pTemp ); - // swap CIs - pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vCis, i ); - Vec_PtrWriteEntry( pNtk->vCis, i, Vec_PtrEntry(pNtk->vCis, k) ); - Vec_PtrWriteEntry( pNtk->vCis, k, pTemp ); + if ( pInPermFile ) + { + k = Vec_IntEntry( vInputs, i ); + pTemp = (Abc_Obj_t *)Vec_PtrEntry( vCis, k ); + Vec_PtrWriteEntry( pNtk->vPis, i, pTemp ); + Vec_PtrWriteEntry( pNtk->vCis, i, pTemp ); + } + else + { + k = rand() % Abc_NtkPiNum(pNtk); + // swap indexes + Entry = Vec_IntEntry( vInputs, i ); + Vec_IntWriteEntry( vInputs, i, Vec_IntEntry(vInputs, k) ); + Vec_IntWriteEntry( vInputs, k, Entry ); + // swap PIs + pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vPis, i ); + Vec_PtrWriteEntry( pNtk->vPis, i, Vec_PtrEntry(pNtk->vPis, k) ); + Vec_PtrWriteEntry( pNtk->vPis, k, pTemp ); + // swap CIs + pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vCis, i ); + Vec_PtrWriteEntry( pNtk->vCis, i, Vec_PtrEntry(pNtk->vCis, k) ); + Vec_PtrWriteEntry( pNtk->vCis, k, pTemp ); + } //printf( "Swapping PIs %d and %d.\n", i, k ); } // permute outputs if ( fOutputs ) for ( i = 0; i < Abc_NtkPoNum(pNtk); i++ ) { - k = rand() % Abc_NtkPoNum(pNtk); - // swap indexes - Entry = Vec_IntEntry( vOutputs, i ); - Vec_IntWriteEntry( vOutputs, i, Vec_IntEntry(vOutputs, k) ); - Vec_IntWriteEntry( vOutputs, k, Entry ); - // swap POs - pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vPos, i ); - Vec_PtrWriteEntry( pNtk->vPos, i, Vec_PtrEntry(pNtk->vPos, k) ); - Vec_PtrWriteEntry( pNtk->vPos, k, pTemp ); - // swap COs - pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vCos, i ); - Vec_PtrWriteEntry( pNtk->vCos, i, Vec_PtrEntry(pNtk->vCos, k) ); - Vec_PtrWriteEntry( pNtk->vCos, k, pTemp ); + if ( pOutPermFile ) + { + k = Vec_IntEntry( vOutputs, i ); + pTemp = (Abc_Obj_t *)Vec_PtrEntry( vCos, k ); + Vec_PtrWriteEntry( pNtk->vPos, i, pTemp ); + Vec_PtrWriteEntry( pNtk->vCos, i, pTemp ); + } + else + { + k = rand() % Abc_NtkPoNum(pNtk); + // swap indexes + Entry = Vec_IntEntry( vOutputs, i ); + Vec_IntWriteEntry( vOutputs, i, Vec_IntEntry(vOutputs, k) ); + Vec_IntWriteEntry( vOutputs, k, Entry ); + // swap POs + pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vPos, i ); + Vec_PtrWriteEntry( pNtk->vPos, i, Vec_PtrEntry(pNtk->vPos, k) ); + Vec_PtrWriteEntry( pNtk->vPos, k, pTemp ); + // swap COs + pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vCos, i ); + Vec_PtrWriteEntry( pNtk->vCos, i, Vec_PtrEntry(pNtk->vCos, k) ); + Vec_PtrWriteEntry( pNtk->vCos, k, pTemp ); + } //printf( "Swapping POs %d and %d.\n", i, k ); } // permute flops @@ -2168,26 +2205,42 @@ void Abc_NtkPermute( Abc_Ntk_t * pNtk, int fInputs, int fOutputs, int fFlops, ch if ( fFlops ) for ( i = 0; i < Abc_NtkLatchNum(pNtk); i++ ) { - k = rand() % Abc_NtkLatchNum(pNtk); - // swap indexes - Entry = Vec_IntEntry( vFlops, i ); - Vec_IntWriteEntry( vFlops, i, Vec_IntEntry(vFlops, k) ); - Vec_IntWriteEntry( vFlops, k, Entry ); - // swap flops - pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vBoxes, i ); - Vec_PtrWriteEntry( pNtk->vBoxes, i, Vec_PtrEntry(pNtk->vBoxes, k) ); - Vec_PtrWriteEntry( pNtk->vBoxes, k, pTemp ); - // swap CIs - pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vCis, Abc_NtkPiNum(pNtk)+i ); - Vec_PtrWriteEntry( pNtk->vCis, Abc_NtkPiNum(pNtk)+i, Vec_PtrEntry(pNtk->vCis, Abc_NtkPiNum(pNtk)+k) ); - Vec_PtrWriteEntry( pNtk->vCis, Abc_NtkPiNum(pNtk)+k, pTemp ); - // swap COs - pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vCos, Abc_NtkPoNum(pNtk)+i ); - Vec_PtrWriteEntry( pNtk->vCos, Abc_NtkPoNum(pNtk)+i, Vec_PtrEntry(pNtk->vCos, Abc_NtkPoNum(pNtk)+k) ); - Vec_PtrWriteEntry( pNtk->vCos, Abc_NtkPoNum(pNtk)+k, pTemp ); + if ( pFlopPermFile ) + { + k = Vec_IntEntry( vFlops, i ); + pTemp = (Abc_Obj_t *)Vec_PtrEntry( vFfs, k ); + Vec_PtrWriteEntry( pNtk->vBoxes, i, pTemp ); + pTemp = (Abc_Obj_t *)Vec_PtrEntry( vCis, Abc_NtkPiNum(pNtk)+k ); + Vec_PtrWriteEntry( pNtk->vCis, Abc_NtkPiNum(pNtk)+i, pTemp ); + pTemp = (Abc_Obj_t *)Vec_PtrEntry( vCos, Abc_NtkPoNum(pNtk)+k ); + Vec_PtrWriteEntry( pNtk->vCos, Abc_NtkPoNum(pNtk)+i, pTemp ); + } + else + { + k = rand() % Abc_NtkLatchNum(pNtk); + // swap indexes + Entry = Vec_IntEntry( vFlops, i ); + Vec_IntWriteEntry( vFlops, i, Vec_IntEntry(vFlops, k) ); + Vec_IntWriteEntry( vFlops, k, Entry ); + // swap flops + pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vBoxes, i ); + Vec_PtrWriteEntry( pNtk->vBoxes, i, Vec_PtrEntry(pNtk->vBoxes, k) ); + Vec_PtrWriteEntry( pNtk->vBoxes, k, pTemp ); + // swap CIs + pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vCis, Abc_NtkPiNum(pNtk)+i ); + Vec_PtrWriteEntry( pNtk->vCis, Abc_NtkPiNum(pNtk)+i, Vec_PtrEntry(pNtk->vCis, Abc_NtkPiNum(pNtk)+k) ); + Vec_PtrWriteEntry( pNtk->vCis, Abc_NtkPiNum(pNtk)+k, pTemp ); + // swap COs + pTemp = (Abc_Obj_t *)Vec_PtrEntry( pNtk->vCos, Abc_NtkPoNum(pNtk)+i ); + Vec_PtrWriteEntry( pNtk->vCos, Abc_NtkPoNum(pNtk)+i, Vec_PtrEntry(pNtk->vCos, Abc_NtkPoNum(pNtk)+k) ); + Vec_PtrWriteEntry( pNtk->vCos, Abc_NtkPoNum(pNtk)+k, pTemp ); + } //printf( "Swapping flops %d and %d.\n", i, k ); } + Vec_PtrFree(vCis); + Vec_PtrFree(vCos); + Vec_PtrFree(vFfs); // invert arrays vInputs = Vec_IntInvert( vTemp = vInputs, -1 ); Vec_IntFree( vTemp ); diff --git a/src/base/abci/abc.c b/src/base/abci/abc.c index 95518cea6..75504ad91 100644 --- a/src/base/abci/abc.c +++ b/src/base/abci/abc.c @@ -26067,7 +26067,7 @@ int Abc_CommandPermute( Abc_Frame_t * pAbc, int argc, char ** argv ) extern Abc_Ntk_t * Abc_NtkRestrashRandom( Abc_Ntk_t * pNtk ); extern void Abc_NtkPermutePiUsingFanout( Abc_Ntk_t * pNtk ); Abc_Ntk_t * pNtk = pAbc->pNtkCur, * pNtkRes = NULL; - char * pFlopPermFile = NULL; + char * pFlopPermFile = NULL, * pInPermFile = NULL, * pOutPermFile = NULL; int fInputs = 1; int fOutputs = 1; int fFlops = 1; @@ -26076,7 +26076,7 @@ int Abc_CommandPermute( Abc_Frame_t * pAbc, int argc, char ** argv ) int Seed = -1; int c; Extra_UtilGetoptReset(); - while ( ( c = Extra_UtilGetopt( argc, argv, "SFiofnxh" ) ) != EOF ) + while ( ( c = Extra_UtilGetopt( argc, argv, "SIOFiofnxh" ) ) != EOF ) { switch ( c ) { @@ -26091,6 +26091,24 @@ int Abc_CommandPermute( Abc_Frame_t * pAbc, int argc, char ** argv ) if ( Seed < 0 ) goto usage; break; + case 'I': + if ( globalUtilOptind >= argc ) + { + Abc_Print( -1, "Command line switch \"-I\" should be followed by a file name.\n" ); + goto usage; + } + pInPermFile = argv[globalUtilOptind]; + globalUtilOptind++; + break; + case 'O': + if ( globalUtilOptind >= argc ) + { + Abc_Print( -1, "Command line switch \"-O\" should be followed by a file name.\n" ); + goto usage; + } + pOutPermFile = argv[globalUtilOptind]; + globalUtilOptind++; + break; case 'F': if ( globalUtilOptind >= argc ) { @@ -26153,12 +26171,12 @@ int Abc_CommandPermute( Abc_Frame_t * pAbc, int argc, char ** argv ) Abc_Print( -1, "Command \"permute\" has failed.\n" ); return 1; } - Abc_NtkPermute( pNtkRes, fInputs, fOutputs, fFlops, pFlopPermFile ); + Abc_NtkPermute( pNtkRes, fInputs, fOutputs, fFlops, pInPermFile, pOutPermFile, pFlopPermFile ); Abc_FrameReplaceCurrentNetwork( pAbc, pNtkRes ); return 0; usage: - Abc_Print( -2, "usage: permute [-S num] [-iofnxh] [-F filename]\n" ); + Abc_Print( -2, "usage: permute [-S num] [-iofnxh] [-I filename] [-O filename] [-F filename]\n" ); Abc_Print( -2, "\t performs random permutation of inputs/outputs/flops\n" ); Abc_Print( -2, "\t-S num : the random seed to generate permutations (0 <= num < INT_MAX) [default = %d]\n", Seed ); Abc_Print( -2, "\t-i : toggle permuting primary inputs [default = %s]\n", fInputs? "yes": "no" ); @@ -26167,6 +26185,8 @@ usage: Abc_Print( -2, "\t-n : toggle deriving new topological ordering of nodes [default = %s]\n", fNodes? "yes": "no" ); Abc_Print( -2, "\t-x : toggle permuting inputs based on their fanout count [default = %s]\n", fFanout? "yes": "no" ); Abc_Print( -2, "\t-h : print the command usage\n"); + Abc_Print( -2, "\t-I : (optional) file with the input permutation\n" ); + Abc_Print( -2, "\t-O : (optional) file with the output permutation\n" ); Abc_Print( -2, "\t-F : (optional) file with the flop permutation\n" ); return 1; } @@ -30881,7 +30901,7 @@ int Abc_CommandBm2( Abc_Frame_t * pAbc, int argc, char ** argv ) return 1; } - Abc_NtkPermute(pNtk2, 1, 1, 0, NULL ); + Abc_NtkPermute(pNtk2, 1, 1, 0, NULL, NULL, NULL ); Abc_NtkShortNames(pNtk2); Abc_NtkForEachCi( pNtk1, pObj, i ) { diff --git a/src/base/io/ioResub.h b/src/base/io/ioResub.h index bd5175fbe..e566a0492 100644 --- a/src/base/io/ioResub.h +++ b/src/base/io/ioResub.h @@ -282,8 +282,8 @@ static inline Abc_RData_t * Abc_RData2Rel( Abc_RData_t * p ) Vec_WrdShrink( vTransOut, p->nPats ); Vec_Wrd_t * vTransInCopy = Vec_WrdDup(vTransIn); Vec_WrdUniqify( vTransInCopy ); - if ( Vec_WrdSize(vTransInCopy) == p->nPats ) - printf( "This resub problem is not a relation.\n" ); +// if ( Vec_WrdSize(vTransInCopy) == p->nPats ) +// printf( "This resub problem is not a relation.\n" ); // create the relation Abc_RData_t * pNew = Abc_RDataStart( p->nIns, 1 << (p->nOuts-1), Vec_WrdSize(vTransInCopy) ); pNew->nOuts = p->nOuts;