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() );
639 const std::vector<COMPONENT*>& aTargetComponents,
640 std::vector<TOPOLOGY_MISMATCH_REASON>& aMismatchReasons )
649 for(
const auto& [reference, footprint] : ref.
footprintOf )
660 reason.
m_reason = wxString::Format(
_(
"%s uses footprint '%s' in the reference area but "
661 "'%s' in the target area." ),
662 reference, footprint, other->second );
663 aMismatchReasons.push_back( reason );
667 std::vector<TOPOLOGY_MISMATCH_REASON> namedParts;
668 std::vector<TOPOLOGY_MISMATCH_REASON> countsOnly;
669 std::vector<wxString> refOnly;
670 std::vector<wxString> targetOnly;
672 const size_t maxNamed = 12;
674 auto joinParts = [](
const std::vector<wxString>& aParts )
678 for(
const wxString& part : aParts )
680 if( !joined.IsEmpty() )
681 joined += wxT(
", " );
689 for(
const auto& [footprint, count] : ref.
counts )
691 if( !target.
counts.count( footprint ) )
692 refOnly.push_back( footprint );
695 for(
const auto& [footprint, count] : target.
counts )
697 if( !ref.
counts.count( footprint ) )
698 targetOnly.push_back( footprint );
703 const bool pairedSwap = refOnly.size() == 1 && targetOnly.size() == 1
704 && ref.
partsUsing.at( refOnly.front() ).size() <= maxNamed
705 && target.
partsUsing.at( targetOnly.front() ).size() <= maxNamed;
711 wxString::Format(
_(
"Footprint '%s' in the reference area (%s) appears as "
712 "'%s' in the target area (%s)." ),
713 refOnly.front(), joinParts( ref.
partsUsing.at( refOnly.front() ) ),
714 targetOnly.front(), joinParts( target.
partsUsing.at( targetOnly.front() ) ) );
715 namedParts.push_back( reason );
718 auto reportUsage = [&](
const wxString& aFootprint,
int aRefUses,
int aTargetUses )
720 if( aRefUses == aTargetUses )
723 if( pairedSwap && ( aFootprint == refOnly.front() || aFootprint == targetOnly.front() ) )
728 const std::vector<wxString>& parts =
732 if( ( aRefUses == 0 || aTargetUses == 0 ) && parts.size() <= maxNamed )
734 const wxString named = joinParts( parts );
738 reason.
m_reason = wxString::Format(
_(
"Footprint '%s' is used by %s in the target "
739 "area but by nothing in the reference area." ),
744 reason.
m_reason = wxString::Format(
_(
"Footprint '%s' is used by %s in the "
745 "reference area but by nothing in the "
750 namedParts.push_back( reason );
754 reason.
m_reason = wxString::Format(
_(
"Footprint '%s': %d in the reference area, %d in "
755 "the target area." ),
756 aFootprint, aRefUses, aTargetUses );
757 countsOnly.push_back( reason );
761 for(
const auto& [footprint, refUses] : ref.
counts )
763 auto used = target.
counts.find( footprint );
765 reportUsage( footprint, refUses, used == target.
counts.end() ? 0 : used->second );
768 for(
const auto& [footprint, targetUses] : target.
counts )
770 if( !ref.
counts.count( footprint ) )
771 reportUsage( footprint, 0, targetUses );
774 aMismatchReasons.insert( aMismatchReasons.end(), namedParts.begin(), namedParts.end() );
775 aMismatchReasons.insert( aMismatchReasons.end(), countsOnly.begin(), countsOnly.end() );
782 std::vector<TOPOLOGY_MISMATCH_REASON>& aMismatchReasons,
785 std::vector<BACKTRACK_STAGE> stack;
788 aMismatchReasons.clear();
794 int backtrackCount = 0;
795 double mrvTotalMs = 0.0;
797 std::vector<TOPOLOGY_MISMATCH_REASON> localReasons;
804 reason.
m_reason =
_(
"Neither area has any footprints to match." );
806 reason.
m_reason =
_(
"The reference area has no footprints to match." );
808 reason.
m_reason =
_(
"The target area has no footprints to match." );
810 aMismatchReasons.push_back( reason );
817 reason.
m_reason = wxString::Format(
_(
"The reference area has %d components and the "
818 "target area has %d." ),
820 aMismatchReasons.push_back( reason );
831 std::vector<std::vector<COMPONENT*>> structuralMatches( numRef );
836 std::vector<TOPOLOGY_MISMATCH_REASON> structuralReasons( numRef );
841 std::vector<std::future<void>> futures;
842 futures.reserve( numRef );
844 const std::atomic<bool>* cancelled = aParams.
m_cancelled;
846 for(
size_t i = 0; i < numRef; i++ )
848 futures.emplace_back(
tp.submit_task(
849 [
this, i, aTarget, &structuralMatches, &structuralReasons, cancelled]()
851 if( cancelled && cancelled->load( std::memory_order_relaxed ) )
854 COMPONENT* ref = m_components[i];
855 TOPOLOGY_MISMATCH_REASON reason;
856 TOPOLOGY_MISMATCH_REASON bestReason;
859 for( COMPONENT* tgt : aTarget->m_components )
861 if( ref->MatchesWith( tgt, reason ) )
863 structuralMatches[i].push_back( tgt );
870 if( tgt->m_reference == ref->m_reference )
872 else if( ref->IsSameKind( *tgt ) )
874 else if( COMPONENT::prefixesShareCommonBase( ref->m_prefix, tgt->m_prefix ) )
877 if( rank >= bestRank )
884 if( structuralMatches[i].empty() )
885 structuralReasons[i] = bestReason;
889 for(
auto& f : futures )
892 timerPrecompute.
Stop();
895 wxT(
"Structural precomputation: %s (%d source x %d target)" ),
896 timerPrecompute.to_string(), (
int) numRef,
897 (
int) aTarget->m_components.size() );
899 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
902 top.m_ref = m_components.front();
905 stack.push_back(
top );
909 while( !stack.empty() )
911 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
915 auto& current = stack.back();
917 for(
auto it = current.m_locked.begin(); it != current.m_locked.end(); it++ )
919 if (it->second == current.m_ref)
921 wxLogTrace(
traceTopoMatch, wxT(
"stk: Remove %s from locked\n" ),
922 current.m_ref->m_reference );
923 current.m_locked.erase( it );
928 if( nloops >= c_ITER_LIMIT )
933 reason.
m_reason = wxString::Format(
_(
"Gave up after %d attempts to pair up the two "
934 "areas. Either their connections differ, or too "
935 "many components are interchangeable to tell "
939 if( aMismatchReasons.empty() )
940 aMismatchReasons.push_back( reason );
942 aMismatchReasons.insert( aMismatchReasons.begin(), reason );
947 if( current.m_currentMatch < 0 )
949 PROF_TIMER timerInitMatch;
951 localReasons.clear();
952 current.m_matches = aTarget->findMatchingComponents(
953 current.m_ref, structuralMatches[current.m_refIndex],
954 structuralReasons[current.m_refIndex], current, localReasons,
955 aParams.m_cancelled );
957 timerInitMatch.
Stop();
960 wxT(
"iter %d: initial match for '%s' (%d pins): %s, %d candidates" ),
961 nloops, current.m_ref->m_reference, current.m_ref->GetPinCount(),
962 timerInitMatch.
to_string(), (
int) current.m_matches.size() );
964 if( current.m_matches.empty() && aMismatchReasons.empty() && !localReasons.empty() )
965 aMismatchReasons = localReasons;
967 current.m_currentMatch = 0;
970 wxLogTrace(
traceTopoMatch, wxT(
"stk: Current '%s' stack %d cm %d/%d locked %d/%d\n" ),
971 current.m_ref->m_reference, (
int) stack.size(), current.m_currentMatch,
972 (
int) current.m_matches.size(), (
int) current.m_locked.size(),
973 (
int) m_components.size() );
975 if( current.m_currentMatch == 0 && current.m_matches.size() > 1 )
976 breakTie( current.m_ref, current.m_matches );
978 if ( current.m_matches.empty() )
980 wxLogTrace(
traceTopoMatch, wxT(
"stk: No matches at all, going up [level=%d]\n" ),
981 (
int) stack.size() );
987 if( current.m_currentMatch >= 0
988 &&
static_cast<size_t>( current.m_currentMatch ) >= current.m_matches.size() )
990 wxLogTrace(
traceTopoMatch, wxT(
"stk: No more matches, going up [level=%d]\n" ),
991 (
int) stack.size() );
997 auto& match = current.m_matches[current.m_currentMatch];
999 wxLogTrace(
traceTopoMatch, wxT(
"stk: candidate '%s', match list : ( " ),
1000 current.m_matches[current.m_currentMatch]->m_reference, current.m_refIndex );
1002 for(
auto m : current.m_matches )
1003 wxLogTrace(
traceTopoMatch, wxT(
"%s " ), m->GetParent()->GetReferenceAsString() );
1007 current.m_currentMatch++;
1008 current.m_locked[match] = current.m_ref;
1010 if( aParams.m_matchedComponents )
1012 aParams.m_matchedComponents->store( (
int) current.m_locked.size(),
1013 std::memory_order_relaxed );
1016 if( current.m_locked.size() == m_components.size() )
1018 current.m_nloops = nloops;
1021 aMismatchReasons.clear();
1023 for(
auto iter : current.m_locked )
1024 aResult[ iter.second->GetParent() ] = iter.first->GetParent();
1028 wxT(
"Isomorphism: %s, %d iterations, %d backtracks, "
1029 "MRV total %0.1f ms (%d candidates)" ),
1030 timerTotal.to_string(), nloops, backtrackCount, mrvTotalMs,
1031 (
int) m_components.size() );
1040 struct MRV_CANDIDATE
1044 std::vector<COMPONENT*> m_matches;
1045 std::vector<TOPOLOGY_MISMATCH_REASON> m_reasons;
1048 std::vector<MRV_CANDIDATE> mrvCandidates;
1052 std::unordered_set<COMPONENT*> lockedRefs;
1053 lockedRefs.reserve( current.m_locked.size() );
1055 for(
const auto& [tgt, ref] : current.m_locked )
1056 lockedRefs.insert( ref );
1058 for(
size_t i = 0; i < m_components.size(); i++ )
1062 if( cmp != current.m_ref && lockedRefs.find( cmp ) == lockedRefs.end() )
1063 mrvCandidates.push_back( { cmp, i, {}, {} } );
1066 static const size_t MRV_PARALLEL_THRESHOLD = 4;
1068 PROF_TIMER timerMrv;
1070 if( mrvCandidates.size() >= MRV_PARALLEL_THRESHOLD )
1073 std::vector<std::future<void>> futures;
1074 futures.reserve( mrvCandidates.size() );
1076 const std::atomic<bool>* cancelled = aParams.m_cancelled;
1078 for( MRV_CANDIDATE& c : mrvCandidates )
1080 futures.emplace_back(
tp.submit_task(
1081 [&c, aTarget, ¤t, &structuralMatches, &structuralReasons, cancelled]()
1083 c.m_matches = aTarget->findMatchingComponents(
1084 c.m_cmp, structuralMatches[c.m_index],
1085 structuralReasons[c.m_index], current, c.m_reasons,
1090 for(
auto& f : futures )
1095 for( MRV_CANDIDATE& c : mrvCandidates )
1097 c.m_matches = aTarget->findMatchingComponents(
1098 c.m_cmp, structuralMatches[c.m_index],
1099 structuralReasons[c.m_index], current, c.m_reasons,
1100 aParams.m_cancelled );
1105 double mrvMs = timerMrv.
msecs();
1106 mrvTotalMs += mrvMs;
1109 wxT(
"iter %d: MRV scan %0.3f ms, %d unlocked candidates" ),
1110 nloops, mrvMs, (
int) mrvCandidates.size() );
1112 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
1115 int minMatches = std::numeric_limits<int>::max();
1118 int bestRefIndex = 0;
1119 int altRefIndex = 0;
1120 std::vector<COMPONENT*> bestMatches;
1122 for( MRV_CANDIDATE& c : mrvCandidates )
1124 int nMatches =
static_cast<int>( c.m_matches.size() );
1128 bestNextRef = c.m_cmp;
1129 bestRefIndex =
static_cast<int>( c.m_index );
1130 bestMatches = std::move( c.m_matches );
1133 else if( nMatches == 0 )
1135 altNextRef = c.m_cmp;
1136 altRefIndex =
static_cast<int>( c.m_index );
1138 if( aMismatchReasons.empty() && !c.m_reasons.empty() )
1139 aMismatchReasons = c.m_reasons;
1141 else if( nMatches < minMatches )
1143 minMatches = nMatches;
1144 bestNextRef = c.m_cmp;
1145 bestRefIndex =
static_cast<int>( c.m_index );
1146 bestMatches = std::move( c.m_matches );
1155 wxT(
"iter %d: MRV picked '%s' (%d matches, best of %d)" ),
1157 (
int) bestMatches.size(), (
int) mrvCandidates.size() );
1159 next.m_ref = bestNextRef;
1160 next.m_refIndex = bestRefIndex;
1161 next.m_matches = std::move( bestMatches );
1162 next.m_currentMatch = 0;
1167 wxT(
"iter %d: MRV dead end, alt='%s'" ),
1168 nloops, altNextRef ? altNextRef->
m_reference : wxString(
"(none)" ) );
1170 next.m_ref = altNextRef;
1171 next.m_refIndex = altRefIndex;
1172 next.m_currentMatch = -1;
1175 stack.push_back(
next );
1180 wxT(
"Isomorphism: %s, %d iterations, %d backtracks, "
1181 "MRV total %0.1f ms (%d candidates)" ),
1182 timerTotal.to_string(), nloops, backtrackCount, mrvTotalMs,
1183 (
int) m_components.size() );