From aa59a01330e800acae977f45cbdeda8ea22a748b Mon Sep 17 00:00:00 2001 From: Aleksandar Janicijevic Date: Thu, 12 Sep 2024 17:43:32 -0400 Subject: [PATCH] Modified script used for Python testing so it doesn't use numpy --- examples/python/source-numpy.py | 83 +++++++++++++++++++++++++++++++++ examples/python/source.py | 54 +++++++-------------- external/perfetto | 2 +- 3 files changed, 102 insertions(+), 37 deletions(-) create mode 100755 examples/python/source-numpy.py diff --git a/examples/python/source-numpy.py b/examples/python/source-numpy.py new file mode 100755 index 000000000..f591b1219 --- /dev/null +++ b/examples/python/source-numpy.py @@ -0,0 +1,83 @@ +#!/usr/bin/python3.10 + +import os +import sys +import time +import omnitrace +from omnitrace.user import region as omni_user_region + +_prefix = "" + + +def fib(n): + return n if n < 2 else (fib(n - 1) + fib(n - 2)) + + +try: + import numpy as np + + def inefficient(n): + print(f"[{_prefix}] ... running inefficient({n}) (1)") + a = 0 + for i in range(n): + a += i + for j in range(n): + a += j + _len = a * n * n + _ret = np.random.rand(_len).sum() + print(f"[{_prefix}] ... sum of {_len} random elements: {_ret}") + return _ret + +except ImportError as e: + print(f"ImportError: {e}") + import random + + def _sum(arr): + print(f"---- in _sum") + return sum(arr) + + 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 + + +@omnitrace.profile() +def run(n): + _ret = 0 + _ret += fib(n) + _ret += inefficient(n) + return _ret + + +if __name__ == "__main__": + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument("-n", "--num-iterations", help="Number", type=int, default=3) + parser.add_argument("-v", "--value", help="Starting value", type=int, default=20) + parser.add_argument( + "-s", + "--stop-profile", + help="Stop tracing after given iterations", + type=int, + default=0, + ) + 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): + with omni_user_region(f"main_loop"): + if args.stop_profile > 0 and i == args.stop_profile: + omnitrace.user.stop_trace() + ans = run(args.value) + print(f"[{_prefix}] [{i}] result of run({args.value}) = {ans}\n") diff --git a/examples/python/source.py b/examples/python/source.py index 2bef32b7b..7b8e21f9d 100755 --- a/examples/python/source.py +++ b/examples/python/source.py @@ -1,51 +1,33 @@ -#!@PYTHON_EXECUTABLE@ +#!/usr/bin/python3.10 import os import sys import time import omnitrace from omnitrace.user import region as omni_user_region +import random _prefix = "" - def fib(n): return n if n < 2 else (fib(n - 1) + fib(n - 2)) - -try: - import numpy as np - - 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 - _ret = np.random.rand(_len).sum() - print(f"[{_prefix}] ... sum of {_len} random elements: {_ret}") - return _ret - -except ImportError: - import random - - def _sum(arr): - return sum(arr) - - 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 +def _sum(arr): + print(f"---- in _sum") + return sum(arr) + +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 @omnitrace.profile() diff --git a/external/perfetto b/external/perfetto index 35b3d9845..c74251226 160000 --- a/external/perfetto +++ b/external/perfetto @@ -1 +1 @@ -Subproject commit 35b3d9845c2f4017865c4dc93fafcf6d202f1651 +Subproject commit c74251226a8caa0b43377902ee06d2570faa0c15