Skip to content

Commit c33e261

Browse files
Added _runner utility file to examples/python
Used run_examples routine implemented in _runner that support comand-line arguments, allows introspect available examples and run all of them, or any subset individually. E.g. ``` (idp) [09:37:14 nuc04 python]$ python subdevices.py -q --run subdivide_root_cpu_device cpu_d is a root device. Sub-device #EU: [4, 4, 4] Sub-device is_root: [False, False, False] Sub-device parent is what we expected: [True, True, True] Sub-sub-device #EU: [[1, 3], [1, 3], [1, 3]] ```
1 parent fc1d299 commit c33e261

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

examples/python/_runner.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import argparse
2+
import inspect
3+
4+
5+
def has_nondefault_params(sgn):
6+
for v in sgn.parameters.values():
7+
if v.default is inspect._empty:
8+
return True
9+
return False
10+
11+
12+
def run_examples(example_description, glbls_dict):
13+
parser = argparse.ArgumentParser(
14+
description=example_description,
15+
)
16+
parser.add_argument(
17+
"-r",
18+
"--run",
19+
type=str,
20+
help="Functions to execute. Use --run all to run all of them.",
21+
)
22+
parser.add_argument(
23+
"-l", "--list", action="store_true", help="List available function names to run"
24+
)
25+
parser.add_argument(
26+
"-q", "--quiet", action="store_true", help="Do not echo example name."
27+
)
28+
args = parser.parse_args()
29+
30+
if args.list or not args.run:
31+
fns = []
32+
for n in glbls_dict:
33+
if inspect.isfunction(glbls_dict.get(n)):
34+
fns.append(n)
35+
if fns:
36+
print("Available examples:")
37+
print(", ".join(fns))
38+
else:
39+
print("No examples are availble.")
40+
exit(0)
41+
if args.run == "all":
42+
fns = []
43+
for n in glbls_dict:
44+
if inspect.isfunction(glbls_dict.get(n)):
45+
fns.append(n)
46+
args.run = fns
47+
else:
48+
args.run = args.run.split()
49+
50+
if args.run:
51+
for fn in args.run:
52+
if fn in glbls_dict:
53+
clbl = glbls_dict.get(fn)
54+
sgn = inspect.signature(clbl)
55+
print("")
56+
if has_nondefault_params(sgn):
57+
if not args.quiet:
58+
print(f"INFO: Skip exectution of {fn} as it requires arguments")
59+
else:
60+
if not args.quiet:
61+
print(f"INFO: Executing example {fn}")
62+
clbl()
63+
if not args.quiet:
64+
print("INFO: ===========================")
65+
66+
else:
67+
raise ValueError("No function to run was specified")

examples/python/device_selection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
def print_device(d):
24+
"Display information about given device argument."
2425
if type(d) is not dpctl.SyclDevice:
2526
raise ValueError
2627
print("Name: ", d.name)
@@ -94,4 +95,12 @@ def custom_select_device():
9495
selected_dev = d
9596
if selected_dev:
9697
print_device(selected_dev)
98+
else:
99+
print("No device with half-precision support is available.")
97100
return selected_dev
101+
102+
103+
if __name__ == "__main__":
104+
import _runner as runner
105+
106+
runner.run_examples("Device selection examples for dpctl.", globals())

examples/python/subdevices.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,15 @@ def subdivide_by_affinity(affinity="numa"):
6464
Create sub-devices partitioning by affinity.
6565
"""
6666
cpu_d = dpctl.SyclDevice("cpu")
67-
sub_devs = cpu_d.create_sub_devices(partition=affinity)
68-
return sub_devs
67+
try:
68+
sub_devs = cpu_d.create_sub_devices(partition=affinity)
69+
print(
70+
"{} sub-devices were created with #EUs".format(
71+
len(sub_devs), [d.max_compute_units for d in sub_devs]
72+
)
73+
)
74+
except:
75+
print("Device partitioning by affinity was not successful.")
6976

7077

7178
def create_subdevice_queue():
@@ -99,9 +106,6 @@ def create_subdevice_queue():
99106

100107

101108
if __name__ == "__main__":
102-
print("")
103-
print("Executing subdivide_root_cpu_device:")
104-
subdivide_root_cpu_device()
105-
print("")
106-
print("Exectuting create_subdevice_queue:")
107-
print(create_subdevice_queue())
109+
import _runner as runner
110+
111+
runner.run_examples("Examples for working with subdevices in dpctl.", globals())

0 commit comments

Comments
 (0)