-
Notifications
You must be signed in to change notification settings - Fork 68
/
py-example.py
76 lines (53 loc) · 1.71 KB
/
py-example.py
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
# Copyright (c) 2024, Lawrence Livermore National Security, LLC.
# See top-level LICENSE file for details.
from pycaliper.high_level import annotate_function
from pycaliper.config_manager import ConfigManager
from pycaliper.instrumentation import (
set_global_byname,
begin_region,
end_region,
)
from pycaliper.loop import Loop
import argparse
import sys
import time
def get_available_specs_doc(mgr: ConfigManager):
doc = ""
for cfg in mgr.available_config_specs():
doc += mgr.get_documentation_for_spec(cfg)
doc += "\n"
return doc
@annotate_function()
def foo(i: int) -> float:
nsecs = max(i * 500, 100000)
secs = nsecs / 10**9
time.sleep(secs)
return 0.5 * i
def main():
mgr = ConfigManager()
parser = argparse.ArgumentParser()
parser.add_argument("--caliper_config", "-P", type=str, default="",
help="Configuration for Caliper\n{}".format(get_available_specs_doc(mgr)))
parser.add_argument("iterations", type=int, nargs="?", default=4,
help="Number of iterations")
args = parser.parse_args()
mgr.add(args.caliper_config)
if mgr.error():
print("Caliper config error:", mgr, file=sys.stderr)
mgr.start()
set_global_byname("iterations", args.iterations)
set_global_byname("caliper.config", args.caliper_config)
begin_region("main")
begin_region("init")
t = 0
end_region("init")
loop_ann = Loop("mainloop")
for i in range(args.iterations):
loop_ann.start_iteration(i)
t *= foo(i)
loop_ann.end_iteration()
loop_ann.end()
end_region("main")
mgr.flush()
if __name__ == "__main__":
main()