Skip to content

Dpctl c api constructors added, dpctl/include restructured #685

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 9 commits into from
Dec 2, 2021
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include versioneer.py
recursive-include dpctl/include *.h
recursive-include dpctl/tensor/include *
include dpctl/include/dpctl4pybind11.hpp
recursive-include dpctl *.pxd
include dpctl/_sycl_context.h
include dpctl/_sycl_context_api.h
Expand All @@ -14,5 +14,6 @@ include dpctl/memory/_memory.h
include dpctl/memory/_memory_api.h
include dpctl/tensor/_usmarray.h
include dpctl/tensor/_usmarray_api.h
recursive-include dpctl/tensor/include *
include dpctl/tests/input_files/*
include dpctl/tests/*.pyx
34 changes: 17 additions & 17 deletions dpctl/_backend.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ from libc.stdint cimport int64_t, uint32_t
from libcpp cimport bool


cdef extern from "dpctl_error_handler_type.h":
cdef extern from "syclinterface/dpctl_error_handler_type.h":
ctypedef void error_handler_callback(int err_code)

cdef extern from "dpctl_utils.h":
cdef extern from "syclinterface/dpctl_utils.h":
cdef void DPCTLCString_Delete(const char *str)
cdef void DPCTLSize_t_Array_Delete(size_t *arr)


cdef extern from "dpctl_sycl_enum_types.h":
cdef extern from "syclinterface/dpctl_sycl_enum_types.h":
ctypedef enum _backend_type 'DPCTLSyclBackendType':
_ALL_BACKENDS 'DPCTL_ALL_BACKENDS'
_CUDA 'DPCTL_CUDA'
Expand Down Expand Up @@ -111,7 +111,7 @@ cdef extern from "dpctl_sycl_enum_types.h":
_COMPLETE 'DPCTL_COMPLETE'


cdef extern from "dpctl_sycl_types.h":
cdef extern from "syclinterface/dpctl_sycl_types.h":
cdef struct DPCTLOpaqueSyclContext
cdef struct DPCTLOpaqueSyclDevice
cdef struct DPCTLOpaqueSyclDeviceSelector
Expand All @@ -133,12 +133,12 @@ cdef extern from "dpctl_sycl_types.h":
ctypedef DPCTLOpaqueSyclUSM *DPCTLSyclUSMRef


cdef extern from "dpctl_sycl_device_manager.h":
cdef extern from "syclinterface/dpctl_sycl_device_manager.h":
cdef struct DPCTLDeviceVector
ctypedef DPCTLDeviceVector *DPCTLDeviceVectorRef


cdef extern from "dpctl_sycl_device_interface.h":
cdef extern from "syclinterface/dpctl_sycl_device_interface.h":
cdef bool DPCTLDevice_AreEq(const DPCTLSyclDeviceRef DRef1,
const DPCTLSyclDeviceRef DRef2)
cdef DPCTLSyclDeviceRef DPCTLDevice_Copy(const DPCTLSyclDeviceRef DRef)
Expand Down Expand Up @@ -192,7 +192,7 @@ cdef extern from "dpctl_sycl_device_interface.h":
cdef DPCTLSyclDeviceRef DPCTLDevice_GetParentDevice(const DPCTLSyclDeviceRef DRef)


cdef extern from "dpctl_sycl_device_manager.h":
cdef extern from "syclinterface/dpctl_sycl_device_manager.h":
cdef DPCTLDeviceVectorRef DPCTLDeviceVector_CreateFromArray(
size_t nelems,
DPCTLSyclDeviceRef *elems)
Expand All @@ -213,7 +213,7 @@ cdef extern from "dpctl_sycl_device_manager.h":
cdef int64_t DPCTLDeviceMgr_GetRelativeId(const DPCTLSyclDeviceRef DRef)


cdef extern from "dpctl_sycl_device_selector_interface.h":
cdef extern from "syclinterface/dpctl_sycl_device_selector_interface.h":
DPCTLSyclDeviceSelectorRef DPCTLAcceleratorSelector_Create()
DPCTLSyclDeviceSelectorRef DPCTLDefaultSelector_Create()
DPCTLSyclDeviceSelectorRef DPCTLCPUSelector_Create()
Expand All @@ -224,7 +224,7 @@ cdef extern from "dpctl_sycl_device_selector_interface.h":
int DPCTLDeviceSelector_Score(DPCTLSyclDeviceSelectorRef, DPCTLSyclDeviceRef)


cdef extern from "dpctl_sycl_event_interface.h":
cdef extern from "syclinterface/dpctl_sycl_event_interface.h":
cdef DPCTLSyclEventRef DPCTLEvent_Create()
cdef DPCTLSyclEventRef DPCTLEvent_Copy(const DPCTLSyclEventRef ERef)
cdef void DPCTLEvent_Wait(DPCTLSyclEventRef ERef)
Expand All @@ -246,13 +246,13 @@ cdef extern from "dpctl_sycl_event_interface.h":
cdef size_t DPCTLEvent_GetProfilingInfoEnd(DPCTLSyclEventRef ERef)


cdef extern from "dpctl_sycl_kernel_interface.h":
cdef extern from "syclinterface/dpctl_sycl_kernel_interface.h":
cdef const char* DPCTLKernel_GetFunctionName(const DPCTLSyclKernelRef KRef)
cdef size_t DPCTLKernel_GetNumArgs(const DPCTLSyclKernelRef KRef)
cdef void DPCTLKernel_Delete(DPCTLSyclKernelRef KRef)


cdef extern from "dpctl_sycl_platform_manager.h":
cdef extern from "syclinterface/dpctl_sycl_platform_manager.h":
cdef struct DPCTLPlatformVector
ctypedef DPCTLPlatformVector *DPCTLPlatformVectorRef

Expand All @@ -265,7 +265,7 @@ cdef extern from "dpctl_sycl_platform_manager.h":
cdef void DPCTLPlatformMgr_PrintInfo(const DPCTLSyclPlatformRef, size_t)


cdef extern from "dpctl_sycl_platform_interface.h":
cdef extern from "syclinterface/dpctl_sycl_platform_interface.h":
cdef DPCTLSyclPlatformRef DPCTLPlatform_Copy(const DPCTLSyclPlatformRef)
cdef DPCTLSyclPlatformRef DPCTLPlatform_Create()
cdef DPCTLSyclPlatformRef DPCTLPlatform_CreateFromSelector(
Expand All @@ -278,7 +278,7 @@ cdef extern from "dpctl_sycl_platform_interface.h":
cdef DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms()


cdef extern from "dpctl_sycl_context_interface.h":
cdef extern from "syclinterface/dpctl_sycl_context_interface.h":
cdef DPCTLSyclContextRef DPCTLContext_Create(
const DPCTLSyclDeviceRef DRef,
error_handler_callback *handler,
Expand All @@ -299,7 +299,7 @@ cdef extern from "dpctl_sycl_context_interface.h":
cdef void DPCTLContext_Delete(DPCTLSyclContextRef CtxRef)


cdef extern from "dpctl_sycl_program_interface.h":
cdef extern from "syclinterface/dpctl_sycl_program_interface.h":
cdef DPCTLSyclProgramRef DPCTLProgram_CreateFromSpirv(
const DPCTLSyclContextRef Ctx,
const void *IL,
Expand All @@ -317,7 +317,7 @@ cdef extern from "dpctl_sycl_program_interface.h":
cdef void DPCTLProgram_Delete(DPCTLSyclProgramRef PRef)


cdef extern from "dpctl_sycl_queue_interface.h":
cdef extern from "syclinterface/dpctl_sycl_queue_interface.h":
cdef bool DPCTLQueue_AreEq(const DPCTLSyclQueueRef QRef1,
const DPCTLSyclQueueRef QRef2)
cdef DPCTLSyclQueueRef DPCTLQueue_Create(
Expand Down Expand Up @@ -381,7 +381,7 @@ cdef extern from "dpctl_sycl_queue_interface.h":
cdef bool DPCTLQueue_HasEnableProfiling(const DPCTLSyclQueueRef QRef)


cdef extern from "dpctl_sycl_queue_manager.h":
cdef extern from "syclinterface/dpctl_sycl_queue_manager.h":
cdef DPCTLSyclQueueRef DPCTLQueueMgr_GetCurrentQueue()
cdef bool DPCTLQueueMgr_GlobalQueueIsCurrent()
cdef bool DPCTLQueueMgr_IsCurrentQueue(const DPCTLSyclQueueRef QRef)
Expand All @@ -391,7 +391,7 @@ cdef extern from "dpctl_sycl_queue_manager.h":
cdef size_t DPCTLQueueMgr_GetQueueStackSize()


cdef extern from "dpctl_sycl_usm_interface.h":
cdef extern from "syclinterface/dpctl_sycl_usm_interface.h":
cdef DPCTLSyclUSMRef DPCTLmalloc_shared(
size_t size,
DPCTLSyclQueueRef QRef)
Expand Down
9 changes: 9 additions & 0 deletions dpctl/_sycl_context.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,12 @@ cdef api DPCTLSyclContextRef get_context_ref(SyclContext ctx):
:class:`dpctl.SyclContext` instance.
"""
return ctx.get_context_ref()


cdef api SyclContext make_SyclContext(DPCTLSyclContextRef CRef):
"""
C-API function to create :class:`dpctl.SyclContext` instance
from the given opaque context reference.
"""
cdef DPCTLSyclContextRef copied_CRef = DPCTLContext_Copy(CRef)
return SyclContext._create(copied_CRef)
9 changes: 9 additions & 0 deletions dpctl/_sycl_device.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1136,3 +1136,12 @@ cdef api DPCTLSyclDeviceRef get_device_ref(SyclDevice dev):
:class:`dpctl.SyclDevice` instance.
"""
return dev.get_device_ref()


cdef api SyclDevice make_SyclDevice(DPCTLSyclDeviceRef DRef):
"""
C-API function to create :class:`dpctl.SyclDevice` instance
from the given opaque device reference.
"""
cdef DPCTLSyclDeviceRef copied_DRef = DPCTLDevice_Copy(DRef)
return SyclDevice._create(copied_DRef)
9 changes: 9 additions & 0 deletions dpctl/_sycl_event.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ cdef api DPCTLSyclEventRef get_event_ref(SyclEvent ev):
return ev.get_event_ref()


cdef api SyclEvent make_SyclEvent(DPCTLSyclEventRef ERef):
"""
C-API function to create :class:`dpctl.SyclEvent`
instance from opaque sycl event reference.
"""
cdef DPCTLSyclEventRef copied_ERef = DPCTLEvent_Copy(ERef)
return SyclEvent._create(copied_ERef)


cdef void _event_capsule_deleter(object o):
cdef DPCTLSyclEventRef ERef = NULL
if pycapsule.PyCapsule_IsValid(o, "SyclEventRef"):
Expand Down
9 changes: 9 additions & 0 deletions dpctl/_sycl_queue.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1007,3 +1007,12 @@ cdef api DPCTLSyclQueueRef get_queue_ref(SyclQueue q):
:class:`dpctl.SyclQueue` instance.
"""
return q.get_queue_ref()


cdef api SyclQueue make_SyclQueue(DPCTLSyclQueueRef QRef):
"""
C-API function to create :class:`dpctl.SyclQueue` instance
from the given opaque queue reference.
"""
cdef DPCTLSyclQueueRef copied_QRef = DPCTLQueue_Copy(QRef)
return SyclQueue._create(copied_QRef)
186 changes: 186 additions & 0 deletions dpctl/apis/include/dpctl4pybind11.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
//===----------- dpctl4pybind11.h - Headers for type pybind11 casters -*-C-*-
//===//
//
// 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.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file defines imports for dcptl's Python C-API
//===----------------------------------------------------------------------===//

#pragma once

#include "dpctl_capi.h"
#include <CL/sycl.hpp>
#include <pybind11/pybind11.h>

namespace py = pybind11;

namespace pybind11
{
namespace detail
{

/* This type caster associates ``sycl::queue`` C++ class with
* :class:`dpctl.SyclQueue` for the purposes of generation of
* Python bindings by pybind11.
*/
template <> struct type_caster<sycl::queue>
{
public:
PYBIND11_TYPE_CASTER(sycl::queue, _("dpctl.SyclQueue"));

bool load(handle src, bool)
{
PyObject *source = src.ptr();
if (PyObject_TypeCheck(source, &PySyclQueueType)) {
DPCTLSyclQueueRef QRef =
get_queue_ref(reinterpret_cast<PySyclQueueObject *>(source));
sycl::queue *q = reinterpret_cast<sycl::queue *>(QRef);
value = *q;
return true;
}
else {
throw std::runtime_error(
"Input is of unexpected type, expected dpctl.SyclQueue");
}
}

static handle cast(sycl::queue src, return_value_policy, handle)
{
auto tmp = make_SyclQueue(reinterpret_cast<DPCTLSyclQueueRef>(&src));
return handle(reinterpret_cast<PyObject *>(tmp));
}
};
} // namespace detail
} // namespace pybind11

