Skip to content

Commit 221a1fc

Browse files
[EXAMPLE] Example of calling MKL on usm_ndarray inputs (#780)
* Example of calling MKL on usm_ndarray inputs * gemv function returns a pair of events, first for host task, second for MKL computation * test_example job needs scikit-build
2 parents aa7071f + 5f428fd commit 221a1fc

File tree

9 files changed

+397
-18
lines changed

9 files changed

+397
-18
lines changed

.github/workflows/conda-package.yml

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ jobs:
317317
runs-on: ${{ matrix.runner }}
318318
strategy:
319319
matrix:
320-
python: [3.8]
320+
python: [3.9]
321321
experimental: [false]
322322
runner: [ubuntu-latest]
323323
continue-on-error: ${{ matrix.experimental }}
@@ -363,57 +363,79 @@ jobs:
363363
restore-keys: |
364364
${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-python-${{ matrix.python }}-
365365
${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-
366-
- name: Install dpctl
366+
- name: Install example requirements
367367
shell: bash -l {0}
368368
run: |
369+
CHANNELS="${{ env.CHANNELS }}"
369370
source $CONDA/etc/profile.d/conda.sh
370-
conda activate
371-
CHANNELS="-c $GITHUB_WORKSPACE/channel ${{ env.CHANNELS }}"
372-
conda install -y $PACKAGE_NAME pytest python=${{ matrix.python }} $CHANNELS
373-
# Test installed packages
374-
conda list
375-
- name: Install example requirements
371+
conda create -n examples -y pytest python=${{ matrix.python }} $CHANNELS
372+
conda install -n examples -y cmake">=3.22" $CHANNELS || exit 1
373+
conda install -n examples -y ninja $CHANNELS || exit 1
374+
conda install -n examples -y pybind11 cython scikit-build $CHANNELS || exit 1
375+
conda install -n examples -y mkl-dpcpp mkl-devel-dpcpp dpcpp_cpp_rt $CHANNELS || exit 1
376+
conda create -y -n build_env -c intel dpcpp_linux-64
377+
- name: Install dpctl
376378
shell: bash -l {0}
377379
run: |
378380
source $CONDA/etc/profile.d/conda.sh
379-
conda install -y pybind11 cython
380-
conda install -y -c intel mkl-dpcpp mkl-devel-dpcpp numba-dppy
381-
conda create -y -n build_env -c intel dpcpp_linux-64
381+
conda activate
382+
CHANNELS="-c $GITHUB_WORKSPACE/channel -c dppy/label/dev -c intel --override-channels"
383+
conda install -n examples -y $CHANNELS numba-dppy numpy dpctl || exit 1
382384
- name: Build and run examples with native extensions
383385
shell: bash -l {0}
384386
run: |
385387
source $CONDA/etc/profile.d/conda.sh
386388
export OCL_ICD_FILENAMES=libintelocl.so
387389
export SYCL_ENABLE_HOST_DEVICE=1
388-
conda activate
390+
conda activate examples
391+
conda list
389392
cd examples/pybind11
390-
export CC=dpcpp
391-
export CXX=dpcpp
392393
for d in $(ls)
393394
do
394395
pushd $d
396+
export MKLROOT=${CONDA_PREFIX}
397+
export TBBROOT=${CONDA_PREFIX}
395398
conda activate --stack build_env
396-
python setup.py build_ext --inplace || exit 1
399+
if [ -e CMakeLists.txt ]
400+
then
401+
python setup.py build_ext --inplace -- -G Ninja \
402+
-DCMAKE_C_COMPILER:PATH=icx -DCMAKE_CXX_COMPILER:PATH=icpx \
403+
-DTBB_LIBRARY_DIR=${TBBROOT}/lib \
404+
-DMKL_LIBRARY_DIR=${MKLROOT}/lib \
405+
-DMKL_INCLUDE_DIR=${MKLROOT}/include \
406+
-DTBB_INCLUDE_DIR=${TBBROOT}/include || exit 1
407+
else
408+
CC=dpcpp CXX=dpcpp LD_SHARED="dpcpp -shared" \
409+
python setup.py build_ext --inplace || exit 1
410+
fi
397411
conda deactivate
398-
python example.py
412+
if [ -e tests ]
413+
then
414+
LD_LIBRARY_PATH=${CONDA_PREFIX}/lib python -m pytest tests || exit 1
415+
else
416+
LD_LIBRARY_PATH=${CONDA_PREFIX}/lib python example.py || exit 1
417+
fi
399418
popd
400419
done
401420
cd ../cython
402421
for d in $(ls)
403422
do
404423
pushd $d
405424
conda activate --stack build_env
406-
python setup.py build_ext --inplace || exit 1
425+
CC=dpcpp CXX=dpcpp LD_SHARED="dpcpp -shared" \
426+
python setup.py build_ext --inplace || exit 1
407427
conda deactivate
408-
python run.py
428+
LD_LIBRARY_PATH=${CONDA_PREFIX}/lib python run.py || exit 1
409429
popd
410430
done
411431
- name: Run Python examples
412432
shell: bash -l {0}
413433
run: |
414434
cd examples/python
435+
source $CONDA/etc/profile.d/conda.sh
415436
export OCL_ICD_FILENAMES=libintelocl.so
416437
export SYCL_ENABLE_HOST_DEVICE=1
438+
conda activate examples
417439
for script in $(find . \( -not -name "_*" -and -name "*.py" \))
418440
do
419441
echo "Executing ${script}"

cmake/FindDpctl.cmake

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#.rst:
2+
#
3+
# Find the include directory for ``dpctl_capi.h``, ``dpctl4pybind11.hpp``.
4+
#
5+
# This module sets the following variables:
6+
#
7+
# ``Dpctl_FOUND``
8+
# True if DPCTL was found.
9+
# ``Dpctl_INCLUDE_DIRS``
10+
# The include directories needed to use Dpctl.
11+
# ``Dpctl_VERSION``
12+
# The version of DPCTL found.
13+
#
14+
# The module will also explicitly define one cache variable:
15+
#
16+
# ``Dpctl_INCLUDE_DIR``
17+
#
18+
19+
if(NOT Dpctl_FOUND)
20+
set(_find_extra_args)
21+
if(Dpctl_FIND_REQUIRED)
22+
list(APPEND _find_extra_args REQUIRED)
23+
endif()
24+
if(Dpctl_FIND_QUIET)
25+
list(APPEND _find_extra_args QUIET)
26+
endif()
27+
find_package(PythonInterp ${_find_extra_args})
28+
find_package(PythonLibs ${_find_extra_args})
29+
30+
if(PYTHON_EXECUTABLE)
31+
execute_process(COMMAND "${PYTHON_EXECUTABLE}"
32+
-c "import dpctl; print(dpctl.get_include())"
33+
OUTPUT_VARIABLE _dpctl_include_dir
34+
OUTPUT_STRIP_TRAILING_WHITESPACE
35+
ERROR_QUIET
36+
)
37+
execute_process(COMMAND "${PYTHON_EXECUTABLE}"
38+
-c "import dpctl; print(dpctl.__version__)"
39+
OUTPUT_VARIABLE Dpctl_VERSION
40+
OUTPUT_STRIP_TRAILING_WHITESPACE
41+
ERROR_QUIET
42+
)
43+
44+
endif()
45+
endif()
46+
47+
find_path(Dpctl_INCLUDE_DIR
48+
dpctl_capi.h dpctl4pybind11.hpp dpctl_sycl_interface.h
49+
PATHS "${_dpctl_include_dir}" "${PYTHON_INCLUDE_DIR}"
50+
PATH_SUFFIXES dpctl/include
51+
)
52+
53+
set(Dpctl_INCLUDE_DIRS ${Dpctl_INCLUDE_DIR})
54+
55+
# handle the QUIETLY and REQUIRED arguments and set Dpctl_FOUND to TRUE if
56+
# all listed variables are TRUE
57+
include(FindPackageHandleStandardArgs)
58+
find_package_handle_standard_args(Dpctl
59+
REQUIRED_VARS
60+
Dpctl_INCLUDE_DIR
61+
VERSION_VAR Dpctl_VERSION
62+
)
63+
64+
mark_as_advanced(Dpctl_INCLUDE_DIR)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
2+
3+
project(example_use_mkl_gemm LANGUAGES CXX)
4+
set(DPCTL_CMAKE_MODULES_PATH "${CMAKE_SOURCE_DIR}/../../../cmake")
5+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${DPCTL_CMAKE_MODULES_PATH})
6+
find_package(IntelDPCPP REQUIRED PATHS ${DPCTL_CMAKE_MODULES_PATH} NO_DEFAULT_PATH)
7+
8+
9+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}")
10+
set(CMAKE_CXX_STANDARD 17)
11+
set(CMAKE_CXX_STANDARD_REQUIRED True)
12+
13+
# Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR
14+
include(GNUInstallDirs)
15+
16+
# Fetch pybind11
17+
include(FetchContent)
18+
FetchContent_Declare(
19+
pybind11
20+
URL https://github.com/pybind/pybind11/archive/refs/tags/v2.9.0.tar.gz
21+
URL_HASH SHA256=057fb68dafd972bc13afb855f3b0d8cf0fa1a78ef053e815d9af79be7ff567cb
22+
)
23+
FetchContent_MakeAvailable(pybind11)
24+
25+
find_package(PythonExtensions REQUIRED)
26+
find_package(Dpctl REQUIRED)
27+
28+
find_library(mkl_core NAMES mkl_core PATH ${MKL_LIBRARY_DIR})
29+
find_library(mkl_sycl NAMES mkl_sycl PATH ${MKL_LIBRARY_DIR})
30+
find_library(mkl_intel_ilp64 NAMES mkl_intel_ilp64 PATH ${MKL_LIBRARY_DIR})
31+
find_library(mkl_tbb_thread NAMES mkl_tbb_thread PATH ${MKL_LIBRARY_DIR})
32+
find_library(tbb NAMES tbb PATH ${TBB_LIBRARY_DIR})
33+
34+
set(py_module_name _onemkl)
35+
36+
pybind11_add_module(${py_module_name}
37+
MODULE
38+
sycl_gemm/_onemkl.cpp
39+
)
40+
target_include_directories(${py_module_name}
41+
PUBLIC ${MKL_INCLUDE_DIR} ${TBB_INCLUDE_DIR}
42+
)
43+
target_link_libraries(${py_module_name}
44+
PUBLIC mkl_sycl mkl_intel_ilp64 mkl_tbb_thread mkl_core tbb
45+
)
46+
47+
install(TARGETS ${py_module_name} DESTINATION sycl_gemm)
48+
target_include_directories(${py_module_name} PUBLIC ${Dpctl_INCLUDE_DIRS})
49+
target_link_directories(${py_module_name} PUBLIC ${MKL_LIBRARY_DIR} ${TBB_LIBRARY_DIR})
50+
51+
get_target_property(_sycl_gemm_sources ${py_module_name} SOURCES)
52+
set_source_files_properties(${_sycl_gemm_sources}
53+
PROPERTIES
54+
COMPILE_OPTIONS "-O3;-Wno-deprecated-declarations"
55+
)
56+
57+
set(ignoreMe "${SKBUILD}")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Example of SYCL built pybind11 extension
2+
3+
To build, use (assumes scikit-build and dpcpp) is installed
4+
5+
```sh
6+
python setup.py develop -- -G "Ninja" -DCMAKE_C_COMPILER:PATH=icx -DCMAKE_CXX_COMPILER:PATH=icpx -DTBB_LIBRARY_DIR=$CONDA_PREFIX/lib -DMKL_LIBRARY_DIR=${CONDA_PREFIX}/lib -DMKL_INCLUDE_DIR=${CONDA_PREFIX}/include -DTBB_INCLUDE_DIR=${CONDA_PREFIX}/include
7+
```
8+
9+
To run test suite
10+
11+
```sh
12+
python -m pytest tests
13+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from skbuild import setup
2+
3+
setup(
4+
name="sycl_gemm",
5+
version="0.0.1",
6+
description="an example of SYCL-powered Python package (with pybind11)",
7+
author="Intel Scripting",
8+
license="Apache 2.0",
9+
packages=["sycl_gemm"],
10+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from ._onemkl import gemv
2+
3+
__all__ = ["gemv"]

0 commit comments

Comments
 (0)