|
42 | 42 | import sys |
43 | 43 | import uuid |
44 | 44 | import time |
45 | | -from multiprocessing import cpu_count |
46 | 45 | from datetime import datetime |
47 | 46 |
|
48 | 47 | import numpy as np |
|
71 | 70 | DATE = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") |
72 | 71 |
|
73 | 72 |
|
| 73 | +# --- General utilities --- |
| 74 | +def run_command( |
| 75 | + args, cmd, timeout=None, stderr_file=None, stdout_file=None, fail_fast=False |
| 76 | +): |
| 77 | + """ |
| 78 | + Wrapper for subprocess.run |
| 79 | + Allows to run shell command, control print and exceptions. |
| 80 | + """ |
| 81 | + process = subprocess.run( |
| 82 | + cmd, timeout=timeout, capture_output=True, text=True, check=False, shell=True |
| 83 | + ) |
| 84 | + if stderr_file is not None and process.stderr != "": |
| 85 | + with open(stderr_file, "a") as file: |
| 86 | + file.write(f"\n\n{cmd}\n{process.stderr}") |
| 87 | + if stdout_file is not None and process.stdout != "": |
| 88 | + with open(stdout_file, "a") as file: |
| 89 | + file.write(f"\n\n{cmd}\n{process.stdout}") |
| 90 | + if args.verbose >= 1: |
| 91 | + print(process.stderr) |
| 92 | + if args.verbose >= 2: |
| 93 | + print(process.stdout) |
| 94 | + |
| 95 | + if fail_fast and process.returncode != 0: |
| 96 | + raise RuntimeError |
| 97 | + |
| 98 | + |
| 99 | +def set_seed(seed: int): |
| 100 | + """Set seed for reproducibility.""" |
| 101 | + import torch |
| 102 | + import random |
| 103 | + |
| 104 | + # TODO: shift seed validation into validate_args during parse_arguments |
| 105 | + # Pre-set seed if user sets seed to 0 |
| 106 | + if seed == 0: |
| 107 | + print( |
| 108 | + "Warning: you have chosen not to set a seed. Do you wish to continue? (y/n)" |
| 109 | + ) |
| 110 | + if input().lower() != "y": |
| 111 | + sys.exit(0) |
| 112 | + seed = None |
| 113 | + else: |
| 114 | + torch.manual_seed(seed) |
| 115 | + np.random.seed(seed) |
| 116 | + random.seed(seed) |
| 117 | + |
| 118 | + |
| 119 | +# --- End General utilities --- |
| 120 | + |
| 121 | + |
| 122 | +# --- OpenROAD: write file utilities --- |
74 | 123 | def write_sdc(variables, path, sdc_original, constraints_sdc): |
75 | 124 | """ |
76 | 125 | Create a SDC file with parameters for current tuning iteration. |
@@ -160,6 +209,10 @@ def write_fast_route(variables, path, platform, fr_original, fastroute_tcl): |
160 | 209 | return file_name |
161 | 210 |
|
162 | 211 |
|
| 212 | +# --- End OpenROAD: write file utilities --- |
| 213 | + |
| 214 | + |
| 215 | +# --- OpenROAD: parse utilities --- |
163 | 216 | def parse_flow_variables(base_dir, platform): |
164 | 217 | """ |
165 | 218 | Parse the flow variables from source |
@@ -262,31 +315,10 @@ def parse_config( |
262 | 315 | return options |
263 | 316 |
|
264 | 317 |
|
265 | | -def run_command( |
266 | | - args, cmd, timeout=None, stderr_file=None, stdout_file=None, fail_fast=False |
267 | | -): |
268 | | - """ |
269 | | - Wrapper for subprocess.run |
270 | | - Allows to run shell command, control print and exceptions. |
271 | | - """ |
272 | | - process = subprocess.run( |
273 | | - cmd, timeout=timeout, capture_output=True, text=True, check=False, shell=True |
274 | | - ) |
275 | | - if stderr_file is not None and process.stderr != "": |
276 | | - with open(stderr_file, "a") as file: |
277 | | - file.write(f"\n\n{cmd}\n{process.stderr}") |
278 | | - if stdout_file is not None and process.stdout != "": |
279 | | - with open(stdout_file, "a") as file: |
280 | | - file.write(f"\n\n{cmd}\n{process.stdout}") |
281 | | - if args.verbose >= 1: |
282 | | - print(process.stderr) |
283 | | - if args.verbose >= 2: |
284 | | - print(process.stdout) |
285 | | - |
286 | | - if fail_fast and process.returncode != 0: |
287 | | - raise RuntimeError |
| 318 | +# --- End OpenROAD: parse utilities --- |
288 | 319 |
|
289 | 320 |
|
| 321 | +# --- OpenROAD specific functions --- |
290 | 322 | def openroad( |
291 | 323 | args, |
292 | 324 | base_dir, |
@@ -602,6 +634,7 @@ def prepare_ray_server(args): |
602 | 634 | return local_dir, orfs_flow_dir, install_path |
603 | 635 |
|
604 | 636 |
|
| 637 | +# --- Ray: OpenROAD wrapper utilities --- |
605 | 638 | @ray.remote |
606 | 639 | def openroad_distributed( |
607 | 640 | args, |
@@ -645,3 +678,6 @@ def consumer(queue): |
645 | 678 | print(f"[INFO TUN-0007] Scheduling run for parameter {name}.") |
646 | 679 | ray.get(openroad_distributed.remote(*next_item)) |
647 | 680 | print(f"[INFO TUN-0008] Finished run for parameter {name}.") |
| 681 | + |
| 682 | + |
| 683 | +# --- End Ray: OpenROAD wrapper utilities --- |
0 commit comments