Skip to content

Improve coverage of dpctl.__init__ file and other service functions #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions dpctl/tests/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Data Parallel Control (dpctl)
#
# Copyright 2020-2021 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.

""" Defines unit test cases for miscellaneous functions.
"""

import ctypes
import ctypes.util
import glob
import os
import os.path
import re

import dpctl


def _get_mkl_version_if_present():
class MKLVersion(ctypes.Structure):
_fields_ = [
("MajorVersion", ctypes.c_int),
("MinorVersion", ctypes.c_int),
("UpdateVersion", ctypes.c_int),
("ProductStatus", ctypes.c_char_p),
("Build", ctypes.c_char_p),
("Processor", ctypes.c_char_p),
("Platform", ctypes.c_char_p),
]

lib = ctypes.util.find_library("mkl_rt")
if lib is None:
return None
try:
lib = ctypes.cdll.LoadLibrary(lib)
get_ver_fn = lib.mkl_get_version
except Exception:
return None
get_ver_fn.argtypes = []
get_ver_fn.restype = MKLVersion
mkl_ver = get_ver_fn()
return ".".join(
[
str(mkl_ver.MajorVersion),
str(mkl_ver.UpdateVersion),
str(mkl_ver.MinorVersion),
]
)


def test_get_include():
incl = dpctl.get_include()
assert type(incl) is str
assert incl != ""
assert os.path.isdir(incl)


def test_get_dpcppversion():
incl_dir = dpctl.get_include()
libs = glob.glob(os.path.join(incl_dir, "..", "*DPCTLSyclInterface*"))
libs = sorted(libs)
assert len(libs) > 0
lib = ctypes.cdll.LoadLibrary(libs[0])
fn = lib.DPCTLService_GetDPCPPVersion
fn.restype = ctypes.c_char_p
fn.argtypes = []
dpcpp_ver = fn()
assert len(dpcpp_ver) > 0
dpcpp_ver = dpcpp_ver.decode("utf-8")
mkl_ver = _get_mkl_version_if_present()
assert mkl_ver is None or mkl_ver == dpcpp_ver


def test___version__():
dpctl_ver = getattr(dpctl, "__version__", None)
assert type(dpctl_ver) is str
assert "unknown" not in dpctl_ver
# Reg expr from PEP-440
reg_expr = (
r"^([1-9][0-9]*!)?(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))"
r"*((a|b|rc)(0|[1-9][0-9]*))?(\.post(0|[1-9][0-9]*))?(\.dev("
r"0|[1-9][0-9]*))?(\+.*)?$"
)
assert re.match(reg_expr, dpctl_ver) is not None
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ VCS = git
versionfile_source = dpctl/_version.py
versionfile_build = dpctl/_version.py
tag_prefix =
parentdir_prefix = DPCTL-
parentdir_prefix = dpctl-

[bdist_wheel]
universal=1
58 changes: 30 additions & 28 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import numpy as np
import setuptools.command.build_ext as orig_build_ext
import setuptools.command.build_py as orig_build_py
import setuptools.command.develop as orig_develop
import setuptools.command.install as orig_install
from Cython.Build import cythonize
Expand Down Expand Up @@ -254,32 +253,35 @@ def run(self):
return super().run()


class build_py(orig_build_py.build_py):
def run(self):
dpctl_src_dir = self.get_package_dir("dpctl")
dpctl_build_dir = os.path.join(self.build_lib, "dpctl")
os.makedirs(dpctl_build_dir, exist_ok=True)
if IS_LIN:
for fn in glob.glob(os.path.join(dpctl_src_dir, "*.so*")):
# Check if the file already exists before copying. The check is
# needed when dealing with symlinks.
if not os.path.exists(
os.path.join(dpctl_build_dir, os.path.basename(fn))
):
shutil.copy(
src=fn,
dst=dpctl_build_dir,
follow_symlinks=False,
)
elif IS_WIN:
for fn in glob.glob(os.path.join(dpctl_src_dir, "*.lib")):
shutil.copy(src=fn, dst=dpctl_build_dir)

for fn in glob.glob(os.path.join(dpctl_src_dir, "*.dll")):
shutil.copy(src=fn, dst=dpctl_build_dir)
else:
raise NotImplementedError("Unsupported platform")
return super().run()
def get_build_py(orig_build_py):
class build_py(orig_build_py):
def run(self):
dpctl_src_dir = self.get_package_dir("dpctl")
dpctl_build_dir = os.path.join(self.build_lib, "dpctl")
os.makedirs(dpctl_build_dir, exist_ok=True)
if IS_LIN:
for fn in glob.glob(os.path.join(dpctl_src_dir, "*.so*")):
# Check if the file already exists before copying.
# The check is needed when dealing with symlinks.
if not os.path.exists(
os.path.join(dpctl_build_dir, os.path.basename(fn))
):
shutil.copy(
src=fn,
dst=dpctl_build_dir,
follow_symlinks=False,
)
elif IS_WIN:
for fn in glob.glob(os.path.join(dpctl_src_dir, "*.lib")):
shutil.copy(src=fn, dst=dpctl_build_dir)

for fn in glob.glob(os.path.join(dpctl_src_dir, "*.dll")):
shutil.copy(src=fn, dst=dpctl_build_dir)
else:
raise NotImplementedError("Unsupported platform")
return super().run()

return build_py


class install(orig_install.install):
Expand Down Expand Up @@ -436,10 +438,10 @@ def run(self):

def _get_cmdclass():
cmdclass = versioneer.get_cmdclass()
cmdclass["build_py"] = get_build_py(cmdclass["build_py"])
cmdclass["install"] = install
cmdclass["develop"] = develop
cmdclass["build_ext"] = build_ext
cmdclass["build_py"] = build_py
return cmdclass


Expand Down