From 220bfcf55dd9cbc5c2e4d6242482844d1bed7b87 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 4 Dec 2024 08:49:04 -0600 Subject: [PATCH 1/5] Support dpctl in vritual environment out of the box Since location of Library\bin in virtual environment is not on the default search path, importing of dpctl failes due to unmet dependencies for native extensions of dpctl submodules. This change introduces _init_helper.py which implements the following logic using built-in os Python module: 1. If os.add_dll_directory exists, and VIRTUAL_ENV environment variable is set, and os.path.join(os.environ["VIRTUAL_ENV"], "Library", "bin") exists, call os.add_dll_directory with that directory. With this change the gh-1745 is fixed, and "python -m dpctl -f" works out of the box. Only GPU devices are visible, and to enable CPU device two additional steps must be performed: 1. Edit %VIRUAL_ENV%\Library\bin\cl.cfg and set CL_CONFIG_TBB_DLL_PATH variable at the bottom of the configuration file to the expanded value of %VIRUAL_ENV%\Library\bin\tbb12.dll but use forward slashes, instead of native backward slashes. 2. Append %VIRUAL_ENV%\Library\bin to the PATH using `set "PATH=%PATH%:%VIRTUAL_ENV%\Library\bin"` After these changes `python -m dpctl -f` should see CPU device. --- dpctl/__init__.py | 2 ++ dpctl/_init_helper.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 dpctl/_init_helper.py diff --git a/dpctl/__init__.py b/dpctl/__init__.py index b7f85cea99..0eb474602b 100644 --- a/dpctl/__init__.py +++ b/dpctl/__init__.py @@ -26,6 +26,7 @@ import os import os.path +from . import _init_helper from ._device_selection import select_device_with_aspects from ._sycl_context import SyclContext, SyclContextCreationError from ._sycl_device import ( @@ -137,3 +138,4 @@ def get_include(): __version__ = get_versions()["version"] del get_versions +del _init_helper diff --git a/dpctl/_init_helper.py b/dpctl/_init_helper.py new file mode 100644 index 0000000000..46686f1248 --- /dev/null +++ b/dpctl/_init_helper.py @@ -0,0 +1,27 @@ +# Data Parallel Control (dpctl) +# +# Copyright 2020-2024 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import os.path + +if hasattr(os, "add_dll_directory"): + # For virtual environments on Windows, add folder + # with DPC++ libraries to the DLL search path gh-1745 + if "VIRTUAL_ENV" in os.environ: + venv_dir = os.environ["VIRTUAL_ENV"] + expected_dir = os.path.join(venv_dir, "Library", "bin") + if os.exists(expected_dir): + os.add_dll_directory(expected_dir) From 9df5b28efaf71cf2aa65ec7d965e5f198ac2bc5b Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Thu, 5 Dec 2024 08:01:23 -0600 Subject: [PATCH 2/5] Update dpctl/_init_helper.py Co-authored-by: ndgrigorian <46709016+ndgrigorian@users.noreply.github.com> --- dpctl/_init_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpctl/_init_helper.py b/dpctl/_init_helper.py index 46686f1248..8524511eb3 100644 --- a/dpctl/_init_helper.py +++ b/dpctl/_init_helper.py @@ -23,5 +23,5 @@ if "VIRTUAL_ENV" in os.environ: venv_dir = os.environ["VIRTUAL_ENV"] expected_dir = os.path.join(venv_dir, "Library", "bin") - if os.exists(expected_dir): + if os.path.exists(expected_dir): os.add_dll_directory(expected_dir) From 231c9c64224a588260873b885223cd3c0a926439 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 6 Dec 2024 13:37:17 -0600 Subject: [PATCH 3/5] More robust check that VIRUAL_ENV is valid --- dpctl/_init_helper.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dpctl/_init_helper.py b/dpctl/_init_helper.py index 8524511eb3..15c6a2e17b 100644 --- a/dpctl/_init_helper.py +++ b/dpctl/_init_helper.py @@ -22,6 +22,8 @@ # with DPC++ libraries to the DLL search path gh-1745 if "VIRTUAL_ENV" in os.environ: venv_dir = os.environ["VIRTUAL_ENV"] - expected_dir = os.path.join(venv_dir, "Library", "bin") - if os.path.exists(expected_dir): - os.add_dll_directory(expected_dir) + # Expect to find file per PEP-0405 + expected_file = os.path.join(venv_dir, "pyvenv.cfg") + dll_dir = os.path.join(venv_dir, "Library", "bin") + if os.path.isdir(dll_dir) and os.path.isfile(expected_file): + os.add_dll_directory(dll_dir) From 1d1b5da6064943207aae9ac727ed3674c837ac8c Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 6 Dec 2024 16:11:35 -0600 Subject: [PATCH 4/5] Avoid reliance of VIRTUAL_ENV environment variable Use check suggested in PEP-0405 and pointed out by @ndgrigorian to compare sys.base_exec_prefix and sys.exec_prefix which would be different under virtual environment. The check that pyvenv.cfg exists is retained. --- dpctl/_init_helper.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/dpctl/_init_helper.py b/dpctl/_init_helper.py index 15c6a2e17b..8ced7eee41 100644 --- a/dpctl/_init_helper.py +++ b/dpctl/_init_helper.py @@ -16,14 +16,19 @@ import os import os.path +import sys -if hasattr(os, "add_dll_directory"): +is_venv_win32 = ( + sys.platform == "win32" + and sys.base_exec_prefix != sys.exec_prefix + and os.path.isfile(os.path.join(sys.exec_prefix, "pyvenv.cfg")) +) + +if is_venv_win32: # For virtual environments on Windows, add folder # with DPC++ libraries to the DLL search path gh-1745 - if "VIRTUAL_ENV" in os.environ: - venv_dir = os.environ["VIRTUAL_ENV"] - # Expect to find file per PEP-0405 - expected_file = os.path.join(venv_dir, "pyvenv.cfg") - dll_dir = os.path.join(venv_dir, "Library", "bin") - if os.path.isdir(dll_dir) and os.path.isfile(expected_file): - os.add_dll_directory(dll_dir) + dll_dir = os.path.join(sys.exec_prefix, "Library", "bin") + if os.path.isdir(dll_dir): + os.add_dll_directory(dll_dir) + +del is_venv_win32 From 2b08941d2ae129e657be5d6b1164c33b0623e5f8 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 6 Dec 2024 18:50:31 -0600 Subject: [PATCH 5/5] Add entry for 0.18.3 and item for gh-1924 --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ba0d90eb7..32dd2d47a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [0.18.2] - Nov. XX, 2024 +## [0.18.3] - Dec. XX, 2024 + +### Fixed + +* Enabled `dpctl` in virtual environment on Windows platform (issue [gh-1745](https://github.com/IntelPython/dpctl/issues/1745)) [gh-1924](https://github.com/IntelPython/dpctl/pull/1924) + +## [0.18.2] - Nov. 21, 2024 ### Maintenance