ZLayout EDA Library v1.0.0
Advanced Electronic Design Automation Layout Library with Bilingual Documentation
Loading...
Searching...
No Matches
hierarchical_component_example.cpp
Go to the documentation of this file.
1
9
10#include <iostream>
11#include <memory>
12#include <vector>
13#include <chrono>
14#include <random>
15
22
23using namespace zlayout;
24using namespace zlayout::components;
25using namespace zlayout::optimization;
26using namespace zlayout::spatial;
27
32 std::cout << "\n=== Basic Components Demo ===\n";
33
34 // Create basic passive components
35 auto resistor = PassiveComponentFactory::createResistor("R1", 1000.0, 5.0);
36 resistor->setPosition(geometry::Point(0, 0));
37 resistor->setBoundingBox(geometry::Rectangle(0, 0, 2.0, 1.0));
38
39 auto capacitor = PassiveComponentFactory::createCapacitor("C1", 100e-12,
41 capacitor->setPosition(geometry::Point(10, 0));
42 capacitor->setBoundingBox(geometry::Rectangle(10, 0, 2.0, 1.0));
43
44 auto inductor = PassiveComponentFactory::createInductor("L1", 10e-9,
46 inductor->setPosition(geometry::Point(20, 0));
47 inductor->setBoundingBox(geometry::Rectangle(20, 0, 3.0, 2.0));
48
49 // Create digital components
50 auto and_gate = DigitalComponentFactory::createLogicGate("AND1",
52 and_gate->setPosition(geometry::Point(0, 10));
53 and_gate->setBoundingBox(geometry::Rectangle(0, 10, 4.0, 3.0));
54
55 auto flip_flop = DigitalComponentFactory::createDFlipFlop("FF1", true, false);
56 flip_flop->setPosition(geometry::Point(10, 10));
57 flip_flop->setBoundingBox(geometry::Rectangle(10, 10, 6.0, 4.0));
58
59 std::cout << "Created components:\n";
60 std::cout << "- " << resistor->getName() << ": " << resistor->getDescription() << "\n";
61 std::cout << "- " << capacitor->getName() << ": " << capacitor->getDescription() << "\n";
62 std::cout << "- " << inductor->getName() << ": " << inductor->getDescription() << "\n";
63 std::cout << "- " << and_gate->getName() << ": " << and_gate->getDescription() << "\n";
64 std::cout << "- " << flip_flop->getName() << ": " << flip_flop->getDescription() << "\n";
65}
66
70std::shared_ptr<Component> createALUBlock() {
71 auto alu = std::make_shared<Component>("ALU", ComponentCategory::Digital);
72 alu->setType("ArithmeticLogicUnit");
73 alu->setPosition(geometry::Point(0, 0));
74
75 // Add basic gates that make up the ALU
76 for (int i = 0; i < 8; ++i) {
77 // AND gates for bit operations
79 "AND_" + std::to_string(i), LogicGate::GateType::AND, 2);
80 and_gate->setPosition(geometry::Point(i * 5.0, 0));
81 and_gate->setBoundingBox(geometry::Rectangle(i * 5.0, 0, 4.0, 3.0));
82 alu->addChild(and_gate);
83
84 // OR gates for bit operations
86 "OR_" + std::to_string(i), LogicGate::GateType::OR, 2);
87 or_gate->setPosition(geometry::Point(i * 5.0, 5));
88 or_gate->setBoundingBox(geometry::Rectangle(i * 5.0, 5, 4.0, 3.0));
89 alu->addChild(or_gate);
90
91 // XOR gates for addition
93 "XOR_" + std::to_string(i), LogicGate::GateType::XOR, 2);
94 xor_gate->setPosition(geometry::Point(i * 5.0, 10));
95 xor_gate->setBoundingBox(geometry::Rectangle(i * 5.0, 10, 4.0, 3.0));
96 alu->addChild(xor_gate);
97 }
98
99 // Add full adders for arithmetic operations
100 for (int i = 0; i < 8; ++i) {
101 auto adder = DigitalComponentFactory::createFullAdder("ADDER_" + std::to_string(i));
102 adder->setPosition(geometry::Point(i * 8.0, 15));
103 adder->setBoundingBox(geometry::Rectangle(i * 8.0, 15, 7.0, 5.0));
104 alu->addChild(adder);
105 }
106
107 // Calculate hierarchical bounding box
108 alu->setBoundingBox(alu->calculateHierarchicalBoundingBox());
109
110 // Set timing characteristics for ALU
111 TimingInfo timing;
112 timing.propagation_delay = 2.5; // ns
113 timing.setup_time = 0.5; // ns
114 timing.hold_time = 0.2; // ns
115 alu->setTimingInfo(timing);
116
117 std::cout << "Created ALU with " << alu->getChildren().size()
118 << " sub-components\n";
119 std::cout << "ALU bounding box: " << alu->getBoundingBox().toString() << "\n";
120 std::cout << "ALU propagation delay: " << timing.propagation_delay << " ns\n";
121
122 return alu;
123}
124
128std::shared_ptr<ProcessorCore> createProcessorCore() {
129 std::cout << "\n=== Creating Processor Core ===\n";
130
131 auto cpu = IPBlockFactory::createCortexA53("CortexA53_Cluster", 4);
132
133 // Configure cache hierarchy
135 l1_config.size = 32 * 1024; // 32KB
136 l1_config.associativity = 2; // 2-way
137 l1_config.line_size = 64; // 64 bytes
138 l1_config.hit_latency = 1.0; // 1 cycle
139 l1_config.miss_latency = 10.0; // 10 cycles
140 cpu->setL1Config(l1_config);
141
143 l2_config.size = 512 * 1024; // 512KB
144 l2_config.associativity = 8; // 8-way
145 l2_config.line_size = 64; // 64 bytes
146 l2_config.hit_latency = 5.0; // 5 cycles
147 l2_config.miss_latency = 100.0; // 100 cycles
148 cpu->setL2Config(l2_config);
149
150 // Add functional units as children
151 auto alu1 = createALUBlock();
152 alu1->setPosition(geometry::Point(0, 0));
153 cpu->addChild(alu1);
154
155 auto alu2 = createALUBlock();
156 alu2->setPosition(geometry::Point(50, 0));
157 cpu->addChild(alu2);
158
159 // Add floating point unit
160 auto fpu = std::make_shared<Component>("FPU", ComponentCategory::Digital);
161 fpu->setType("FloatingPointUnit");
162 fpu->setPosition(geometry::Point(100, 0));
163 fpu->setBoundingBox(geometry::Rectangle(100, 0, 40, 30));
164 cpu->addChild(fpu);
165
166 // Set overall bounding box
167 cpu->setBoundingBox(cpu->calculateHierarchicalBoundingBox());
168
169 std::cout << "Processor core configuration:\n";
170 std::cout << "- Architecture: ARM Cortex-A53\n";
171 std::cout << "- Cores: " << cpu->getCoreCount() << "\n";
172 std::cout << "- L1 Cache: " << l1_config.size / 1024 << " KB\n";
173 std::cout << "- L2 Cache: " << l2_config.size / 1024 << " KB\n";
174 std::cout << "- Total sub-components: " << cpu->getTotalGateCount() << "\n";
175 std::cout << "- Total area: " << cpu->calculateTotalArea() << " square units\n";
176
177 return cpu;
178}
179
183std::shared_ptr<SoC> createSmartphoneSoC() {
184 std::cout << "\n=== Creating Smartphone SoC ===\n";
185
186 auto soc = std::make_shared<SoC>("SmartphoneSoC", "Custom_SoC_2nm");
187 soc->setTechNode(TechNode::um_2);
188
189 // Add processor cores
190 auto cpu_cluster = createProcessorCore();
191 cpu_cluster->setPosition(geometry::Point(0, 0));
192 soc->addProcessorCore(cpu_cluster);
193
194 // Add GPU
195 auto gpu = IPBlockFactory::createMaliG78("Mali_G78", 16);
196 gpu->setPosition(geometry::Point(200, 0));
197 gpu->setBoundingBox(geometry::Rectangle(200, 0, 80, 60));
198 soc->addGPUCore(gpu);
199
200 // Add DSP
201 auto dsp = IPBlockFactory::createHexagonDSP("Hexagon_DSP");
202 dsp->setPosition(geometry::Point(300, 0));
203 dsp->setBoundingBox(geometry::Rectangle(300, 0, 40, 40));
204 soc->addDSPCore(dsp);
205
206 // Add memory controller
207 auto memory_ctrl = IPBlockFactory::createLPDDR5Controller("LPDDR5_Ctrl", 4);
208 memory_ctrl->setPosition(geometry::Point(0, 100));
209 memory_ctrl->setBoundingBox(geometry::Rectangle(0, 100, 60, 30));
210 soc->addMemoryController(memory_ctrl);
211
212 // Add communication interfaces
213 auto usb_if = IPBlockFactory::createUSB3Interface("USB3_IF");
214 usb_if->setPosition(geometry::Point(100, 100));
215 usb_if->setBoundingBox(geometry::Rectangle(100, 100, 20, 15));
216 soc->addInterface(usb_if);
217
218 auto pcie_if = IPBlockFactory::createPCIe4Interface("PCIe4_IF");
219 pcie_if->setPosition(geometry::Point(150, 100));
220 pcie_if->setBoundingBox(geometry::Rectangle(150, 100, 25, 20));
221 soc->addInterface(pcie_if);
222
223 // Add power management
224 auto pmu = std::make_shared<PowerManagementUnit>("PMU");
225 pmu->setPosition(geometry::Point(200, 100));
226 pmu->setBoundingBox(geometry::Rectangle(200, 100, 30, 25));
227 pmu->addPowerDomain("CPU", 1.0, 2.5); // 1V, 2.5A
228 pmu->addPowerDomain("GPU", 0.9, 3.0); // 0.9V, 3A
229 pmu->addPowerDomain("DSP", 0.8, 0.5); // 0.8V, 0.5A
230 pmu->addPowerDomain("IO", 1.8, 0.3); // 1.8V, 0.3A
231 soc->addPMU(pmu);
232
233 // Calculate overall SoC metrics
234 soc->setBoundingBox(soc->calculateHierarchicalBoundingBox());
235
236 std::cout << "SoC Configuration:\n";
237 std::cout << "- Technology: 2nm\n";
238 std::cout << "- Total area: " << soc->calculateTotalArea() << " square units\n";
239 std::cout << "- Total power: " << soc->getTotalPower() << " W\n";
240 std::cout << "- Processor cores: " << cpu_cluster->getCoreCount() << "\n";
241 std::cout << "- GPU compute units: " << gpu->getComputeUnits() << "\n";
242 std::cout << "- Memory channels: " << memory_ctrl->getChannelCount() << "\n";
243 std::cout << "- Total gate count: " << soc->getTotalGateCount() << "\n";
244
245 return soc;
246}
247
252 std::cout << "\n=== Hierarchical Optimization Demo ===\n";
253
254 // Create multiple SoCs for a server design
255 std::vector<std::shared_ptr<Component>> socs;
256
257 for (int i = 0; i < 8; ++i) {
258 auto soc = createSmartphoneSoC();
259 soc->setName("SoC_" + std::to_string(i));
260
261 // Position SoCs in a grid
262 double x = (i % 4) * 400.0;
263 double y = (i / 4) * 200.0;
264 soc->setPosition(geometry::Point(x, y));
265
266 socs.push_back(soc);
267 }
268
269 std::cout << "\nCreated " << socs.size() << " SoCs for server design\n";
270
271 // Calculate total complexity
272 size_t total_components = 0;
273 double total_area = 0.0;
274
275 for (const auto& soc : socs) {
276 total_components += soc->getTotalGateCount();
277 total_area += soc->calculateTotalArea();
278 }
279
280 std::cout << "Total design complexity:\n";
281 std::cout << "- Total components: " << total_components << "\n";
282 std::cout << "- Total area: " << total_area << " square units\n";
283
284 // Demonstrate hierarchical optimization benefits
285 std::cout << "\nHierarchical Optimization Benefits:\n";
286 std::cout << "- Without hierarchy: Need to optimize " << total_components
287 << " individual components\n";
288 std::cout << "- With hierarchy: Optimize " << socs.size()
289 << " SoC blocks at top level\n";
290 std::cout << "- Complexity reduction: "
291 << (double)total_components / socs.size() << "x\n";
292
293 // Create spatial index for optimization
294 geometry::Rectangle boundary(0, 0, 1600, 400);
295 auto spatial_index = std::make_unique<HierarchicalSpatialIndex>(boundary, 16, 8);
296
297 // Insert SoCs into spatial index
298 for (const auto& soc : socs) {
299 spatial_index->insert(soc, [](std::shared_ptr<Component> comp) {
300 return comp->getBoundingBox();
301 });
302 }
303
304 std::cout << "\nSpatial indexing completed for hierarchical optimization\n";
305
306 // Demonstrate timing-driven optimization
307 auto timing_optimizer = std::make_unique<TimingDrivenOptimizer>(boundary);
308
309 // Add timing constraints
310 timing_optimizer->addTimingConstraint("CPU_to_Memory", 5.0); // 5ns max delay
311 timing_optimizer->addTimingConstraint("CPU_to_GPU", 3.0); // 3ns max delay
312 timing_optimizer->addTimingConstraint("Clock", 1.0); // 1GHz clock
313
314 std::cout << "Added timing constraints for critical paths\n";
315
316 // Run hierarchical optimization
317 auto start_time = std::chrono::high_resolution_clock::now();
318
319 for (int iteration = 0; iteration < 10; ++iteration) {
320 // At each level, optimize placement of major blocks
321 for (auto& soc : socs) {
322 geometry::Point current_pos = soc->getPosition();
323
324 // Simple optimization: minimize distance to center
325 geometry::Point center(800, 200);
326 geometry::Point direction = center - current_pos;
327 double distance = direction.magnitude();
328
329 if (distance > 1.0) {
330 direction = direction.normalize();
331 geometry::Point new_pos = current_pos + direction * 10.0;
332 soc->setPosition(new_pos);
333 }
334 }
335 }
336
337 auto end_time = std::chrono::high_resolution_clock::now();
338 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
339 end_time - start_time).count();
340
341 std::cout << "Hierarchical optimization completed in " << duration << " ms\n";
342}
343
348 std::cout << "\n=== Optimization Benchmark ===\n";
349
350 // Test different scales
351 std::vector<size_t> component_counts = {1000, 10000, 100000, 1000000};
352
353 for (size_t count : component_counts) {
354 std::cout << "\nBenchmarking with " << count << " components:\n";
355
356 // Create mock components
357 std::vector<std::shared_ptr<Component>> components;
358 std::random_device rd;
359 std::mt19937 gen(rd());
360 std::uniform_real_distribution<> pos_dist(0.0, 1000.0);
361 std::uniform_real_distribution<> size_dist(1.0, 10.0);
362
363 for (size_t i = 0; i < count; ++i) {
364 auto comp = std::make_shared<Component>("Comp_" + std::to_string(i),
366
367 double x = pos_dist(gen);
368 double y = pos_dist(gen);
369 double w = size_dist(gen);
370 double h = size_dist(gen);
371
372 comp->setPosition(geometry::Point(x, y));
373 comp->setBoundingBox(geometry::Rectangle(x, y, w, h));
374
375 components.push_back(comp);
376 }
377
378 // Benchmark flat optimization
379 auto start_time = std::chrono::high_resolution_clock::now();
380
381 geometry::Rectangle boundary(0, 0, 1000, 1000);
382 auto flat_optimizer = std::make_unique<SimulatedAnnealingOptimizer>(boundary);
383 flat_optimizer->setComponentCount(count);
384
385 // Simulate optimization iterations
386 for (int iter = 0; iter < 100; ++iter) {
387 // Mock optimization step
388 size_t idx = gen() % components.size();
389 auto& comp = components[idx];
390
391 double new_x = pos_dist(gen);
392 double new_y = pos_dist(gen);
393 comp->setPosition(geometry::Point(new_x, new_y));
394 }
395
396 auto end_time = std::chrono::high_resolution_clock::now();
397 auto flat_duration = std::chrono::duration_cast<std::chrono::microseconds>(
398 end_time - start_time).count();
399
400 // Benchmark hierarchical optimization
401 start_time = std::chrono::high_resolution_clock::now();
402
403 // Group components into hierarchical blocks
404 size_t block_size = std::min(count / 100, size_t(1000));
405 size_t block_count = (count + block_size - 1) / block_size;
406
407 std::vector<std::shared_ptr<Component>> blocks;
408 for (size_t b = 0; b < block_count; ++b) {
409 auto block = std::make_shared<Component>("Block_" + std::to_string(b),
411
412 // Add components to block
413 size_t start_idx = b * block_size;
414 size_t end_idx = std::min(start_idx + block_size, count);
415
416 for (size_t i = start_idx; i < end_idx; ++i) {
417 block->addChild(components[i]);
418 }
419
420 block->setBoundingBox(block->calculateHierarchicalBoundingBox());
421 blocks.push_back(block);
422 }
423
424 // Optimize at block level
425 auto hierarchical_optimizer = std::make_unique<HierarchicalOptimizer>(boundary);
426 hierarchical_optimizer->setHierarchyDepth(2);
427
428 for (int iter = 0; iter < 100; ++iter) {
429 // Mock block-level optimization
430 if (!blocks.empty()) {
431 size_t idx = gen() % blocks.size();
432 auto& block = blocks[idx];
433
434 double new_x = pos_dist(gen);
435 double new_y = pos_dist(gen);
436 block->setPosition(geometry::Point(new_x, new_y));
437 }
438 }
439
440 end_time = std::chrono::high_resolution_clock::now();
441 auto hierarchical_duration = std::chrono::duration_cast<std::chrono::microseconds>(
442 end_time - start_time).count();
443
444 // Report results
445 std::cout << "- Flat optimization: " << flat_duration << " μs\n";
446 std::cout << "- Hierarchical optimization: " << hierarchical_duration << " μs\n";
447 std::cout << "- Speedup: " << (double)flat_duration / hierarchical_duration << "x\n";
448 std::cout << "- Hierarchy levels: " << 2 << "\n";
449 std::cout << "- Blocks created: " << blocks.size() << "\n";
450 std::cout << "- Components per block: " << block_size << "\n";
451 }
452}
453
454int main() {
455 std::cout << "Hierarchical EDA Component System Demo\n";
456 std::cout << "======================================\n";
457
458 try {
459 // Demonstrate basic component creation
461
462 // Create and demonstrate hierarchical structures
463 auto soc = createSmartphoneSoC();
464
465 // Demonstrate hierarchical optimization
467
468 // Benchmark different approaches
470
471 std::cout << "\n=== Summary ===\n";
472 std::cout << "✓ Successfully demonstrated hierarchical component system\n";
473 std::cout << "✓ Created complex SoC from basic building blocks\n";
474 std::cout << "✓ Showed dramatic complexity reduction through hierarchy\n";
475 std::cout << "✓ Benchmarked optimization performance improvements\n";
476 std::cout << "\nKey Benefits:\n";
477 std::cout << "- Scalability: Handle billion-component designs\n";
478 std::cout << "- Modularity: Reusable IP blocks\n";
479 std::cout << "- Performance: Hierarchical optimization speedup\n";
480 std::cout << "- Maintainability: Clear component organization\n";
481
482 } catch (const std::exception& e) {
483 std::cerr << "Error: " << e.what() << std::endl;
484 return 1;
485 }
486
487 return 0;
488}
Advanced spatial indexing for ultra-large scale EDA layouts.
static std::shared_ptr< FullAdder > createFullAdder(const std::string &name)
static std::shared_ptr< DFlipFlop > createDFlipFlop(const std::string &name, bool has_reset=true, bool has_set=false)
static std::shared_ptr< LogicGate > createLogicGate(const std::string &name, LogicGate::GateType type, size_t input_count=2)
static std::shared_ptr< CommunicationInterface > createUSB3Interface(const std::string &name)
static std::shared_ptr< CommunicationInterface > createPCIe4Interface(const std::string &name)
static std::shared_ptr< MemoryController > createLPDDR5Controller(const std::string &name, size_t channels=4)
static std::shared_ptr< ProcessorCore > createCortexA53(const std::string &name, size_t cores=4)
static std::shared_ptr< DSPCore > createHexagonDSP(const std::string &name)
static std::shared_ptr< GPUCore > createMaliG78(const std::string &name, size_t compute_units=16)
static std::shared_ptr< Resistor > createResistor(const std::string &name, double resistance, double tolerance=5.0)
static std::shared_ptr< Capacitor > createCapacitor(const std::string &name, double capacitance, Capacitor::DielectricType dielectric=Capacitor::DielectricType::Ceramic)
static std::shared_ptr< Inductor > createInductor(const std::string &name, double inductance, Inductor::CoreType core=Inductor::CoreType::Air)
2D point with high-precision coordinates and utility methods
Definition point.hpp:23
Point normalize() const
Normalize vector to unit length.
Definition point.cpp:109
double magnitude() const
Calculate vector magnitude (length)
Definition point.cpp:101
Axis-aligned rectangle for bounding boxes and simple EDA components.
Definition rectangle.hpp:26
Hierarchical EDA Component System.
Digital Logic Circuit Components.
std::shared_ptr< ProcessorCore > createProcessorCore()
Create a processor core from functional blocks.
std::shared_ptr< SoC > createSmartphoneSoC()
Create a complete SoC with multiple IP blocks.
void demonstrateHierarchicalOptimization()
Demonstrate hierarchical optimization.
void demonstrateBasicComponents()
Demonstrate basic component creation and hierarchy.
std::shared_ptr< Component > createALUBlock()
Create a simple functional block (ALU) from basic components.
void benchmarkOptimizationApproaches()
Benchmark different optimization approaches.
Standard IP Blocks and Complex Digital Systems.
Advanced EDA layout optimization algorithms.
Main namespace for ZLayout library.
Definition component.hpp:20
Passive Electronic Components Implementation.
Component timing information.