Skip to content

Commit 3a3a978

Browse files
committed
Exercise 13
1 parent e79e579 commit 3a3a978

12 files changed

+1602
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.bender
2-
tmp
2+
tmp
3+
plots/
4+
__pycache__/

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ help: Makefile
156156
###########
157157
CLANG_FORMAT_EXECUTABLE ?= clang-format
158158

159+
## Automatically format the code using clang-format and black
159160
format:
160161
@echo -e "\033[1m-> Formatting Python Code...\033[0m"
161162
@black */*.py

ex13/01a_plot_area_bar.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright 2025 ETH Zurich and University of Bologna.
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Philip Wiese <[email protected]>
6+
7+
# ------------------------------------------------------------------------------
8+
# STUDENT TASK 1a:
9+
#
10+
# In this exercise, you will visualize the area breakdown of the i_croc module
11+
# using a bar plot. You will learn common tasks such as scaling axes, setting
12+
# titles, and customizing the appearance of the plot.
13+
#
14+
# The code below extracts the hierarchical area data. Your task is to visualize
15+
# the area breakdown of the i_croc module as a bar plot and customize the plot
16+
# to improve its readability.
17+
#
18+
# Follow the instructions in the code comments to complete the exercise.
19+
# ------------------------------------------------------------------------------
20+
21+
# Import the modules to extract hierarchical area data and plot the results
22+
from area import extract_areas, get_path
23+
24+
# Argparse is a module that allows you to parse command-line arguments
25+
# See https://docs.python.org/3/library/argparse.html for more information
26+
import argparse
27+
28+
# Import the matplotlib library for plotting
29+
# See https://matplotlib.org/stable/gallery/index.html for examples
30+
from matplotlib import pyplot as plt
31+
32+
# Import the os module to create directories
33+
import os
34+
35+
# IHP-130 sg13g2_stdcell library (1 GE = 7.25 um^2)
36+
# See http://eda.ee.ethz.ch/index.php?title=Ihp13#What_is_one_gate_equivalent_?
37+
AREA_PER_GE = 7.25
38+
39+
if __name__ == "__main__":
40+
# Usage: python 01a_plot_area_bar.py <area_report_file>
41+
parser = argparse.ArgumentParser(
42+
description="Extract hierarchical areas from an OpenROAD area report."
43+
)
44+
parser.add_argument("file", type=str, help="Path to the area report file")
45+
args = parser.parse_args()
46+
47+
# Extract the hierarchical area data
48+
area_tree = extract_areas(args.file)
49+
50+
if area_tree is None:
51+
print("No hierarchical area data found.")
52+
exit(-1)
53+
54+
# Extract the hierarchical area data
55+
node_i_croc = get_path(area_tree, ["<top>", "i_croc_soc", "i_croc"])
56+
57+
# Extract names and convert areas to kGE
58+
names = []
59+
areas_kge = []
60+
61+
for node in node_i_croc.children:
62+
print(node)
63+
# Convert area from um^2 to kGE
64+
area_kge = node.area / (AREA_PER_GE * 1e3)
65+
areas_kge.append(area_kge)
66+
names.append(node.name)
67+
68+
# ==========================================================================
69+
# STUDENT TASKS - MODIFY THE CODE BELOW TO COMPLETE THE EXERCISE
70+
# ==========================================================================
71+
72+
# --------------------------------------------------------------------------
73+
# STUDENT TASK 1a.1: Create a bar plot
74+
# --------------------------------------------------------------------------
75+
# Use `plt.bar(...)` to create a bar chart.
76+
#
77+
# - The x-axis should represent the component names.
78+
# - The y-axis should represent the area values.
79+
# --------------------------------------------------------------------------
80+
81+
# --------------------------------------------------------------------------
82+
# STUDENT TASK 1a.2: Improve the readability of the plot
83+
# --------------------------------------------------------------------------
84+
# Improve the visualization by:
85+
# - Rotating x-axis labels if they overlap
86+
# - Adding grid lines for better readability
87+
# - Use distinct colors for each bar
88+
#
89+
# HINT: Use `plt.xticks(...)` for better readability.
90+
# You can also use `plt.yscale('log')` if areas vary significantly.
91+
# --------------------------------------------------------------------------
92+
93+
# --------------------------------------------------------------------------
94+
# STUDENT TASK 1a.3: Remap the names of the components
95+
# --------------------------------------------------------------------------
96+
# Remap the names of the components for better readability.
97+
# Remember to move the plot commands to the correct position in the code.
98+
#
99+
# HINT: Use the `names` list to remap the names of the components.
100+
# --------------------------------------------------------------------------
101+
102+
# --------------------------------------------------------------------------
103+
# STUDENT TASK 1a.4: Save and display the plot
104+
# --------------------------------------------------------------------------
105+
# Save the figure as a high-quality PDF and display the plot.
106+
#
107+
# HINT: Use `plt.tight_layout()` to adjust the layout for better
108+
# readability.
109+
# Save the figure using `plt.savefig(...)
110+
# --------------------------------------------------------------------------

ex13/01a_plot_area_bar_sol.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Copyright 2025 ETH Zurich and University of Bologna.
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Philip Wiese <[email protected]>
6+
7+
# ------------------------------------------------------------------------------
8+
# STUDENT TASK 1a:
9+
#
10+
# In this exercise, you will visualize the area breakdown of the i_croc module
11+
# using a bar plot. You will learn common tasks such as scaling axes, setting
12+
# titles, and customizing the appearance of the plot.
13+
#
14+
# The code below extracts the hierarchical area data. Your task is to visualize
15+
# the area breakdown of the i_croc module as a bar plot and customize the plot
16+
# to improve its readability.
17+
#
18+
# Follow the instructions in the code comments to complete the exercise.
19+
# ------------------------------------------------------------------------------
20+
21+
# Import the modules to extract hierarchical area data and plot the results
22+
from area import extract_areas, get_path
23+
24+
# Argparse is a module that allows you to parse command-line arguments
25+
# See https://docs.python.org/3/library/argparse.html for more information
26+
import argparse
27+
28+
# Import the matplotlib library for plotting
29+
# See https://matplotlib.org/stable/gallery/index.html for examples
30+
from matplotlib import pyplot as plt
31+
32+
# Import the os module to create directories
33+
import os
34+
35+
# IHP-130 sg13g2_stdcell library (1 GE = 7.25 um^2)
36+
# See http://eda.ee.ethz.ch/index.php?title=Ihp13#What_is_one_gate_equivalent_?
37+
AREA_PER_GE = 7.25
38+
39+
if __name__ == "__main__":
40+
# Usage: python 01a_plot_area_bar.py <area_report_file>
41+
parser = argparse.ArgumentParser(
42+
description="Extract hierarchical areas from an OpenROAD area report."
43+
)
44+
parser.add_argument("file", type=str, help="Path to the area report file")
45+
args = parser.parse_args()
46+
47+
# Extract the hierarchical area data
48+
area_tree = extract_areas(args.file)
49+
50+
if area_tree is None:
51+
print("No hierarchical area data found.")
52+
exit(-1)
53+
54+
# Extract the hierarchical area data
55+
node_i_croc = get_path(area_tree, ["<top>", "i_croc_soc", "i_croc"])
56+
57+
# Extract names and convert areas to kGE
58+
names = []
59+
areas_kge = []
60+
61+
for node in node_i_croc.children:
62+
print(node)
63+
# Convert area from um^2 to kGE
64+
area_kge = node.area / (AREA_PER_GE * 1e3)
65+
areas_kge.append(area_kge)
66+
names.append(node.name)
67+
68+
# ==========================================================================
69+
# STUDENT TASKS - MODIFY THE CODE BELOW TO COMPLETE THE EXERCISE
70+
# ==========================================================================
71+
72+
# --------------------------------------------------------------------------
73+
# STUDENT TASK 1a.1: Create a bar plot
74+
# --------------------------------------------------------------------------
75+
# Use `plt.bar(...)` to create a bar chart.
76+
#
77+
# - The x-axis should represent the component names.
78+
# - The y-axis should represent the area values.
79+
# --------------------------------------------------------------------------
80+
81+
# Set the figure size
82+
plt.figure(figsize=(10, 5))
83+
84+
# Label for y-axis
85+
plt.ylabel("Area [kGE]")
86+
87+
# Label for x-axis
88+
plt.xlabel("Component")
89+
90+
# Title
91+
plt.title("Area Breakdown: i_croc", fontsize=12, fontweight="bold")
92+
93+
# --------------------------------------------------------------------------
94+
# STUDENT TASK 1a.2: Improve the readability of the plot
95+
# --------------------------------------------------------------------------
96+
# Improve the visualization by:
97+
# - Rotating x-axis labels if they overlap
98+
# - Adding grid lines for better readability
99+
# - Use distinct colors for each bar
100+
#
101+
# HINT: Use `plt.xticks(...)` for better readability.
102+
# You can also use `plt.yscale('log')` if areas vary significantly.
103+
# --------------------------------------------------------------------------
104+
105+
# Rotate x-axis labels for better readability
106+
plt.xticks(rotation=45, ha="right")
107+
108+
# Add grid lines for better readability
109+
plt.grid(axis="y", linestyle="--", alpha=0.6)
110+
111+
# Use distinct colors for each bar
112+
cmap = plt.get_cmap("tab10")
113+
colors = [cmap(i) for i in range(len(names))]
114+
115+
# --------------------------------------------------------------------------
116+
# STUDENT TASK 1a.3: Remap the names of the components
117+
# --------------------------------------------------------------------------
118+
# Remap the names of the components for better readability.
119+
# Remember to move the plot commands to the correct position in the code.
120+
#
121+
# HINT: Use the `names` list to remap the names of the components.
122+
# --------------------------------------------------------------------------
123+
124+
# Remap the names of the components for better readability
125+
names = [
126+
"SRAM Bank 1",
127+
"SRAM Bank 2",
128+
"Core",
129+
"Debug Module",
130+
"JTAG TAP",
131+
"GPIO",
132+
"SoC Control",
133+
"Timer",
134+
"UART",
135+
]
136+
137+
# --------------------------------------------------------------------------
138+
# STUDENT TASK 1a.4: Save and display the plot
139+
# --------------------------------------------------------------------------
140+
# Save the figure as a high-quality PDF and display the plot.
141+
#
142+
# HINT: Use `plt.tight_layout()` to adjust the layout for better
143+
# readability.
144+
# Save the figure using `plt.savefig(...)
145+
# --------------------------------------------------------------------------
146+
147+
# Create a bar chart
148+
plt.bar(names, areas_kge, color=colors)
149+
150+
# Adjust layout for better
151+
plt.tight_layout()
152+
153+
# Create a directory if it does not exist
154+
os.makedirs("plots", exist_ok=True)
155+
156+
# Save figure
157+
plt.savefig("plots/01a_area_breakdown_bar.pdf")
158+
plt.savefig("plots/01a_area_breakdown_bar.png")
159+
160+
# Display the plot
161+
plt.show()

ex13/01b_plot_area_pie.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2025 ETH Zurich and University of Bologna.
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Philip Wiese <[email protected]>
6+
7+
# ------------------------------------------------------------------------------
8+
# STUDENT TASK 1b:
9+
#
10+
# In this exercise, you will visualize the area breakdown of the i_croc module
11+
# using a pie chart. The area data is extracted from an OpenROAD area report.
12+
#
13+
# The code below extracts the hierarchical area data and visualizes it as a
14+
# bar plot. Your task is to modify the code to generate a pie chart instead.
15+
#
16+
# Follow the instructions in the code comments to complete the exercise.
17+
# ------------------------------------------------------------------------------
18+
19+
# Import the modules to extract hierarchical area data and plot the results
20+
from area import extract_areas, get_path
21+
22+
# Argparse is a module that allows you to parse command-line arguments
23+
# See https://docs.python.org/3/library/argparse.html for more information
24+
import argparse
25+
26+
# Import the matplotlib library for plotting
27+
# See https://matplotlib.org/stable/gallery/index.html for examples
28+
from matplotlib import pyplot as plt
29+
30+
# Import the os module to create directories
31+
import os
32+
33+
if __name__ == "__main__":
34+
# Usage: python 01b_plot_area_pie_sol.py <area_report_file>
35+
parser = argparse.ArgumentParser(
36+
description="Extract hierarchical areas from an OpenROAD area report."
37+
)
38+
parser.add_argument("file", type=str, help="Path to the area report file")
39+
args = parser.parse_args()
40+
41+
# Extract the hierarchical area data
42+
area_tree = extract_areas(args.file)
43+
44+
if area_tree is None:
45+
print("No hierarchical area data found.")
46+
exit(-1)
47+
48+
# Extract the hierarchical area data
49+
node_i_croc = get_path(area_tree, ["<top>", "i_croc_soc", "i_croc"])
50+
51+
# Extract names and areas
52+
names = []
53+
areas = []
54+
55+
for node in node_i_croc.children:
56+
print(node)
57+
names.append(node.name)
58+
areas.append(node.area)
59+
60+
# ==========================================================================
61+
# STUDENT TASKS - MODIFY THE CODE BELOW TO COMPLETE THE EXERCISE
62+
# ==========================================================================
63+
64+
# --------------------------------------------------------------------------
65+
# STUDENT TASK 1b.1: Create a pie chart instead of a bar plot
66+
# --------------------------------------------------------------------------
67+
# Instead of a bar plot, generate a pie chart that visualizes the area
68+
# distribution.
69+
# Use `plt.pie()` instead of `plt.bar()`.
70+
#
71+
# HINT: The `plt.pie()` function requires:
72+
# - The area values
73+
# --------------------------------------------------------------------------
74+
75+
# --------------------------------------------------------------------------
76+
# STUDENT TASK 1b.2: Improve the readability of the pie chart
77+
# --------------------------------------------------------------------------
78+
# Matplotlib provides a colormap function `get_cmap()` that can generate a
79+
# set of visually distinguishable colors. Alternatively, you can define your
80+
# own custom colors. Use the colors to visualize the area distribution in
81+
# the pie chart.
82+
#
83+
# Feel free to experiment with other features to improve the readability of
84+
# the pie chart.
85+
#
86+
# HINT: Look at `matplotlib.cm.get_cmap()`
87+
# Documentation: https://matplotlib.org/stable/api/cm_api.html#matplotlib.cm.get_cmap
88+
#
89+
# HINT: The `plt.pie()` function can be suplied with:
90+
# - Labels (the names of the components)
91+
# - Colors (from Task 1b.1)
92+
# - Optional: Use `autopct='%1.1f%%'` to show percentages on the chart
93+
# --------------------------------------------------------------------------

0 commit comments

Comments
 (0)