Skip to content

Commit bbde0f5

Browse files
Merge pull request #685 from IntelPython/dpctl_c_api-constructors
2 parents 6fb23a3 + d42e403 commit bbde0f5

24 files changed

+577
-210
lines changed

MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include versioneer.py
22
recursive-include dpctl/include *.h
3-
recursive-include dpctl/tensor/include *
3+
include dpctl/include/dpctl4pybind11.hpp
44
recursive-include dpctl *.pxd
55
include dpctl/_sycl_context.h
66
include dpctl/_sycl_context_api.h
@@ -14,5 +14,6 @@ include dpctl/memory/_memory.h
1414
include dpctl/memory/_memory_api.h
1515
include dpctl/tensor/_usmarray.h
1616
include dpctl/tensor/_usmarray_api.h
17+
recursive-include dpctl/tensor/include *
1718
include dpctl/tests/input_files/*
1819
include dpctl/tests/*.pyx

dpctl/_backend.pxd

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ from libc.stdint cimport int64_t, uint32_t
2525
from libcpp cimport bool
2626

2727

28-
cdef extern from "dpctl_error_handler_type.h":
28+
cdef extern from "syclinterface/dpctl_error_handler_type.h":
2929
ctypedef void error_handler_callback(int err_code)
3030

31-
cdef extern from "dpctl_utils.h":
31+
cdef extern from "syclinterface/dpctl_utils.h":
3232
cdef void DPCTLCString_Delete(const char *str)
3333
cdef void DPCTLSize_t_Array_Delete(size_t *arr)
3434

3535

36-
cdef extern from "dpctl_sycl_enum_types.h":
36+
cdef extern from "syclinterface/dpctl_sycl_enum_types.h":
3737
ctypedef enum _backend_type 'DPCTLSyclBackendType':
3838
_ALL_BACKENDS 'DPCTL_ALL_BACKENDS'
3939
_CUDA 'DPCTL_CUDA'
@@ -111,7 +111,7 @@ cdef extern from "dpctl_sycl_enum_types.h":
111111
_COMPLETE 'DPCTL_COMPLETE'
112112

113113

114-
cdef extern from "dpctl_sycl_types.h":
114+
cdef extern from "syclinterface/dpctl_sycl_types.h":
115115
cdef struct DPCTLOpaqueSyclContext
116116
cdef struct DPCTLOpaqueSyclDevice
117117
cdef struct DPCTLOpaqueSyclDeviceSelector
@@ -133,12 +133,12 @@ cdef extern from "dpctl_sycl_types.h":
133133
ctypedef DPCTLOpaqueSyclUSM *DPCTLSyclUSMRef
134134

135135

136-
cdef extern from "dpctl_sycl_device_manager.h":
136+
cdef extern from "syclinterface/dpctl_sycl_device_manager.h":
137137
cdef struct DPCTLDeviceVector
138138
ctypedef DPCTLDeviceVector *DPCTLDeviceVectorRef
139139

140140

141-
cdef extern from "dpctl_sycl_device_interface.h":
141+
cdef extern from "syclinterface/dpctl_sycl_device_interface.h":
142142
cdef bool DPCTLDevice_AreEq(const DPCTLSyclDeviceRef DRef1,
143143
const DPCTLSyclDeviceRef DRef2)
144144
cdef DPCTLSyclDeviceRef DPCTLDevice_Copy(const DPCTLSyclDeviceRef DRef)
@@ -192,7 +192,7 @@ cdef extern from "dpctl_sycl_device_interface.h":
192192
cdef DPCTLSyclDeviceRef DPCTLDevice_GetParentDevice(const DPCTLSyclDeviceRef DRef)
193193

194194

195-
cdef extern from "dpctl_sycl_device_manager.h":
195+
cdef extern from "syclinterface/dpctl_sycl_device_manager.h":
196196
cdef DPCTLDeviceVectorRef DPCTLDeviceVector_CreateFromArray(
197197
size_t nelems,
198198
DPCTLSyclDeviceRef *elems)
@@ -213,7 +213,7 @@ cdef extern from "dpctl_sycl_device_manager.h":
213213
cdef int64_t DPCTLDeviceMgr_GetRelativeId(const DPCTLSyclDeviceRef DRef)
214214

215215

216-
cdef extern from "dpctl_sycl_device_selector_interface.h":
216+
cdef extern from "syclinterface/dpctl_sycl_device_selector_interface.h":
217217
DPCTLSyclDeviceSelectorRef DPCTLAcceleratorSelector_Create()
218218
DPCTLSyclDeviceSelectorRef DPCTLDefaultSelector_Create()
219219
DPCTLSyclDeviceSelectorRef DPCTLCPUSelector_Create()
@@ -224,7 +224,7 @@ cdef extern from "dpctl_sycl_device_selector_interface.h":
224224
int DPCTLDeviceSelector_Score(DPCTLSyclDeviceSelectorRef, DPCTLSyclDeviceRef)
225225

226226

227-
cdef extern from "dpctl_sycl_event_interface.h":
227+
cdef extern from "syclinterface/dpctl_sycl_event_interface.h":
228228
cdef DPCTLSyclEventRef DPCTLEvent_Create()
229229
cdef DPCTLSyclEventRef DPCTLEvent_Copy(const DPCTLSyclEventRef ERef)
230230
cdef void DPCTLEvent_Wait(DPCTLSyclEventRef ERef)
@@ -246,13 +246,13 @@ cdef extern from "dpctl_sycl_event_interface.h":
246246
cdef size_t DPCTLEvent_GetProfilingInfoEnd(DPCTLSyclEventRef ERef)
247247

248248

249-
cdef extern from "dpctl_sycl_kernel_interface.h":
249+
cdef extern from "syclinterface/dpctl_sycl_kernel_interface.h":
250250
cdef const char* DPCTLKernel_GetFunctionName(const DPCTLSyclKernelRef KRef)
251251
cdef size_t DPCTLKernel_GetNumArgs(const DPCTLSyclKernelRef KRef)
252252
cdef void DPCTLKernel_Delete(DPCTLSyclKernelRef KRef)
253253

254254

255-
cdef extern from "dpctl_sycl_platform_manager.h":
255+
cdef extern from "syclinterface/dpctl_sycl_platform_manager.h":
256256
cdef struct DPCTLPlatformVector
257257
ctypedef DPCTLPlatformVector *DPCTLPlatformVectorRef
258258

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

267267

268-
cdef extern from "dpctl_sycl_platform_interface.h":
268+
cdef extern from "syclinterface/dpctl_sycl_platform_interface.h":
269269
cdef DPCTLSyclPlatformRef DPCTLPlatform_Copy(const DPCTLSyclPlatformRef)
270270
cdef DPCTLSyclPlatformRef DPCTLPlatform_Create()
271271
cdef DPCTLSyclPlatformRef DPCTLPlatform_CreateFromSelector(
@@ -278,7 +278,7 @@ cdef extern from "dpctl_sycl_platform_interface.h":
278278
cdef DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms()
279279

280280

281-
cdef extern from "dpctl_sycl_context_interface.h":
281+
cdef extern from "syclinterface/dpctl_sycl_context_interface.h":
282282
cdef DPCTLSyclContextRef DPCTLContext_Create(
283283
const DPCTLSyclDeviceRef DRef,
284284
error_handler_callback *handler,
@@ -299,7 +299,7 @@ cdef extern from "dpctl_sycl_context_interface.h":
299299
cdef void DPCTLContext_Delete(DPCTLSyclContextRef CtxRef)
300300

301301

302-
cdef extern from "dpctl_sycl_program_interface.h":
302+
cdef extern from "syclinterface/dpctl_sycl_program_interface.h":
303303
cdef DPCTLSyclProgramRef DPCTLProgram_CreateFromSpirv(
304304
const DPCTLSyclContextRef Ctx,
305305
const void *IL,
@@ -317,7 +317,7 @@ cdef extern from "dpctl_sycl_program_interface.h":
317317
cdef void DPCTLProgram_Delete(DPCTLSyclProgramRef PRef)
318318

319319

320-
cdef extern from "dpctl_sycl_queue_interface.h":
320+
cdef extern from "syclinterface/dpctl_sycl_queue_interface.h":
321321
cdef bool DPCTLQueue_AreEq(const DPCTLSyclQueueRef QRef1,
322322
const DPCTLSyclQueueRef QRef2)
323323
cdef DPCTLSyclQueueRef DPCTLQueue_Create(
@@ -381,7 +381,7 @@ cdef extern from "dpctl_sycl_queue_interface.h":
381381
cdef bool DPCTLQueue_HasEnableProfiling(const DPCTLSyclQueueRef QRef)
382382

383383

384-
cdef extern from "dpctl_sycl_queue_manager.h":
384+
cdef extern from "syclinterface/dpctl_sycl_queue_manager.h":
385385
cdef DPCTLSyclQueueRef DPCTLQueueMgr_GetCurrentQueue()
386386
cdef bool DPCTLQueueMgr_GlobalQueueIsCurrent()
387387
cdef bool DPCTLQueueMgr_IsCurrentQueue(const DPCTLSyclQueueRef QRef)
@@ -391,7 +391,7 @@ cdef extern from "dpctl_sycl_queue_manager.h":
391391
cdef size_t DPCTLQueueMgr_GetQueueStackSize()
392392

393393

394-
cdef extern from "dpctl_sycl_usm_interface.h":
394+
cdef extern from "syclinterface/dpctl_sycl_usm_interface.h":
395395
cdef DPCTLSyclUSMRef DPCTLmalloc_shared(
396396
size_t size,
397397
DPCTLSyclQueueRef QRef)

dpctl/_sycl_context.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,12 @@ cdef api DPCTLSyclContextRef get_context_ref(SyclContext ctx):
485485
:class:`dpctl.SyclContext` instance.
486486
"""
487487
return ctx.get_context_ref()
488+
489+
490+
cdef api SyclContext make_SyclContext(DPCTLSyclContextRef CRef):
491+
"""
492+
C-API function to create :class:`dpctl.SyclContext` instance
493+
from the given opaque context reference.
494+
"""
495+
cdef DPCTLSyclContextRef copied_CRef = DPCTLContext_Copy(CRef)
496+
return SyclContext._create(copied_CRef)

dpctl/_sycl_device.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,3 +1136,12 @@ cdef api DPCTLSyclDeviceRef get_device_ref(SyclDevice dev):
11361136
:class:`dpctl.SyclDevice` instance.
11371137
"""
11381138
return dev.get_device_ref()
1139+
1140+
1141+
cdef api SyclDevice make_SyclDevice(DPCTLSyclDeviceRef DRef):
1142+
"""
1143+
C-API function to create :class:`dpctl.SyclDevice` instance
1144+
from the given opaque device reference.
1145+
"""
1146+
cdef DPCTLSyclDeviceRef copied_DRef = DPCTLDevice_Copy(DRef)
1147+
return SyclDevice._create(copied_DRef)

dpctl/_sycl_event.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ cdef api DPCTLSyclEventRef get_event_ref(SyclEvent ev):
6464
return ev.get_event_ref()
6565

6666

67+
cdef api SyclEvent make_SyclEvent(DPCTLSyclEventRef ERef):
68+
"""
69+
C-API function to create :class:`dpctl.SyclEvent`
70+
instance from opaque sycl event reference.
71+
"""
72+
cdef DPCTLSyclEventRef copied_ERef = DPCTLEvent_Copy(ERef)
73+
return SyclEvent._create(copied_ERef)
74+
75+
6776
cdef void _event_capsule_deleter(object o):
6877
cdef DPCTLSyclEventRef ERef = NULL
6978
if pycapsule.PyCapsule_IsValid(o, "SyclEventRef"):

dpctl/_sycl_queue.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,3 +1007,12 @@ cdef api DPCTLSyclQueueRef get_queue_ref(SyclQueue q):
10071007
:class:`dpctl.SyclQueue` instance.
10081008
"""
10091009
return q.get_queue_ref()
1010+
1011+
1012+
cdef api SyclQueue make_SyclQueue(DPCTLSyclQueueRef QRef):
1013+
"""
1014+
C-API function to create :class:`dpctl.SyclQueue` instance
1015+
from the given opaque queue reference.
1016+
"""
1017+
cdef DPCTLSyclQueueRef copied_QRef = DPCTLQueue_Copy(QRef)
1018+
return SyclQueue._create(copied_QRef)

dpctl/apis/include/dpctl4pybind11.hpp

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
//===----------- dpctl4pybind11.h - Headers for type pybind11 casters -*-C-*-
2+
//===//
3+
//
4+
// Data Parallel Control (dpctl)
5+
//
6+
// Copyright 2020-2021 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 defines imports for dcptl's Python C-API
24+
//===----------------------------------------------------------------------===//
25+
26+
#pragma once
27+
28+
#include "dpctl_capi.h"
29+
#include <CL/sycl.hpp>
30+
#include <pybind11/pybind11.h>
31+
32+
namespace py = pybind11;
33+
34+
namespace pybind11
35+
{
36+
namespace detail
37+
{
38+
39+
/* This type caster associates ``sycl::queue`` C++ class with
40+
* :class:`dpctl.SyclQueue` for the purposes of generation of
41+
* Python bindings by pybind11.
42+
*/
43+
template <> struct type_caster<sycl::queue>
44+
{
45+
public:
46+
PYBIND11_TYPE_CASTER(sycl::queue, _("dpctl.SyclQueue"));
47+
48+
bool load(handle src, bool)
49+
{
50+
PyObject *source = src.ptr();
51+
if (PyObject_TypeCheck(source, &PySyclQueueType)) {
52+
DPCTLSyclQueueRef QRef =
53+
get_queue_ref(reinterpret_cast<PySyclQueueObject *>(source));
54+
sycl::queue *q = reinterpret_cast<sycl::queue *>(QRef);
55+
value = *q;
56+
return true;
57+
}
58+
else {
59+
throw std::runtime_error(
60+
"Input is of unexpected type, expected dpctl.SyclQueue");
61+
}
62+
}
63+
64+
static handle cast(sycl::queue src, return_value_policy, handle)
65+
{
66+
auto tmp = make_SyclQueue(reinterpret_cast<DPCTLSyclQueueRef>(&src));
67+
return handle(reinterpret_cast<PyObject *>(tmp));
68+
}
69+
};
70+
} // namespace detail
71+
} // namespace pybind11
72+
73+
namespace pybind11
74+
{
75+
namespace detail
76+
{
77+
/* This type caster associates ``sycl::device`` C++ class with
78+
* :class:`dpctl.SyclDevice` for the purposes of generation of
79+
* Python bindings by pybind11.
80+
*/
81+
template <> struct type_caster<sycl::device>
82+
{
83+
public:
84+
PYBIND11_TYPE_CASTER(sycl::device, _("dpctl.SyclDevice"));
85+
86+
bool load(handle src, bool)
87+
{
88+
PyObject *source = src.ptr();
89+
if (PyObject_TypeCheck(source, &PySyclDeviceType)) {
90+
DPCTLSyclDeviceRef DRef =
91+
get_device_ref(reinterpret_cast<PySyclDeviceObject *>(source));
92+
sycl::device *d = reinterpret_cast<sycl::device *>(DRef);
93+
value = *d;
94+
return true;
95+
}
96+
else {
97+
throw std::runtime_error(
98+
"Input is of unexpected type, expected dpctl.SyclDevice");
99+
}
100+
}
101+
102+
static handle cast(sycl::device src, return_value_policy, handle)
103+
{
104+
auto tmp = make_SyclDevice(reinterpret_cast<DPCTLSyclDeviceRef>(&src));
105+
return handle(reinterpret_cast<PyObject *>(tmp));
106+
}
107+
};
108+
} // namespace detail
109+
} // namespace pybind11
110+
111+
namespace pybind11
112+
{
113+
namespace detail
114+
{
115+
/* This type caster associates ``sycl::context`` C++ class with
116+
* :class:`dpctl.SyclContext` for the purposes of generation of
117+
* Python bindings by pybind11.
118+
*/
119+
template <> struct type_caster<sycl::context>
120+
{
121+
public:
122+
PYBIND11_TYPE_CASTER(sycl::context, _("dpctl.SyclContext"));
123+
124+
bool load(handle src, bool)
125+
{
126+
PyObject *source = src.ptr();
127+
if (PyObject_TypeCheck(source, &PySyclContextType)) {
128+
DPCTLSyclContextRef CRef = get_context_ref(
129+
reinterpret_cast<PySyclContextObject *>(source));
130+
sycl::context *ctx = reinterpret_cast<sycl::context *>(CRef);
131+
value = *ctx;
132+
return true;
133+
}
134+
else {
135+
throw std::runtime_error(
136+
"Input is of unexpected type, expected dpctl.SyclContext");
137+
}
138+
}
139+
140+
static handle cast(sycl::context src, return_value_policy, handle)
141+
{
142+
auto tmp =
143+
make_SyclContext(reinterpret_cast<DPCTLSyclContextRef>(&src));
144+
return handle(reinterpret_cast<PyObject *>(tmp));
145+
}
146+
};
147+
} // namespace detail
148+
} // namespace pybind11
149+
150+
namespace pybind11
151+
{
152+
namespace detail
153+
{
154+
/* This type caster associates ``sycl::event`` C++ class with
155+
* :class:`dpctl.SyclEvent` for the purposes of generation of
156+
* Python bindings by pybind11.
157+
*/
158+
template <> struct type_caster<sycl::event>
159+
{
160+
public:
161+
PYBIND11_TYPE_CASTER(sycl::event, _("dpctl.SyclEvent"));
162+
163+
bool load(handle src, bool)
164+
{
165+
PyObject *source = src.ptr();
166+
if (PyObject_TypeCheck(source, &PySyclEventType)) {
167+
DPCTLSyclEventRef ERef =
168+
get_event_ref(reinterpret_cast<PySyclEventObject *>(source));
169+
sycl::event *ev = reinterpret_cast<sycl::event *>(ERef);
170+
value = *ev;
171+
return true;
172+
}
173+
else {
174+
throw std::runtime_error(
175+
"Input is of unexpected type, expected dpctl.SyclEvent");
176+
}
177+
}
178+
179+
static handle cast(sycl::event src, return_value_policy, handle)
180+
{
181+
auto tmp = make_SyclEvent(reinterpret_cast<DPCTLSyclEventRef>(&src));
182+
return handle(reinterpret_cast<PyObject *>(tmp));
183+
}
184+
};
185+
} // namespace detail
186+
} // namespace pybind11

0 commit comments

Comments
 (0)