|
| 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 | +} |
0 commit comments