namespace pybind11
{
namespace detail
{
/* This type caster associates ``sycl::device`` C++ class with
* :class:`dpctl.SyclDevice` for the purposes of generation of
* Python bindings by pybind11.
*/
template <> struct type_caster<sycl::device>
{
public:
PYBIND11_TYPE_CASTER(sycl::device, _("dpctl.SyclDevice"));

bool load(handle src, bool)
{
PyObject *source = src.ptr();
if (PyObject_TypeCheck(source, &PySyclDeviceType)) {
DPCTLSyclDeviceRef DRef =
get_device_ref(reinterpret_cast<PySyclDeviceObject *>(source));
sycl::device *d = reinterpret_cast<sycl::device *>(DRef);
value = *d;
return true;
}
else {
throw std::runtime_error(
"Input is of unexpected type, expected dpctl.SyclDevice");
}
}

static handle cast(sycl::device src, return_value_policy, handle)
{
auto tmp = make_SyclDevice(reinterpret_cast<DPCTLSyclDeviceRef>(&src));
return handle(reinterpret_cast<PyObject *>(tmp));
}
};
} // namespace detail
} // namespace pybind11

namespace pybind11
{
namespace detail
{
/* This type caster associates ``sycl::context`` C++ class with
* :class:`dpctl.SyclContext` for the purposes of generation of
* Python bindings by pybind11.
*/
template <> struct type_caster<sycl::context>
{
public:
PYBIND11_TYPE_CASTER(sycl::context, _("dpctl.SyclContext"));

bool load(handle src, bool)
{
PyObject *source = src.ptr();
if (PyObject_TypeCheck(source, &PySyclContextType)) {
DPCTLSyclContextRef CRef = get_context_ref(
reinterpret_cast<PySyclContextObject *>(source));
sycl::context *ctx = reinterpret_cast<sycl::context *>(CRef);
value = *ctx;
return true;
}
else {
throw std::runtime_error(
"Input is of unexpected type, expected dpctl.SyclContext");
}
}

static handle cast(sycl::context src, return_value_policy, handle)
{
auto tmp =
make_SyclContext(reinterpret_cast<DPCTLSyclContextRef>(&src));
return handle(reinterpret_cast<PyObject *>(tmp));
}
};
} // namespace detail
} // namespace pybind11

namespace pybind11
{
namespace detail
{
/* This type caster associates ``sycl::event`` C++ class with
* :class:`dpctl.SyclEvent` for the purposes of generation of
* Python bindings by pybind11.
*/
template <> struct type_caster<sycl::event>
{
public:
PYBIND11_TYPE_CASTER(sycl::event, _("dpctl.SyclEvent"));

bool load(handle src, bool)
{
PyObject *source = src.ptr();
if (PyObject_TypeCheck(source, &PySyclEventType)) {
DPCTLSyclEventRef ERef =
get_event_ref(reinterpret_cast<PySyclEventObject *>(source));
sycl::event *ev = reinterpret_cast<sycl::event *>(ERef);
value = *ev;
return true;
}
else {
throw std::runtime_error(
"Input is of unexpected type, expected dpctl.SyclEvent");
}
}

static handle cast(sycl::event src, return_value_policy, handle)
{
auto tmp = make_SyclEvent(reinterpret_cast<DPCTLSyclEventRef>(&src));
return handle(reinterpret_cast<PyObject *>(tmp));
}
};
} // namespace detail
} // namespace pybind11
Loading