-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMLCAD2025_60_run.sh
More file actions
243 lines (202 loc) · 7.1 KB
/
MLCAD2025_60_run.sh
File metadata and controls
243 lines (202 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
# Default values
TOTAL_ITERS=6
PARALLEL_RUNS=50
TIMEOUT="45m"
TOTAL_CPUS=110
TOTAL_RAM=220
ECP_WEIGHT=0.5
WL_WEIGHT=0.5
ECP_WEIGHT_SURROGATE=0.5
WL_WEIGHT_SURROGATE=0.5
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-p|--platform)
platform="$2"
shift 2
;;
-d|--design)
design="$2"
shift 2
;;
-i|--iterations)
TOTAL_ITERS="$2"
shift 2
;;
-r|--parallel-runs)
PARALLEL_RUNS="$2"
shift 2
;;
-t|--timeout)
TIMEOUT="$2"
shift 2
;;
-o|--objective)
objective="$2"
shift 2
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Validate required arguments
if [[ -z "$platform" || -z "$design" ]]; then
echo "Usage: $0 -p <platform> -d <design> [-i iterations] [-r parallel_runs] [-t timeout] [-o objective]"
echo " platform: asap7 or sky130hd"
echo " design: aes, ibex, or jpeg"
echo " iterations: total number of iterations (default: 6)"
echo " parallel_runs: number of parallel runs (default: 50)"
echo " timeout: timeout per run (default: 45m)"
echo " objective: ecp, wl, or weighted (default: ecp)"
exit 1
fi
# Set default objective if not specified
objective=${objective:-"ECP"}
# Convert objective to uppercase for consistency
objective="${objective^^}"
# Validate platform
if [[ "$platform" != "asap7" && "$platform" != "sky130hd" ]]; then
echo "Error: platform must be either asap7 or sky130hd"
exit 1
fi
# Validate design
if [[ "$design" != "aes" && "$design" != "ibex" && "$design" != "jpeg" ]]; then
echo "Error: design must be one of: aes, ibex, jpeg"
exit 1
fi
# Validate objective
if [[ "$objective" != "ECP" && "$objective" != "DWL" && "$objective" != "COMBO" ]]; then
echo "Error: objective must be one of: ECP, DWL, COMBO"
exit 1
fi
# Validate weights sum to 1
if [[ "$objective" == "COMBO" ]]; then
# Ensure jq is installed
if ! command -v jq &> /dev/null; then
echo "Error: jq is required but not installed. Please install jq."
exit 1
fi
# Read weights from opt_config.json
weights=$(jq -r \
--arg pdk "$platform" \
--arg design "$design" \
--arg goal "$objective" \
'.configurations[] | select(.platform == $pdk and .design == $design and .goal == $goal)' \
opt_config.json)
if [[ -z "$weights" || "$weights" == "null" ]]; then
echo "Error: Could not find weights for platform $platform and design $design in opt_config.json."
exit 1
fi
# Extract real metric weights
ECP_WEIGHT=$(echo "$weights" | jq -r '.weights.ecp')
WL_WEIGHT=$(echo "$weights" | jq -r '.weights.dwl')
# Extract surrogate metric weights
ECP_WEIGHT_SURROGATE=$(echo "$weights" | jq -r '.weights_surrogate.ecp')
WL_WEIGHT_SURROGATE=$(echo "$weights" | jq -r '.weights_surrogate.dwl')
# Validate that the weights sum to 1
weight_sum=$(echo "$ECP_WEIGHT + $WL_WEIGHT" | bc -l)
if (( $(echo "$weight_sum != 1" | bc -l) )); then
echo "Error: ECP_WEIGHT ($ECP_WEIGHT) and WL_WEIGHT ($WL_WEIGHT) must sum to 1."
exit 1
fi
surrogate_weight_sum=$(echo "$ECP_WEIGHT_SURROGATE + $WL_WEIGHT_SURROGATE" | bc -l)
if (( $(echo "$surrogate_weight_sum != 1" | bc -l) )); then
echo "Error: ECP_WEIGHT_SURROGATE ($ECP_WEIGHT_SURROGATE) and WL_WEIGHT_SURROGATE ($WL_WEIGHT_SURROGATE) must sum to 1."
exit 1
fi
# Export weights for use in optimize.py
export ECP_WEIGHT
export WL_WEIGHT
export ECP_WEIGHT_SURROGATE
export WL_WEIGHT_SURROGATE
fi
# Calculate resources per run
cpus_per_run=$(( TOTAL_CPUS / PARALLEL_RUNS ))
ram_per_run=$(( TOTAL_RAM / PARALLEL_RUNS ))
# Ensure minimum resources
if [[ $cpus_per_run -lt 2 ]]; then
echo "Warning: Not enough CPUs. Reducing parallel runs to $((TOTAL_CPUS / 2))"
PARALLEL_RUNS=$((TOTAL_CPUS / 2))
cpus_per_run=2
fi
if [[ $ram_per_run -lt 4 ]]; then
echo "Warning: Not enough RAM. Reducing parallel runs to $((TOTAL_RAM / 4))"
PARALLEL_RUNS=$((TOTAL_RAM / 4))
ram_per_run=4
fi
# Export resource variables for child scripts
export PARALLEL_RUNS
export CPUS_PER_RUN=$cpus_per_run
export RAM_PER_RUN=$ram_per_run
export TIMEOUT
# Create logs directory
mkdir -p logs
# Define the line numbers for DESIGN_CONFIG lines
start_line=9
end_line=15
# Function to comment all DESIGN_CONFIG lines
comment_all_design_configs() {
sed -i "${start_line},${end_line} s/^\([^#]\)/#\1/" Makefile
}
# Function to uncomment a specific DESIGN_CONFIG line
uncomment_design_config_line() {
local line_num=$1
sed -i "${line_num} s/^#//" Makefile
}
# Comment all DESIGN_CONFIG lines first
comment_all_design_configs
# Find the line number matching the desired DESIGN_CONFIG
config_pattern="DESIGN_CONFIG=./designs/${platform}/${design}/config_\\\$(INT_PARAM).mk"
line_num=$(grep -n "$config_pattern" Makefile | cut -d: -f1)
if [ -n "$line_num" ]; then
# Uncomment the specific DESIGN_CONFIG line
uncomment_design_config_line $line_num
else
echo "Error: Could not find DESIGN_CONFIG line for platform: $platform, design: $design"
exit 1
fi
# Function to create backup
create_backup() {
local platform=$1
local design=$2
local iteration=$3
local backup_dir="../result_dump_${iteration}"
echo "Creating backup for iteration ${iteration}..."
mkdir -p "$backup_dir"
# Move config and constraint files
mv designs/${platform}/${design}/config_*.mk "$backup_dir"/ 2>/dev/null
mv designs/${platform}/${design}/constraint_*.sdc "$backup_dir"/ 2>/dev/null
if [[ "$platform" == "asap7" && "$design" == "jpeg" ]]; then
mv designs/${platform}/${design}/jpeg_encoder15_7nm_*.sdc "$backup_dir"/ 2>/dev/null
fi
# Move logs
mkdir -p "$backup_dir/logs_dump"
mv logs/${platform}/${design}/* "$backup_dir/logs_dump"/ 2>/dev/null
# Move results
mkdir -p "$backup_dir/results_dump"
mv results/${platform}/${design}/* "$backup_dir/results_dump"/ 2>/dev/null
# Move platform_design log files from current directory
mv ${platform}_${design}*.log "$backup_dir"/ 2>/dev/null
echo "Backup created in ${backup_dir}"
}
# Main iteration loop
for i in $(seq 1 $TOTAL_ITERS); do
echo "Starting iteration $i of $TOTAL_ITERS"
# Run sequential phase
./run_sequential.sh "$platform" "$design" "$PARALLEL_RUNS" "$i"
# Run parallel phase with timeout
timeout "$TIMEOUT" ./run_parallel.sh "$platform" "$design" "$PARALLEL_RUNS" || true
# Kill any remaining parallel jobs
pkill -P $$ || true
# Create backup of this iteration's results
create_backup "$platform" "$design" "$i"
# Generate constraints for next iteration (skip for last iteration)
if [ "$i" -lt "$TOTAL_ITERS" ]; then
echo "Running optimization for next iteration..."
python3 optimize.py "$platform" "$design" "$objective" "$PARALLEL_RUNS"
fi
done
echo "All iterations complete"