Files
rocm-systems/projects/rocprofiler-systems/examples/python/source.py
T

70 lines
1.7 KiB
Python
Raw Normal View History

2022-04-05 00:24:34 -05:00
#!@PYTHON_EXECUTABLE@
# Copyright (c) Advanced Micro Devices, Inc.
# SPDX-License-Identifier: MIT
2022-04-21 21:36:07 -05:00
import os
2022-04-05 00:24:34 -05:00
import sys
2022-07-25 20:24:34 -05:00
import time
import rocprofsys
from rocprofsys.user import region as omni_user_region
2024-09-13 13:43:26 -04:00
import random
2022-04-05 00:24:34 -05:00
2022-04-21 21:36:07 -05:00
_prefix = ""
2022-04-05 00:24:34 -05:00
def fib(n):
return n if n < 2 else (fib(n - 1) + fib(n - 2))
2024-09-13 13:43:26 -04:00
def _sum(arr):
print(f"---- in _sum")
return sum(arr)
2022-04-21 21:36:07 -05:00
2024-09-13 13:43:26 -04:00
def inefficient(n):
print(f"[{_prefix}] ... running inefficient({n})")
a = 0
for i in range(n):
a += i
for j in range(n):
a += j
_len = a * n * n
_arr = [random.random() for _ in range(_len)]
_ret = _sum(_arr)
print(f"[{_prefix}] ... sum of {_len} random elements: {_ret}")
return _ret
2022-04-05 00:24:34 -05:00
@rocprofsys.profile()
2022-04-21 21:36:07 -05:00
def run(n):
_ret = 0
_ret += fib(n)
_ret += inefficient(n)
return _ret
2022-04-05 00:24:34 -05:00
if __name__ == "__main__":
2022-04-21 21:36:07 -05:00
import argparse
parser = argparse.ArgumentParser()
2022-06-24 03:03:46 -05:00
parser.add_argument("-n", "--num-iterations", help="Number", type=int, default=3)
parser.add_argument("-v", "--value", help="Starting value", type=int, default=20)
2022-07-25 20:24:34 -05:00
parser.add_argument(
"-s",
"--stop-profile",
help="Stop tracing after given iterations",
type=int,
default=0,
)
2022-04-21 21:36:07 -05:00
args = parser.parse_args()
_prefix = os.path.basename(__file__)
print(f"[{_prefix}] Executing {args.num_iterations} iterations...\n")
for i in range(args.num_iterations):
2022-07-25 20:24:34 -05:00
with omni_user_region(f"main_loop"):
if args.stop_profile > 0 and i == args.stop_profile:
rocprofsys.user.stop_trace()
2022-07-25 20:24:34 -05:00
ans = run(args.value)
print(f"[{_prefix}] [{i}] result of run({args.value}) = {ans}\n")