Skip to content

Commit 1e70439

Browse files
Merge pull request #903 from IntelPython/feature/contextmanager-onetrace_enabled
Adding onetrace_enabled
2 parents d759a66 + d03f085 commit 1e70439

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

dpctl/tests/test_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,14 @@ def validate_usm_type_arg():
111111
dpctl.utils.validate_usm_type("inv", allow_none=True)
112112
with pytest.raises(ValueError):
113113
dpctl.utils.validate_usm_type("inv", allow_none=False)
114+
115+
116+
@pytest.mark.filterwarnings("ignore:.*:RuntimeWarning")
117+
def test_onetrace_enabled():
118+
import os
119+
120+
v_name = "PTI_ENABLE_COLLECTION"
121+
v_v = os.getenv(v_name, None)
122+
with dpctl.utils.onetrace_enabled():
123+
assert os.getenv(v_name, None) == "1"
124+
assert os.getenv(v_name, None) == v_v

dpctl/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
get_execution_queue,
2424
validate_usm_type,
2525
)
26+
from ._onetrace_context import onetrace_enabled
2627

2728
__all__ = [
2829
"get_execution_queue",
2930
"get_coerced_usm_type",
3031
"validate_usm_type",
32+
"onetrace_enabled",
3133
]

dpctl/utils/_onetrace_context.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2022 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from contextlib import contextmanager
18+
from os import environ, getenv
19+
from platform import system as sys_platform
20+
21+
_UNCHECKED = sys_platform() == "Linux"
22+
del sys_platform
23+
24+
25+
@contextmanager
26+
def onetrace_enabled():
27+
"""Enable `onetrace` collection for kernels executed in this context.
28+
29+
N.B.: Proper working of this utility assumes that Python interpreter
30+
has been launched by `onetrace` tool from intel/pti-gpu project.
31+
32+
:Example:
33+
Launch the Python interpreter using `onetrace` tool: ::
34+
35+
$ onetrace --conditional-collection -v -t --demangle python app.py
36+
37+
Now using the context manager in the Python sessions enables
38+
data collection and its output for every offloaded kernel ::
39+
40+
import dpctl.tensor as dpt
41+
from dpctl.utils import onetrace_enabled
42+
43+
# onetrace output reporting on execution of the kernel
44+
# should be seen, starting with "Device Timeline"
45+
with onetrace_enabled():
46+
dpt.arange(100, dtype='int16')
47+
48+
"""
49+
global _UNCHECKED
50+
51+
if _UNCHECKED:
52+
_UNCHECKED = False
53+
if not (
54+
getenv("PTI_ENABLE", None) == "1"
55+
and "onetrace_tool" in getenv("LD_PRELOAD", "")
56+
):
57+
import warnings
58+
59+
warnings.warn(
60+
"It looks like Python interpreter was not started using "
61+
"`onetrace` utility. Using `onetrace_enabled` may have "
62+
"no effect. See `onetrace_enabled.__doc__` for usage.",
63+
RuntimeWarning,
64+
stacklevel=2,
65+
)
66+
67+
_env_var_name = "PTI_ENABLE_COLLECTION"
68+
saved = getenv(_env_var_name, None)
69+
try:
70+
environ[_env_var_name] = "1"
71+
yield
72+
finally:
73+
if saved is None:
74+
del environ[_env_var_name]
75+
else:
76+
environ[_env_var_name] = saved

0 commit comments

Comments
 (0)