From cad68869237ee0f2b273c0eb3608eaaba527afb6 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Thu, 25 Jan 2024 11:43:56 -0600 Subject: [PATCH 1/3] dpctl/__main__.py no longer triggers loading of libsycl library for queries `python -m dpctl --includes` will no longer load SYCL runtime libraries. Module file acquired three more options --include-dir --tensor-include-dir --library-dir This would output absolute path to directory of include headers, tensor include hedaers, and directory containing DPCTLSyclInterface library. `FindDpctl.cmake` has been renamed to `dpctl-config.cmake`. This allows downstream objects to hint CMake to finding Dpctl package by using standard mechanism: `-DDpctl_ROOT=$(python -m dpctl --cmakedir)` --- cmake/{FindDpctl.cmake => dpctl-config.cmake} | 2 +- dpctl/__main__.py | 62 +++++++++++++++---- dpctl/tests/test_service.py | 47 +++++++++++++- 3 files changed, 97 insertions(+), 14 deletions(-) rename cmake/{FindDpctl.cmake => dpctl-config.cmake} (97%) diff --git a/cmake/FindDpctl.cmake b/cmake/dpctl-config.cmake similarity index 97% rename from cmake/FindDpctl.cmake rename to cmake/dpctl-config.cmake index 149c75bd51..b8c28fc570 100644 --- a/cmake/FindDpctl.cmake +++ b/cmake/dpctl-config.cmake @@ -22,7 +22,7 @@ if(NOT Dpctl_FOUND) if(Python_EXECUTABLE) execute_process(COMMAND "${Python_EXECUTABLE}" - -c "import dpctl; print(dpctl.get_include())" + -m dpctl --include-dir OUTPUT_VARIABLE _dpctl_include_dir OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET diff --git a/dpctl/__main__.py b/dpctl/__main__.py index 78c5f7fde0..9b51d74903 100644 --- a/dpctl/__main__.py +++ b/dpctl/__main__.py @@ -15,42 +15,57 @@ # limitations under the License. import argparse +import importlib import os import os.path import platform import sys import warnings -import dpctl - def _dpctl_dir() -> str: - abs_path = os.path.abspath(dpctl.__file__) - dpctl_dir = os.path.dirname(abs_path) - return dpctl_dir + dpctl_dir = importlib.util.find_spec("dpctl").submodule_search_locations[0] + abs_dpctl_dir = os.path.abspath(dpctl_dir) + return abs_dpctl_dir -def print_includes() -> None: +def get_include_dir() -> str: "Prints include flags for dpctl and SyclInterface library" - print("-I " + dpctl.get_include()) + return os.path.join(_dpctl_dir(), "include") -def print_tensor_includes() -> None: +def print_include_flags() -> None: "Prints include flags for dpctl and SyclInterface library" + print("-I " + get_include_dir()) + + +def get_tensor_include_dir() -> str: dpctl_dir = _dpctl_dir() libtensor_dir = os.path.join(dpctl_dir, "tensor", "libtensor", "include") + return libtensor_dir + + +def print_tensor_include_flags() -> None: + "Prints include flags for dpctl and SyclInterface library" + libtensor_dir = get_tensor_include_dir() print("-I " + libtensor_dir) def print_cmake_dir() -> None: "Prints directory with FindDpctl.cmake" dpctl_dir = _dpctl_dir() - print(os.path.join(dpctl_dir, "resources", "cmake")) + cmake_dir = os.path.join(dpctl_dir, "resources", "cmake") + print(cmake_dir) + + +def get_library_dir() -> str: + dpctl_dir = _dpctl_dir() + return dpctl_dir def print_library() -> None: "Prints linker flags for SyclInterface library" - dpctl_dir = _dpctl_dir() + dpctl_dir = get_library_dir() plt = platform.platform() ld_flags = "-L " + dpctl_dir if plt != "Windows": @@ -73,6 +88,8 @@ def _warn_if_any_set(args, li) -> None: def print_lsplatform(verbosity: int) -> None: + import dpctl + dpctl.lsplatform(verbosity=verbosity) @@ -84,11 +101,21 @@ def main() -> None: action="store_true", help="Include flags for dpctl headers.", ) + parser.add_argument( + "--include-dir", + action="store_true", + help="Path to dpctl include directory.", + ) parser.add_argument( "--tensor-includes", action="store_true", help="Include flags for dpctl libtensor headers.", ) + parser.add_argument( + "--tensor-include-dir", + action="store_true", + help="Path to dpctl libtensor include directory.", + ) parser.add_argument( "--cmakedir", action="store_true", @@ -99,6 +126,11 @@ def main() -> None: action="store_true", help="Linker flags for SyclInterface library.", ) + parser.add_argument( + "--library-dir", + action="store_true", + help="Path to directory containing DPCTLSyclInterface library", + ) parser.add_argument( "-f", "--full-list", @@ -139,13 +171,19 @@ def main() -> None: print_lsplatform(0) return if args.includes: - print_includes() + print_include_flags() + if args.include_dir: + print(get_include_dir()) if args.tensor_includes: - print_tensor_includes() + print_tensor_include_flags() + if args.tensor_include_dir: + print(get_tensor_include_dir()) if args.cmakedir: print_cmake_dir() if args.library: print_library() + if args.library_dir: + print(get_library_dir()) if __name__ == "__main__": diff --git a/dpctl/tests/test_service.py b/dpctl/tests/test_service.py index f16ff51227..754730e8d1 100644 --- a/dpctl/tests/test_service.py +++ b/dpctl/tests/test_service.py @@ -173,13 +173,30 @@ def test_syclinterface(): raise RuntimeError("Unsupported system") +def test_main_include_dir(): + res = subprocess.run( + [sys.executable, "-m", "dpctl", "--include-dir"], capture_output=True + ) + assert res.returncode == 0 + assert res.stdout + dir_path = res.stdout.decode("utf-8").strip() + assert os.path.exists(dir_path) + + def test_main_includes(): res = subprocess.run( [sys.executable, "-m", "dpctl", "--includes"], capture_output=True ) assert res.returncode == 0 assert res.stdout - assert res.stdout.decode("utf-8").startswith("-I") + flags = res.stdout.decode("utf-8") + res = subprocess.run( + [sys.executable, "-m", "dpctl", "--include-dir"], capture_output=True + ) + assert res.returncode == 0 + assert res.stdout + dir = res.stdout.decode("utf-8") + assert flags == "-I " + dir def test_main_library(): @@ -191,6 +208,34 @@ def test_main_library(): assert res.stdout.decode("utf-8").startswith("-L") +def test_tensor_includes(): + res = subprocess.run( + [sys.executable, "-m", "dpctl", "--tensor-includes"], + capture_output=True, + ) + assert res.returncode == 0 + assert res.stdout + flags = res.stdout.decode("utf-8") + res = subprocess.run( + [sys.executable, "-m", "dpctl", "--tensor-include-dir"], + capture_output=True, + ) + assert res.returncode == 0 + assert res.stdout + dir = res.stdout.decode("utf-8") + assert flags == "-I " + dir + + +def test_main_library_dir(): + res = subprocess.run( + [sys.executable, "-m", "dpctl", "--library-dir"], capture_output=True + ) + assert res.returncode == 0 + assert res.stdout + dir_path = res.stdout.decode("utf-8").strip() + assert os.path.exists(dir_path) + + def test_cmakedir(): res = subprocess.run( [sys.executable, "-m", "dpctl", "--cmakedir"], capture_output=True From 4fa17ec08db3ace8d41d0acbd2607cdd655da5c4 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Thu, 25 Jan 2024 14:20:32 -0600 Subject: [PATCH 2/3] Install dpctl-config.cmake to lib/cmake folder Update comments in dpctl-config.cmake --- CMakeLists.txt | 4 ++++ cmake/dpctl-config.cmake | 11 +++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7688ff040c..eb1346a423 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,10 @@ file(GLOB _cmake_scripts ${CMAKE_SOURCE_DIR}/cmake/*.cmake) install(FILES ${_cmake_scripts} DESTINATION dpctl/resources/cmake ) +install(FILES + ${CMAKE_SOURCE_DIR}/cmake/dpctl-config.cmake + DESTINATION lib/cmake/dpctl +) if (DPCTL_GENERATE_DOCS) add_subdirectory(docs) diff --git a/cmake/dpctl-config.cmake b/cmake/dpctl-config.cmake index b8c28fc570..fa3f136b47 100644 --- a/cmake/dpctl-config.cmake +++ b/cmake/dpctl-config.cmake @@ -6,14 +6,17 @@ # # ``Dpctl_FOUND`` # True if DPCTL was found. -# ``Dpctl_INCLUDE_DIRS`` -# The include directories needed to use Dpctl. +# ``Dpctl_INCLUDE_DIR`` +# The include directory needed to use dpctl. +# ``Dpctl_TENSOR_INCLUDE_DIR`` +# The include directory for tensor kernels implementation. # ``Dpctl_VERSION`` -# The version of DPCTL found. +# The version of dpctl found. # -# The module will also explicitly define one cache variable: +# The module will also explicitly define two cache variables: # # ``Dpctl_INCLUDE_DIR`` +# ``Dpctl_TENSOR_INCLUDE_DIR`` # if(NOT Dpctl_FOUND) From 6e30b848849f6a72ebed398b51675ddbbe835b6c Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Thu, 25 Jan 2024 15:22:27 -0600 Subject: [PATCH 3/3] Replaced FindDpctl hardcoded name in test_service.py --- dpctl/tests/test_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpctl/tests/test_service.py b/dpctl/tests/test_service.py index 754730e8d1..328e1371a0 100644 --- a/dpctl/tests/test_service.py +++ b/dpctl/tests/test_service.py @@ -243,7 +243,7 @@ def test_cmakedir(): assert res.returncode == 0 assert res.stdout cmake_dir = res.stdout.decode("utf-8").strip() - assert os.path.exists(os.path.join(cmake_dir, "FindDpctl.cmake")) + assert os.path.exists(os.path.join(cmake_dir, "dpctl-config.cmake")) def test_main_full_list():