266 const std::vector<COMPONENT*>& aStructuralMatches,
268 std::vector<TOPOLOGY_MISMATCH_REASON>& aMismatchReasons,
269 const std::atomic<bool>* aCancelled )
271 if( aCancelled && aCancelled->load( std::memory_order_relaxed ) )
276 aMismatchReasons.clear();
277 std::vector<COMPONENT*> matches;
278 int candidatesChecked = 0;
284 double netCheckMs = 0.0;
286 for(
COMPONENT* cmpTarget : aStructuralMatches )
288 if( partialMatches.
m_locked.find( cmpTarget ) != partialMatches.
m_locked.end() )
294 cmpTarget->m_reference );
298 localReason.
m_candidate = cmpTarget->GetParent()->GetReferenceAsString();
304 netCheckMs += timerNet.
msecs();
309 matches.push_back( cmpTarget );
313 wxLogTrace(
traceTopoMatch, wxT(
"Reject [net topo mismatch]\n" ) );
314 aMismatchReasons.push_back( localReason );
320 std::unordered_map<COMPONENT*, double> simScores;
321 simScores.reserve( matches.size() );
327 for(
size_t i = 0; i < aRef->
m_pins.size(); i++ )
329 if( aRef->
m_pins[i]->GetNetCode() == match->m_pins[i]->GetNetCode() )
333 simScores[match] =
static_cast<double>( n ) /
static_cast<double>( aRef->
m_pins.size() );
336 std::sort( matches.begin(), matches.end(),
339 double simA = simScores[a];
340 double simB = simScores[b];
345 return a->GetParent()->GetReferenceAsString()
346 < b->GetParent()->GetReferenceAsString();
351 if( matches.empty() )
355 reason.
m_reason =
_(
"No compatible component found in the target area." );
357 if( aMismatchReasons.empty() )
358 aMismatchReasons.push_back( reason );
364 wxT(
" findMatch '%s' (%d pins): %s total, checked %d/%d structural, "
365 "netCheck %0.3f ms, score %0.3f ms, %d matches" ),
367 candidatesChecked, (
int) aStructuralMatches.size(),
368 netCheckMs, timerScore.
msecs(),
369 (
int) matches.size() );
410 auto getSymbolInstanceUuid =
421 const KIID& symbolUuid =
path.back();
427 const KIID refSymbolUuid = getSymbolInstanceUuid( refFp );
428 wxString candidateSymbolUuids;
429 wxString matchingSymbolCandidates;
430 int symbolUuidHitCount = 0;
431 int uniqueMatchIdx = -1;
443 for(
size_t i = 0; i < aMatches.size(); i++ )
445 FOOTPRINT* candidateFp = aMatches[i]->GetParent();
447 const KIID candidateSymbolUuid = getSymbolInstanceUuid( candidateFp );
450 candidateSymbolUuids += wxT(
", " );
452 if( candidateSymbolUuid ==
niluuid )
453 candidateSymbolUuids += candidateRef + wxT(
"=<none>" );
455 candidateSymbolUuids += candidateRef + wxT(
"=" ) + candidateSymbolUuid.
AsString();
457 if( candidateSymbolUuid == refSymbolUuid )
459 if( uniqueMatchIdx < 0 )
460 uniqueMatchIdx =
static_cast<int>( i );
462 symbolUuidHitCount++;
464 if( !matchingSymbolCandidates.IsEmpty() )
465 matchingSymbolCandidates += wxT(
", " );
467 matchingSymbolCandidates += candidateRef;
474 wxLogTrace(
traceTopoMatchDetail, wxT(
"Tie candidate symbol UUIDs: %s" ), candidateSymbolUuids );
478 if( symbolUuidHitCount == 1 )
483 std::rotate( aMatches.begin(), aMatches.begin() + uniqueMatchIdx, aMatches.begin() + uniqueMatchIdx + 1 );
491 else if( symbolUuidHitCount > 1 )
493 wxLogTrace(
traceTopoMatchDetail, wxT(
"Symbol UUID multiple matches (not usable) for %s: %s" ),
587 std::vector<TOPOLOGY_MISMATCH_REASON>& aMismatchReasons,
590 std::vector<BACKTRACK_STAGE> stack;
593 aMismatchReasons.clear();
599 int backtrackCount = 0;
600 double mrvTotalMs = 0.0;
602 std::vector<TOPOLOGY_MISMATCH_REASON> localReasons;
607 reason.
m_reason =
_(
"One or both of the areas has no components assigned." );
608 aMismatchReasons.push_back( reason );
615 reason.
m_reason =
_(
"Component count mismatch" );
616 aMismatchReasons.push_back( reason );
624 std::vector<std::vector<COMPONENT*>> structuralMatches( numRef );
629 std::vector<std::future<void>> futures;
630 futures.reserve( numRef );
632 const std::atomic<bool>* cancelled = aParams.
m_cancelled;
634 for(
size_t i = 0; i < numRef; i++ )
636 futures.emplace_back(
tp.submit_task(
637 [
this, i, aTarget, &structuralMatches, cancelled]()
639 if( cancelled && cancelled->load( std::memory_order_relaxed ) )
642 COMPONENT* ref = m_components[i];
643 TOPOLOGY_MISMATCH_REASON reason;
645 for( COMPONENT* tgt : aTarget->m_components )
647 if( ref->MatchesWith( tgt, reason ) )
648 structuralMatches[i].push_back( tgt );
653 for(
auto& f : futures )
656 timerPrecompute.
Stop();
659 wxT(
"Structural precomputation: %s (%d source x %d target)" ),
660 timerPrecompute.to_string(), (
int) numRef,
661 (
int) aTarget->m_components.size() );
663 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
666 top.m_ref = m_components.front();
669 stack.push_back(
top );
673 while( !stack.empty() )
675 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
679 auto& current = stack.back();
681 for(
auto it = current.m_locked.begin(); it != current.m_locked.end(); it++ )
683 if (it->second == current.m_ref)
685 wxLogTrace(
traceTopoMatch, wxT(
"stk: Remove %s from locked\n" ),
686 current.m_ref->m_reference );
687 current.m_locked.erase( it );
692 if( nloops >= c_ITER_LIMIT )
697 reason.
m_reason =
_(
"Iteration count exceeded (timeout)" );
699 if( aMismatchReasons.empty() )
700 aMismatchReasons.push_back( reason );
702 aMismatchReasons.insert( aMismatchReasons.begin(), reason );
707 if( current.m_currentMatch < 0 )
709 PROF_TIMER timerInitMatch;
711 localReasons.clear();
712 current.m_matches = aTarget->findMatchingComponents(
713 current.m_ref, structuralMatches[current.m_refIndex],
714 current, localReasons, aParams.m_cancelled );
716 timerInitMatch.
Stop();
719 wxT(
"iter %d: initial match for '%s' (%d pins): %s, %d candidates" ),
720 nloops, current.m_ref->m_reference, current.m_ref->GetPinCount(),
721 timerInitMatch.
to_string(), (
int) current.m_matches.size() );
723 if( current.m_matches.empty() && aMismatchReasons.empty() && !localReasons.empty() )
724 aMismatchReasons = localReasons;
726 current.m_currentMatch = 0;
729 wxLogTrace(
traceTopoMatch, wxT(
"stk: Current '%s' stack %d cm %d/%d locked %d/%d\n" ),
730 current.m_ref->m_reference, (
int) stack.size(), current.m_currentMatch,
731 (
int) current.m_matches.size(), (
int) current.m_locked.size(),
732 (
int) m_components.size() );
734 if( current.m_currentMatch == 0 && current.m_matches.size() > 1 )
735 breakTie( current.m_ref, current.m_matches );
737 if ( current.m_matches.empty() )
739 wxLogTrace(
traceTopoMatch, wxT(
"stk: No matches at all, going up [level=%d]\n" ),
740 (
int) stack.size() );
746 if( current.m_currentMatch >= 0
747 &&
static_cast<size_t>( current.m_currentMatch ) >= current.m_matches.size() )
749 wxLogTrace(
traceTopoMatch, wxT(
"stk: No more matches, going up [level=%d]\n" ),
750 (
int) stack.size() );
756 auto& match = current.m_matches[current.m_currentMatch];
758 wxLogTrace(
traceTopoMatch, wxT(
"stk: candidate '%s', match list : ( " ),
759 current.m_matches[current.m_currentMatch]->m_reference, current.m_refIndex );
761 for(
auto m : current.m_matches )
762 wxLogTrace(
traceTopoMatch, wxT(
"%s " ), m->GetParent()->GetReferenceAsString() );
766 current.m_currentMatch++;
767 current.m_locked[match] = current.m_ref;
769 if( aParams.m_matchedComponents )
771 aParams.m_matchedComponents->store( (
int) current.m_locked.size(),
772 std::memory_order_relaxed );
775 if( current.m_locked.size() == m_components.size() )
777 current.m_nloops = nloops;
780 aMismatchReasons.clear();
782 for(
auto iter : current.m_locked )
783 aResult[ iter.second->GetParent() ] = iter.first->GetParent();
787 wxT(
"Isomorphism: %s, %d iterations, %d backtracks, "
788 "MRV total %0.1f ms (%d candidates)" ),
789 timerTotal.to_string(), nloops, backtrackCount, mrvTotalMs,
790 (
int) m_components.size() );
803 std::vector<COMPONENT*> m_matches;
804 std::vector<TOPOLOGY_MISMATCH_REASON> m_reasons;
807 std::vector<MRV_CANDIDATE> mrvCandidates;
811 std::unordered_set<COMPONENT*> lockedRefs;
812 lockedRefs.reserve( current.m_locked.size() );
814 for(
const auto& [tgt, ref] : current.m_locked )
815 lockedRefs.insert( ref );
817 for(
size_t i = 0; i < m_components.size(); i++ )
821 if( cmp != current.m_ref && lockedRefs.find( cmp ) == lockedRefs.end() )
822 mrvCandidates.push_back( { cmp, i, {}, {} } );
825 static const size_t MRV_PARALLEL_THRESHOLD = 4;
829 if( mrvCandidates.size() >= MRV_PARALLEL_THRESHOLD )
832 std::vector<std::future<void>> futures;
833 futures.reserve( mrvCandidates.size() );
835 const std::atomic<bool>* cancelled = aParams.m_cancelled;
837 for( MRV_CANDIDATE& c : mrvCandidates )
839 futures.emplace_back(
tp.submit_task(
840 [&c, aTarget, ¤t, &structuralMatches, cancelled]()
842 c.m_matches = aTarget->findMatchingComponents(
843 c.m_cmp, structuralMatches[c.m_index],
844 current, c.m_reasons, cancelled );
848 for(
auto& f : futures )
853 for( MRV_CANDIDATE& c : mrvCandidates )
855 c.m_matches = aTarget->findMatchingComponents(
856 c.m_cmp, structuralMatches[c.m_index],
857 current, c.m_reasons, aParams.m_cancelled );
862 double mrvMs = timerMrv.
msecs();
866 wxT(
"iter %d: MRV scan %0.3f ms, %d unlocked candidates" ),
867 nloops, mrvMs, (
int) mrvCandidates.size() );
869 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
872 int minMatches = std::numeric_limits<int>::max();
875 int bestRefIndex = 0;
877 std::vector<COMPONENT*> bestMatches;
879 for( MRV_CANDIDATE& c : mrvCandidates )
881 int nMatches =
static_cast<int>( c.m_matches.size() );
885 bestNextRef = c.m_cmp;
886 bestRefIndex =
static_cast<int>( c.m_index );
887 bestMatches = std::move( c.m_matches );
890 else if( nMatches == 0 )
892 altNextRef = c.m_cmp;
893 altRefIndex =
static_cast<int>( c.m_index );
895 if( aMismatchReasons.empty() && !c.m_reasons.empty() )
896 aMismatchReasons = c.m_reasons;
898 else if( nMatches < minMatches )
900 minMatches = nMatches;
901 bestNextRef = c.m_cmp;
902 bestRefIndex =
static_cast<int>( c.m_index );
903 bestMatches = std::move( c.m_matches );
912 wxT(
"iter %d: MRV picked '%s' (%d matches, best of %d)" ),
914 (
int) bestMatches.size(), (
int) mrvCandidates.size() );
916 next.m_ref = bestNextRef;
917 next.m_refIndex = bestRefIndex;
918 next.m_matches = std::move( bestMatches );
919 next.m_currentMatch = 0;
924 wxT(
"iter %d: MRV dead end, alt='%s'" ),
925 nloops, altNextRef ? altNextRef->
m_reference : wxString(
"(none)" ) );
927 next.m_ref = altNextRef;
928 next.m_refIndex = altRefIndex;
929 next.m_currentMatch = -1;
932 stack.push_back(
next );
937 wxT(
"Isomorphism: %s, %d iterations, %d backtracks, "
938 "MRV total %0.1f ms (%d candidates)" ),
939 timerTotal.to_string(), nloops, backtrackCount, mrvTotalMs,
940 (
int) m_components.size() );