Skip to content

Test environment requires compilers #1348

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 3 commits into from
Aug 16, 2023
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
2 changes: 2 additions & 0 deletions conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ requirements:

test:
requires:
- {{ compiler('c') }}
- {{ compiler('cxx') }}
- cython
- setuptools
- pytest
Expand Down
110 changes: 110 additions & 0 deletions dpctl/tests/_c_ext.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//==- py_sycl-ls.c - Example of C extension working with -===//
// DPCTLSyclInterface C-interface library.
//
// Data Parallel Control (dpctl)
//
// Copyright 2022 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.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements C Python extension using DPCTLSyclInterface library.
///
//===----------------------------------------------------------------------===//

// clang-format off
#include "Python.h"
#include "dpctl_capi.h"
// clang-format on

PyObject *py_is_usm_ndarray(PyObject *self_unused, PyObject *args)
{
PyObject *arg = NULL;
PyObject *res = NULL;
int status = -1;
int check = -1;

(void)(self_unused); // avoid unused arguments warning
status = PyArg_ParseTuple(args, "O", &arg);
if (!status) {
PyErr_SetString(PyExc_TypeError, "Expecting single argument");
return NULL;
}

check = PyObject_TypeCheck(arg, &PyUSMArrayType);
if (check == -1) {
PyErr_SetString(PyExc_RuntimeError, "Type check failed");
return NULL;
}

res = (check) ? Py_True : Py_False;
Py_INCREF(res);

return res;
}

PyObject *py_usm_ndarray_ndim(PyObject *self_unused, PyObject *args)
{
PyObject *arg = NULL;
struct PyUSMArrayObject *array = NULL;
PyObject *res = NULL;
int status = -1;
int ndim = -1;

(void)(self_unused); // avoid unused arguments warning
status = PyArg_ParseTuple(args, "O!", &PyUSMArrayType, &arg);
if (!status) {
PyErr_SetString(
PyExc_TypeError,
"Expecting single argument of type dpctl.tensor.usm_ndarray");
return NULL;
}

array = (struct PyUSMArrayObject *)arg;
ndim = UsmNDArray_GetNDim(array);

res = PyLong_FromLong(ndim);
return res;
}

static PyMethodDef CExtMethods[] = {
{"is_usm_ndarray", py_is_usm_ndarray, METH_VARARGS,
"Checks if input object is an usm_ndarray instance"},
{"usm_ndarray_ndim", py_usm_ndarray_ndim, METH_VARARGS,
"Get ndim property of an usm_ndarray instance"},
{NULL, NULL, 0, NULL} /* Sentinel */
};

static struct PyModuleDef c_ext_module = {
PyModuleDef_HEAD_INIT,
"_c_ext", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
CExtMethods,
NULL,
NULL,
NULL,
NULL};

PyMODINIT_FUNC PyInit__c_ext(void)
{
PyObject *m = NULL;

import_dpctl();

m = PyModule_Create(&c_ext_module);
return m;
}
32 changes: 32 additions & 0 deletions dpctl/tests/setup_c_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Data Parallel Control (dpctl)
#
# Copyright 2020-2023 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 setuptools

import dpctl

ext = setuptools.Extension(
"_c_ext",
["_c_ext.c"],
include_dirs=[dpctl.get_include()],
language="c",
)

setuptools.setup(
name="test_c_ext",
version="0.0.0",
ext_modules=[ext],
)
2 changes: 1 addition & 1 deletion dpctl/tests/setup_cython_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Data Parallel Control (dpctl)
#
# Copyright 2020-2022 Intel Corporation
# Copyright 2020-2023 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
52 changes: 52 additions & 0 deletions dpctl/tests/test_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest

import dpctl
import dpctl.tensor as dpt


@pytest.fixture(scope="session")
def dpctl_c_extension(tmp_path_factory):
import os
import os.path
import shutil
import subprocess
import sys
import sysconfig

curr_dir = os.path.dirname(__file__)
dr = tmp_path_factory.mktemp("_c_ext")
for fn in ["_c_ext.c", "setup_c_ext.py"]:
shutil.copy(
src=os.path.join(curr_dir, fn),
dst=dr,
follow_symlinks=False,
)
res = subprocess.run(
[sys.executable, "setup_c_ext.py", "build_ext", "--inplace"],
cwd=dr,
env=os.environ,
)
if res.returncode == 0:
import glob
from importlib.util import module_from_spec, spec_from_file_location

sfx = sysconfig.get_config_vars()["EXT_SUFFIX"]
pth = glob.glob(os.path.join(dr, "_c_ext*" + sfx))
if not pth:
pytest.fail("C extension was not built")
spec = spec_from_file_location("_c_ext", pth[0])
builder_module = module_from_spec(spec)
spec.loader.exec_module(builder_module)
return builder_module
else:
pytest.fail("C extension could not be built")


def test_c_headers(dpctl_c_extension):
try:
x = dpt.empty(10)
except (dpctl.SyclDeviceCreationError, dpctl.SyclQueueCreationError):
pytest.skip()

assert dpctl_c_extension.is_usm_ndarray(x)
assert dpctl_c_extension.usm_ndarray_ndim(x) == x.ndim