KiCad PCB EDA Suite
Loading...
Searching...
No Matches
topo_match.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <cstdio>
25#include <cstdlib>
26#include <cmath>
27#include <string>
28#include <vector>
29#include <algorithm>
30#include <cassert>
31#include <future>
32#include <map>
33#include <set>
34#include <unordered_map>
35#include <unordered_set>
36
37#include <thread_pool.h>
38#include <cctype>
39
40#include <core/profile.h>
41#include <pad.h>
42#include <footprint.h>
43#include <refdes_utils.h>
44#include <board.h>
45#include <wx/string.h>
46#include <wx/log.h>
47
48#include "topo_match.h"
49
50
51static const wxString traceTopoMatch = wxT( "TOPO_MATCH" );
52static const wxString traceTopoMatchDetail = wxT( "TOPO_MATCH_DETAIL" );
53
54
55namespace TMATCH
56{
57
58bool PIN::IsIsomorphic( const PIN& b, TOPOLOGY_MISMATCH_REASON& aReason ) const
59{
60 if( m_conns.size() != b.m_conns.size() )
61 {
62 wxLogTrace( traceTopoMatch,
63 wxT( "[conns mismatch n1 %d n2 %d c-ref %d c-other %d thispin %s-%s "
64 "otherpin %s-%s" ),
66 b.m_netcode,
67 (int) m_conns.size(),
68 (int) b.m_conns.size(),
69 m_parent->m_reference,
70 m_ref,
72 b.m_ref );
73
74 aReason.m_reference = m_parent->GetParent()->GetReferenceAsString();
76 aReason.m_reason = wxString::Format(
77 _( "Pad %s of %s connects to %lu pads, but candidate pad %s of %s connects to %lu." ), m_ref,
78 aReason.m_reference, static_cast<unsigned long>( m_conns.size() ), b.m_ref, aReason.m_candidate,
79 static_cast<unsigned long>( b.m_conns.size() ) );
80
81 for( auto c : m_conns )
82 {
83 wxLogTrace( traceTopoMatch, wxT( "%s-%s " ), c->m_parent->m_reference, c->m_ref );
84 }
85
86 wxLogTrace( traceTopoMatch, wxT( "||" ) );
87
88 for( auto c : b.m_conns )
89 {
90 wxLogTrace( traceTopoMatch, wxT( "%s-%s " ), c->m_parent->m_reference, c->m_ref );
91 }
92
93
94 wxLogTrace( traceTopoMatch, wxT( "] " ) );
95 return false;
96 }
97
98 if( m_conns.empty() )
99 {
100 wxLogTrace( traceTopoMatch, wxT( "[conns empty]" ) );
101 return true;
102 }
103
104 std::vector<bool> matches( m_conns.size() );
105
106 for( int i = 0; i < m_conns.size(); i++ )
107 matches[i] = false;
108
109 int nref = 0;
110
111 for( auto& cref : m_conns )
112 {
113 for( int i = 0; i < m_conns.size(); i++ )
114 {
115 if( b.m_conns[i]->IsTopologicallySimilar( *cref ) )
116 {
117 matches[nref] = true;
118 break;
119 }
120 }
121
122 nref++;
123 }
124
125 for( int i = 0; i < m_conns.size(); i++ )
126 {
127 if( !matches[i] )
128 {
129 aReason.m_reference = m_parent->GetParent()->GetReferenceAsString();
131 aReason.m_reason = wxString::Format(
132 _( "Pad %s of %s cannot match candidate pad %s of %s due to differing connectivity." ), m_ref,
133 aReason.m_reference, b.m_ref, aReason.m_candidate );
134
135 return false;
136 }
137 }
138
139 return true;
140}
141
142
143std::unordered_map<int, int> buildBaseNetMapping( const BACKTRACK_STAGE& aMatches )
144{
145 std::unordered_map<int, int> mapping;
146 mapping.reserve( aMatches.GetMatchingComponentPairs().size() * 4 );
147
148 for( const auto& [tgtCmp, refCmp] : aMatches.GetMatchingComponentPairs() )
149 {
150 auto& refPins = refCmp->Pins();
151 auto& tgtPins = tgtCmp->Pins();
152
153 for( size_t i = 0; i < refPins.size() && i < tgtPins.size(); i++ )
154 mapping[refPins[i]->GetNetCode()] = tgtPins[i]->GetNetCode();
155 }
156
157 return mapping;
158}
159
160
161bool checkCandidateNetConsistency( const std::unordered_map<int, int>& aBaseMapping,
162 COMPONENT* aRef, COMPONENT* aTgt,
163 TOPOLOGY_MISMATCH_REASON& aReason )
164{
165 if( aRef->Pins().size() != aTgt->Pins().size() )
166 {
167 aReason.m_reference = aRef->GetParent()->GetReferenceAsString();
168 aReason.m_candidate = aTgt->GetParent()->GetReferenceAsString();
169 aReason.m_reason =
170 wxString::Format( _( "Component %s expects %lu matching pads but candidate %s provides %lu." ),
171 aReason.m_reference, static_cast<unsigned long>( aRef->Pins().size() ),
172 aReason.m_candidate, static_cast<unsigned long>( aTgt->Pins().size() ) );
173 return false;
174 }
175
176 // Track net mappings introduced by this candidate's pins that aren't yet in the
177 // base mapping. Two pins sharing the same ref net must map to the same target net.
178 std::unordered_map<int, int> candidateAdditions;
179
180 for( size_t i = 0; i < aRef->Pins().size(); i++ )
181 {
182 int refNet = aRef->Pins()[i]->GetNetCode();
183 int tgtNet = aTgt->Pins()[i]->GetNetCode();
184
185 auto baseIt = aBaseMapping.find( refNet );
186
187 if( baseIt != aBaseMapping.end() )
188 {
189 if( baseIt->second != tgtNet )
190 {
191 wxLogTrace( traceTopoMatch, wxT( "nets inconsistent\n" ) );
192
193 aReason.m_reference = aRef->GetParent()->GetReferenceAsString();
194 aReason.m_candidate = aTgt->GetParent()->GetReferenceAsString();
195
196 wxString refNetName;
197 wxString tgtNetName;
198
199 if( const BOARD* board = aRef->GetParent()->GetBoard() )
200 {
201 if( const NETINFO_ITEM* net = board->FindNet( refNet ) )
202 refNetName = net->GetNetname();
203 }
204
205 if( const BOARD* board = aTgt->GetParent()->GetBoard() )
206 {
207 if( const NETINFO_ITEM* net = board->FindNet( tgtNet ) )
208 tgtNetName = net->GetNetname();
209 }
210
211 if( refNetName.IsEmpty() )
212 refNetName = wxString::Format( _( "net %d" ), refNet );
213
214 if( tgtNetName.IsEmpty() )
215 tgtNetName = wxString::Format( _( "net %d" ), tgtNet );
216
217 aReason.m_reason = wxString::Format(
218 _( "Pad %s of %s is on net %s but its match in candidate %s is on net %s." ),
219 aRef->Pins()[i]->GetReference(), aReason.m_reference, refNetName,
220 aReason.m_candidate, tgtNetName );
221
222 return false;
223 }
224
225 continue;
226 }
227
228 auto localIt = candidateAdditions.find( refNet );
229
230 if( localIt != candidateAdditions.end() )
231 {
232 if( localIt->second != tgtNet )
233 {
234 wxLogTrace( traceTopoMatch, wxT( "nets inconsistent (candidate internal)\n" ) );
235
236 aReason.m_reference = aRef->GetParent()->GetReferenceAsString();
237 aReason.m_candidate = aTgt->GetParent()->GetReferenceAsString();
238 aReason.m_reason = wxString::Format(
239 _( "Pad %s of %s has inconsistent net mapping in candidate %s." ),
240 aRef->Pins()[i]->GetReference(), aReason.m_reference,
241 aReason.m_candidate );
242
243 return false;
244 }
245
246 continue;
247 }
248
249 candidateAdditions[refNet] = tgtNet;
250 }
251
252 return true;
253}
254
255
256std::vector<COMPONENT*>
258 const std::vector<COMPONENT*>& aStructuralMatches,
259 const BACKTRACK_STAGE& partialMatches,
260 std::vector<TOPOLOGY_MISMATCH_REASON>& aMismatchReasons,
261 const std::atomic<bool>* aCancelled )
262{
263 if( aCancelled && aCancelled->load( std::memory_order_relaxed ) )
264 return {};
265
266 PROF_TIMER timerFmc;
267
268 aMismatchReasons.clear();
269 std::vector<COMPONENT*> matches;
270 int candidatesChecked = 0;
271 int skippedLocked = 0;
272
273 // Build the net consistency map from locked pairs once for this entire evaluation
274 // pass, rather than rebuilding it from scratch for every candidate.
275 std::unordered_map<int, int> baseNetMapping = buildBaseNetMapping( partialMatches );
276
277 double netCheckMs = 0.0;
278
279 for( COMPONENT* cmpTarget : aStructuralMatches )
280 {
281 if( partialMatches.m_locked.find( cmpTarget ) != partialMatches.m_locked.end() )
282 {
283 skippedLocked++;
284 continue;
285 }
286
287 candidatesChecked++;
288
289 wxLogTrace( traceTopoMatch, wxT( "Check '%s'/'%s' " ), aRef->m_reference,
290 cmpTarget->m_reference );
291
292 TOPOLOGY_MISMATCH_REASON localReason;
293 localReason.m_reference = aRef->GetParent()->GetReferenceAsString();
294 localReason.m_candidate = cmpTarget->GetParent()->GetReferenceAsString();
295
296 PROF_TIMER timerNet;
297 bool netResult = checkCandidateNetConsistency( baseNetMapping, aRef, cmpTarget, localReason );
298 timerNet.Stop();
299 netCheckMs += timerNet.msecs();
300
301 if( netResult )
302 {
303 wxLogTrace( traceTopoMatch, wxT( "match!\n" ) );
304 matches.push_back( cmpTarget );
305 }
306 else
307 {
308 wxLogTrace( traceTopoMatch, wxT( "Reject [net topo mismatch]\n" ) );
309 aMismatchReasons.push_back( localReason );
310 }
311 }
312
313 PROF_TIMER timerScore;
314
315 std::unordered_map<COMPONENT*, double> simScores;
316 simScores.reserve( matches.size() );
317
318 for( COMPONENT* match : matches )
319 {
320 int n = 0;
321
322 for( size_t i = 0; i < aRef->m_pins.size(); i++ )
323 {
324 if( aRef->m_pins[i]->GetNetCode() == match->m_pins[i]->GetNetCode() )
325 n++;
326 }
327
328 simScores[match] = static_cast<double>( n ) / static_cast<double>( aRef->m_pins.size() );
329 }
330
331 std::sort( matches.begin(), matches.end(),
332 [&]( COMPONENT* a, COMPONENT* b ) -> bool
333 {
334 double simA = simScores[a];
335 double simB = simScores[b];
336
337 if( simA != simB )
338 return simA > simB;
339
340 return a->GetParent()->GetReferenceAsString()
341 < b->GetParent()->GetReferenceAsString();
342 } );
343
344 timerScore.Stop();
345
346 if( matches.empty() )
347 {
349 reason.m_reference = aRef->GetParent()->GetReferenceAsString();
350 reason.m_reason = _( "No compatible component found in the target area." );
351
352 if( aMismatchReasons.empty() )
353 aMismatchReasons.push_back( reason );
354 }
355
356 timerFmc.Stop();
357
358 wxLogTrace( traceTopoMatchDetail,
359 wxT( " findMatch '%s' (%d pins): %s total, checked %d/%d structural, "
360 "netCheck %0.3f ms, score %0.3f ms, %d matches" ),
361 aRef->m_reference, aRef->GetPinCount(), timerFmc.to_string(),
362 candidatesChecked, (int) aStructuralMatches.size(),
363 netCheckMs, timerScore.msecs(),
364 (int) matches.size() );
365
366 return matches;
367}
368
369
370void CONNECTION_GRAPH::breakTie( COMPONENT* aRef, std::vector<COMPONENT*>& aMatches ) const
371{
372 if( aMatches.size() <= 1 )
373 return;
374
375 wxString candidateRefs;
376
377 for( size_t i = 0; i < aMatches.size(); i++ )
378 {
379 if( i > 0 )
380 candidateRefs += wxT( ", " );
381
382 candidateRefs += aMatches[i]->GetParent()->GetReferenceAsString();
383 }
384
385 wxLogTrace( traceTopoMatch, wxT( "Topology tie for %s: %s" ),
386 aRef->GetParent()->GetReferenceAsString(), candidateRefs );
387
388 if( breakTieBySymbolUuid( aRef, aMatches ) )
389 {
390 wxLogTrace( traceTopoMatchDetail, wxT( "Broke tie with symbol UUID match for %s" ),
391 aRef->GetParent()->GetReferenceAsString() );
392 }
393 // TODO: other tie breakers can be added, e.g. based on position or reference designators,
394 // just waiting for actual user test cases
395 else
396 {
397 wxLogTrace( traceTopoMatchDetail, wxT( "No tie breakers worked for %s, leaving match order alone." ),
398 aRef->GetParent()->GetReferenceAsString() );
399 }
400}
401
402
403bool CONNECTION_GRAPH::breakTieBySymbolUuid( COMPONENT* aRef, std::vector<COMPONENT*>& aMatches ) const
404{
405 auto getSymbolInstanceUuid =
406 []( const FOOTPRINT* aFootprint ) -> KIID
407 {
408 if( !aFootprint )
409 return niluuid;
410
411 const KIID_PATH& path = aFootprint->GetPath();
412
413 if( path.empty() )
414 return niluuid;
415
416 const KIID& symbolUuid = path.back();
417
418 return symbolUuid;
419 };
420
421 FOOTPRINT* refFp = aRef ? aRef->GetParent() : nullptr;
422 const KIID refSymbolUuid = getSymbolInstanceUuid( refFp );
423 wxString candidateSymbolUuids;
424 wxString matchingSymbolCandidates;
425 int symbolUuidHitCount = 0;
426 int uniqueMatchIdx = -1;
427
428 if( refSymbolUuid == niluuid )
429 {
430 wxLogTrace( traceTopoMatchDetail, wxT( "Tie symbol UUID unavailable for %s" ),
431 refFp ? refFp->GetReferenceAsString() : wxString( wxT( "<null>" ) ) );
432 return false;
433 }
434
435 // Inspect every tied candidate and collect:
436 // 1) a detailed ref->symbol UUID mapping string for traces, and
437 // 2) the subset of candidates whose symbol-path tail UUID matches the reference.
438 for( size_t i = 0; i < aMatches.size(); i++ )
439 {
440 FOOTPRINT* candidateFp = aMatches[i]->GetParent();
441 const wxString candidateRef = candidateFp->GetReferenceAsString();
442 const KIID candidateSymbolUuid = getSymbolInstanceUuid( candidateFp );
443
444 if( i > 0 )
445 candidateSymbolUuids += wxT( ", " );
446
447 if( candidateSymbolUuid == niluuid )
448 candidateSymbolUuids += candidateRef + wxT( "=<none>" );
449 else
450 candidateSymbolUuids += candidateRef + wxT( "=" ) + candidateSymbolUuid.AsString();
451
452 if( candidateSymbolUuid == refSymbolUuid )
453 {
454 if( uniqueMatchIdx < 0 )
455 uniqueMatchIdx = static_cast<int>( i );
456
457 symbolUuidHitCount++;
458
459 if( !matchingSymbolCandidates.IsEmpty() )
460 matchingSymbolCandidates += wxT( ", " );
461
462 matchingSymbolCandidates += candidateRef;
463 }
464 }
465
466 wxLogTrace( traceTopoMatchDetail, wxT( "Tie reference symbol UUID for %s: %s (hits=%d)" ),
467 refFp->GetReferenceAsString(), refSymbolUuid.AsString(), symbolUuidHitCount );
468
469 wxLogTrace( traceTopoMatchDetail, wxT( "Tie candidate symbol UUIDs: %s" ), candidateSymbolUuids );
470
471 // One match is what we want, we should have one match between the source symbol instance
472 // and the destination only since in theory we are repeating across two instances of the same sheet
473 if( symbolUuidHitCount == 1 )
474 {
475 wxLogTrace( traceTopoMatchDetail, wxT( "Symbol UUID unique match (usable) for %s: %s" ),
476 refFp->GetReferenceAsString(), matchingSymbolCandidates );
477
478 std::rotate( aMatches.begin(), aMatches.begin() + uniqueMatchIdx, aMatches.begin() + uniqueMatchIdx + 1 );
479
480 wxLogTrace( traceTopoMatchDetail, wxT( "Applied symbol UUID tie-break for %s: selected %s" ),
481 refFp->GetReferenceAsString(), aMatches.front()->GetParent()->GetReferenceAsString() );
482
483 return true;
484 }
485 // Copy and pasting footprints can result in multiple matches
486 else if( symbolUuidHitCount > 1 )
487 {
488 wxLogTrace( traceTopoMatchDetail, wxT( "Symbol UUID multiple matches (not usable) for %s: %s" ),
489 refFp->GetReferenceAsString(), matchingSymbolCandidates );
490 return false;
491 }
492 // Probably not sheet instances, break the tie some other way
493 else
494 {
495 wxLogTrace( traceTopoMatchDetail, wxT( "No symbol UUID candidate match (not usable) for %s" ),
496 refFp->GetReferenceAsString() );
497 return false;
498 }
499}
500
501
503{
504 std::sort( m_pins.begin(), m_pins.end(),
505 []( PIN* a, PIN* b )
506 {
507 return a->GetReference() < b->GetReference();
508 } );
509}
510
511
513{
514 std::sort( m_components.begin(), m_components.end(),
515 []( COMPONENT* a, COMPONENT* b )
516 {
517 if( a->GetPinCount() != b->GetPinCount() )
518 return a->GetPinCount() > b->GetPinCount();
519
520 return a->GetParent()->GetReferenceAsString() < b->GetParent()->GetReferenceAsString();
521 } );
522}
523
524
526{
527 std::map<int, std::vector<PIN*>> nets;
528
530
531 for( auto c : m_components )
532 {
533 c->sortPinsByName();
534
535 for( auto p : c->Pins() )
536 {
537 if( p->GetNetCode() > 0 )
538 nets[p->GetNetCode()].push_back( p );
539 }
540 }
541
542 for( auto& [netcode, pins] : nets )
543 {
544 wxLogTrace( traceTopoMatch, wxT( "net %d: %d connections\n" ), netcode,
545 (int) pins.size() );
546
547 for( PIN* p : pins )
548 {
549 p->m_conns.reserve( pins.size() - 1 );
550
551 for( PIN* p2 : pins )
552 {
553 if( p != p2 )
554 p->m_conns.push_back( p2 );
555 }
556 }
557 }
558
559/* for( auto c : m_components )
560 for( auto p : c->Pins() )
561 {
562 printf("pin %s: \n", p->m_ref.c_str().AsChar() );
563
564 for( auto c : p->m_conns )
565 printf( "%s ", c->m_ref.c_str().AsChar() );
566 printf("\n");
567 }
568 */
569}
570
571
573 std::vector<TOPOLOGY_MISMATCH_REASON>& aMismatchReasons,
574 const ISOMORPHISM_PARAMS& aParams )
575{
576 std::vector<BACKTRACK_STAGE> stack;
578
579 aMismatchReasons.clear();
580
581 if( aParams.m_totalComponents )
582 aParams.m_totalComponents->store( (int) m_components.size(), std::memory_order_relaxed );
583
584 PROF_TIMER timerTotal;
585 int backtrackCount = 0;
586 double mrvTotalMs = 0.0;
587
588 std::vector<TOPOLOGY_MISMATCH_REASON> localReasons;
589
590 if( m_components.empty()|| aTarget->m_components.empty() )
591 {
593 reason.m_reason = _( "One or both of the areas has no components assigned." );
594 aMismatchReasons.push_back( reason );
595 return false;
596 }
597
598 if( m_components.size() != aTarget->m_components.size() )
599 {
601 reason.m_reason = _( "Component count mismatch" );
602 aMismatchReasons.push_back( reason );
603 return false;
604 }
605
606 // Structural compatibility (MatchesWith) depends only on pin count, footprint ID, and
607 // pin connection topology -- all immutable graph properties. Precompute it once per
608 // source component so the backtracking loop never repeats these comparisons.
609 size_t numRef = m_components.size();
610 std::vector<std::vector<COMPONENT*>> structuralMatches( numRef );
611
612 PROF_TIMER timerPrecompute;
613 {
615 std::vector<std::future<void>> futures;
616 futures.reserve( numRef );
617
618 const std::atomic<bool>* cancelled = aParams.m_cancelled;
619
620 for( size_t i = 0; i < numRef; i++ )
621 {
622 futures.emplace_back( tp.submit_task(
623 [this, i, aTarget, &structuralMatches, cancelled]()
624 {
625 if( cancelled && cancelled->load( std::memory_order_relaxed ) )
626 return;
627
628 COMPONENT* ref = m_components[i];
629 TOPOLOGY_MISMATCH_REASON reason;
630
631 for( COMPONENT* tgt : aTarget->m_components )
632 {
633 if( ref->MatchesWith( tgt, reason ) )
634 structuralMatches[i].push_back( tgt );
635 }
636 } ) );
637 }
638
639 for( auto& f : futures )
640 f.wait();
641 }
642 timerPrecompute.Stop();
643
644 wxLogTrace( traceTopoMatchDetail,
645 wxT( "Structural precomputation: %s (%d source x %d target)" ),
646 timerPrecompute.to_string(), (int) numRef,
647 (int) aTarget->m_components.size() );
648
649 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
650 return false;
651
652 top.m_ref = m_components.front();
653 top.m_refIndex = 0;
654
655 stack.push_back( top );
656
657 bool matchFound = false;
658 int nloops = 0;
659
660 while( !stack.empty() )
661 {
662 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
663 return false;
664
665 nloops++;
666 auto& current = stack.back();
667
668 for( auto it = current.m_locked.begin(); it != current.m_locked.end(); it++ )
669 {
670 if (it->second == current.m_ref)
671 {
672 wxLogTrace( traceTopoMatch, wxT( "stk: Remove %s from locked\n" ),
673 current.m_ref->m_reference );
674 current.m_locked.erase( it );
675 break;
676 }
677 }
678
679 if( nloops >= c_ITER_LIMIT )
680 {
681 wxLogTrace( traceTopoMatch, wxT( "stk: Iter cnt exceeded\n" ) );
682
684 reason.m_reason = _( "Iteration count exceeded (timeout)" );
685
686 if( aMismatchReasons.empty() )
687 aMismatchReasons.push_back( reason );
688 else
689 aMismatchReasons.insert( aMismatchReasons.begin(), reason );
690
691 return false;
692 }
693
694 if( current.m_currentMatch < 0 )
695 {
696 PROF_TIMER timerInitMatch;
697
698 localReasons.clear();
699 current.m_matches = aTarget->findMatchingComponents(
700 current.m_ref, structuralMatches[current.m_refIndex],
701 current, localReasons, aParams.m_cancelled );
702
703 timerInitMatch.Stop();
704
705 wxLogTrace( traceTopoMatchDetail,
706 wxT( "iter %d: initial match for '%s' (%d pins): %s, %d candidates" ),
707 nloops, current.m_ref->m_reference, current.m_ref->GetPinCount(),
708 timerInitMatch.to_string(), (int) current.m_matches.size() );
709
710 if( current.m_matches.empty() && aMismatchReasons.empty() && !localReasons.empty() )
711 aMismatchReasons = localReasons;
712
713 current.m_currentMatch = 0;
714 }
715
716 wxLogTrace( traceTopoMatch, wxT( "stk: Current '%s' stack %d cm %d/%d locked %d/%d\n" ),
717 current.m_ref->m_reference, (int) stack.size(), current.m_currentMatch,
718 (int) current.m_matches.size(), (int) current.m_locked.size(),
719 (int) m_components.size() );
720
721 if( current.m_currentMatch == 0 && current.m_matches.size() > 1 )
722 breakTie( current.m_ref, current.m_matches );
723
724 if ( current.m_matches.empty() )
725 {
726 wxLogTrace( traceTopoMatch, wxT( "stk: No matches at all, going up [level=%d]\n" ),
727 (int) stack.size() );
728 stack.pop_back();
729 backtrackCount++;
730 continue;
731 }
732
733 if( current.m_currentMatch >= 0 && current.m_currentMatch >= current.m_matches.size() )
734 {
735 wxLogTrace( traceTopoMatch, wxT( "stk: No more matches, going up [level=%d]\n" ),
736 (int) stack.size() );
737 stack.pop_back();
738 backtrackCount++;
739 continue;
740 }
741
742 auto& match = current.m_matches[current.m_currentMatch];
743
744 wxLogTrace( traceTopoMatch, wxT( "stk: candidate '%s', match list : ( " ),
745 current.m_matches[current.m_currentMatch]->m_reference, current.m_refIndex );
746
747 for( auto m : current.m_matches )
748 wxLogTrace( traceTopoMatch, wxT( "%s " ), m->GetParent()->GetReferenceAsString() );
749
750 wxLogTrace( traceTopoMatch, wxT( "\n" ) );
751
752 current.m_currentMatch++;
753 current.m_locked[match] = current.m_ref;
754
755 if( aParams.m_matchedComponents )
756 {
757 aParams.m_matchedComponents->store( (int) current.m_locked.size(),
758 std::memory_order_relaxed );
759 }
760
761 if( current.m_locked.size() == m_components.size() )
762 {
763 current.m_nloops = nloops;
764
765 aResult.clear();
766 aMismatchReasons.clear();
767
768 for( auto iter : current.m_locked )
769 aResult[ iter.second->GetParent() ] = iter.first->GetParent();
770
771 timerTotal.Stop();
772 wxLogTrace( traceTopoMatch,
773 wxT( "Isomorphism: %s, %d iterations, %d backtracks, "
774 "MRV total %0.1f ms (%d candidates)" ),
775 timerTotal.to_string(), nloops, backtrackCount, mrvTotalMs,
776 (int) m_components.size() );
777
778 return true;
779 }
780
781
782 // MRV heuristic: find the unlocked component with the fewest candidate matches.
783 // Collect unlocked components, then evaluate them in parallel since each
784 // findMatchingComponents call is independent (read-only on graphs and current stage).
785 struct MRV_CANDIDATE
786 {
787 COMPONENT* m_cmp;
788 size_t m_index;
789 std::vector<COMPONENT*> m_matches;
790 std::vector<TOPOLOGY_MISMATCH_REASON> m_reasons;
791 };
792
793 std::vector<MRV_CANDIDATE> mrvCandidates;
794
795 // Build a set of all ref-components already locked so we can skip them in O(1)
796 // instead of scanning m_locked values for each component.
797 std::unordered_set<COMPONENT*> lockedRefs;
798 lockedRefs.reserve( current.m_locked.size() );
799
800 for( const auto& [tgt, ref] : current.m_locked )
801 lockedRefs.insert( ref );
802
803 for( size_t i = 0; i < m_components.size(); i++ )
804 {
805 COMPONENT* cmp = m_components[i];
806
807 if( cmp != current.m_ref && lockedRefs.find( cmp ) == lockedRefs.end() )
808 mrvCandidates.push_back( { cmp, i, {}, {} } );
809 }
810
811 static const size_t MRV_PARALLEL_THRESHOLD = 4;
812
813 PROF_TIMER timerMrv;
814
815 if( mrvCandidates.size() >= MRV_PARALLEL_THRESHOLD )
816 {
818 std::vector<std::future<void>> futures;
819 futures.reserve( mrvCandidates.size() );
820
821 const std::atomic<bool>* cancelled = aParams.m_cancelled;
822
823 for( MRV_CANDIDATE& c : mrvCandidates )
824 {
825 futures.emplace_back( tp.submit_task(
826 [&c, aTarget, &current, &structuralMatches, cancelled]()
827 {
828 c.m_matches = aTarget->findMatchingComponents(
829 c.m_cmp, structuralMatches[c.m_index],
830 current, c.m_reasons, cancelled );
831 } ) );
832 }
833
834 for( auto& f : futures )
835 f.wait();
836 }
837 else
838 {
839 for( MRV_CANDIDATE& c : mrvCandidates )
840 {
841 c.m_matches = aTarget->findMatchingComponents(
842 c.m_cmp, structuralMatches[c.m_index],
843 current, c.m_reasons, aParams.m_cancelled );
844 }
845 }
846
847 timerMrv.Stop();
848 double mrvMs = timerMrv.msecs();
849 mrvTotalMs += mrvMs;
850
851 wxLogTrace( traceTopoMatchDetail,
852 wxT( "iter %d: MRV scan %0.3f ms, %d unlocked candidates" ),
853 nloops, mrvMs, (int) mrvCandidates.size() );
854
855 if( aParams.m_cancelled && aParams.m_cancelled->load( std::memory_order_relaxed ) )
856 return false;
857
858 int minMatches = std::numeric_limits<int>::max();
859 COMPONENT* altNextRef = nullptr;
860 COMPONENT* bestNextRef = nullptr;
861 int bestRefIndex = 0;
862 int altRefIndex = 0;
863 std::vector<COMPONENT*> bestMatches;
864
865 for( MRV_CANDIDATE& c : mrvCandidates )
866 {
867 int nMatches = static_cast<int>( c.m_matches.size() );
868
869 if( nMatches == 1 )
870 {
871 bestNextRef = c.m_cmp;
872 bestRefIndex = static_cast<int>( c.m_index );
873 bestMatches = std::move( c.m_matches );
874 break;
875 }
876 else if( nMatches == 0 )
877 {
878 altNextRef = c.m_cmp;
879 altRefIndex = static_cast<int>( c.m_index );
880
881 if( aMismatchReasons.empty() && !c.m_reasons.empty() )
882 aMismatchReasons = c.m_reasons;
883 }
884 else if( nMatches < minMatches )
885 {
886 minMatches = nMatches;
887 bestNextRef = c.m_cmp;
888 bestRefIndex = static_cast<int>( c.m_index );
889 bestMatches = std::move( c.m_matches );
890 }
891 }
892
893 BACKTRACK_STAGE next( current );
894
895 if( bestNextRef )
896 {
897 wxLogTrace( traceTopoMatchDetail,
898 wxT( "iter %d: MRV picked '%s' (%d matches, best of %d)" ),
899 nloops, bestNextRef->m_reference,
900 (int) bestMatches.size(), (int) mrvCandidates.size() );
901
902 next.m_ref = bestNextRef;
903 next.m_refIndex = bestRefIndex;
904 next.m_matches = std::move( bestMatches );
905 next.m_currentMatch = 0;
906 }
907 else
908 {
909 wxLogTrace( traceTopoMatchDetail,
910 wxT( "iter %d: MRV dead end, alt='%s'" ),
911 nloops, altNextRef ? altNextRef->m_reference : wxString( "(none)" ) );
912
913 next.m_ref = altNextRef;
914 next.m_refIndex = altRefIndex;
915 next.m_currentMatch = -1;
916 }
917
918 stack.push_back( next );
919 };
920
921 timerTotal.Stop();
922 wxLogTrace( traceTopoMatch,
923 wxT( "Isomorphism: %s, %d iterations, %d backtracks, "
924 "MRV total %0.1f ms (%d candidates)" ),
925 timerTotal.to_string(), nloops, backtrackCount, mrvTotalMs,
926 (int) m_components.size() );
927
928 return false;
929}
930
931
932#if 0
933int main()
934{
935 FILE * f = fopen("connectivity.dump","rb" );
936 auto cgRef = loadCGraph(f);
937 auto cgTarget = loadCGraph(f);
938
939 cgRef->buildConnectivity();
940 cgTarget->buildConnectivity();
941
942 int attempts = 0;
943 int max_loops = 0;
944
945 for( ;; )
946 {
947 cgRef->shuffle();
948 cgTarget->shuffle();
949
950 const BacktrackStage latest = cgRef->matchCGraphs( cgTarget );
951
952 if( !latest.locked.size() )
953 {
954 printf("MATCH FAIL\n");
955 break;
956 }
957
958 //printf("loops: %d\n", latest.nloops );
959 //printf("Locked: %d\n", latest.locked.size() );
960
961 //if (matchFound)
962 //{
963 // for( auto& iter : latest.locked )
964 //{
965 // printf("%-10s : %-10s\n", iter.first->reference.c_str(), iter.second->reference.c_str() );
966 //}
967
968 //}
969
970 if( latest.nloops > max_loops )
971 {
972 max_loops = latest.nloops;
973 }
974
975 if (attempts % 10000 == 0)
976 {
977 printf("attempts: %d maxloops: %d\n", attempts, max_loops );
978 }
979
980 attempts++;
981
982 }
983
984 fclose(f);
985
986 return 0;
987}
988
989#endif
990
991
992COMPONENT::COMPONENT( const wxString& aRef, FOOTPRINT* aParentFp,
993 std::optional<VECTOR2I> aRaOffset ) :
994 m_reference( aRef ),
995 m_parentFootprint( aParentFp ), m_raOffset( aRaOffset )
996{
998}
999
1000
1001bool COMPONENT::isChannelSuffix( const wxString& aSuffix )
1002{
1003 if( aSuffix.IsEmpty() )
1004 return true;
1005
1006 for( wxUniChar ch : aSuffix )
1007 {
1008 if( std::isalpha( static_cast<int>( ch ) ) )
1009 return false;
1010 }
1011
1012 return true;
1013}
1014
1015
1016bool COMPONENT::prefixesShareCommonBase( const wxString& aPrefixA, const wxString& aPrefixB )
1017{
1018 if( aPrefixA == aPrefixB )
1019 return true;
1020
1021 size_t commonLen = 0;
1022 size_t minLen = std::min( aPrefixA.length(), aPrefixB.length() );
1023
1024 while( commonLen < minLen && aPrefixA[commonLen] == aPrefixB[commonLen] )
1025 commonLen++;
1026
1027 if( commonLen == 0 )
1028 return false;
1029
1030 wxString suffixA = aPrefixA.Mid( commonLen );
1031 wxString suffixB = aPrefixB.Mid( commonLen );
1032
1033 return isChannelSuffix( suffixA ) && isChannelSuffix( suffixB );
1034}
1035
1036
1037bool COMPONENT::IsSameKind( const COMPONENT& b ) const
1038{
1040 return false;
1041
1042 return ( m_parentFootprint->GetFPID() == b.m_parentFootprint->GetFPID() )
1043 || ( m_parentFootprint->GetFPID().empty() && b.m_parentFootprint->GetFPID().empty() );
1044}
1045
1046
1048{
1049 m_pins.push_back( aPin );
1050 aPin->SetParent( this );
1051}
1052
1053
1055{
1056 if( GetPinCount() != b->GetPinCount() )
1057 {
1059 aReason.m_candidate = b->GetParent()->GetReferenceAsString();
1060 aReason.m_reason =
1061 wxString::Format( _( "Component %s has %d pads but candidate %s has %d." ), aReason.m_reference,
1062 GetPinCount(), aReason.m_candidate, b->GetPinCount() );
1063 return false;
1064 }
1065
1066 if( !IsSameKind( *b ) )
1067 {
1069 aReason.m_candidate = b->GetParent()->GetReferenceAsString();
1070
1072 {
1073 aReason.m_reason = wxString::Format(
1074 _( "Reference prefix mismatch: %s uses prefix '%s' but candidate %s uses '%s'." ),
1075 aReason.m_reference, m_prefix, aReason.m_candidate, b->m_prefix );
1076 }
1077 else
1078 {
1079 wxString refFootprint = GetParent()->GetFPIDAsString();
1080 wxString candFootprint = b->GetParent()->GetFPIDAsString();
1081
1082 if( refFootprint.IsEmpty() )
1083 refFootprint = _( "(no library ID)" );
1084
1085 if( candFootprint.IsEmpty() )
1086 candFootprint = _( "(no library ID)" );
1087
1088 aReason.m_reason =
1089 wxString::Format( _( "Library link mismatch: %s expects '%s' but candidate %s is '%s'." ),
1090 aReason.m_reference, refFootprint, aReason.m_candidate, candFootprint );
1091 }
1092
1093 return false;
1094 }
1095
1096 for( int pin = 0; pin < b->GetPinCount(); pin++ )
1097 {
1098 if( !b->m_pins[pin]->IsIsomorphic( *m_pins[pin], aReason ) )
1099 {
1100 if( aReason.m_reason.IsEmpty() )
1101 {
1103 aReason.m_candidate = b->GetParent()->GetReferenceAsString();
1104 aReason.m_reason = wxString::Format( _( "Component pads differ between %s and %s." ),
1105 aReason.m_reference, aReason.m_candidate );
1106 }
1107
1108 return false;
1109 }
1110
1111 }
1112
1113 return true;
1114}
1115
1116
1118{
1119 auto cmp = new COMPONENT( aFp->GetReference(), aFp );
1120
1121 for( auto pad : aFp->Pads() )
1122 {
1123 auto pin = new PIN( );
1124 pin->m_netcode = pad->GetNetCode();
1125 pin->m_ref = pad->GetNumber();
1126 cmp->AddPin( pin );
1127 }
1128
1129 m_components.push_back( cmp );
1130}
1131
1132
1133std::unique_ptr<CONNECTION_GRAPH>
1134CONNECTION_GRAPH::BuildFromFootprintSet( const std::set<FOOTPRINT*>& aFps )
1135{
1136 auto cgraph = std::make_unique<CONNECTION_GRAPH>();
1137 VECTOR2I ref(0, 0);
1138
1139 if( aFps.size() > 0 )
1140 ref = (*aFps.begin())->GetPosition();
1141
1142 for( auto fp : aFps )
1143 {
1144 cgraph->AddFootprint( fp, fp->GetPosition() - ref );
1145 }
1146
1147 cgraph->BuildConnectivity();
1148
1149 return std::move(cgraph);
1150}
1151
1152
1157
1158
1160{
1161 for( COMPONENT* fp : m_components )
1162 {
1163 delete fp;
1164 }
1165}
1166
1167
1169{
1170 for( PIN* p : m_pins )
1171 {
1172 delete p;
1173 }
1174}
1175
1176
1177}; // namespace TMATCH
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
COMPONENT(const LIB_ID &aFPID, const wxString &aReference, const wxString &aValue, const KIID_PATH &aPath, const std::vector< KIID > &aKiids)
std::deque< PAD * > & Pads()
Definition footprint.h:306
wxString GetFPIDAsString() const
Definition footprint.h:357
const LIB_ID & GetFPID() const
Definition footprint.h:351
wxString GetReferenceAsString() const
Definition footprint.h:760
const wxString & GetReference() const
Definition footprint.h:751
Definition kiid.h:49
wxString AsString() const
Definition kiid.cpp:244
bool empty() const
Definition lib_id.h:193
Handle the data for a net.
Definition netinfo.h:54
A small class to help profiling.
Definition profile.h:49
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:88
std::string to_string()
Definition profile.h:155
double msecs(bool aSinceLast=false)
Definition profile.h:149
const std::unordered_map< COMPONENT *, COMPONENT * > & GetMatchingComponentPairs() const
Definition topo_match.h:170
std::unordered_map< COMPONENT *, COMPONENT * > m_locked
Definition topo_match.h:180
static bool prefixesShareCommonBase(const wxString &aPrefixA, const wxString &aPrefixB)
Check if two prefixes share a common starting sequence.
bool IsSameKind(const COMPONENT &b) const
int GetPinCount() const
Definition topo_match.h:72
COMPONENT(const wxString &aRef, FOOTPRINT *aParentFp, std::optional< VECTOR2I > aRaOffset=std::optional< VECTOR2I >())
FOOTPRINT * m_parentFootprint
Definition topo_match.h:103
std::optional< VECTOR2I > m_raOffset
Definition topo_match.h:100
bool MatchesWith(COMPONENT *b, TOPOLOGY_MISMATCH_REASON &aDetail)
void AddPin(PIN *p)
std::vector< PIN * > & Pins()
Definition topo_match.h:74
friend class PIN
Definition topo_match.h:63
wxString m_reference
Definition topo_match.h:101
static bool isChannelSuffix(const wxString &aSuffix)
Check if a suffix looks like a channel identifier.
std::vector< PIN * > m_pins
Definition topo_match.h:104
FOOTPRINT * GetParent() const
Definition topo_match.h:75
bool FindIsomorphism(CONNECTION_GRAPH *target, COMPONENT_MATCHES &result, std::vector< TOPOLOGY_MISMATCH_REASON > &aFailureDetails, const ISOMORPHISM_PARAMS &aParams={})
std::vector< COMPONENT * > findMatchingComponents(COMPONENT *ref, const std::vector< COMPONENT * > &aStructuralMatches, const BACKTRACK_STAGE &partialMatches, std::vector< TOPOLOGY_MISMATCH_REASON > &aFailureDetails, const std::atomic< bool > *aCancelled=nullptr)
std::vector< COMPONENT * > m_components
Definition topo_match.h:224
void AddFootprint(FOOTPRINT *aFp, const VECTOR2I &aOffset)
bool breakTieBySymbolUuid(COMPONENT *aRef, std::vector< COMPONENT * > &aMatches) const
The most useful tie breaker is based on symbol/sheet instances, since multiple channels in a design a...
static std::unique_ptr< CONNECTION_GRAPH > BuildFromFootprintSet(const std::set< FOOTPRINT * > &aFps)
void breakTie(COMPONENT *aRef, std::vector< COMPONENT * > &aMatches) const
Many times components are electrically/topologically identical, e.g.
std::vector< PIN * > m_conns
Definition topo_match.h:144
void SetParent(COMPONENT *parent)
Definition topo_match.h:115
COMPONENT * m_parent
Definition topo_match.h:143
bool IsIsomorphic(const PIN &b, TOPOLOGY_MISMATCH_REASON &aDetail) const
wxString m_ref
Definition topo_match.h:141
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
KIID niluuid(0)
std::unordered_map< int, int > buildBaseNetMapping(const BACKTRACK_STAGE &aMatches)
std::map< FOOTPRINT *, FOOTPRINT * > COMPONENT_MATCHES
Definition topo_match.h:184
bool checkCandidateNetConsistency(const std::unordered_map< int, int > &aBaseMapping, COMPONENT *aRef, COMPONENT *aTgt, TOPOLOGY_MISMATCH_REASON &aReason)
wxString GetRefDesPrefix(const wxString &aRefDes)
Get the (non-numeric) prefix from a refdes - e.g.
CITER next(CITER it)
Definition ptree.cpp:124
Collection of utility functions for component reference designators (refdes)
std::atomic< bool > * m_cancelled
Definition topo_match.h:49
std::atomic< int > * m_totalComponents
Definition topo_match.h:51
std::string path
KIBIS top(path, &reporter)
KIBIS_PIN * pin
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:31
static const wxString traceTopoMatchDetail
static const wxString traceTopoMatch
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695