From 0fd16edeb2705ad6751d0126300d5ee79b647cf9 Mon Sep 17 00:00:00 2001 From: chen chen <707413436@qq.com> Date: Fri, 25 Jul 2025 11:25:52 +0800 Subject: [PATCH 1/2] evluate choices used in mapping --- src/aig/gia/giaNf.c | 200 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/src/aig/gia/giaNf.c b/src/aig/gia/giaNf.c index b38093f233..52fd65c793 100644 --- a/src/aig/gia/giaNf.c +++ b/src/aig/gia/giaNf.c @@ -2161,6 +2161,45 @@ void Nf_ManFixPoDrivers( Nf_Man_t * p ) //printf( "Fixed %d PO drivers.\n", Count ); } + + +static int IsLeafInCut(int iNode, int *pLeaves, int nLeaves) +{ + int i; + for (i = 0; i < nLeaves; i++) + if (pLeaves[i] == iNode) + return 1; + return 0; +} +void Gia_MarkCutNodes_rec(Gia_Man_t *pGia, int iNode, int *pLeaves, int nLeaves) +{ + Gia_Obj_t *pObj = Gia_ManObj(pGia, iNode); + if (pObj->fMark0) + return; + pObj->fMark0 = 1; + + if (IsLeafInCut(iNode, pLeaves, nLeaves)) + return; + if (Gia_ObjIsAnd(pObj)) { + Gia_MarkCutNodes_rec(pGia, Gia_ObjFaninId0(pObj, iNode), pLeaves, nLeaves); + Gia_MarkCutNodes_rec(pGia, Gia_ObjFaninId1(pObj, iNode), pLeaves, nLeaves); + } +} + +void Gia_ClearAllMark0(Gia_Man_t *pGia) +{ + int i; + Gia_Obj_t *pObj; + Gia_ManForEachObj(pGia, pObj, i) + pObj->fMark0 = 0; +} +void Gia_ClearAllMark1(Gia_Man_t *pGia) +{ + int i; + Gia_Obj_t *pObj; + Gia_ManForEachObj(pGia, pObj, i) + pObj->fMark1 = 0; +} /**Function************************************************************* Synopsis [Dump matches.] @@ -2244,7 +2283,151 @@ void Nf_ManDumpMatches( Nf_Man_t * p ) } fclose( pFile ); } +void DumpVMappingToFile(Vec_Int_t *vMapping, const char *filename) { + FILE *file = fopen(filename, "w"); + if (!file) { + printf("Error: Unable to open file %s for writing.\n", filename); + return; + } + + fprintf(file, "vMapping contents:\n"); + for (int i = 0; i < Vec_IntSize(vMapping); i++) { + fprintf(file, "Index %d: %d\n", i, Vec_IntEntry(vMapping, i)); + } + + fclose(file); + printf("vMapping has been dumped to %s\n", filename); +} + + +void Gia_ConeMark0_rec(Gia_Man_t *pGia, int iObj) +{ + Gia_Obj_t *pObj = Gia_ManObj(pGia, iObj); + if (pObj->fMark0) return; + pObj->fMark0 = 1; + if (!Gia_ObjIsAnd(pObj) && !Gia_ObjIsCi(pObj)) + return; + if (Gia_ObjIsAnd(pObj)) { + Gia_ConeMark0_rec(pGia, Gia_ObjFaninId0(pObj, iObj)); + Gia_ConeMark0_rec(pGia, Gia_ObjFaninId1(pObj, iObj)); + } +} + +void Gia_ConeMark1_rec(Gia_Man_t *pGia, int iObj) +{ + Gia_Obj_t *pObj = Gia_ManObj(pGia, iObj); + if (pObj->fMark1) return; + pObj->fMark1 = 1; + if (!Gia_ObjIsAnd(pObj) && !Gia_ObjIsCi(pObj)) + return; + if (Gia_ObjIsAnd(pObj)) { + Gia_ConeMark1_rec(pGia, Gia_ObjFaninId0(pObj, iObj)); + Gia_ConeMark1_rec(pGia, Gia_ObjFaninId1(pObj, iObj)); + } +} + + + +void Nf_PrintChoiceSelection(Nf_Man_t *p) +{ + Nf_Mat_t * pM; + int i, c, id, * pCut; + int mappedCount=0, mark0Count=0, mark1Count=0; + if (Gia_ManHasChoices(p->pGia) != 1) { + return; + } + Gia_ClearAllMark0( p->pGia ); + Gia_ManForEachAndId( p->pGia, i ) + { + Gia_Obj_t * pObj = Gia_ManObj(p->pGia, i); + if ( Gia_ObjIsBuf(pObj) ) + { + continue; + } + for ( c = 0; c < 2; c++ ) + if ( Nf_ObjMapRefNum(p, i, c) ) + { + pM = Nf_ObjMatchBest( p, i, c ); + pCut = Nf_CutFromHandle( Nf_ObjCutSet(p, i), pM->CutH ); + int *pLeaves = Nf_CutLeaves(pCut); + int nLeaves = Nf_CutSize(pCut); + Gia_MarkCutNodes_rec(p->pGia, i, pLeaves, nLeaves); + } + } + // collect mapping nodes + Vec_Int_t *vMappedNodes = Vec_IntAlloc(Gia_ManAndNum(p->pGia)); + Gia_Obj_t *pObj; + Gia_ManForEachObj(p->pGia, pObj, i) { + if (pObj->fMark0) + Vec_IntPush(vMappedNodes, i); + } + // clear marks + Gia_ClearAllMark0(p->pGia); + Gia_ClearAllMark1(p->pGia); + + // collect vRepr and vChoice nodes + Vec_Int_t *vRepr = Vec_IntAlloc(Gia_ManChoiceNum(p->pGia)); + Vec_Int_t *vChoice = Vec_IntAlloc(Gia_ManChoiceNum(p->pGia)); + Gia_ManForEachObj(p->pGia, pObj, i) { + if (Gia_ObjIsHead(p->pGia, i)) + Vec_IntPush(vRepr, i); + } + int iHead; + Vec_IntForEachEntry(vRepr, iHead, i) { + int iNext = Gia_ObjNext(p->pGia, iHead); + while (iNext > 0) { + Vec_IntPush(vChoice, iNext); + iNext = Gia_ObjNext(p->pGia, iNext); + } + } + Vec_IntForEachEntry(vRepr, id, i) + Gia_ConeMark0_rec(p->pGia, id); + Vec_IntForEachEntry(vChoice, id, i) + Gia_ConeMark1_rec(p->pGia, id); + + Vec_IntForEachEntry(vMappedNodes, id, i) { + mappedCount++; + } + Gia_ManForEachObj(p->pGia, pObj, i) { + if (pObj->fMark0) + mark0Count++; + } + Gia_ManForEachObj(p->pGia, pObj, i) { + if (pObj->fMark1 && !pObj->fMark0) + mark1Count++; + } + + // assert(mark1Count + mark0Count == Gia_ManCiNum(p->pGia)+Gia_ManAndNum(p->pGia)); + int reprInMap = 0,choiceInMap = 0; + int mark0InMapped = 0, mark1InMapped = 0; + Gia_ManForEachObj(p->pGia, pObj, i) { + if (pObj->fMark0 && !pObj->fMark1 && Vec_IntFind(vMappedNodes, i) >= 0) { + reprInMap++; + } + if (pObj->fMark1 && !pObj->fMark0 && Vec_IntFind(vMappedNodes, i) >= 0) { + choiceInMap++; + } + } + Vec_IntForEachEntry(vMappedNodes, id, i) { + Gia_Obj_t* pObj = Gia_ManObj(p->pGia, id); + if (pObj->fMark0 && !pObj->fMark1) + mark0InMapped++; + if (pObj->fMark1 && !pObj->fMark0) + mark1InMapped++; + } + int allMarkedInMapped = mark0InMapped + mark1InMapped; + + float reprSupportRatio = (allMarkedInMapped > 0) ? (float)reprInMap / allMarkedInMapped * 100.0f : 0.0f; + float choiceSupportRatio = (allMarkedInMapped > 0) ? (float)choiceInMap / allMarkedInMapped * 100.0f : 0.0f; + + printf("vRepr unique support: %d/%d (%.2f%%), vChoice unique support: %d/%d (%.2f%%)\n", reprInMap, allMarkedInMapped, reprSupportRatio, choiceInMap, allMarkedInMapped, choiceSupportRatio); + Gia_ClearAllMark0(p->pGia); + Gia_ClearAllMark1(p->pGia); + Vec_IntFree(vRepr); + Vec_IntFree(vChoice); + Vec_IntFree(vMappedNodes); +} /**Function************************************************************* Synopsis [Deriving mapping.] @@ -2256,6 +2439,10 @@ void Nf_ManDumpMatches( Nf_Man_t * p ) SeeAlso [] ***********************************************************************/ + + + + Gia_Man_t * Nf_ManDeriveMapping( Nf_Man_t * p ) { Vec_Int_t * vMapping; @@ -2264,6 +2451,7 @@ Gia_Man_t * Nf_ManDeriveMapping( Nf_Man_t * p ) assert( p->pGia->vCellMapping == NULL ); vMapping = Vec_IntAlloc( 2*Gia_ManObjNum(p->pGia) + (int)p->pPars->Edge + (int)p->pPars->Area * 2 ); Vec_IntFill( vMapping, 2*Gia_ManObjNum(p->pGia), 0 ); + // Gia_ClearAllMark0( p->pGia ); // create CI inverters Gia_ManForEachCiId( p->pGia, Id, i ) if ( Nf_ObjMapRefNum(p, Id, 1) ) @@ -2300,10 +2488,22 @@ Gia_Man_t * Nf_ManDeriveMapping( Nf_Man_t * p ) } // assert( Vec_IntCap(vMapping) == 16 || Vec_IntSize(vMapping) == Vec_IntCap(vMapping) ); p->pGia->vCellMapping = vMapping; + // DumpVMappingToFile(vMapping, "vMapping_dump.txt"); + if(p->pPars->fVerbose) + Nf_PrintChoiceSelection(p); + + //vmapping_dump + if ( p->pPars->ZFile ) Nf_ManDumpMatches( p ); return p->pGia; } + + + + + + void Nf_ManUpdateStats( Nf_Man_t * p ) { Nf_Mat_t * pM; From c2e85b1cfa0425a02a55c8459dcd559d75febad3 Mon Sep 17 00:00:00 2001 From: chen chen <707413436@qq.com> Date: Fri, 25 Jul 2025 11:46:40 +0800 Subject: [PATCH 2/2] evluate choices used in mapping --- src/aig/gia/giaNf.c | 106 ++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 69 deletions(-) diff --git a/src/aig/gia/giaNf.c b/src/aig/gia/giaNf.c index 52fd65c793..7d044c0f75 100644 --- a/src/aig/gia/giaNf.c +++ b/src/aig/gia/giaNf.c @@ -2161,45 +2161,6 @@ void Nf_ManFixPoDrivers( Nf_Man_t * p ) //printf( "Fixed %d PO drivers.\n", Count ); } - - -static int IsLeafInCut(int iNode, int *pLeaves, int nLeaves) -{ - int i; - for (i = 0; i < nLeaves; i++) - if (pLeaves[i] == iNode) - return 1; - return 0; -} -void Gia_MarkCutNodes_rec(Gia_Man_t *pGia, int iNode, int *pLeaves, int nLeaves) -{ - Gia_Obj_t *pObj = Gia_ManObj(pGia, iNode); - if (pObj->fMark0) - return; - pObj->fMark0 = 1; - - if (IsLeafInCut(iNode, pLeaves, nLeaves)) - return; - if (Gia_ObjIsAnd(pObj)) { - Gia_MarkCutNodes_rec(pGia, Gia_ObjFaninId0(pObj, iNode), pLeaves, nLeaves); - Gia_MarkCutNodes_rec(pGia, Gia_ObjFaninId1(pObj, iNode), pLeaves, nLeaves); - } -} - -void Gia_ClearAllMark0(Gia_Man_t *pGia) -{ - int i; - Gia_Obj_t *pObj; - Gia_ManForEachObj(pGia, pObj, i) - pObj->fMark0 = 0; -} -void Gia_ClearAllMark1(Gia_Man_t *pGia) -{ - int i; - Gia_Obj_t *pObj; - Gia_ManForEachObj(pGia, pObj, i) - pObj->fMark1 = 0; -} /**Function************************************************************* Synopsis [Dump matches.] @@ -2283,23 +2244,46 @@ void Nf_ManDumpMatches( Nf_Man_t * p ) } fclose( pFile ); } -void DumpVMappingToFile(Vec_Int_t *vMapping, const char *filename) { - FILE *file = fopen(filename, "w"); - if (!file) { - printf("Error: Unable to open file %s for writing.\n", filename); - return; - } - fprintf(file, "vMapping contents:\n"); - for (int i = 0; i < Vec_IntSize(vMapping); i++) { - fprintf(file, "Index %d: %d\n", i, Vec_IntEntry(vMapping, i)); - } - fclose(file); - printf("vMapping has been dumped to %s\n", filename); + +static int IsLeafInCut(int iNode, int *pLeaves, int nLeaves) +{ + int i; + for (i = 0; i < nLeaves; i++) + if (pLeaves[i] == iNode) + return 1; + return 0; } +void Gia_MarkCutNodes_rec(Gia_Man_t *pGia, int iNode, int *pLeaves, int nLeaves) +{ + Gia_Obj_t *pObj = Gia_ManObj(pGia, iNode); + if (pObj->fMark0) + return; + pObj->fMark0 = 1; + if (IsLeafInCut(iNode, pLeaves, nLeaves)) + return; + if (Gia_ObjIsAnd(pObj)) { + Gia_MarkCutNodes_rec(pGia, Gia_ObjFaninId0(pObj, iNode), pLeaves, nLeaves); + Gia_MarkCutNodes_rec(pGia, Gia_ObjFaninId1(pObj, iNode), pLeaves, nLeaves); + } +} +void Gia_ClearAllMark0(Gia_Man_t *pGia) +{ + int i; + Gia_Obj_t *pObj; + Gia_ManForEachObj(pGia, pObj, i) + pObj->fMark0 = 0; +} +void Gia_ClearAllMark1(Gia_Man_t *pGia) +{ + int i; + Gia_Obj_t *pObj; + Gia_ManForEachObj(pGia, pObj, i) + pObj->fMark1 = 0; +} void Gia_ConeMark0_rec(Gia_Man_t *pGia, int iObj) { Gia_Obj_t *pObj = Gia_ManObj(pGia, iObj); @@ -2397,8 +2381,6 @@ void Nf_PrintChoiceSelection(Nf_Man_t *p) if (pObj->fMark1 && !pObj->fMark0) mark1Count++; } - - // assert(mark1Count + mark0Count == Gia_ManCiNum(p->pGia)+Gia_ManAndNum(p->pGia)); int reprInMap = 0,choiceInMap = 0; int mark0InMapped = 0, mark1InMapped = 0; Gia_ManForEachObj(p->pGia, pObj, i) { @@ -2439,10 +2421,6 @@ void Nf_PrintChoiceSelection(Nf_Man_t *p) SeeAlso [] ***********************************************************************/ - - - - Gia_Man_t * Nf_ManDeriveMapping( Nf_Man_t * p ) { Vec_Int_t * vMapping; @@ -2451,7 +2429,6 @@ Gia_Man_t * Nf_ManDeriveMapping( Nf_Man_t * p ) assert( p->pGia->vCellMapping == NULL ); vMapping = Vec_IntAlloc( 2*Gia_ManObjNum(p->pGia) + (int)p->pPars->Edge + (int)p->pPars->Area * 2 ); Vec_IntFill( vMapping, 2*Gia_ManObjNum(p->pGia), 0 ); - // Gia_ClearAllMark0( p->pGia ); // create CI inverters Gia_ManForEachCiId( p->pGia, Id, i ) if ( Nf_ObjMapRefNum(p, Id, 1) ) @@ -2488,22 +2465,13 @@ Gia_Man_t * Nf_ManDeriveMapping( Nf_Man_t * p ) } // assert( Vec_IntCap(vMapping) == 16 || Vec_IntSize(vMapping) == Vec_IntCap(vMapping) ); p->pGia->vCellMapping = vMapping; - // DumpVMappingToFile(vMapping, "vMapping_dump.txt"); - if(p->pPars->fVerbose) - Nf_PrintChoiceSelection(p); - - //vmapping_dump + if(p->pPars->fVerbose) + Nf_PrintChoiceSelection(p); if ( p->pPars->ZFile ) Nf_ManDumpMatches( p ); return p->pGia; } - - - - - - void Nf_ManUpdateStats( Nf_Man_t * p ) { Nf_Mat_t * pM;