Skip to content

Commit 47c82f5

Browse files
Merge pull request #1348 from IntelPython/test-requires-compilers
Test environment requires compilers
2 parents 02fd974 + 7f7897c commit 47c82f5

File tree

5 files changed

+197
-1
lines changed

5 files changed

+197
-1
lines changed

conda-recipe/meta.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ requirements:
3333

3434
test:
3535
requires:
36+
- {{ compiler('c') }}
37+
- {{ compiler('cxx') }}
3638
- cython
3739
- setuptools
3840
- pytest

dpctl/tests/_c_ext.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//==- py_sycl-ls.c - Example of C extension working with -===//
2+
// DPCTLSyclInterface C-interface library.
3+
//
4+
// Data Parallel Control (dpctl)
5+
//
6+
// Copyright 2022 Intel Corporation
7+
//
8+
// Licensed under the Apache License, Version 2.0 (the "License");
9+
// you may not use this file except in compliance with the License.
10+
// You may obtain a copy of the License at
11+
//
12+
// http://www.apache.org/licenses/LICENSE-2.0
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
//
20+
//===----------------------------------------------------------------------===//
21+
///
22+
/// \file
23+
/// This file implements C Python extension using DPCTLSyclInterface library.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
// clang-format off
28+
#include "Python.h"
29+
#include "dpctl_capi.h"
30+
// clang-format on
31+
32+
PyObject *py_is_usm_ndarray(PyObject *self_unused, PyObject *args)
33+
{
34+
PyObject *arg = NULL;
35+
PyObject *res = NULL;
36+
int status = -1;
37+
int check = -1;
38+
39+
(void)(self_unused); // avoid unused arguments warning
40+
status = PyArg_ParseTuple(args, "O", &arg);
41+
if (!status) {
42+
PyErr_SetString(PyExc_TypeError, "Expecting single argument");
43+
return NULL;
44+
}
45+
46+
check = PyObject_TypeCheck(arg, &PyUSMArrayType);
47+
if (check == -1) {
48+
PyErr_SetString(PyExc_RuntimeError, "Type check failed");
49+
return NULL;
50+
}
51+
52+
res = (check) ? Py_True : Py_False;
53+
Py_INCREF(res);
54+
55+
return res;
56+
}
57+
58+
PyObject *py_usm_ndarray_ndim(PyObject *self_unused, PyObject *args)
59+
{
60+
PyObject *arg = NULL;
61+
struct PyUSMArrayObject *array = NULL;
62+
PyObject *res = NULL;
63+
int status = -1;
64+
int ndim = -1;
65+
66+
(void)(self_unused); // avoid unused arguments warning
67+
status = PyArg_ParseTuple(args, "O!", &PyUSMArrayType, &arg);
68+
if (!status) {
69+
PyErr_SetString(
70+
PyExc_TypeError,
71+
"Expecting single argument of type dpctl.tensor.usm_ndarray");
72+
return NULL;
73+
}
74+
75+
array = (struct PyUSMArrayObject *)arg;
76+
ndim = UsmNDArray_GetNDim(array);
77+
78+
res = PyLong_FromLong(ndim);
79+
return res;
80+
}
81+
82+
static PyMethodDef CExtMethods[] = {
83+
{"is_usm_ndarray", py_is_usm_ndarray, METH_VARARGS,
84+
"Checks if input object is an usm_ndarray instance"},
85+
{"usm_ndarray_ndim", py_usm_ndarray_ndim, METH_VARARGS,
86+
"Get ndim property of an usm_ndarray instance"},
87+
{NULL, NULL, 0, NULL} /* Sentinel */
88+
};
89+
90+
static struct PyModuleDef c_ext_module = {
91+
PyModuleDef_HEAD_INIT,
92+
"_c_ext", /* name of module */
93+
"", /* module documentation, may be NULL */
94+
-1, /* size of per-interpreter state of the module,
95+
or -1 if the module keeps state in global variables. */
96+
CExtMethods,
97+
NULL,
98+
NULL,
99+
NULL,
100+
NULL};
101+
102+
PyMODINIT_FUNC PyInit__c_ext(void)
103+
{
104+
PyObject *m = NULL;
105+
106+
import_dpctl();
107+
108+
m = PyModule_Create(&c_ext_module);
109+
return m;
110+
}

dpctl/tests/setup_c_ext.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2023 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 setuptools
18+
19+
import dpctl
20+
21+
ext = setuptools.Extension(
22+
"_c_ext",
23+
["_c_ext.c"],
24+
include_dirs=[dpctl.get_include()],
25+
language="c",
26+
)
27+
28+
setuptools.setup(
29+
name="test_c_ext",
30+
version="0.0.0",
31+
ext_modules=[ext],
32+
)

dpctl/tests/setup_cython_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Data Parallel Control (dpctl)
22
#
3-
# Copyright 2020-2022 Intel Corporation
3+
# Copyright 2020-2023 Intel Corporation
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.

dpctl/tests/test_headers.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytest
2+
3+
import dpctl
4+
import dpctl.tensor as dpt
5+
6+
7+
@pytest.fixture(scope="session")
8+
def dpctl_c_extension(tmp_path_factory):
9+
import os
10+
import os.path
11+
import shutil
12+
import subprocess
13+
import sys
14+
import sysconfig
15+
16+
curr_dir = os.path.dirname(__file__)
17+
dr = tmp_path_factory.mktemp("_c_ext")
18+
for fn in ["_c_ext.c", "setup_c_ext.py"]:
19+
shutil.copy(
20+
src=os.path.join(curr_dir, fn),
21+
dst=dr,
22+
follow_symlinks=False,
23+
)
24+
res = subprocess.run(
25+
[sys.executable, "setup_c_ext.py", "build_ext", "--inplace"],
26+
cwd=dr,
27+
env=os.environ,
28+
)
29+
if res.returncode == 0:
30+
import glob
31+
from importlib.util import module_from_spec, spec_from_file_location
32+
33+
sfx = sysconfig.get_config_vars()["EXT_SUFFIX"]
34+
pth = glob.glob(os.path.join(dr, "_c_ext*" + sfx))
35+
if not pth:
36+
pytest.fail("C extension was not built")
37+
spec = spec_from_file_location("_c_ext", pth[0])
38+
builder_module = module_from_spec(spec)
39+
spec.loader.exec_module(builder_module)
40+
return builder_module
41+
else:
42+
pytest.fail("C extension could not be built")
43+
44+
45+
def test_c_headers(dpctl_c_extension):
46+
try:
47+
x = dpt.empty(10)
48+
except (dpctl.SyclDeviceCreationError, dpctl.SyclQueueCreationError):
49+
pytest.skip()
50+
51+
assert dpctl_c_extension.is_usm_ndarray(x)
52+
assert dpctl_c_extension.usm_ndarray_ndim(x) == x.ndim

0 commit comments

Comments
 (0)