262 const std::vector<COMPONENT*>& aStructuralMatches,
265 std::vector<TOPOLOGY_MISMATCH_REASON>& aMismatchReasons,
266 const std::atomic<bool>* aCancelled )
268 if( aCancelled && aCancelled->load( std::memory_order_relaxed ) )
273 aMismatchReasons.clear();
274 std::vector<COMPONENT*> matches;
275 int candidatesChecked = 0;
281 double netCheckMs = 0.0;
283 for(
COMPONENT* cmpTarget : aStructuralMatches )
285 if( partialMatches.
m_locked.find( cmpTarget ) != partialMatches.
m_locked.end() )
291 cmpTarget->m_reference );
295 localReason.
m_candidate = cmpTarget->GetParent()->GetReferenceAsString();
301 netCheckMs += timerNet.
msecs();
306 matches.push_back( cmpTarget );
310 wxLogTrace(
traceTopoMatch, wxT(
"Reject [net topo mismatch]\n" ) );
311 aMismatchReasons.push_back( localReason );
317 std::unordered_map<COMPONENT*, double> simScores;
318 simScores.reserve( matches.size() );
324 for(
size_t i = 0; i < aRef->
m_pins.size(); i++ )
326 if( aRef->
m_pins[i]->GetNetCode() == match->m_pins[i]->GetNetCode() )
330 simScores[match] =
static_cast<double>( n ) /
static_cast<double>( aRef->
m_pins.size() );
333 std::sort( matches.begin(), matches.end(),
336 double simA = simScores[a];
337 double simB = simScores[b];
342 return a->GetParent()->GetReferenceAsString()
343 < b->GetParent()->GetReferenceAsString();
348 if( matches.empty() && aMismatchReasons.empty() )
355 if( !aStructuralReason.
m_reason.IsEmpty() )
357 aMismatchReasons.push_back( aStructuralReason );
363 reason.
m_reason =
_(
"No compatible component found in the target area." );
364 aMismatchReasons.push_back( reason );
371 wxT(
" findMatch '%s' (%d pins): %s total, checked %d/%d structural, "
372 "netCheck %0.3f ms, score %0.3f ms, %d matches" ),
374 candidatesChecked, (
int) aStructuralMatches.size(),
375 netCheckMs, timerScore.
msecs(),
376 (
int) matches.size() );
417 auto getSymbolInstanceUuid =
428 const KIID& symbolUuid =
path.back();
434 const KIID refSymbolUuid = getSymbolInstanceUuid( refFp );
435 wxString candidateSymbolUuids;
436 wxString matchingSymbolCandidates;
437 int symbolUuidHitCount = 0;
438 int uniqueMatchIdx = -1;
450 for(
size_t i = 0; i < aMatches.size(); i++ )
452 FOOTPRINT* candidateFp = aMatches[i]->GetParent();
454 const KIID candidateSymbolUuid = getSymbolInstanceUuid( candidateFp );
457 candidateSymbolUuids += wxT(
", " );
459 if( candidateSymbolUuid ==
niluuid )
460 candidateSymbolUuids += candidateRef + wxT(
"=<none>" );
462 candidateSymbolUuids += candidateRef + wxT(
"=" ) + candidateSymbolUuid.
AsString();
464 if( candidateSymbolUuid == refSymbolUuid )
466 if( uniqueMatchIdx < 0 )
467 uniqueMatchIdx =
static_cast<int>( i );
469 symbolUuidHitCount++;
471 if( !matchingSymbolCandidates.IsEmpty() )
472 matchingSymbolCandidates += wxT(
", " );
474 matchingSymbolCandidates += candidateRef;
481 wxLogTrace(
traceTopoMatchDetail, wxT(
"Tie candidate symbol UUIDs: %s" ), candidateSymbolUuids );
485 if( symbolUuidHitCount == 1 )
490 std::rotate( aMatches.begin(), aMatches.begin() + uniqueMatchIdx, aMatches.begin() + uniqueMatchIdx + 1 );
498 else if( symbolUuidHitCount > 1 )
500 wxLogTrace(
traceTopoMatchDetail, wxT(
"Symbol UUID multiple matches (not usable) for %s: %s" ),
594 std::vector<TOPOLOGY_MISMATCH_REASON>& aMismatchReasons,
597 std::vector<BACKTRACK_STAGE> stack;
600 aMismatchReasons.clear();
606 int backtrackCount = 0;
607 double mrvTotalMs = 0.0;
609 std::vector<TOPOLOGY_MISMATCH_REASON> localReasons;
614 reason.
m_reason =
_(
"One or both of the areas has no components assigned." );
615 aMismatchReasons.push_back( reason );
622 reason.
m_reason =
_(
"Component count mismatch" );
623 aMismatchReasons.push_back( reason );
631 std::vector<std::vector<COMPONENT*>> structuralMatches( numRef );
636 std::vector<TOPOLOGY_MISMATCH_REASON> structuralReasons( numRef );
641 std::vector<std::future<void>> futures;
642 futures.reserve( numRef );
644 const std::atomic<bool>* cancelled = aParams.
m_cancelled;
646 for(
size_t i = 0; i < numRef; i++ )
648 futures.emplace_back(
tp.submit_task(
649 [
this, i, aTarget, &structuralMatches, &structuralReasons, cancelled]()
651 if( cancelled && cancelled->load( std::memory_order_relaxed ) )
654 COMPONENT* ref = m_components[i];
655 TOPOLOGY_MISMATCH_REASON reason;
656 TOPOLOGY_MISMATCH_REASON bestReason;
658 for( COMPONENT* tgt : aTarget->m_components )
660 if( ref->MatchesWith( tgt, reason ) )
662 structuralMatches[i].push_back( tgt );
664 else if( bestReason.m_reason.IsEmpty() || ref->IsSameKind( *tgt ) )
673 if( structuralMatches[i].empty() )
674 structuralReasons[i] = bestReason;
678 for(
auto& f : futures )
681 timerPrecompute.
Stop();
684 wxT(
"Structural precomputation: %s (%d source x %d target)" ),
685 timerPrecompute.to_string(), (
int) numRef,
686 (
int) aTarget->m_components.size() );
688 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
691 top.m_ref = m_components.front();
694 stack.push_back(
top );
698 while( !stack.empty() )
700 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
704 auto& current = stack.back();
706 for(
auto it = current.m_locked.begin(); it != current.m_locked.end(); it++ )
708 if (it->second == current.m_ref)
710 wxLogTrace(
traceTopoMatch, wxT(
"stk: Remove %s from locked\n" ),
711 current.m_ref->m_reference );
712 current.m_locked.erase( it );
717 if( nloops >= c_ITER_LIMIT )
722 reason.
m_reason =
_(
"Iteration count exceeded (timeout)" );
724 if( aMismatchReasons.empty() )
725 aMismatchReasons.push_back( reason );
727 aMismatchReasons.insert( aMismatchReasons.begin(), reason );
732 if( current.m_currentMatch < 0 )
734 PROF_TIMER timerInitMatch;
736 localReasons.clear();
737 current.m_matches = aTarget->findMatchingComponents(
738 current.m_ref, structuralMatches[current.m_refIndex],
739 structuralReasons[current.m_refIndex], current, localReasons,
740 aParams.m_cancelled );
742 timerInitMatch.
Stop();
745 wxT(
"iter %d: initial match for '%s' (%d pins): %s, %d candidates" ),
746 nloops, current.m_ref->m_reference, current.m_ref->GetPinCount(),
747 timerInitMatch.
to_string(), (
int) current.m_matches.size() );
749 if( current.m_matches.empty() && aMismatchReasons.empty() && !localReasons.empty() )
750 aMismatchReasons = localReasons;
752 current.m_currentMatch = 0;
755 wxLogTrace(
traceTopoMatch, wxT(
"stk: Current '%s' stack %d cm %d/%d locked %d/%d\n" ),
756 current.m_ref->m_reference, (
int) stack.size(), current.m_currentMatch,
757 (
int) current.m_matches.size(), (
int) current.m_locked.size(),
758 (
int) m_components.size() );
760 if( current.m_currentMatch == 0 && current.m_matches.size() > 1 )
761 breakTie( current.m_ref, current.m_matches );
763 if ( current.m_matches.empty() )
765 wxLogTrace(
traceTopoMatch, wxT(
"stk: No matches at all, going up [level=%d]\n" ),
766 (
int) stack.size() );
772 if( current.m_currentMatch >= 0
773 &&
static_cast<size_t>( current.m_currentMatch ) >= current.m_matches.size() )
775 wxLogTrace(
traceTopoMatch, wxT(
"stk: No more matches, going up [level=%d]\n" ),
776 (
int) stack.size() );
782 auto& match = current.m_matches[current.m_currentMatch];
784 wxLogTrace(
traceTopoMatch, wxT(
"stk: candidate '%s', match list : ( " ),
785 current.m_matches[current.m_currentMatch]->m_reference, current.m_refIndex );
787 for(
auto m : current.m_matches )
788 wxLogTrace(
traceTopoMatch, wxT(
"%s " ), m->GetParent()->GetReferenceAsString() );
792 current.m_currentMatch++;
793 current.m_locked[match] = current.m_ref;
795 if( aParams.m_matchedComponents )
797 aParams.m_matchedComponents->store( (
int) current.m_locked.size(),
798 std::memory_order_relaxed );
801 if( current.m_locked.size() == m_components.size() )
803 current.m_nloops = nloops;
806 aMismatchReasons.clear();
808 for(
auto iter : current.m_locked )
809 aResult[ iter.second->GetParent() ] = iter.first->GetParent();
813 wxT(
"Isomorphism: %s, %d iterations, %d backtracks, "
814 "MRV total %0.1f ms (%d candidates)" ),
815 timerTotal.to_string(), nloops, backtrackCount, mrvTotalMs,
816 (
int) m_components.size() );
829 std::vector<COMPONENT*> m_matches;
830 std::vector<TOPOLOGY_MISMATCH_REASON> m_reasons;
833 std::vector<MRV_CANDIDATE> mrvCandidates;
837 std::unordered_set<COMPONENT*> lockedRefs;
838 lockedRefs.reserve( current.m_locked.size() );
840 for(
const auto& [tgt, ref] : current.m_locked )
841 lockedRefs.insert( ref );
843 for(
size_t i = 0; i < m_components.size(); i++ )
847 if( cmp != current.m_ref && lockedRefs.find( cmp ) == lockedRefs.end() )
848 mrvCandidates.push_back( { cmp, i, {}, {} } );
851 static const size_t MRV_PARALLEL_THRESHOLD = 4;
855 if( mrvCandidates.size() >= MRV_PARALLEL_THRESHOLD )
858 std::vector<std::future<void>> futures;
859 futures.reserve( mrvCandidates.size() );
861 const std::atomic<bool>* cancelled = aParams.m_cancelled;
863 for( MRV_CANDIDATE& c : mrvCandidates )
865 futures.emplace_back(
tp.submit_task(
866 [&c, aTarget, ¤t, &structuralMatches, &structuralReasons, cancelled]()
868 c.m_matches = aTarget->findMatchingComponents(
869 c.m_cmp, structuralMatches[c.m_index],
870 structuralReasons[c.m_index], current, c.m_reasons,
875 for(
auto& f : futures )
880 for( MRV_CANDIDATE& c : mrvCandidates )
882 c.m_matches = aTarget->findMatchingComponents(
883 c.m_cmp, structuralMatches[c.m_index],
884 structuralReasons[c.m_index], current, c.m_reasons,
885 aParams.m_cancelled );
890 double mrvMs = timerMrv.
msecs();
894 wxT(
"iter %d: MRV scan %0.3f ms, %d unlocked candidates" ),
895 nloops, mrvMs, (
int) mrvCandidates.size() );
897 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
900 int minMatches = std::numeric_limits<int>::max();
903 int bestRefIndex = 0;
905 std::vector<COMPONENT*> bestMatches;
907 for( MRV_CANDIDATE& c : mrvCandidates )
909 int nMatches =
static_cast<int>( c.m_matches.size() );
913 bestNextRef = c.m_cmp;
914 bestRefIndex =
static_cast<int>( c.m_index );
915 bestMatches = std::move( c.m_matches );
918 else if( nMatches == 0 )
920 altNextRef = c.m_cmp;
921 altRefIndex =
static_cast<int>( c.m_index );
923 if( aMismatchReasons.empty() && !c.m_reasons.empty() )
924 aMismatchReasons = c.m_reasons;
926 else if( nMatches < minMatches )
928 minMatches = nMatches;
929 bestNextRef = c.m_cmp;
930 bestRefIndex =
static_cast<int>( c.m_index );
931 bestMatches = std::move( c.m_matches );
940 wxT(
"iter %d: MRV picked '%s' (%d matches, best of %d)" ),
942 (
int) bestMatches.size(), (
int) mrvCandidates.size() );
944 next.m_ref = bestNextRef;
945 next.m_refIndex = bestRefIndex;
946 next.m_matches = std::move( bestMatches );
947 next.m_currentMatch = 0;
952 wxT(
"iter %d: MRV dead end, alt='%s'" ),
953 nloops, altNextRef ? altNextRef->
m_reference : wxString(
"(none)" ) );
955 next.m_ref = altNextRef;
956 next.m_refIndex = altRefIndex;
957 next.m_currentMatch = -1;
960 stack.push_back(
next );
965 wxT(
"Isomorphism: %s, %d iterations, %d backtracks, "
966 "MRV total %0.1f ms (%d candidates)" ),
967 timerTotal.to_string(), nloops, backtrackCount, mrvTotalMs,
968 (
int) m_components.size() );