|
| 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