|
| 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 | +import os |
| 18 | +from contextlib import contextmanager |
| 19 | + |
| 20 | + |
| 21 | +@contextmanager |
| 22 | +def onetrace_enabled(): |
| 23 | + """ Enable `onetrace` collection for kernels executed in this context. |
| 24 | +
|
| 25 | + N.B.: Proper working of this utility assumes that Python interpreter |
| 26 | + has been launched by `onetrace` tool from intel/pti-gpu project. |
| 27 | +
|
| 28 | + .. Example:: bash |
| 29 | +
|
| 30 | + $ onetrace --conditional-collection -v -t --demangle \ |
| 31 | + python script.py |
| 32 | +
|
| 33 | + .. Example:: bash |
| 34 | +
|
| 35 | + import dpctl.tensor as dpt |
| 36 | + from dpctl.utils import onetrace_enabled |
| 37 | +
|
| 38 | + # onetrace output reporting on execution of the kernel |
| 39 | + # should be seen, starting with "Device Timeline" |
| 40 | + with onetrace_enabled(): |
| 41 | + dpt.arange(100, dtype='int16') |
| 42 | +
|
| 43 | + """ |
| 44 | + _env_var_name = "PTI_ENABLE_COLLECTION" |
| 45 | + saved = os.getenv(_env_var_name, None) |
| 46 | + try: |
| 47 | + os.environ[_env_var_name] = "1" |
| 48 | + yield |
| 49 | + finally: |
| 50 | + if saved is None: |
| 51 | + del os.environ[_env_var_name] |
| 52 | + else: |
| 53 | + os.environ[_env_var_name] = saved |
0 commit comments