Skip to content

Commit 8ddfa5c

Browse files
author
Diptorup Deb
committed
Move SyclKernel, SyclProgram, program creation functions into a separate sub-module.
1 parent fe7b59a commit 8ddfa5c

File tree

10 files changed

+414
-290
lines changed

10 files changed

+414
-290
lines changed

dpctl-capi/source/dpctl_sycl_program_interface.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPCTLSyclContextRef)
3838
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(program, DPCTLSyclProgramRef)
3939
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(kernel, DPCTLSyclKernelRef)
4040

41-
__dppl_give DPCTLSyclProgramRef
41+
__dpctl_give DPCTLSyclProgramRef
4242
createOpenCLInterOpProgram (const context &SyclCtx,
43-
__dppl_keep const void *IL,
43+
__dpctl_keep const void *IL,
4444
size_t length)
4545
{
4646
cl_int err;
@@ -85,12 +85,12 @@ createOpenCLInterOpProgram (const context &SyclCtx,
8585

8686
} /* end of anonymous namespace */
8787

88-
__dppl_give DPCTLSyclProgramRef
89-
DPCTLProgram_CreateFromOCLSpirv (__dppl_keep const DPPLSyclContextRef CtxRef,
90-
__dppl_keep const void *IL,
88+
__dpctl_give DPCTLSyclProgramRef
89+
DPCTLProgram_CreateFromOCLSpirv (__dpctl_keep const DPCTLSyclContextRef CtxRef,
90+
__dpctl_keep const void *IL,
9191
size_t length)
9292
{
93-
DPPLSyclProgramRef Pref = nullptr;
93+
DPCTLSyclProgramRef Pref = nullptr;
9494
context *SyclCtx = nullptr;
9595
if(!CtxRef) {
9696
// \todo handle error
@@ -114,10 +114,10 @@ DPCTLProgram_CreateFromOCLSpirv (__dppl_keep const DPPLSyclContextRef CtxRef,
114114
return Pref;
115115
}
116116

117-
__dppl_give DPCTLSyclProgramRef
118-
DPCTLProgram_CreateFromOCLSource (__dppl_keep const DPPLSyclContextRef Ctx,
119-
__dppl_keep const char *Source,
120-
__dppl_keep const char *CompileOpts)
117+
__dpctl_give DPCTLSyclProgramRef
118+
DPCTLProgram_CreateFromOCLSource (__dpctl_keep const DPCTLSyclContextRef Ctx,
119+
__dpctl_keep const char *Source,
120+
__dpctl_keep const char *CompileOpts)
121121
{
122122
std::string compileOpts;
123123
context *SyclCtx = nullptr;

dpctl/_sycl_core.pxd

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
##===------------- sycl_core.pxd - dpctl module --------*- Cython -*-------===##
2-
##
3-
## Data Parallel Control (dpCtl)
4-
##
5-
## Copyright 2020 Intel Corporation
6-
##
7-
## Licensed under the Apache License, Version 2.0 (the "License");
8-
## you may not use this file except in compliance with the License.
9-
## You may obtain a copy of the License at
10-
##
11-
## http://www.apache.org/licenses/LICENSE-2.0
12-
##
13-
## Unless required by applicable law or agreed to in writing, software
14-
## distributed under the License is distributed on an "AS IS" BASIS,
15-
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16-
## See the License for the specific language governing permissions and
17-
## limitations under the License.
18-
##
19-
##===----------------------------------------------------------------------===##
20-
##
21-
## \file
22-
## This file declares the extension types and functions for the Cython API
23-
## implemented in sycl_core.pyx.
24-
##
25-
##===----------------------------------------------------------------------===##
1+
# ===------------- sycl_core.pxd - dpctl module --------*- Cython -*--------===#
2+
#
3+
# Data Parallel Control (dpCtl)
4+
#
5+
# Copyright 2020 Intel Corporation
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
# ===-----------------------------------------------------------------------===#
20+
#
21+
# \file
22+
# This file declares the extension types and functions for the Cython API
23+
# implemented in sycl_core.pyx.
24+
#
25+
# ===-----------------------------------------------------------------------===#
2626

2727
# distutils: language = c++
2828
# cython: language_level=3
2929

3030
from ._backend cimport *
31+
from .program._program cimport SyclKernel
3132
from libc.stdint cimport uint32_t
3233

3334

@@ -85,34 +86,6 @@ cdef class SyclEvent:
8586
cpdef void wait (self)
8687

8788

88-
cdef class SyclKernel:
89-
''' Wraps a sycl::kernel object created from an OpenCL interoperability
90-
kernel.
91-
'''
92-
cdef DPCTLSyclKernelRef _kernel_ref
93-
cdef const char *_function_name
94-
cdef DPCTLSyclKernelRef get_kernel_ref (self)
95-
96-
@staticmethod
97-
cdef SyclKernel _create (DPCTLSyclKernelRef kref)
98-
99-
100-
cdef class SyclProgram:
101-
''' Wraps a sycl::program object created from an OpenCL interoperability
102-
program.
103-
104-
SyclProgram exposes the C API from dpctl_sycl_program_interface.h. A
105-
SyclProgram can be created from either a source string or a SPIR-V
106-
binary file.
107-
'''
108-
cdef DPCTLSyclProgramRef _program_ref
109-
110-
@staticmethod
111-
cdef SyclProgram _create (DPCTLSyclProgramRef pref)
112-
cdef DPCTLSyclProgramRef get_program_ref (self)
113-
cpdef SyclKernel get_sycl_kernel(self, str kernel_name)
114-
115-
11689
cdef class SyclQueue:
11790
''' Wrapper class for a Sycl queue.
11891
'''

dpctl/_sycl_core.pyx

Lines changed: 24 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
##===------------- sycl_core.pyx - dpctl module -------*- Cython -*--------===##
2-
##
3-
## Data Parallel Control (dpCtl)
4-
##
5-
## Copyright 2020 Intel Corporation
6-
##
7-
## Licensed under the Apache License, Version 2.0 (the "License");
8-
## you may not use this file except in compliance with the License.
9-
## You may obtain a copy of the License at
10-
##
11-
## http://www.apache.org/licenses/LICENSE-2.0
12-
##
13-
## Unless required by applicable law or agreed to in writing, software
14-
## distributed under the License is distributed on an "AS IS" BASIS,
15-
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16-
## See the License for the specific language governing permissions and
17-
## limitations under the License.
18-
##
19-
##===----------------------------------------------------------------------===##
20-
##
21-
## \file
22-
## This file implements a sub-set of Sycl's interface using dpctl's CAPI.
23-
##
24-
##===----------------------------------------------------------------------===##
1+
# ===------------ sycl_core.pyx - dpctl module -------*- Cython -*----------===#
2+
#
3+
# Data Parallel Control (dpCtl)
4+
#
5+
# Copyright 2020 Intel Corporation
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
# ===-----------------------------------------------------------------------===#
20+
#
21+
# \file
22+
# This file implements a sub-set of Sycl's interface using dpctl's CAPI.
23+
#
24+
# ===-----------------------------------------------------------------------===#
2525

2626
# distutils: language = c++
2727
# cython: language_level=3
@@ -46,16 +46,12 @@ __all__ = [
4646
"has_sycl_platforms",
4747
"set_default_queue",
4848
"is_in_device_context",
49-
"create_program_from_source",
50-
"create_program_from_spirv",
5149
"device_type",
5250
"backend_type",
5351
"device_context",
5452
"SyclContext",
5553
"SyclDevice",
5654
"SyclEvent",
57-
"SyclKernel",
58-
"SyclProgram",
5955
"SyclQueue"
6056
]
6157

@@ -92,12 +88,6 @@ cdef class UnsupportedDeviceError (Exception):
9288
"""
9389
pass
9490

95-
cdef class SyclProgramCompilationError (Exception):
96-
"""This exception is raised when a sycl program could not be built from
97-
either a spirv binary file or a string source.
98-
"""
99-
pass
100-
10191
cdef class SyclKernelSubmitError (Exception):
10292
"""This exception is raised when a sycl program could not be built from
10393
either a spirv binary file or a string source.
@@ -306,88 +296,6 @@ cdef class SyclEvent:
306296
"""
307297
return int(<size_t>self._event_ref)
308298

309-
310-
cdef class SyclKernel:
311-
""" Wraps a sycl::kernel object created from an OpenCL interoperability
312-
kernel.
313-
"""
314-
315-
@staticmethod
316-
cdef SyclKernel _create (DPCTLSyclKernelRef kref):
317-
cdef SyclKernel ret = SyclKernel.__new__(SyclKernel)
318-
ret._kernel_ref = kref
319-
ret._function_name = DPCTLKernel_GetFunctionName(kref)
320-
return ret
321-
322-
def __dealloc__ (self):
323-
DPCTLKernel_Delete(self._kernel_ref)
324-
DPCTLCString_Delete(self._function_name)
325-
326-
def get_function_name (self):
327-
""" Returns the name of the Kernel function.
328-
"""
329-
return self._function_name.decode()
330-
331-
def get_num_args (self):
332-
""" Returns the number of arguments for this kernel function.
333-
"""
334-
return DPCTLKernel_GetNumArgs(self._kernel_ref)
335-
336-
cdef DPCTLSyclKernelRef get_kernel_ref (self):
337-
""" Returns the DPCTLSyclKernelRef pointer for this SyclKernel.
338-
"""
339-
return self._kernel_ref
340-
341-
def addressof_ref (self):
342-
""" Returns the address of the C API DPCTLSyclKernelRef pointer
343-
as a long.
344-
345-
Returns:
346-
The address of the DPCTLSyclKernelRef object used to create this
347-
SyclKernel cast to a long.
348-
"""
349-
return int(<size_t>self._kernel_ref)
350-
351-
cdef class SyclProgram:
352-
""" Wraps a sycl::program object created from an OpenCL interoperability
353-
program.
354-
355-
SyclProgram exposes the C API from dpctl_sycl_program_interface.h. A
356-
SyclProgram can be created from either a source string or a SPIR-V
357-
binary file.
358-
"""
359-
360-
@staticmethod
361-
cdef SyclProgram _create (DPCTLSyclProgramRef pref):
362-
cdef SyclProgram ret = SyclProgram.__new__(SyclProgram)
363-
ret._program_ref = pref
364-
return ret
365-
366-
def __dealloc__ (self):
367-
DPCTLProgram_Delete(self._program_ref)
368-
369-
cdef DPCTLSyclProgramRef get_program_ref (self):
370-
return self._program_ref
371-
372-
cpdef SyclKernel get_sycl_kernel(self, str kernel_name):
373-
name = kernel_name.encode('utf8')
374-
return SyclKernel._create(DPCTLProgram_GetKernel(self._program_ref,
375-
name))
376-
377-
def has_sycl_kernel(self, str kernel_name):
378-
name = kernel_name.encode('utf8')
379-
return DPCTLProgram_HasKernel(self._program_ref, name)
380-
381-
def addressof_ref (self):
382-
"""Returns the address of the C API DPCTLSyclProgramRef pointer
383-
as a long.
384-
385-
Returns:
386-
The address of the DPCTLSyclProgramRef object used to create this
387-
SyclProgram cast to a long.
388-
"""
389-
return int(<size_t>self._program_ref)
390-
391299
import ctypes
392300

393301
cdef class SyclQueue:
@@ -894,80 +802,6 @@ cpdef get_current_backend():
894802
"""
895803
return _mgr.get_current_backend()
896804

897-
def create_program_from_source (SyclQueue q, unicode source, unicode copts=""):
898-
"""
899-
Creates a Sycl interoperability program from an OpenCL source string.
900-
901-
We use the DPCTLProgram_CreateFromOCLSource() C API function to create
902-
a Sycl progrma from an OpenCL source program that can contain multiple
903-
kernels.
904-
905-
Parameters:
906-
q (SyclQueue) : The :class:`SyclQueue` for which the
907-
:class:`SyclProgram` is going to be built.
908-
source (unicode): Source string for an OpenCL program.
909-
copts (unicode) : Optional compilation flags that will be used
910-
when compiling the program.
911-
912-
Returns:
913-
program (SyclProgram): A :class:`SyclProgram` object wrapping the sycl::program returned by the C API.
914-
"""
915-
916-
BE = q.get_sycl_backend()
917-
if BE != backend_type.opencl:
918-
raise ValueError(
919-
"Cannot create program for a ", BE, "type backend. Currently only "
920-
"OpenCL devices are supported for program creations."
921-
)
922-
923-
cdef DPCTLSyclProgramRef Pref
924-
cdef bytes bSrc = source.encode('utf8')
925-
cdef bytes bCOpts = copts.encode('utf8')
926-
cdef const char *Src = <const char*>bSrc
927-
cdef const char *COpts = <const char*>bCOpts
928-
cdef DPCTLSyclContextRef CRef = q.get_sycl_context().get_context_ref()
929-
Pref = DPCTLProgram_CreateFromOCLSource(CRef, Src, COpts)
930-
931-
if Pref is NULL:
932-
raise SyclProgramCompilationError()
933-
934-
return SyclProgram._create(Pref)
935-
936-
cimport cython.array
937-
938-
def create_program_from_spirv (SyclQueue q, const unsigned char[:] IL):
939-
"""
940-
Creates a Sycl interoperability program from an SPIR-V binary.
941-
942-
We use the DPCTLProgram_CreateFromOCLSpirv() C API function to create
943-
a Sycl progrma from an compiled SPIR-V binary file.
944-
945-
Parameters:
946-
q (SyclQueue): The :class:`SyclQueue` for which the
947-
:class:`SyclProgram` is going to be built.
948-
IL (const char[:]) : SPIR-V binary IL file for an OpenCL program.
949-
950-
Returns:
951-
program (SyclProgram): A :class:`SyclProgram` object wrapping the sycl::program returned by the C API.
952-
"""
953-
BE = q.get_sycl_backend()
954-
if BE != backend_type.opencl:
955-
raise ValueError(
956-
"Cannot create program for a ", BE, "type backend. Currently only "
957-
"OpenCL devices are supported for program creations."
958-
)
959-
960-
cdef DPCTLSyclProgramRef Pref
961-
cdef const unsigned char *dIL = &IL[0]
962-
cdef DPCTLSyclContextRef CRef = q.get_sycl_context().get_context_ref()
963-
cdef size_t length = IL.shape[0]
964-
Pref = DPCTLProgram_CreateFromOCLSpirv(CRef, <const void*>dIL, length)
965-
if Pref is NULL:
966-
raise SyclProgramCompilationError()
967-
968-
return SyclProgram._create(Pref)
969-
970-
971805
from contextlib import contextmanager
972806

973807
@contextmanager

0 commit comments

Comments
 (0)