From defc264e68470e0ff9c582bf9495392b8b82f819 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Tue, 15 Sep 2020 10:32:59 +0300 Subject: [PATCH 01/93] Move _memory.pyx --- dppl/_memory.pyx | 154 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 dppl/_memory.pyx diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx new file mode 100644 index 0000000000..4d6639a0c4 --- /dev/null +++ b/dppl/_memory.pyx @@ -0,0 +1,154 @@ +import dppl + +from cython.operator cimport dereference as deref + +from cpython.pycapsule cimport PyCapsule_GetPointer +from cpython cimport Py_buffer + +cdef extern from "CL/sycl.hpp" namespace "cl::sycl::usm": + cdef enum alloc: + host 'cl::sycl::usm::alloc::host' + device 'cl::sycl::usm::alloc::device' + shared 'cl::sycl::usm::alloc::shared' + unknown 'cl::sycl::usm::alloc::unknown' + +cdef extern from "CL/sycl.hpp" namespace "cl::sycl": + cdef cppclass context nogil: + pass + + cdef cppclass queue nogil: + context get_context() nogil + pass + + cdef void* malloc_shared(Py_ssize_t, queue&) nogil + cdef void free(void *, queue&) nogil + cdef alloc get_pointer_type(void *, context&) nogil + + +cdef class SyclQueue: + cdef object queue_cap + cdef queue q + + def __cinit__(self): + cdef void* q_ptr + self.queue_cap = dppl.get_current_queue() + q_ptr = PyCapsule_GetPointer(self.queue_cap, NULL) + if (q_ptr): + self.q = deref(q_ptr) + else: + raise ValueError("NULL pointer returned by the Capsule") + + def get_pointer_type(self, Py_ssize_t p): + cdef context ctx = self.q.get_context() + cdef void * p_ptr = p + + ptr_type = get_pointer_type(p_ptr, ctx) + if (ptr_type == alloc.shared): + return "shared" + elif (ptr_type == alloc.host): + return "host" + elif (ptr_type == alloc.device): + return "device" + else: + return "unknown" + + property get_capsule: + def __get__(self): + return self.queue_cap + + cdef queue get_queue(self): + return self.q + + +cdef class Memory: + cdef void* _ptr + cdef Py_ssize_t nbytes + cdef object queue_cap + + def __cinit__(self, Py_ssize_t nbytes): + cdef object q_cap + cdef void* queue_ptr + cdef void* p + + self._ptr = NULL + self.queue_cap = None + self.nbytes = 0 + + if (nbytes > 0): + q_cap = dppl.get_current_queue() + queue_ptr = PyCapsule_GetPointer(q_cap, NULL) + p = malloc_shared(nbytes, deref(queue_ptr)) + if (p): + self._ptr = p + self.nbytes = nbytes + self.queue_cap = q_cap + else: + raise RuntimeError("Null memory pointer returned") + else: + raise ValueError("Non-positive number of bytes found.") + + def __dealloc__(self): + cdef void* queue_ptr + + if (self._ptr): + queue_ptr = PyCapsule_GetPointer(self.queue_cap, NULL) + free(self._ptr, deref(queue_ptr)) + self._ptr = NULL + self.nbytes = 0 + self.queue_cap = None + + def __getbuffer__(self, Py_buffer *buffer, int flags): + buffer.buf = self._ptr + buffer.format = 'B' # byte + buffer.internal = NULL # see References + buffer.itemsize = 1 + buffer.len = self.nbytes + buffer.ndim = 1 + buffer.obj = self + buffer.readonly = 0 + buffer.shape = &self.nbytes + buffer.strides = &buffer.itemsize + buffer.suboffsets = NULL # for pointer arrays only + + property pointer: + def __get__(self): + return (self._ptr) + + property nbytes: + def __get__(self): + return self.nbytes + + property _queue: + def __get__(self): + return self.queue_cap + + def __repr__(self): + return "".format(self.nbytes, hex((self._ptr))) + + def _usm_type(self, qcaps=None): + cdef void *q_ptr + cdef alloc ptr_type + + _cap = qcaps if (qcaps) else self.queue_cap + q_ptr = PyCapsule_GetPointer(_cap, NULL) + ptr_type = get_pointer_type(self._ptr, deref(q_ptr).get_context()) + if (ptr_type == alloc.shared): + return "shared" + elif (ptr_type == alloc.host): + return "host" + elif (ptr_type == alloc.device): + return "device" + else: + return "unknown" + +# cdef void* _ptr +# cdef Py_ssize_t nbytes +# cdef object queue_cap + + @staticmethod + cdef Memory create(void *p, Py_ssize_t nbytes, object queue_cap): + cdef Memory ret = Memory.__new__() + ret._ptr = p + ret.nbytes = nbytes + ret.q_cap = queue_cap + return ret From 8b811c593d661b3d190fde6c1e160438e170d8b6 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Tue, 15 Sep 2020 10:54:56 +0300 Subject: [PATCH 02/93] Import setuptools before Cython. Otherwise, both might disagree about the class to use. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a7c209ee3c..b0f188327e 100644 --- a/setup.py +++ b/setup.py @@ -25,8 +25,8 @@ import sys import versioneer -from Cython.Build import cythonize from setuptools import setup, Extension, find_packages +from Cython.Build import cythonize import numpy as np From 07042585f52c65ec2f55259e5bb08320d8d8c9b4 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Tue, 15 Sep 2020 15:50:28 +0300 Subject: [PATCH 03/93] Add dppl._memory Cython module. --- setup.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/setup.py b/setup.py index b0f188327e..76a7070412 100644 --- a/setup.py +++ b/setup.py @@ -104,17 +104,22 @@ def getpyexts(): elif IS_WIN: runtime_library_dirs = [] - exts = cythonize(Extension('dppl._sycl_core', - [os.path.abspath('dppl/sycl_core.pyx'),], - depends=[dppl_sycl_interface_include,], - include_dirs=[np.get_include(), - dppl_sycl_interface_include], - extra_compile_args=eca + get_other_cxxflags(), - extra_link_args=ela, - libraries=libs, - library_dirs=librarys, - runtime_library_dirs=runtime_library_dirs, - language='c++')) + extension_args = { + "depends": [dppl_sycl_interface_include,], + "include_dirs": [np.get_include(), dppl_sycl_interface_include], + "extra_compile_args": eca + get_other_cxxflags(), + "extra_link_args": ela, "libraries": libs, "library_dirs": librarys, + "runtime_library_dirs": runtime_library_dirs, "language": 'c++', + } + + extensions = [ + Extension('dppl._sycl_core', [os.path.abspath('dppl/sycl_core.pyx'),], + **extension_args), + Extension('dppl._memory', [os.path.abspath('dppl/_memory.pyx'),], + **extension_args), + ] + + exts = cythonize(extensions) return exts setup( From e57927dd4960ca1fd502277307ee3bbfc9bef8fd Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Tue, 15 Sep 2020 15:51:35 +0300 Subject: [PATCH 04/93] Run dppl/tests/dppl_tests too when run all unit tests. --- dppl/tests/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dppl/tests/__init__.py b/dppl/tests/__init__.py index b7d39c5dfd..6bd91b208f 100644 --- a/dppl/tests/__init__.py +++ b/dppl/tests/__init__.py @@ -1 +1,2 @@ from .test_dump_functions import * +from .dppl_tests import * From 267492f0021cb3e0407db58e2afc4d4161c75f47 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Tue, 15 Sep 2020 17:17:48 +0300 Subject: [PATCH 05/93] Add tests for memory manager. --- dppl/tests/dppl_tests/__init__.py | 3 +- .../dppl_tests/test_sycl_memory_manager.py | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 dppl/tests/dppl_tests/test_sycl_memory_manager.py diff --git a/dppl/tests/dppl_tests/__init__.py b/dppl/tests/dppl_tests/__init__.py index adcaad3954..7017278251 100644 --- a/dppl/tests/dppl_tests/__init__.py +++ b/dppl/tests/dppl_tests/__init__.py @@ -1 +1,2 @@ -from .test_sycl_queue_manager import * \ No newline at end of file +from .test_sycl_queue_manager import * +from .test_sycl_memory_manager import * diff --git a/dppl/tests/dppl_tests/test_sycl_memory_manager.py b/dppl/tests/dppl_tests/test_sycl_memory_manager.py new file mode 100644 index 0000000000..06114984ed --- /dev/null +++ b/dppl/tests/dppl_tests/test_sycl_memory_manager.py @@ -0,0 +1,45 @@ +##===---------- test_sycl_queue_manager.py - dppl -------*- Python -*-----===## +## +## Python Data Parallel Processing Library (PyDPPL) +## +## Copyright 2020 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 unittest +import dppl +import dppl._memory as mem + + +class TestMemory (unittest.TestCase): + # @unittest.skipIf(not dppl.has_sycl_platforms, "No SYCL platforms available") + def test_memory_create (self): + nbytes = 1024 + mobj = mem.Memory(nbytes) + self.assertEqual(mobj.nbytes, nbytes) + + # Without context + self.assertEqual(mem.SyclQueue().get_pointer_type(mobj.pointer), 'shared') + self.assertEqual(mobj._usm_type(), 'shared') + + # CPU context + with dppl.device_context(dppl.device_type.cpu): + self.assertEqual(mem.SyclQueue().get_pointer_type(mobj.pointer), 'unknown') + self.assertEqual(mobj._usm_type(), 'shared') + + # GPU context + with dppl.device_context(dppl.device_type.gpu): + self.assertEqual(mem.SyclQueue().get_pointer_type(mobj.pointer), 'unknown') + self.assertEqual(mobj._usm_type(), 'shared') From 5ff3eb083619eac408a0e16d444c9ed79a73802d Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Tue, 15 Sep 2020 17:25:02 +0300 Subject: [PATCH 06/93] Split tests for memory. One test one context (no, CPU, GPU). --- dppl/tests/dppl_tests/test_sycl_memory_manager.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dppl/tests/dppl_tests/test_sycl_memory_manager.py b/dppl/tests/dppl_tests/test_sycl_memory_manager.py index 06114984ed..95051310db 100644 --- a/dppl/tests/dppl_tests/test_sycl_memory_manager.py +++ b/dppl/tests/dppl_tests/test_sycl_memory_manager.py @@ -30,15 +30,29 @@ def test_memory_create (self): mobj = mem.Memory(nbytes) self.assertEqual(mobj.nbytes, nbytes) + def _create_memory (self): + nbytes = 1024 + mobj = mem.Memory(nbytes) + return mobj + + def test_memory_without_context (self): + mobj = self._create_memory() + # Without context self.assertEqual(mem.SyclQueue().get_pointer_type(mobj.pointer), 'shared') self.assertEqual(mobj._usm_type(), 'shared') + def test_memory_cpu_context (self): + mobj = self._create_memory() + # CPU context with dppl.device_context(dppl.device_type.cpu): self.assertEqual(mem.SyclQueue().get_pointer_type(mobj.pointer), 'unknown') self.assertEqual(mobj._usm_type(), 'shared') + def test_memory_gpu_context (self): + mobj = self._create_memory() + # GPU context with dppl.device_context(dppl.device_type.gpu): self.assertEqual(mem.SyclQueue().get_pointer_type(mobj.pointer), 'unknown') From 902bc8a54f74d8e9434a1348965fb4b208ff5904 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Tue, 15 Sep 2020 17:50:29 +0300 Subject: [PATCH 07/93] [opt] Rename getpyexts() to extensions(). --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 76a7070412..2a90338032 100644 --- a/setup.py +++ b/setup.py @@ -78,7 +78,7 @@ def get_other_cxxflags(): # what compiler we are using. return ['/Ox', '/std:c++17'] -def getpyexts(): +def extensions(): # Security flags eca = get_sdl_cflags() ela = get_sdl_ldflags() @@ -131,7 +131,7 @@ def getpyexts(): author="Intel Corporation", url='https://github.com/IntelPython/PyDPPL', packages=find_packages(include=["dppl", "dppl.*"]), - ext_modules = getpyexts(), + ext_modules = extensions(), setup_requires=requirements, cffi_modules=[ "./dppl/opencl_core.py:ffi" From 5d8187968ec30bf4263d3bce06617a179c9a3ea5 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 15 Sep 2020 17:46:17 -0500 Subject: [PATCH 08/93] Adds C and Cython API for portions of Sycl queue, device, context interfaces. - Separates out the queue manager from the sycl queue interface wrapper. - Added API to get context, device froma Sycl queue. - Added C-API wrappers for sycl device, queue, context interfaces exposing some of the API functionalities. More can be added as needed. - The dppl extension now defines extension types for sycl quque, device, context to mimic the C API. - Sycl queues are now returned to callers as a SyclQueue extension type rather than a Py_capsule. --- backends/CMakeLists.txt | 3 + .../include/dppl_sycl_context_interface.h | 52 +++ backends/include/dppl_sycl_device_interface.h | 178 ++++++++ backends/include/dppl_sycl_queue_interface.h | 155 +------ backends/include/dppl_sycl_queue_manager.h | 151 +++++++ backends/include/dppl_sycl_types.h | 27 +- backends/include/error_check_macros.h | 1 + .../source/dppl_sycl_context_interface.cpp | 58 +++ .../source/dppl_sycl_device_interface.cpp | 147 +++++++ backends/source/dppl_sycl_queue_interface.cpp | 387 +----------------- backends/source/dppl_sycl_queue_manager.cpp | 366 +++++++++++++++++ backends/tests/CMakeLists.txt | 2 +- backends/tests/test_sycl_queue_manager.cpp | 40 +- dppl/sycl_core.pyx | 161 ++++++-- .../dppl_tests/test_sycl_queue_manager.py | 5 +- dppl/tests/test_dump_functions.py | 6 +- 16 files changed, 1193 insertions(+), 546 deletions(-) create mode 100644 backends/include/dppl_sycl_context_interface.h create mode 100644 backends/include/dppl_sycl_device_interface.h create mode 100644 backends/include/dppl_sycl_queue_manager.h create mode 100644 backends/source/dppl_sycl_context_interface.cpp create mode 100644 backends/source/dppl_sycl_device_interface.cpp create mode 100644 backends/source/dppl_sycl_queue_manager.cpp diff --git a/backends/CMakeLists.txt b/backends/CMakeLists.txt index 3a9a0434d4..921bb1d3eb 100644 --- a/backends/CMakeLists.txt +++ b/backends/CMakeLists.txt @@ -96,7 +96,10 @@ message(STATUS "OpenCL_LIBRARY: ${OpenCL_LIBRARY}") add_library( DPPLSyclInterface SHARED + source/dppl_sycl_context_interface.cpp + source/dppl_sycl_device_interface.cpp source/dppl_sycl_queue_interface.cpp + source/dppl_sycl_queue_manager.cpp ) # Install DPPLOpenCLInterface diff --git a/backends/include/dppl_sycl_context_interface.h b/backends/include/dppl_sycl_context_interface.h new file mode 100644 index 0000000000..6cd3c271a2 --- /dev/null +++ b/backends/include/dppl_sycl_context_interface.h @@ -0,0 +1,52 @@ +//===--- dppl_sycl_context_interface.h - DPPL-SYCL interface --*--C++ --*--===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 header declares a C API to SYCL's sycl::context interface. +/// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "dppl_data_types.h" +#include "dppl_sycl_types.h" +#include "Support/DllExport.h" +#include "Support/ExternC.h" +#include "Support/MemOwnershipAttrs.h" +#include + +DPPL_C_EXTERN_C_BEGIN + +/*! + * @brief Returns true if this SYCL context is a host context. + * + * @param CtxtRef A opaque pointer to a sycl::context. + * @return True if the SYCL context is a host context, else False. + */ +bool DPPLIsHostContext (__dppl_keep const DPPLSyclContextRef CtxtRef); + +/*! + * @brief Delete the pointer after casting it to sycl::context + * + * @param CtxtRef The DPPLSyclContextRef pointer to be deleted. + */ +void DPPLDeleteSyclContext (__dppl_take DPPLSyclContextRef CtxtRef); + +DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_device_interface.h b/backends/include/dppl_sycl_device_interface.h new file mode 100644 index 0000000000..e379f7d733 --- /dev/null +++ b/backends/include/dppl_sycl_device_interface.h @@ -0,0 +1,178 @@ +//===--- dppl_sycl_device_interface.h - DPPL-SYCL interface ---*---C++ -*---===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 header declares a C interface to sycl::device. Not all of the device +/// API is exposed, only the bits needed in other places like context and queue +/// interfaces. +/// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "dppl_data_types.h" +#include "dppl_sycl_types.h" +#include "Support/DllExport.h" +#include "Support/ExternC.h" +#include "Support/MemOwnershipAttrs.h" + +DPPL_C_EXTERN_C_BEGIN + +/*! + * @brief Redefinition of Sycl's device_type so that we do not have to include + * sycl.hpp here and in the Python bindings. + * + */ +typedef enum +{ + DPPL_CPU, + DPPL_GPU, + DPPL_ACCELERATOR, + DPPL_CUSTOM, + DPPL_AUTOMATIC, + DPPL_HOST, + DPPL_ALL +} DPPLSyclDeviceType; + +/*! + * @brief Prints out some of the info::deivice attributes for the device. + * + * @param DRef A DPPLSyclDeviceRef pointer. + */ +DPPL_API +void DPPLDumpDeviceInfo (__dppl_keep const DPPLSyclDeviceRef DRef); + +/*! + * @brief Deletes a DPPLSyclDeviceRef pointer after casting to to sycl::device. + * + * @param DRef The DPPLSyclDeviceRef pointer to be freed. + */ +DPPL_API +void DPPLDeleteSyclDevice (__dppl_take DPPLSyclDeviceRef DRef); + +/*! + * @brief Returns true if this SYCL device is an OpenCL device and the device + * type is sycl::info::device_type::accelerator. + * + * @param DRef Opaque pointer to a sycl::device + * @return True if the device type is an accelerator, else False. + */ +DPPL_API +bool DPPLDeviceIsAccelerator (__dppl_keep const DPPLSyclDeviceRef DRef); + +/*! + * @brief Returns true if this SYCL device is an OpenCL device and the device + * type is sycl::info::device_type::cpu. + * + * @param DRef Opaque pointer to a sycl::device + * @return True if the device type is a cpu, else False. + */ +DPPL_API +bool DPPLDeviceIsCPU (__dppl_keep const DPPLSyclDeviceRef DRef); + +/*! + * @brief Returns true if this SYCL device is an OpenCL device and the device + * type is sycl::info::device_type::gpu. + * + * @param DRef Opaque pointer to a sycl::device + * @return True if the device type is a gpu, else False. + */ +DPPL_API +bool DPPLDeviceIsGPU (__dppl_keep const DPPLSyclDeviceRef DRef); + +/*! + * @brief Returns true if this SYCL device is a host device. + * + * @param DRef Opaque pointer to a sycl::device + * @return True if the device is a host device, else False. + */ +DPPL_API +bool DPPLDeviceIsHost (__dppl_keep const DPPLSyclDeviceRef DRef); + +/*! + * @brief Returns the OpenCL software driver version as a C string. + * + * @param DRef Opaque pointer to a sycl::device + * @return A C string in the form major_number.minor.number that corresponds + * to the OpenCL driver version if this is a OpenCL device. + */ +DPPL_API +__dppl_give const char* +DPPLGetDeviceDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef); + +/*! + * @brief Deletes a C string corresponding to the string returned by + * DPPLGetDeviceDriverInfo(). + * + * @param DriverInfo C String for the driver number. + */ +DPPL_API +void DPPLDeleteDeviceDriverInfo (__dppl_take const char* DriverInfo); + +/*! + * @brief Returns a C string for the device name. + * + * @param DRef Opaque pointer to a sycl::device + * @return A C string containing the OpenCL device name. + */ +DPPL_API +__dppl_give const char* +DPPLGetDeviceName (__dppl_keep const DPPLSyclDeviceRef DRef); + +/*! + * @brief Deletes a C string corresponding to the string returned by + * DPPLGetDeviceName(). + * + * @param DeviceName C String for the device name. + */ +DPPL_API +void DPPLDeleteDeviceName (__dppl_take const char* DeviceName); + +/*! + * @brief Returns a C string corresponding to the vendor name. + * + * @param DRef Opaque pointer to a sycl::device + * @return A C string containing the OpenCL device vendor name. + */ +DPPL_API +__dppl_give const char* +DPPLGetDeviceVendorName (__dppl_keep const DPPLSyclDeviceRef DRef); + +/*! + * @brief Deletes a C string corresponding to the string returned by + * DPPLGetDeviceVendorName(). + * + * @param char C String for the vendor name. + */ +DPPL_API +void DPPLDeleteDeviceVendorName (__dppl_take const char* VendorName); + +/*! + * @brief Returns True if the device and the host share a unified memory + * subsystem, else returns False. + * + * @param DRef Opaque pointer to a sycl::device + * @return Boolean indicating if the device shares a unified memory subsystem + * with the host. + */ +DPPL_API +bool DPPLGetDeviceHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef); + +DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index d85803b735..f2493b87f9 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -19,15 +19,9 @@ //===----------------------------------------------------------------------===// /// /// \file -/// This header declares a C interface to DPPL's sycl::queue manager class that -/// maintains a thread local stack of sycl::queues objects for use inside -/// Python programs. The C interface is designed in a way to not have to -/// include the Sycl headers inside a Python extension module, since that would -/// require the extension to be compiled using dpc++ or another Sycl compiler. -/// Compiling the extension with a compiler different from what was used to -/// compile the Python interpreter can cause run-time problems especially on MS -/// Windows. Additionally, the C interface makes it easier to interoperate with -/// Numba without having to deal with C++ name mangling. +/// This header declares a C interface to sycl::queue member functions. Note +/// that sycl::queue constructors are not exposed in this interface. Instead, +/// users should use the functions in dppl_sycl_queue_manager.h. /// //===----------------------------------------------------------------------===// @@ -42,151 +36,38 @@ DPPL_C_EXTERN_C_BEGIN /*! - * @brief Redefinition of Sycl's device_type so that we do not have to include - * sycl.hpp here and in the Python bindings. - * - */ -typedef enum -{ - DPPL_CPU, - DPPL_GPU, - DPPL_ACCELERATOR, - DPPL_CUSTOM, - DPPL_AUTOMATIC, - DPPL_HOST, - DPPL_ALL -} DPPLSyclDeviceType; - - -/*! - * @brief Get the number of sycl::platform available on the system. - * - * @return The number of available sycl::platforms. - */ -DPPL_API -size_t DPPLGetNumPlatforms (); - -/*! - * @brief Get the sycl::queue object that is currently activated for this - * thread. - * - * @return A copy of the current (top of the stack) sycl::queue is returned - * wrapped inside an opaque DPPLSyclQueueRef pointer. - */ -DPPL_API -__dppl_give DPPLSyclQueueRef DPPLGetCurrentQueue (); - -/*! - * @brief Get a sycl::queue object of the specified type and device id. - * - * @param DeviceTy The type of Sycl device (sycl_device_type) - * @param DNum Device id for the device (defaults to 0) - * - * @return A copy of the sycl::queue corresponding to the device is returned - * wrapped inside a DPPLSyclDeviceType pointer. A runtime_error exception is - * raised if no such device exists. - */ -DPPL_API -__dppl_give DPPLSyclQueueRef DPPLGetQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum); - -/*! - * @brief Get the number of activated queues not including the global or - * default queue. - * - * @return The number of activated queues. - */ -DPPL_API -size_t DPPLGetNumActivatedQueues (); - -/*! - * @brief Get the number of GPU queues available on the system. - * - * @return The number of available GPU queues. - */ -DPPL_API -size_t DPPLGetNumGPUQueues (); - -/*! - * @brief Get the number of CPU queues available on the system. - * - * @return The number of available CPU queues. - */ -DPPL_API -size_t DPPLGetNumCPUQueues (); - -/*! -* @brief Set the default DPPL queue to the sycl::queue for the given device. -* -* If no such device is found the a runtime_error exception is thrown. -* -* @param DeviceTy The type of Sycl device (sycl_device_type) -* @param DNum Device id for the device (defaults to 0) -*/ -DPPL_API -void DPPLSetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum); - -/*! - * @brief Pushes a new sycl::queue object to the top of DPPL's thread-local - * stack of a "activated" queues, and returns a copy of the queue to caller. - * - * DPPL maintains a thread-local stack of sycl::queue objects to facilitate - * nested parallelism. The sycl::queue at the top of the stack is termed as the - * currently activated queue, and is always the one returned by - * DPPLGetCurrentQueue(). DPPLPushSyclQueueToStack creates a new sycl::queue - * corresponding to the specified device and pushes it to the top of the stack. - * A copy of the sycl::queue is returned to the caller wrapped inside the - * opaque DPPLSyclQueueRef pointer. A runtime_error exception is thrown when - * a new sycl::queue could not be created for the specified device. - * - * @param DeviceTy The type of Sycl device (sycl_device_type) - * @param DNum Device id for the device (defaults to 0) - * - * @return A copy of the sycl::queue that was pushed to the top of DPPL's - * stack of sycl::queue objects. + * @brief Prints out information about the Sycl environment, such as + * number of available platforms, number of activated queues, etc. */ DPPL_API -__dppl_give DPPLSyclQueueRef DPPLPushSyclQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum); +void DPPLDumpPlatformInfo (); /*! - * @brief Pops the top of stack element from DPPL's stack of activated - * sycl::queue objects. - * - * DPPLPopSyclQueue only removes the reference from the DPPL stack of - * sycl::queue objects. Any instance of the popped queue that were previously - * acquired by calling DPPLPushSyclQueue() or DPPLGetCurrentQueue() needs to be - * freed separately. In addition, a runtime_error is thrown when the stack - * contains only one sycl::queue, i.e., the default queue. + * @brief Returns the Sycl context for the queue. * + * @param QRef An opaque pointer to the sycl queue. + * @return A DPPLSyclContextRef pointer to the sycl context for the queue. */ DPPL_API -void DPPLPopSyclQueue (); - -/*! - * @brief Prints out information about the Sycl environment, such as - * number of available platforms, number of activated queues, etc. - */ -DPPL_API -void DPPLDumpPlatformInfo (); +__dppl_give DPPLSyclContextRef +DPPLGetContextFromQueue (__dppl_keep const DPPLSyclQueueRef QRef); /*! - * @brief Prints out information about the device corresponding to the - * sycl::queue argument. + * @brief returns the Sycl device for the queue. * - * @param QRef A DPPLSyclQueueRef pointer whose metadata will be - * printed out. + * @param QRef An opaque pointer to the sycl queue. + * @return A DPPLSyclDeviceRef pointer to the sycl device for the queue. */ DPPL_API -void DPPLDumpDeviceInfo (__dppl_keep const DPPLSyclQueueRef QRef); +__dppl_give DPPLSyclDeviceRef +DPPLGetDeviceFromQueue (__dppl_keep const DPPLSyclQueueRef QRef); /*! - * @brief Delete the pointer after static casting it to sycl::queue. + * @brief Delete the pointer after casting it to sycl::queue. * * @param QRef A DPPLSyclQueueRef pointer that gets deleted. */ DPPL_API -void DPPLDeleteQueue (__dppl_take DPPLSyclQueueRef QRef); +void DPPLDeleteSyclQueue (__dppl_take DPPLSyclQueueRef QRef); DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_queue_manager.h b/backends/include/dppl_sycl_queue_manager.h new file mode 100644 index 0000000000..741cb51ad2 --- /dev/null +++ b/backends/include/dppl_sycl_queue_manager.h @@ -0,0 +1,151 @@ +//===--- dppl_sycl_queue_manager.h - DPPL-SYCL interface ---*---C++ ---*---===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 header declares a C interface to DPPL's sycl::queue manager to +/// maintain a thread local stack of sycl::queues objects for use inside +/// Python programs. The C interface is designed in a way to not have to +/// include the Sycl headers inside a Python extension module, since that would +/// require the extension to be compiled using dpc++ or another Sycl compiler. +/// Compiling the extension with a compiler different from what was used to +/// compile the Python interpreter can cause run-time problems especially on MS +/// Windows. Additionally, the C interface makes it easier to interoperate with +/// Numba without having to deal with C++ name mangling. +/// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "dppl_data_types.h" +#include "dppl_sycl_types.h" +#include "dppl_sycl_device_interface.h" +#include "Support/DllExport.h" +#include "Support/ExternC.h" +#include "Support/MemOwnershipAttrs.h" + +DPPL_C_EXTERN_C_BEGIN + +/*! + * @brief Get the number of sycl::platform available on the system. + * + * @return The number of available sycl::platforms. + */ +DPPL_API +size_t DPPLGetNumPlatforms (); + +/*! + * @brief Get the sycl::queue object that is currently activated for this + * thread. + * + * @return A copy of the current (top of the stack) sycl::queue is returned + * wrapped inside an opaque DPPLSyclQueueRef pointer. + */ +DPPL_API +__dppl_give DPPLSyclQueueRef DPPLGetCurrentQueue (); + +/*! + * @brief Get a sycl::queue object of the specified type and device id. + * + * @param DeviceTy The type of Sycl device (sycl_device_type) + * @param DNum Device id for the device (defaults to 0) + * + * @return A copy of the sycl::queue corresponding to the device is returned + * wrapped inside a DPPLSyclDeviceType pointer. A runtime_error exception is + * raised if no such device exists. + */ +DPPL_API +__dppl_give DPPLSyclQueueRef DPPLGetQueue (DPPLSyclDeviceType DeviceTy, + size_t DNum); + +/*! + * @brief Get the number of activated queues not including the global or + * default queue. + * + * @return The number of activated queues. + */ +DPPL_API +size_t DPPLGetNumActivatedQueues (); + +/*! + * @brief Get the number of GPU queues available on the system. + * + * @return The number of available GPU queues. + */ +DPPL_API +size_t DPPLGetNumGPUQueues (); + +/*! + * @brief Get the number of CPU queues available on the system. + * + * @return The number of available CPU queues. + */ +DPPL_API +size_t DPPLGetNumCPUQueues (); + +/*! +* @brief Set the default DPPL queue to the sycl::queue for the given device. +* +* If no such device is found the a runtime_error exception is thrown. +* +* @param DeviceTy The type of Sycl device (sycl_device_type) +* @param DNum Device id for the device (defaults to 0) +*/ +DPPL_API +void DPPLSetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, + size_t DNum); + +/*! + * @brief Pushes a new sycl::queue object to the top of DPPL's thread-local + * stack of a "activated" queues, and returns a copy of the queue to caller. + * + * DPPL maintains a thread-local stack of sycl::queue objects to facilitate + * nested parallelism. The sycl::queue at the top of the stack is termed as the + * currently activated queue, and is always the one returned by + * DPPLGetCurrentQueue(). DPPLPushSyclQueueToStack creates a new sycl::queue + * corresponding to the specified device and pushes it to the top of the stack. + * A copy of the sycl::queue is returned to the caller wrapped inside the + * opaque DPPLSyclQueueRef pointer. A runtime_error exception is thrown when + * a new sycl::queue could not be created for the specified device. + * + * @param DeviceTy The type of Sycl device (sycl_device_type) + * @param DNum Device id for the device (defaults to 0) + * + * @return A copy of the sycl::queue that was pushed to the top of DPPL's + * stack of sycl::queue objects. + */ +DPPL_API +__dppl_give DPPLSyclQueueRef DPPLPushSyclQueue (DPPLSyclDeviceType DeviceTy, + size_t DNum); + +/*! + * @brief Pops the top of stack element from DPPL's stack of activated + * sycl::queue objects. + * + * DPPLPopSyclQueue only removes the reference from the DPPL stack of + * sycl::queue objects. Any instance of the popped queue that were previously + * acquired by calling DPPLPushSyclQueue() or DPPLGetCurrentQueue() needs to be + * freed separately. In addition, a runtime_error is thrown when the stack + * contains only one sycl::queue, i.e., the default queue. + * + */ +DPPL_API +void DPPLPopSyclQueue (); + +DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_types.h b/backends/include/dppl_sycl_types.h index ea3d6b479b..d1777aae5d 100644 --- a/backends/include/dppl_sycl_types.h +++ b/backends/include/dppl_sycl_types.h @@ -23,10 +23,35 @@ /// //===----------------------------------------------------------------------===// +#pragma once + +/*! + * @brief + * + */ +typedef struct DPPLOpaqueSyclContext *DPPLSyclContextRef; + +/*! + * @brief + * + */ +typedef struct DPPLOpaqueSyclDevice *DPPLSyclDeviceRef; + +/*! + * @brief + * + */ +typedef struct DPPLOpaqueSyclPlatform *DPPLSyclPlatformRef; /*! - * Used to pass a sycl::queue opaquely through DPPL interfaces. + * @brief Used to pass a sycl::queue opaquely through DPPL interfaces. * * @see sycl::queue */ typedef struct DPPLOpaqueSyclQueue *DPPLSyclQueueRef; + +/*! + * @brief Used to pass a sycl::program opaquely through DPPL interfaces. + * + */ +typedef struct DPPLOpaqueSyclProgram *DPPLSyclProgramRef; diff --git a/backends/include/error_check_macros.h b/backends/include/error_check_macros.h index 201a0f071a..77bd462e5a 100644 --- a/backends/include/error_check_macros.h +++ b/backends/include/error_check_macros.h @@ -23,6 +23,7 @@ /// codes. /// //===----------------------------------------------------------------------===// + #pragma once #include diff --git a/backends/source/dppl_sycl_context_interface.cpp b/backends/source/dppl_sycl_context_interface.cpp new file mode 100644 index 0000000000..d27b150f74 --- /dev/null +++ b/backends/source/dppl_sycl_context_interface.cpp @@ -0,0 +1,58 @@ +//===--- dppl_sycl_context_interface.cpp - DPPL-SYCL interface --*- C++ -*-===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 the data types and functions declared in +/// dppl_sycl_context_interface.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_sycl_context_interface.h" +#include "Support/CBindingWrapping.h" +#include + +using namespace cl::sycl; + +namespace +{ + // Create wrappers for C Binding types (see CBindingWrapping.h). + DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPPLSyclContextRef) +} /* end of anonymous namespace */ + +/*! + * @brief + * + * @param CtxtRef My Param doc + * @return {return} My Param doc + */ +bool DPPLIsHostContext (__dppl_keep const DPPLSyclContextRef CtxtRef) +{ + return unwrap(CtxtRef)->is_host(); +} + +/*! + * @brief + * + * @param CtxtRef My Param doc + */ +void DPPLDeleteSyclContext (__dppl_take DPPLSyclContextRef CtxtRef) +{ + delete unwrap(CtxtRef); +} diff --git a/backends/source/dppl_sycl_device_interface.cpp b/backends/source/dppl_sycl_device_interface.cpp new file mode 100644 index 0000000000..4efcf40580 --- /dev/null +++ b/backends/source/dppl_sycl_device_interface.cpp @@ -0,0 +1,147 @@ +//===--- dppl_sycl_device_interface.cpp - DPPL-SYCL interface --*- C++ -*--===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 the data types and functions declared in +/// dppl_sycl_device_interface.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_sycl_device_interface.h" +#include "Support/CBindingWrapping.h" +#include +#include +#include /* SYCL headers */ + +using namespace cl::sycl; + +namespace +{ +// Create wrappers for C Binding types (see CBindingWrapping.h). + DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPPLSyclDeviceRef) + + /*! + * @brief + * + * @param Device My Param doc + */ +void dump_device_info (const device & Device) +{ + std::stringstream ss; + + ss << std::setw(4) << " " << std::left << std::setw(16) << "Name" + << Device.get_info() << '\n'; + ss << std::setw(4) << " " << std::left << std::setw(16) << "Driver version" + << Device.get_info() << '\n'; + ss << std::setw(4) << " " << std::left << std::setw(16) << "Vendor" + << Device.get_info() << '\n'; + ss << std::setw(4) << " " << std::left << std::setw(16) << "Profile" + << Device.get_info() << '\n'; + + std::cout << ss.str(); +} + +} /* end of anonymous namespace */ + +/*! + * Prints some of the device info metadata for the device corresponding to the + * specified sycl::queue. Currently, device name, driver version, device + * vendor, and device profile are printed out. More attributed may be added + * later. + */ +void DPPLDumpDeviceInfo (__dppl_keep const DPPLSyclDeviceRef DRef) +{ + auto Device = unwrap(DRef); + dump_device_info(*Device); +} + + +void DPPLDeleteSyclDevice (__dppl_take DPPLSyclDeviceRef DRef) +{ + delete unwrap(DRef); +} + +bool DPPLDeviceIsAccelerator (__dppl_keep const DPPLSyclDeviceRef DRef) +{ + return unwrap(DRef)->is_accelerator(); +} + +bool DPPLDeviceIsCPU (__dppl_keep const DPPLSyclDeviceRef DRef) +{ + return unwrap(DRef)->is_cpu(); +} + +bool DPPLDeviceIsGPU (__dppl_keep const DPPLSyclDeviceRef DRef) +{ + return unwrap(DRef)->is_gpu(); +} + + +bool DPPLDeviceIsHost (__dppl_keep const DPPLSyclDeviceRef DRef) +{ + return unwrap(DRef)->is_host(); +} + +__dppl_give const char* +DPPLGetDeviceName (__dppl_keep const DPPLSyclDeviceRef DRef) +{ + auto name = unwrap(DRef)->get_info(); + auto cstr_name = new char [name.length()+1]; + std::strcpy (cstr_name, name.c_str()); + return cstr_name; +} + +void DPPLDeleteDeviceName (__dppl_take const char *DeviceName) +{ + delete DeviceName; +} + +__dppl_give const char* +DPPLGetDeviceVendorName (__dppl_keep const DPPLSyclDeviceRef DRef) +{ + auto vendor = unwrap(DRef)->get_info(); + auto cstr_vendor = new char [vendor.length()+1]; + std::strcpy (cstr_vendor, vendor.c_str()); + return cstr_vendor; +} + +void DPPLDeleteDeviceVendorName (__dppl_take const char *VendorName) +{ + delete VendorName; +} + +__dppl_give const char* +DPPLGetDeviceDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef) +{ + auto driver = unwrap(DRef)->get_info(); + auto cstr_driver = new char [driver.length()+1]; + std::strcpy (cstr_driver, driver.c_str()); + return cstr_driver; +} + +void DPPLDeleteDeviceDriverInfo (__dppl_take const char *DriverInfo) +{ + delete DriverInfo; +} + +bool DPPLGetDeviceHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef) +{ + return unwrap(DRef)->get_info(); +} diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index cf215c6795..2dad78b604 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -20,53 +20,25 @@ /// /// \file /// This file implements the data types and functions declared in -/// dppl_sycl_queue_interface.hpp. +/// dppl_sycl_queue_interface.h. /// //===----------------------------------------------------------------------===// + #include "dppl_sycl_queue_interface.h" #include "Support/CBindingWrapping.h" -#include -#include #include #include #include -#include -#include #include /* SYCL headers */ using namespace cl::sycl; -/*------------------------------- Private helpers ----------------------------*/ - -// Anonymous namespace for private helpers namespace { - - // Create wrappers for C Binding types (see CBindingWrapping.h). +// Create wrappers for C Binding types (see CBindingWrapping.h). DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) -/*! - * @brief - * - * @param Device My Param doc - */ -void dump_device_info (const device & Device) -{ - std::stringstream ss; - - ss << std::setw(4) << " " << std::left << std::setw(16) << "Name" - << Device.get_info() << '\n'; - ss << std::setw(4) << " " << std::left << std::setw(16) << "Driver version" - << Device.get_info() << '\n'; - ss << std::setw(4) << " " << std::left << std::setw(16) << "Vendor" - << Device.get_info() << '\n'; - ss << std::setw(4) << " " << std::left << std::setw(16) << "Profile" - << Device.get_info() << '\n'; - - std::cout << ss.str(); -} - /*! * @brief * @@ -88,257 +60,8 @@ void dump_platform_info (const platform & Platform) std::cout << ss.str(); } -void error_reporter (const std::string & msg) -{ - throw std::runtime_error("Error: " + msg); -} - -/*! - * @brief A helper class to support the DPPLSyclQueuemanager. - * - * The QMgrHelper is needed so that sycl headers are not exposed at the - * top-level DPPL API. - * - */ -class QMgrHelper -{ -public: - static std::vector& - cpu_queues_ () - { - static std::vector* cpu_queues = - QMgrHelper::init_queues(info::device_type::cpu); - return *cpu_queues; - } - - static std::vector& - gpu_queues_ () - { - static std::vector* gpu_queues = - QMgrHelper::init_queues(info::device_type::gpu); - return *gpu_queues; - } - - static std::vector& - active_queues_ () - { - thread_local static std::vector* active_queues = - new std::vector({default_selector()}); - return *active_queues; - } - - static __dppl_give DPPLSyclQueueRef - getQueue (DPPLSyclDeviceType DeviceTy, size_t DNum); - - static __dppl_give DPPLSyclQueueRef - getCurrentQueue (); - - static void - setAsDefaultQueue (DPPLSyclDeviceType DeviceTy, size_t DNum); - - static __dppl_give DPPLSyclQueueRef - pushSyclQueue (DPPLSyclDeviceType DeviceTy, size_t DNum); - - static void - popSyclQueue (); - - static cl::sycl::vector_class* - init_queues (info::device_type device_ty) - { - auto queues = new std::vector(); - for(auto d : device::get_devices(device_ty)) - queues->emplace_back(d); - return queues; - } -}; - -// make function call like access to variable -// it is for minimizing code changes during replacing static vars with functions -// it could be refactored by replacing variable with function call -// scope of this variables is only this file -#define cpu_queues cpu_queues_() -#define gpu_queues gpu_queues_() -#define active_queues active_queues_() - -/*! - * Allocates a new copy of the present top of stack queue, which can be the - * default queue and returns to caller. The caller owns the pointer and is - * responsible for deallocating it. The helper function deleteQueue can be used - * is for that purpose. - */ -DPPLSyclQueueRef QMgrHelper::getCurrentQueue () -{ - if(active_queues.empty()) - error_reporter("No currently active queues."); - auto last = QMgrHelper::active_queues.size() - 1; - return wrap(new queue(QMgrHelper::active_queues[last])); -} - -/*! - * Allocates a sycl::queue by copying from the cached {cpu|gpu}_queues vector - * and returns it to the caller. The caller owns the pointer and is responsible - * for deallocating it. The helper function deleteQueue can be used is for that - * purpose. - */ -DPPLSyclQueueRef -QMgrHelper::getQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum) -{ - queue *QRef = nullptr; - - switch (DeviceTy) - { - case DPPLSyclDeviceType::DPPL_CPU: - { - if (DNum >= cpu_queues.size()) { - std::stringstream ss; - ss << "SYCL CPU device " << DNum << " not found on system."; - error_reporter(ss.str()); - } - QRef = new queue(QMgrHelper::cpu_queues[DNum]); - break; - } - case DPPLSyclDeviceType::DPPL_GPU: - { - if (DNum >= gpu_queues.size()) { - std::stringstream ss; - ss << "SYCL GPU device " << DNum << " not found on system."; - error_reporter(ss.str()); - } - QRef = new queue(QMgrHelper::gpu_queues[DNum]); - break; - } - default: - error_reporter("Unsupported device type."); - } - - return wrap(QRef); -} - -/*! - * Changes the first entry into the stack, i.e., the default queue to a new - * sycl::queue corresponding to the device type and device number. - */ -void -QMgrHelper::setAsDefaultQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) -{ - if(active_queues.empty()) - error_reporter("active queue vector is corrupted."); - - switch (DeviceTy) - { - case DPPLSyclDeviceType::DPPL_CPU: - { - if (DNum >= cpu_queues.size()) { - std::stringstream ss; - ss << "SYCL CPU device " << DNum << " not found on system."; - error_reporter(ss.str()); - } - active_queues[0] = cpu_queues[DNum]; - break; - } - case DPPLSyclDeviceType::DPPL_GPU: - { - if (DNum >= gpu_queues.size()) { - std::stringstream ss; - ss << "SYCL GPU device " << DNum << " not found on system."; - error_reporter(ss.str()); - } - active_queues[0] = gpu_queues[DNum]; - break; - } - default: - { - error_reporter("Unsupported device type."); - } - } -} - -/*! - * Allocates a new sycl::queue by copying from the cached {cpu|gpu}_queues - * vector. The pointer returned is now owned by the caller and must be properly - * cleaned up. The helper function DPPLDeleteQueue() can be used is for that - * purpose. - */ -DPPLSyclQueueRef -QMgrHelper::pushSyclQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) -{ - queue *QRef = nullptr; - if(active_queues.empty()) - error_reporter("Why is there no previous global context?"); - - switch (DeviceTy) - { - case DPPLSyclDeviceType::DPPL_CPU: - { - if (DNum >= cpu_queues.size()) { - std::stringstream ss; - ss << "SYCL CPU device " << DNum << " not found on system."; - error_reporter(ss.str()); - } - active_queues.emplace_back(cpu_queues[DNum]); - QRef = new queue(active_queues[active_queues.size()-1]); - break; - } - case DPPLSyclDeviceType::DPPL_GPU: - { - if (DNum >= gpu_queues.size()) { - std::stringstream ss; - ss << "SYCL GPU device " << DNum << " not found on system."; - error_reporter(ss.str()); - } - active_queues.emplace_back(gpu_queues[DNum]); - QRef = new queue(active_queues[active_queues.size()-1]); - break; - } - default: - { - error_reporter("Unsupported device type."); - } - } - - return wrap(QRef); -} - -/*! - * If there were any sycl::queue that were activated and added to the stack of - * activated queues then the top of the stack entry is popped. Note that since - * the same std::vector is used to keep track of the activated queues and the - * global queue a popSyclQueue call can never make the stack empty. Even - * after all activated queues are popped, the global queue is still available as - * the first element added to the stack. - */ -void -QMgrHelper::popSyclQueue () -{ - // The first queue which is the "default" queue can not be removed. - if(active_queues.size() <= 1 ) - error_reporter("No active contexts"); - active_queues.pop_back(); -} - } /* end of anonymous namespace */ -/*! - * Delete the passed in pointer after verifying it points to a sycl::queue. - */ -void DPPLDeleteQueue (__dppl_take DPPLSyclQueueRef QRef) -{ - delete unwrap(QRef); -} - -/*! - * Prints some of the device info metadata for the device corresponding to the - * specified sycl::queue. Currently, device name, driver version, device - * vendor, and device profile are printed out. More attributed may be added - * later. - */ -void DPPLDumpDeviceInfo (__dppl_keep const DPPLSyclQueueRef QRef) -{ - auto Q = unwrap(QRef); - dump_device_info(Q->get_device()); -} - /*! * Prints out number of available SYCL platforms, number of CPU queues, number * of GPU queues, metadata about the current global queue, and how many queues @@ -359,106 +82,28 @@ void DPPLDumpPlatformInfo () dump_platform_info(p); ++i; } - - // Print out the info for CPU devices - if (QMgrHelper::cpu_queues.size()) - std::cout << "---Number of available SYCL CPU queues: " - << QMgrHelper::cpu_queues.size() << '\n'; - else - std::cout << "---No available SYCL CPU device\n"; - - // Print out the info for GPU devices - if (QMgrHelper::gpu_queues.size()) - std::cout << "---Number of available SYCL GPU queues: " - << QMgrHelper::gpu_queues.size() << '\n'; - else - std::cout << "---No available SYCL GPU device\n"; - - std::cout << "---Current queue :\n"; - DPPLDumpDeviceInfo(wrap(&QMgrHelper::active_queues[0])); - - std::cout << "---Number of active queues : " - << QMgrHelper::active_queues.size() - << '\n'; -} - -/*! - * Returns the number of sycl::platform on the system. - */ -size_t DPPLGetNumPlatforms () -{ - return platform::get_platforms().size(); } -/*! - * Returns inside the number of activated queues not including the global queue - * (QMgrHelper::active_queues[0]). - */ -size_t DPPLGetNumActivatedQueues () -{ - if (QMgrHelper::active_queues.empty()) - error_reporter("No active contexts"); - return QMgrHelper::active_queues.size() - 1; -} - -/*! - * Returns the number of CPU queues. - */ -size_t DPPLGetNumCPUQueues () -{ - return QMgrHelper::cpu_queues.size(); -} - -/*! - * Returns the number of GPU queues. - */ -size_t DPPLGetNumGPUQueues () -{ - return QMgrHelper::gpu_queues.size(); -} - -/*! - * \see QMgrHelper::getCurrentQueue() - */ -DPPLSyclQueueRef DPPLGetCurrentQueue () +__dppl_give DPPLSyclDeviceRef +DPPLGetDeviceFromQueue (__dppl_keep const DPPLSyclQueueRef QRef) { - return QMgrHelper::getCurrentQueue(); -} - -/*! - * Returns a copy of a sycl::queue corresponding to the specified device type - * and device number. A runtime_error gets thrown if no such device exists. - */ -DPPLSyclQueueRef DPPLGetQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum) -{ - return QMgrHelper::getQueue(DeviceTy, DNum); -} - -/*! - * The function sets the global queue, i.e., the sycl::queue object at - * QMgrHelper::active_queues[0] vector to the sycl::queue corresponding to the - * specified device type and id. A runtime_error gets thrown if no such device - * exists. - */ -void DPPLSetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) -{ - QMgrHelper::setAsDefaultQueue(DeviceTy, DNum); + auto Q = unwrap(QRef); + auto Device = new device(Q->get_device()); + return reinterpret_cast(Device); } -/*! - * \see QMgrHelper::pushSyclQueue() - */ -__dppl_give DPPLSyclQueueRef DPPLPushSyclQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum) +__dppl_give DPPLSyclContextRef +DPPLGetContextFromQueue (__dppl_keep const DPPLSyclQueueRef QRef) { - return QMgrHelper::pushSyclQueue(DeviceTy, DNum); + auto Q = unwrap(QRef); + auto Context = new context(Q->get_context()); + return reinterpret_cast(Context); } /*! - * \see QMgrHelper::popSyclQueue() + * Delete the passed in pointer after verifying it points to a sycl::queue. */ -void DPPLPopSyclQueue () +void DPPLDeleteSyclQueue (__dppl_take DPPLSyclQueueRef QRef) { - QMgrHelper::popSyclQueue(); + delete unwrap(QRef); } diff --git a/backends/source/dppl_sycl_queue_manager.cpp b/backends/source/dppl_sycl_queue_manager.cpp new file mode 100644 index 0000000000..e6e4485942 --- /dev/null +++ b/backends/source/dppl_sycl_queue_manager.cpp @@ -0,0 +1,366 @@ +//===--- dppl_sycl_queue_manager.cpp - DPPL-SYCL interface --*- C++ -*---===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 the data types and functions declared in +/// dppl_sycl_queue_manager.h. +/// +//===----------------------------------------------------------------------===// +#include "dppl_sycl_queue_manager.h" +#include "Support/CBindingWrapping.h" +#include +#include +#include + +#include /* SYCL headers */ + +using namespace cl::sycl; + +/*------------------------------- Private helpers ----------------------------*/ + +// Anonymous namespace for private helpers +namespace +{ + + // Create wrappers for C Binding types (see CBindingWrapping.h). + DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) + +void error_reporter (const std::string & msg) +{ + throw std::runtime_error("Error: " + msg); +} + +/*! + * @brief A helper class to support the DPPLSyclQueuemanager. + * + * The QMgrHelper is needed so that sycl headers are not exposed at the + * top-level DPPL API. + * + */ +class QMgrHelper +{ +public: + static std::vector& + cpu_queues_ () + { + static std::vector* cpu_queues = + QMgrHelper::init_queues(info::device_type::cpu); + return *cpu_queues; + } + + static std::vector& + gpu_queues_ () + { + static std::vector* gpu_queues = + QMgrHelper::init_queues(info::device_type::gpu); + return *gpu_queues; + } + + static std::vector& + active_queues_ () + { + thread_local static std::vector* active_queues = + new std::vector({default_selector()}); + return *active_queues; + } + + static __dppl_give DPPLSyclQueueRef + getQueue (DPPLSyclDeviceType DeviceTy, size_t DNum); + + static __dppl_give DPPLSyclQueueRef + getCurrentQueue (); + + static void + setAsDefaultQueue (DPPLSyclDeviceType DeviceTy, size_t DNum); + + static __dppl_give DPPLSyclQueueRef + pushSyclQueue (DPPLSyclDeviceType DeviceTy, size_t DNum); + + static void + popSyclQueue (); + + static cl::sycl::vector_class* + init_queues (info::device_type device_ty) + { + auto queues = new std::vector(); + for(auto d : device::get_devices(device_ty)) + queues->emplace_back(d); + return queues; + } +}; + +// make function call like access to variable +// it is for minimizing code changes during replacing static vars with functions +// it could be refactored by replacing variable with function call +// scope of this variables is only this file +#define cpu_queues cpu_queues_() +#define gpu_queues gpu_queues_() +#define active_queues active_queues_() + + +//----------------------------- Public API -----------------------------------// + +/*! + * Returns the number of sycl::platform on the system. + */ +size_t DPPLGetNumPlatforms () +{ + return platform::get_platforms().size(); +} + +/*! + * Allocates a new copy of the present top of stack queue, which can be the + * default queue and returns to caller. The caller owns the pointer and is + * responsible for deallocating it. The helper function deleteQueue can be used + * is for that purpose. + */ +DPPLSyclQueueRef QMgrHelper::getCurrentQueue () +{ + if(active_queues.empty()) + error_reporter("No currently active queues."); + auto last = QMgrHelper::active_queues.size() - 1; + return wrap(new queue(QMgrHelper::active_queues[last])); +} + +/*! + * Allocates a sycl::queue by copying from the cached {cpu|gpu}_queues vector + * and returns it to the caller. The caller owns the pointer and is responsible + * for deallocating it. The helper function deleteQueue can be used is for that + * purpose. + */ +DPPLSyclQueueRef +QMgrHelper::getQueue (DPPLSyclDeviceType DeviceTy, + size_t DNum) +{ + queue *QRef = nullptr; + + switch (DeviceTy) + { + case DPPLSyclDeviceType::DPPL_CPU: + { + if (DNum >= cpu_queues.size()) { + std::stringstream ss; + ss << "SYCL CPU device " << DNum << " not found on system."; + error_reporter(ss.str()); + } + QRef = new queue(QMgrHelper::cpu_queues[DNum]); + break; + } + case DPPLSyclDeviceType::DPPL_GPU: + { + if (DNum >= gpu_queues.size()) { + std::stringstream ss; + ss << "SYCL GPU device " << DNum << " not found on system."; + error_reporter(ss.str()); + } + QRef = new queue(QMgrHelper::gpu_queues[DNum]); + break; + } + default: + error_reporter("Unsupported device type."); + } + + return wrap(QRef); +} + +/*! + * Changes the first entry into the stack, i.e., the default queue to a new + * sycl::queue corresponding to the device type and device number. + */ +void +QMgrHelper::setAsDefaultQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) +{ + if(active_queues.empty()) + error_reporter("active queue vector is corrupted."); + + switch (DeviceTy) + { + case DPPLSyclDeviceType::DPPL_CPU: + { + if (DNum >= cpu_queues.size()) { + std::stringstream ss; + ss << "SYCL CPU device " << DNum << " not found on system."; + error_reporter(ss.str()); + } + active_queues[0] = cpu_queues[DNum]; + break; + } + case DPPLSyclDeviceType::DPPL_GPU: + { + if (DNum >= gpu_queues.size()) { + std::stringstream ss; + ss << "SYCL GPU device " << DNum << " not found on system."; + error_reporter(ss.str()); + } + active_queues[0] = gpu_queues[DNum]; + break; + } + default: + { + error_reporter("Unsupported device type."); + } + } +} + +/*! + * Allocates a new sycl::queue by copying from the cached {cpu|gpu}_queues + * vector. The pointer returned is now owned by the caller and must be properly + * cleaned up. The helper function DPPLDeleteSyclQueue() can be used is for that + * purpose. + */ +DPPLSyclQueueRef +QMgrHelper::pushSyclQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) +{ + queue *QRef = nullptr; + if(active_queues.empty()) + error_reporter("Why is there no previous global context?"); + + switch (DeviceTy) + { + case DPPLSyclDeviceType::DPPL_CPU: + { + if (DNum >= cpu_queues.size()) { + std::stringstream ss; + ss << "SYCL CPU device " << DNum << " not found on system."; + error_reporter(ss.str()); + } + active_queues.emplace_back(cpu_queues[DNum]); + QRef = new queue(active_queues[active_queues.size()-1]); + break; + } + case DPPLSyclDeviceType::DPPL_GPU: + { + if (DNum >= gpu_queues.size()) { + std::stringstream ss; + ss << "SYCL GPU device " << DNum << " not found on system."; + error_reporter(ss.str()); + } + active_queues.emplace_back(gpu_queues[DNum]); + QRef = new queue(active_queues[active_queues.size()-1]); + break; + } + default: + { + error_reporter("Unsupported device type."); + } + } + + return wrap(QRef); +} + +/*! + * If there were any sycl::queue that were activated and added to the stack of + * activated queues then the top of the stack entry is popped. Note that since + * the same std::vector is used to keep track of the activated queues and the + * global queue a popSyclQueue call can never make the stack empty. Even + * after all activated queues are popped, the global queue is still available as + * the first element added to the stack. + */ +void +QMgrHelper::popSyclQueue () +{ + // The first queue which is the "default" queue can not be removed. + if(active_queues.size() <= 1 ) + error_reporter("No active contexts"); + active_queues.pop_back(); +} + +} /* end of anonymous namespace */ + +/*! + * Returns the number of sycl::platform on the system. + */ +size_t DPPLGetNumPlatforms () +{ + return platform::get_platforms().size(); +} + +/*! + * Returns inside the number of activated queues not including the global queue + * (QMgrHelper::active_queues[0]). + */ +size_t DPPLGetNumActivatedQueues () +{ + if (QMgrHelper::active_queues.empty()) + error_reporter("No active contexts"); + return QMgrHelper::active_queues.size() - 1; +} + +/*! + * Returns the number of CPU queues. + */ +size_t DPPLGetNumCPUQueues () +{ + return QMgrHelper::cpu_queues.size(); +} + +/*! + * Returns the number of GPU queues. + */ +size_t DPPLGetNumGPUQueues () +{ + return QMgrHelper::gpu_queues.size(); +} + +/*! + * \see QMgrHelper::getCurrentQueue() + */ +DPPLSyclQueueRef DPPLGetCurrentQueue () +{ + return QMgrHelper::getCurrentQueue(); +} + +/*! + * Returns a copy of a sycl::queue corresponding to the specified device type + * and device number. A runtime_error gets thrown if no such device exists. + */ +DPPLSyclQueueRef DPPLGetQueue (DPPLSyclDeviceType DeviceTy, + size_t DNum) +{ + return QMgrHelper::getQueue(DeviceTy, DNum); +} + +/*! + * The function sets the global queue, i.e., the sycl::queue object at + * QMgrHelper::active_queues[0] vector to the sycl::queue corresponding to the + * specified device type and id. A runtime_error gets thrown if no such device + * exists. + */ +void DPPLSetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) +{ + QMgrHelper::setAsDefaultQueue(DeviceTy, DNum); +} + +/*! + * \see QMgrHelper::pushSyclQueue() + */ +__dppl_give DPPLSyclQueueRef DPPLPushSyclQueue (DPPLSyclDeviceType DeviceTy, + size_t DNum) +{ + return QMgrHelper::pushSyclQueue(DeviceTy, DNum); +} + +/*! + * \see QMgrHelper::popSyclQueue() + */ +void DPPLPopSyclQueue () +{ + QMgrHelper::popSyclQueue(); +} diff --git a/backends/tests/CMakeLists.txt b/backends/tests/CMakeLists.txt index b2558f4fab..e83f408f05 100644 --- a/backends/tests/CMakeLists.txt +++ b/backends/tests/CMakeLists.txt @@ -11,7 +11,7 @@ else() # We need thread support for gtest find_package(Threads REQUIRED) - set(CMAKE_CTEST_COMMAND ctest -V) + set(CMAKE_CTEST_COMMAND ctest -V) # Emulate autotools like make check target to build tests add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}) diff --git a/backends/tests/test_sycl_queue_manager.cpp b/backends/tests/test_sycl_queue_manager.cpp index a64d06cdab..8ca98aa66a 100644 --- a/backends/tests/test_sycl_queue_manager.cpp +++ b/backends/tests/test_sycl_queue_manager.cpp @@ -1,6 +1,32 @@ +//===--- test_sycl_queue_manager.cpp - DPPL-SYCL interface --*- C++ ---*---===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 has unit test cases for functions defined in +/// dppl_sycl_queue_interface.h. +/// +//===----------------------------------------------------------------------===// +#include "dppl_sycl_device_interface.h" +#include "dppl_sycl_queue_manager.h" #include "dppl_sycl_queue_interface.h" #include -#include #include using namespace std; @@ -15,8 +41,8 @@ namespace num = DPPLGetNumActivatedQueues(); DPPLPopSyclQueue(); DPPLPopSyclQueue(); - DPPLDeleteQueue(q1); - DPPLDeleteQueue(q2); + DPPLDeleteSyclQueue(q1); + DPPLDeleteSyclQueue(q2); } void bar (size_t & num) @@ -25,7 +51,7 @@ namespace // Capture the number of active queues in second num = DPPLGetNumActivatedQueues(); DPPLPopSyclQueue(); - DPPLDeleteQueue(q1); + DPPLDeleteSyclQueue(q1); } } @@ -102,7 +128,7 @@ TEST_F (TestDPPLSyclQueuemanager, CheckGetNumActivatedQueues) EXPECT_EQ(num2, 1); EXPECT_EQ(num4, 0); - DPPLDeleteQueue(q); + DPPLDeleteSyclQueue(q); } @@ -115,8 +141,8 @@ TEST_F (TestDPPLSyclQueuemanager, CheckDPPLDumpPlatformInfo) TEST_F (TestDPPLSyclQueuemanager, CheckDPPLDumpDeviceInfo) { auto q = DPPLGetCurrentQueue(); - EXPECT_NO_FATAL_FAILURE(DPPLDumpDeviceInfo(q)); - EXPECT_NO_FATAL_FAILURE(DPPLDeleteQueue(q)); + EXPECT_NO_FATAL_FAILURE(DPPLDumpDeviceInfo(DPPLGetDeviceFromQueue(q))); + EXPECT_NO_FATAL_FAILURE(DPPLDeleteSyclQueue(q)); } diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index 2a8a5cd840..f167cc3cdf 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -27,11 +27,9 @@ # cython: language_level=3 from __future__ import print_function -from cpython.pycapsule cimport (PyCapsule_New, - PyCapsule_IsValid, - PyCapsule_GetPointer) from enum import Enum, auto import logging +from libcpp cimport bool _logger = logging.getLogger(__name__) @@ -46,20 +44,56 @@ cdef class UnsupportedDeviceTypeError(Exception): ''' pass -cdef extern from "dppl_sycl_types.h": +cdef extern from "dppl_sycl_types.h": + cdef struct DPPLOpaqueSyclContext: + pass cdef struct DPPLOpaqueSyclQueue: pass + cdef struct DPPLOpaqueSyclDevice: + pass + + ctypedef DPPLOpaqueSyclContext* DPPLSyclContextRef ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef + ctypedef DPPLOpaqueSyclDevice* DPPLSyclDeviceRef + +cdef extern from "dppl_sycl_context_interface.h": + cdef void DPPLDeleteSyclContext (DPPLSyclContextRef CtxtRef) except + -cdef extern from "dppl_sycl_queue_interface.h": +cdef extern from "dppl_sycl_queue_interface.h": + cdef void DPPLDeleteSyclQueue (DPPLSyclQueueRef QRef) except + + cdef void DPPLDumpPlatformInfo () except + + cdef DPPLSyclContextRef DPPLGetContextFromQueue (const DPPLSyclQueueRef Q) \ + except+ + cdef DPPLSyclDeviceRef DPPLGetDeviceFromQueue (const DPPLSyclQueueRef Q) \ + except + + + +cdef extern from "dppl_sycl_device_interface.h": + cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + + cdef void DPPLDeleteSyclDevice (DPPLSyclDeviceRef DRef) except + + cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDeviceIsAccelerator (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDeviceIsCPU (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDeviceIsGPU (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDeviceIsHost (const DPPLSyclDeviceRef DRef) except + + cdef const char* DPPLGetDeviceDriverInfo (const DPPLSyclDeviceRef DRef) \ + except + + cdef const char* DPPLGetDeviceName (const DPPLSyclDeviceRef DRef) except + + cdef const char* DPPLGetDeviceVendorName (const DPPLSyclDeviceRef DRef) \ + except + + cdef bool DPPLGetDeviceHostUnifiedMemory (const DPPLSyclDeviceRef DRef) \ + except + + cdef void DPPLDeleteDeviceName (const char *DeviceName) except + + cdef void DPPLDeleteDeviceVendorName (const char *VendorName) except + + cdef void DPPLDeleteDeviceDriverInfo (const char *DriverInfo) except + + +cdef extern from "dppl_sycl_queue_manager.h": cdef enum _device_type 'DPPLSyclDeviceType': _GPU 'DPPL_GPU' _CPU 'DPPL_CPU' - cdef void DPPLDumpPlatformInfo () except + - cdef void DPPLDumpDeviceInfo (const DPPLSyclQueueRef Q) except + cdef DPPLSyclQueueRef DPPLGetCurrentQueue () except + cdef size_t DPPLGetNumCPUQueues () except + cdef size_t DPPLGetNumGPUQueues () except + @@ -72,15 +106,101 @@ cdef extern from "dppl_sycl_queue_interface.h": size_t device_num) except + cdef void DPPLSetAsDefaultQueue (_device_type DTy, size_t device_num) except + - cdef void DPPLDeleteQueue (DPPLSyclQueueRef Q) except + -# Destructor for a PyCapsule containing a SYCL queue -cdef void delete_queue (object cap): - DPPLDeleteQueue(PyCapsule_GetPointer(cap, NULL)) +cdef class SyclContext: + cdef DPPLSyclContextRef ctxt_ptr -cdef class _SyclQueueManager: + @staticmethod + cdef SyclContext _create (DPPLSyclContextRef ctxt): + cdef SyclContext ret = SyclContext.__new__(SyclContext) + ret.ctxt_ptr = ctxt + return ret + + def __dealloc__ (self): + DPPLDeleteSyclContext(self.ctxt_ptr) + + cdef DPPLSyclContextRef get_context_ref (self): + return self.ctxt_ptr + + +cdef class SyclDevice: + ''' Wrapper class for a Sycl Device + ''' + cdef DPPLSyclDeviceRef device_ptr + cdef const char *vendor_name + cdef const char *device_name + cdef const char *driver_version + + @staticmethod + cdef SyclDevice _create (DPPLSyclDeviceRef dref): + cdef SyclDevice ret = SyclDevice.__new__(SyclDevice) + ret.device_ptr = dref + ret.vendor_name = DPPLGetDeviceVendorName(dref) + ret.device_name = DPPLGetDeviceName(dref) + ret.driver_version = DPPLGetDeviceDriverInfo(dref) + return ret + + def __dealloc__ (self): + DPPLDeleteSyclDevice(self.device_ptr) + DPPLDeleteDeviceName(self.device_name) + DPPLDeleteDeviceVendorName(self.vendor_name) + DPPLDeleteDeviceDriverInfo(self.driver_version) + + def dump_device_info (self): + ''' Print information about the SYCL device. + ''' + DPPLDumpDeviceInfo(self.device_ptr) + def get_device_name (self): + ''' Returns the name of the device as a string + ''' + return self.device_name + + def get_vendor_name (self): + ''' Returns the device vendor name as a string + ''' + return self.vendor_name + + def get_driver_version (self): + ''' Returns the OpenCL software driver version as a string + in the form: major number.minor number, if this SYCL + device is an OpenCL device. Returns a string class + with the value "1.2" if this SYCL device is a host device. + ''' + return self.driver_version + + cdef DPPLSyclDeviceRef get_device_ptr (self): + ''' Returns the DPPLSyclDeviceRef pointer for this class. + ''' + return self.device_ptr + + +cdef class SyclQueue: + ''' Wrapper class for a Sycl queue. + ''' + cdef DPPLSyclQueueRef queue_ptr + + @staticmethod + cdef SyclQueue _create (DPPLSyclQueueRef qref): + cdef SyclQueue ret = SyclQueue.__new__(SyclQueue) + ret.queue_ptr = qref + return ret + + def __dealloc__ (self): + DPPLDeleteSyclQueue(self.queue_ptr) + + cpdef get_sycl_context (self): + return SyclContext._create(DPPLGetContextFromQueue(self.queue_ptr)) + + cpdef get_sycl_device (self): + return SyclDevice._create(DPPLGetDeviceFromQueue(self.queue_ptr)) + + cdef DPPLSyclQueueRef get_queue_ref (self): + return self.queue_ptr + + +cdef class _SyclQueueManager: def _set_as_current_queue (self, device_ty, device_id): cdef DPPLSyclQueueRef queue_ptr if device_ty == device_type.gpu: @@ -91,12 +211,11 @@ cdef class _SyclQueueManager: e = UnsupportedDeviceTypeError("Device can only be cpu or gpu") raise e - return PyCapsule_New(queue_ptr, NULL, &delete_queue) + return SyclQueue._create(queue_ptr) def _remove_current_queue (self): DPPLPopSyclQueue() - def has_sycl_platforms (self): cdef size_t num_platforms = DPPLGetNumPlatforms() if num_platforms: @@ -117,8 +236,7 @@ cdef class _SyclQueueManager: def get_current_queue (self): ''' Returns the activated SYCL queue as a PyCapsule. ''' - cdef DPPLSyclQueueRef queue_ptr = DPPLGetCurrentQueue() - return PyCapsule_New(queue_ptr, NULL, &delete_queue) + return SyclQueue._create(DPPLGetCurrentQueue()) def set_default_queue (self, device_ty, device_id): if device_ty == device_type.gpu: @@ -148,16 +266,6 @@ cdef class _SyclQueueManager: ''' DPPLDumpPlatformInfo() - def dump_device_info (self, queue_cap): - ''' Prints information about the SYCL queue object. - ''' - if PyCapsule_IsValid(queue_cap, NULL): - DPPLDumpDeviceInfo( - PyCapsule_GetPointer(queue_cap, NULL) - ) - else: - raise ValueError("Expected a PyCapsule encapsulating a SYCL queue") - def is_in_dppl_ctxt (self): cdef size_t num = DPPLGetNumActivatedQueues() if num: @@ -171,7 +279,6 @@ _qmgr = _SyclQueueManager() # Global bound functions dump = _qmgr.dump -dump_device_info = _qmgr.dump_device_info get_current_queue = _qmgr.get_current_queue get_num_platforms = _qmgr.get_num_platforms get_num_activated_queues = _qmgr.get_num_activated_queues diff --git a/dppl/tests/dppl_tests/test_sycl_queue_manager.py b/dppl/tests/dppl_tests/test_sycl_queue_manager.py index 9fb44730d4..6f83f0fdc9 100644 --- a/dppl/tests/dppl_tests/test_sycl_queue_manager.py +++ b/dppl/tests/dppl_tests/test_sycl_queue_manager.py @@ -43,7 +43,7 @@ def test_dppl_dump (self): def test_dppl_dump_device_info (self): q = dppl.get_current_queue() try: - dppl.dump_device_info(q) + q.get_sycl_device().dump_device_info() except Exception: self.fail("Encountered an exception inside dump_device_info().") @@ -92,3 +92,6 @@ def SessionThread (self): self.assertEqual(dppl.get_num_activated_queues(), 1) Session1.start() Session2.start() + +if __name__ == '__main__': + unittest.main() diff --git a/dppl/tests/test_dump_functions.py b/dppl/tests/test_dump_functions.py index 3b054548fb..2af4744b8e 100644 --- a/dppl/tests/test_dump_functions.py +++ b/dppl/tests/test_dump_functions.py @@ -31,6 +31,10 @@ def test_dppl_dump (self): def test_dppl_dump_device_info (self): q = dppl.get_current_queue() try: - dppl.dump_device_info(q) + q.get_sycl_device().dump_device_info() except Exception: self.fail("Encountered an exception inside dump_device_info().") + + +if __name__ == '__main__': + unittest.main() From 758aacca5058ac712058851da1a296e94a45ed61 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Wed, 16 Sep 2020 14:34:01 +0300 Subject: [PATCH 09/93] Add C-API stub library for sycl memory. --- backends/CMakeLists.txt | 1 + backends/include/dppl_sycl_usm_interface.h | 32 +++++++++++++++++++++ backends/source/dppl_sycl_usm_interface.cpp | 29 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 backends/include/dppl_sycl_usm_interface.h create mode 100644 backends/source/dppl_sycl_usm_interface.cpp diff --git a/backends/CMakeLists.txt b/backends/CMakeLists.txt index 3a9a0434d4..dfb664859e 100644 --- a/backends/CMakeLists.txt +++ b/backends/CMakeLists.txt @@ -97,6 +97,7 @@ add_library( DPPLSyclInterface SHARED source/dppl_sycl_queue_interface.cpp + source/dppl_sycl_usm_interface.cpp ) # Install DPPLOpenCLInterface diff --git a/backends/include/dppl_sycl_usm_interface.h b/backends/include/dppl_sycl_usm_interface.h new file mode 100644 index 0000000000..94d3ca46be --- /dev/null +++ b/backends/include/dppl_sycl_usm_interface.h @@ -0,0 +1,32 @@ +//===--- dppl_sycl_usm_interface.h - DPPL-SYCL interface ---*---C++ -*---===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 +/// TODO +/// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "Support/ExternC.h" + +DPPL_C_EXTERN_C_BEGIN + +DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_usm_interface.cpp b/backends/source/dppl_sycl_usm_interface.cpp new file mode 100644 index 0000000000..974f8c0ad6 --- /dev/null +++ b/backends/source/dppl_sycl_usm_interface.cpp @@ -0,0 +1,29 @@ +//===--- dppl_sycl_usm_interface.cpp - DPPL-SYCL interface --*- C++ -*---===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 +/// TODO +/// +//===----------------------------------------------------------------------===// +#include "dppl_sycl_usm_interface.h" + +#include /* SYCL headers */ + +using namespace cl::sycl; From 35a467419f8fa48fb649642ec269071cddd22992 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 16 Sep 2020 12:21:55 -0500 Subject: [PATCH 10/93] Add missing DPPL_API. --- backends/include/dppl_sycl_context_interface.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backends/include/dppl_sycl_context_interface.h b/backends/include/dppl_sycl_context_interface.h index 6cd3c271a2..1f3506930e 100644 --- a/backends/include/dppl_sycl_context_interface.h +++ b/backends/include/dppl_sycl_context_interface.h @@ -40,6 +40,7 @@ DPPL_C_EXTERN_C_BEGIN * @param CtxtRef A opaque pointer to a sycl::context. * @return True if the SYCL context is a host context, else False. */ +DPPL_API bool DPPLIsHostContext (__dppl_keep const DPPLSyclContextRef CtxtRef); /*! @@ -47,6 +48,7 @@ bool DPPLIsHostContext (__dppl_keep const DPPLSyclContextRef CtxtRef); * * @param CtxtRef The DPPLSyclContextRef pointer to be deleted. */ +DPPL_API void DPPLDeleteSyclContext (__dppl_take DPPLSyclContextRef CtxtRef); DPPL_C_EXTERN_C_END From 7bb5faf2cefe7ef507f3cb59985681d9d140d0c7 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 16 Sep 2020 15:08:11 -0500 Subject: [PATCH 11/93] Move platform specific functions into a separate file. --- backends/CMakeLists.txt | 1 + .../include/dppl_sycl_platform_interface.h | 49 ++++++++++++ backends/include/dppl_sycl_queue_interface.h | 7 -- backends/include/dppl_sycl_queue_manager.h | 8 -- .../source/dppl_sycl_platform_interface.cpp | 74 +++++++++++++++++++ backends/source/dppl_sycl_queue_interface.cpp | 46 ------------ backends/source/dppl_sycl_queue_manager.cpp | 16 ---- backends/tests/CMakeLists.txt | 1 + .../tests/test_sycl_platform_interface.cpp | 50 +++++++++++++ backends/tests/test_sycl_queue_manager.cpp | 18 +---- dppl/sycl_core.pyx | 28 +++---- 11 files changed, 191 insertions(+), 107 deletions(-) create mode 100644 backends/include/dppl_sycl_platform_interface.h create mode 100644 backends/source/dppl_sycl_platform_interface.cpp create mode 100644 backends/tests/test_sycl_platform_interface.cpp diff --git a/backends/CMakeLists.txt b/backends/CMakeLists.txt index 921bb1d3eb..d1a6dd8aa2 100644 --- a/backends/CMakeLists.txt +++ b/backends/CMakeLists.txt @@ -98,6 +98,7 @@ add_library( SHARED source/dppl_sycl_context_interface.cpp source/dppl_sycl_device_interface.cpp + source/dppl_sycl_platform_interface.cpp source/dppl_sycl_queue_interface.cpp source/dppl_sycl_queue_manager.cpp ) diff --git a/backends/include/dppl_sycl_platform_interface.h b/backends/include/dppl_sycl_platform_interface.h new file mode 100644 index 0000000000..459a39a1da --- /dev/null +++ b/backends/include/dppl_sycl_platform_interface.h @@ -0,0 +1,49 @@ +//===--- dppl_sycl_platform_interface.h - DPPL-SYCL interface ---*--C++ -*-===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 header declares a C interface to sycl::platform interface functions. +/// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "dppl_data_types.h" +#include "Support/DllExport.h" +#include "Support/ExternC.h" + +DPPL_C_EXTERN_C_BEGIN + +/*! + * @brief Get the number of sycl::platform available on the system. + * + * @return The number of available sycl::platforms. + */ +DPPL_API +size_t DPPLPlatform_GetNumPlatforms (); + +/*! + * @brief Prints out some selected info about all sycl::platform on the system. + * + */ +DPPL_API +void DPPLPlatform_DumpInfo (); + +DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index f2493b87f9..60d080e3f0 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -35,13 +35,6 @@ DPPL_C_EXTERN_C_BEGIN -/*! - * @brief Prints out information about the Sycl environment, such as - * number of available platforms, number of activated queues, etc. - */ -DPPL_API -void DPPLDumpPlatformInfo (); - /*! * @brief Returns the Sycl context for the queue. * diff --git a/backends/include/dppl_sycl_queue_manager.h b/backends/include/dppl_sycl_queue_manager.h index 741cb51ad2..8d550be6b9 100644 --- a/backends/include/dppl_sycl_queue_manager.h +++ b/backends/include/dppl_sycl_queue_manager.h @@ -42,14 +42,6 @@ DPPL_C_EXTERN_C_BEGIN -/*! - * @brief Get the number of sycl::platform available on the system. - * - * @return The number of available sycl::platforms. - */ -DPPL_API -size_t DPPLGetNumPlatforms (); - /*! * @brief Get the sycl::queue object that is currently activated for this * thread. diff --git a/backends/source/dppl_sycl_platform_interface.cpp b/backends/source/dppl_sycl_platform_interface.cpp new file mode 100644 index 0000000000..271a64b31a --- /dev/null +++ b/backends/source/dppl_sycl_platform_interface.cpp @@ -0,0 +1,74 @@ +//===--- dppl_sycl_platform_interface.cpp - DPPL-SYCL interface --*- C++ -*-===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 the functions declared in +/// dppl_sycl_platform_interface.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_sycl_platform_interface.h" +#include +#include +#include + +#include + +using namespace cl::sycl; + +/*! + * Prints out the following sycl::info::platform attributes for each platform + * found on the system: + * - info::platform::name + * - info::platform::version + * - info::platform::profile + * + */ +void DPPLPlatform_DumpInfo () +{ + size_t i = 0; + + // Print out the info for each platform + auto platforms = platform::get_platforms(); + for (auto &p : platforms) { + std::cout << "---Platform " << i << '\n'; + std::stringstream ss; + + ss << std::setw(4) << " " << std::left << std::setw(12) << "Name" + << p.get_info() << '\n'; + ss << std::setw(4) << " " << std::left << std::setw(12) << "Version" + << p.get_info() << '\n'; + ss << std::setw(4) << " " << std::left << std::setw(12) << "Vendor" + << p.get_info() << '\n'; + ss << std::setw(4) << " " << std::left << std::setw(12) << "Profile" + << p.get_info() << '\n'; + + std::cout << ss.str(); + ++i; + } +} + +/*! + * Returns the number of sycl::platform on the system. + */ +size_t DPPLPlatform_GetNumPlatforms () +{ + return platform::get_platforms().size(); +} diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index 2dad78b604..e02479d69d 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -26,9 +26,6 @@ #include "dppl_sycl_queue_interface.h" #include "Support/CBindingWrapping.h" -#include -#include -#include #include /* SYCL headers */ @@ -39,51 +36,8 @@ namespace // Create wrappers for C Binding types (see CBindingWrapping.h). DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) -/*! - * @brief - * - * @param Platform My Param doc - */ -void dump_platform_info (const platform & Platform) -{ - std::stringstream ss; - - ss << std::setw(4) << " " << std::left << std::setw(12) << "Name" - << Platform.get_info() << '\n'; - ss << std::setw(4) << " " << std::left << std::setw(12) << "Version" - << Platform.get_info() << '\n'; - ss << std::setw(4) << " " << std::left << std::setw(12) << "Vendor" - << Platform.get_info() << '\n'; - ss << std::setw(4) << " " << std::left << std::setw(12) << "Profile" - << Platform.get_info() << '\n'; - - std::cout << ss.str(); -} - } /* end of anonymous namespace */ -/*! - * Prints out number of available SYCL platforms, number of CPU queues, number - * of GPU queues, metadata about the current global queue, and how many queues - * are currently activated. More information can be added in future, and - * functions to extract these information using SYCL API (e.g. device_info) - * may also be added. For now, this function can be used as a basic canary test - * to check if the queue manager was properly initialized. - * - */ -void DPPLDumpPlatformInfo () -{ - size_t i = 0; - - // Print out the info for each platform - auto platforms = platform::get_platforms(); - for (auto &p : platforms) { - std::cout << "---Platform " << i << '\n'; - dump_platform_info(p); - ++i; - } -} - __dppl_give DPPLSyclDeviceRef DPPLGetDeviceFromQueue (__dppl_keep const DPPLSyclQueueRef QRef) { diff --git a/backends/source/dppl_sycl_queue_manager.cpp b/backends/source/dppl_sycl_queue_manager.cpp index e6e4485942..17776fda11 100644 --- a/backends/source/dppl_sycl_queue_manager.cpp +++ b/backends/source/dppl_sycl_queue_manager.cpp @@ -117,14 +117,6 @@ class QMgrHelper //----------------------------- Public API -----------------------------------// -/*! - * Returns the number of sycl::platform on the system. - */ -size_t DPPLGetNumPlatforms () -{ - return platform::get_platforms().size(); -} - /*! * Allocates a new copy of the present top of stack queue, which can be the * default queue and returns to caller. The caller owns the pointer and is @@ -284,14 +276,6 @@ QMgrHelper::popSyclQueue () } /* end of anonymous namespace */ -/*! - * Returns the number of sycl::platform on the system. - */ -size_t DPPLGetNumPlatforms () -{ - return platform::get_platforms().size(); -} - /*! * Returns inside the number of activated queues not including the global queue * (QMgrHelper::active_queues[0]). diff --git a/backends/tests/CMakeLists.txt b/backends/tests/CMakeLists.txt index e83f408f05..c3add176ba 100644 --- a/backends/tests/CMakeLists.txt +++ b/backends/tests/CMakeLists.txt @@ -24,6 +24,7 @@ else() set(PYDPPL_BACKEND_TEST_CASES test_sycl_queue_manager + test_sycl_platform_interface ) foreach(TEST_CASE ${PYDPPL_BACKEND_TEST_CASES}) diff --git a/backends/tests/test_sycl_platform_interface.cpp b/backends/tests/test_sycl_platform_interface.cpp new file mode 100644 index 0000000000..3de7029b40 --- /dev/null +++ b/backends/tests/test_sycl_platform_interface.cpp @@ -0,0 +1,50 @@ +//===--- test_sycl_platform_interface.cpp - DPPL-SYCL interface -*- C++ -*-===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 has unit test cases for functions defined in +/// dppl_sycl_platform_interface.h. +/// +//===----------------------------------------------------------------------===// +#include "dppl_sycl_platform_interface.h" +#include + +struct TestDPPLSyclPlatformInterface : public ::testing::Test +{ }; + + +TEST_F (TestDPPLSyclPlatformInterface, CheckGetNumPlatforms) +{ + auto nplatforms = DPPLPlatform_GetNumPlatforms(); + EXPECT_GE(nplatforms, 0); +} + +TEST_F (TestDPPLSyclPlatformInterface, CheckDPPLPlatformDumpInfo) +{ + EXPECT_NO_FATAL_FAILURE(DPPLPlatform_DumpInfo()); +} + +int +main (int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + return ret; +} diff --git a/backends/tests/test_sycl_queue_manager.cpp b/backends/tests/test_sycl_queue_manager.cpp index 8ca98aa66a..d38ddb2a8a 100644 --- a/backends/tests/test_sycl_queue_manager.cpp +++ b/backends/tests/test_sycl_queue_manager.cpp @@ -20,7 +20,7 @@ /// /// \file /// This file has unit test cases for functions defined in -/// dppl_sycl_queue_interface.h. +/// dppl_sycl_queue_interface.h and dppl_sycl_queue_manager.h. /// //===----------------------------------------------------------------------===// #include "dppl_sycl_device_interface.h" @@ -56,15 +56,7 @@ namespace } struct TestDPPLSyclQueuemanager : public ::testing::Test -{ - -}; - -TEST_F (TestDPPLSyclQueuemanager, CheckGetNumPlatforms) -{ - auto nplatforms = DPPLGetNumPlatforms(); - EXPECT_GE(nplatforms, 0); -} +{ }; TEST_F (TestDPPLSyclQueuemanager, CheckDPPLGetCurrentQueue) @@ -132,12 +124,6 @@ TEST_F (TestDPPLSyclQueuemanager, CheckGetNumActivatedQueues) } -TEST_F (TestDPPLSyclQueuemanager, CheckDPPLDumpPlatformInfo) -{ - EXPECT_NO_FATAL_FAILURE(DPPLDumpPlatformInfo()); -} - - TEST_F (TestDPPLSyclQueuemanager, CheckDPPLDumpDeviceInfo) { auto q = DPPLGetCurrentQueue(); diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index f167cc3cdf..4018b3b734 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -60,16 +60,6 @@ cdef extern from "dppl_sycl_types.h": cdef extern from "dppl_sycl_context_interface.h": cdef void DPPLDeleteSyclContext (DPPLSyclContextRef CtxtRef) except + - -cdef extern from "dppl_sycl_queue_interface.h": - cdef void DPPLDeleteSyclQueue (DPPLSyclQueueRef QRef) except + - cdef void DPPLDumpPlatformInfo () except + - cdef DPPLSyclContextRef DPPLGetContextFromQueue (const DPPLSyclQueueRef Q) \ - except+ - cdef DPPLSyclDeviceRef DPPLGetDeviceFromQueue (const DPPLSyclQueueRef Q) \ - except + - - cdef extern from "dppl_sycl_device_interface.h": cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + cdef void DPPLDeleteSyclDevice (DPPLSyclDeviceRef DRef) except + @@ -89,6 +79,17 @@ cdef extern from "dppl_sycl_device_interface.h": cdef void DPPLDeleteDeviceVendorName (const char *VendorName) except + cdef void DPPLDeleteDeviceDriverInfo (const char *DriverInfo) except + +cdef extern from "dppl_sycl_platform_interface.h": + cdef size_t DPPLPlatform_GetNumPlatforms () + cdef void DPPLPlatform_DumpInfo () + +cdef extern from "dppl_sycl_queue_interface.h": + cdef void DPPLDeleteSyclQueue (DPPLSyclQueueRef QRef) except + + cdef DPPLSyclContextRef DPPLGetContextFromQueue (const DPPLSyclQueueRef Q) \ + except+ + cdef DPPLSyclDeviceRef DPPLGetDeviceFromQueue (const DPPLSyclQueueRef Q) \ + except + + cdef extern from "dppl_sycl_queue_manager.h": cdef enum _device_type 'DPPLSyclDeviceType': _GPU 'DPPL_GPU' @@ -98,7 +99,6 @@ cdef extern from "dppl_sycl_queue_manager.h": cdef size_t DPPLGetNumCPUQueues () except + cdef size_t DPPLGetNumGPUQueues () except + cdef size_t DPPLGetNumActivatedQueues () except + - cdef size_t DPPLGetNumPlatforms () except + cdef DPPLSyclQueueRef DPPLGetQueue (_device_type DTy, size_t device_num) except + cdef void DPPLPopSyclQueue () except + @@ -217,7 +217,7 @@ cdef class _SyclQueueManager: DPPLPopSyclQueue() def has_sycl_platforms (self): - cdef size_t num_platforms = DPPLGetNumPlatforms() + cdef size_t num_platforms = DPPLPlatform_GetNumPlatforms() if num_platforms: return True else: @@ -226,7 +226,7 @@ cdef class _SyclQueueManager: def get_num_platforms (self): ''' Returns the number of available SYCL/OpenCL platforms. ''' - return DPPLGetNumPlatforms() + return DPPLPlatform_GetNumPlatforms() def get_num_activated_queues (self): ''' Return the number of currently activated queues for this thread. @@ -264,7 +264,7 @@ cdef class _SyclQueueManager: def dump (self): ''' Prints information about the Runtime object. ''' - DPPLDumpPlatformInfo() + DPPLPlatform_DumpInfo() def is_in_dppl_ctxt (self): cdef size_t num = DPPLGetNumActivatedQueues() From 4a114909cffa612e302dd26c8c14ea2775ebc2a2 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 16 Sep 2020 15:38:09 -0500 Subject: [PATCH 12/93] Create a single utility function to delete C strings. --- backends/CMakeLists.txt | 1 + backends/include/dppl_sycl_device_interface.h | 27 ------------ backends/include/dppl_utils.h | 41 +++++++++++++++++++ .../source/dppl_sycl_device_interface.cpp | 17 +------- backends/source/dppl_utils.cpp | 31 ++++++++++++++ dppl/sycl_core.pyx | 11 +++-- 6 files changed, 79 insertions(+), 49 deletions(-) create mode 100644 backends/include/dppl_utils.h create mode 100644 backends/source/dppl_utils.cpp diff --git a/backends/CMakeLists.txt b/backends/CMakeLists.txt index d1a6dd8aa2..b812978ed0 100644 --- a/backends/CMakeLists.txt +++ b/backends/CMakeLists.txt @@ -101,6 +101,7 @@ add_library( source/dppl_sycl_platform_interface.cpp source/dppl_sycl_queue_interface.cpp source/dppl_sycl_queue_manager.cpp + source/dppl_utils.cpp ) # Install DPPLOpenCLInterface diff --git a/backends/include/dppl_sycl_device_interface.h b/backends/include/dppl_sycl_device_interface.h index e379f7d733..adbc216662 100644 --- a/backends/include/dppl_sycl_device_interface.h +++ b/backends/include/dppl_sycl_device_interface.h @@ -117,15 +117,6 @@ DPPL_API __dppl_give const char* DPPLGetDeviceDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef); -/*! - * @brief Deletes a C string corresponding to the string returned by - * DPPLGetDeviceDriverInfo(). - * - * @param DriverInfo C String for the driver number. - */ -DPPL_API -void DPPLDeleteDeviceDriverInfo (__dppl_take const char* DriverInfo); - /*! * @brief Returns a C string for the device name. * @@ -136,15 +127,6 @@ DPPL_API __dppl_give const char* DPPLGetDeviceName (__dppl_keep const DPPLSyclDeviceRef DRef); -/*! - * @brief Deletes a C string corresponding to the string returned by - * DPPLGetDeviceName(). - * - * @param DeviceName C String for the device name. - */ -DPPL_API -void DPPLDeleteDeviceName (__dppl_take const char* DeviceName); - /*! * @brief Returns a C string corresponding to the vendor name. * @@ -155,15 +137,6 @@ DPPL_API __dppl_give const char* DPPLGetDeviceVendorName (__dppl_keep const DPPLSyclDeviceRef DRef); -/*! - * @brief Deletes a C string corresponding to the string returned by - * DPPLGetDeviceVendorName(). - * - * @param char C String for the vendor name. - */ -DPPL_API -void DPPLDeleteDeviceVendorName (__dppl_take const char* VendorName); - /*! * @brief Returns True if the device and the host share a unified memory * subsystem, else returns False. diff --git a/backends/include/dppl_utils.h b/backends/include/dppl_utils.h new file mode 100644 index 0000000000..89071f19f9 --- /dev/null +++ b/backends/include/dppl_utils.h @@ -0,0 +1,41 @@ +//===------------- dppl_utils.h - DPPL-SYCL interface --*-- C++ -----*-----===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 common helper functions used in other places in DPPL. +//===----------------------------------------------------------------------===// + +#pragma once + +#include "Support/DllExport.h" +#include "Support/ExternC.h" +#include "Support/MemOwnershipAttrs.h" + +DPPL_C_EXTERN_C_BEGIN + +/*! + * @brief Deletes the C String argument + * + * @param str C string to be deleted + */ +DPPL_API +void DPPLDeleteCString (__dppl_take const char* str); + +DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_device_interface.cpp b/backends/source/dppl_sycl_device_interface.cpp index 4efcf40580..f70f34be1e 100644 --- a/backends/source/dppl_sycl_device_interface.cpp +++ b/backends/source/dppl_sycl_device_interface.cpp @@ -38,7 +38,7 @@ namespace DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPPLSyclDeviceRef) /*! - * @brief + * @brief Helper function to print the metadata for a sycl::device. * * @param Device My Param doc */ @@ -108,11 +108,6 @@ DPPLGetDeviceName (__dppl_keep const DPPLSyclDeviceRef DRef) return cstr_name; } -void DPPLDeleteDeviceName (__dppl_take const char *DeviceName) -{ - delete DeviceName; -} - __dppl_give const char* DPPLGetDeviceVendorName (__dppl_keep const DPPLSyclDeviceRef DRef) { @@ -122,11 +117,6 @@ DPPLGetDeviceVendorName (__dppl_keep const DPPLSyclDeviceRef DRef) return cstr_vendor; } -void DPPLDeleteDeviceVendorName (__dppl_take const char *VendorName) -{ - delete VendorName; -} - __dppl_give const char* DPPLGetDeviceDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef) { @@ -136,11 +126,6 @@ DPPLGetDeviceDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef) return cstr_driver; } -void DPPLDeleteDeviceDriverInfo (__dppl_take const char *DriverInfo) -{ - delete DriverInfo; -} - bool DPPLGetDeviceHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef) { return unwrap(DRef)->get_info(); diff --git a/backends/source/dppl_utils.cpp b/backends/source/dppl_utils.cpp new file mode 100644 index 0000000000..f9c7460f73 --- /dev/null +++ b/backends/source/dppl_utils.cpp @@ -0,0 +1,31 @@ +//===--------- dppl_utils.cpp - DPPL-SYCL interface ----*---- C++ ----*----===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 the helper functions defined in dppl_utils.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_utils.h" + +void DPPLDeleteCString (__dppl_take const char* str) +{ + delete str; +} \ No newline at end of file diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index 4018b3b734..1ed47334df 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -44,6 +44,8 @@ cdef class UnsupportedDeviceTypeError(Exception): ''' pass +cdef extern from "dppl_utils.h": + cdef void DPPLDeleteCString (const char *str) cdef extern from "dppl_sycl_types.h": cdef struct DPPLOpaqueSyclContext: @@ -75,9 +77,6 @@ cdef extern from "dppl_sycl_device_interface.h": except + cdef bool DPPLGetDeviceHostUnifiedMemory (const DPPLSyclDeviceRef DRef) \ except + - cdef void DPPLDeleteDeviceName (const char *DeviceName) except + - cdef void DPPLDeleteDeviceVendorName (const char *VendorName) except + - cdef void DPPLDeleteDeviceDriverInfo (const char *DriverInfo) except + cdef extern from "dppl_sycl_platform_interface.h": cdef size_t DPPLPlatform_GetNumPlatforms () @@ -143,9 +142,9 @@ cdef class SyclDevice: def __dealloc__ (self): DPPLDeleteSyclDevice(self.device_ptr) - DPPLDeleteDeviceName(self.device_name) - DPPLDeleteDeviceVendorName(self.vendor_name) - DPPLDeleteDeviceDriverInfo(self.driver_version) + DPPLDeleteCString(self.device_name) + DPPLDeleteCString(self.vendor_name) + DPPLDeleteCString(self.driver_version) def dump_device_info (self): ''' Print information about the SYCL device. From 6e246fb11723fef219a585571604a1b510a5ac6e Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 10:30:21 +0300 Subject: [PATCH 13/93] Update backends/source/dppl_utils.cpp New line at EOF. --- backends/source/dppl_utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backends/source/dppl_utils.cpp b/backends/source/dppl_utils.cpp index f9c7460f73..39006153ae 100644 --- a/backends/source/dppl_utils.cpp +++ b/backends/source/dppl_utils.cpp @@ -28,4 +28,4 @@ void DPPLDeleteCString (__dppl_take const char* str) { delete str; -} \ No newline at end of file +} From abcf6f3f4bba0add91ed46030d24fa8265a660fe Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 13:25:54 +0300 Subject: [PATCH 14/93] Add _sycl_core.pxd file. --- dppl/_memory.pyx | 3 ++- dppl/_sycl_core.pxd | 43 +++++++++++++++++++++++++++++++++++++++++++ dppl/sycl_core.pyx | 1 - 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 dppl/_sycl_core.pxd diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 4d6639a0c4..052bf44d37 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -1,4 +1,5 @@ import dppl +cimport dppl._sycl_core from cython.operator cimport dereference as deref @@ -66,7 +67,7 @@ cdef class Memory: cdef object queue_cap def __cinit__(self, Py_ssize_t nbytes): - cdef object q_cap + cdef dppl._sycl_core.SyclQueue q_cap cdef void* queue_ptr cdef void* p diff --git a/dppl/_sycl_core.pxd b/dppl/_sycl_core.pxd new file mode 100644 index 0000000000..9f62a13124 --- /dev/null +++ b/dppl/_sycl_core.pxd @@ -0,0 +1,43 @@ +##===------------- sycl_core.pxd - DPPL interface ------*- Cython -*-------===## +## +## Python Data Parallel Processing Library (PyDPPL) +## +## Copyright 2020 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 the Cython interface for the Sycl API of PyDPPL. +## +##===----------------------------------------------------------------------===## + +cdef extern from "dppl_sycl_types.h": + cdef struct DPPLOpaqueSyclQueue + + ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef + + +cdef class SyclQueue: + ''' Wrapper class for a Sycl queue. + ''' + + cdef DPPLSyclQueueRef queue_ptr + + @staticmethod + cdef SyclQueue _create (DPPLSyclQueueRef qref) + cpdef get_sycl_context (self) + cpdef get_sycl_device (self) + cdef DPPLSyclQueueRef get_queue_ref (self) + diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index 1ed47334df..96993eea05 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -178,7 +178,6 @@ cdef class SyclDevice: cdef class SyclQueue: ''' Wrapper class for a Sycl queue. ''' - cdef DPPLSyclQueueRef queue_ptr @staticmethod cdef SyclQueue _create (DPPLSyclQueueRef qref): From 039b79453df85f8fe878fffaf3951f94c7ff8ddb Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 13:39:06 +0300 Subject: [PATCH 15/93] Remove using of PyCapsule in _memory.pyx. --- dppl/_memory.pyx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 052bf44d37..66b3303293 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -3,7 +3,6 @@ cimport dppl._sycl_core from cython.operator cimport dereference as deref -from cpython.pycapsule cimport PyCapsule_GetPointer from cpython cimport Py_buffer cdef extern from "CL/sycl.hpp" namespace "cl::sycl::usm": @@ -27,13 +26,13 @@ cdef extern from "CL/sycl.hpp" namespace "cl::sycl": cdef class SyclQueue: - cdef object queue_cap + cdef dppl._sycl_core.SyclQueue queue_cap cdef queue q def __cinit__(self): cdef void* q_ptr self.queue_cap = dppl.get_current_queue() - q_ptr = PyCapsule_GetPointer(self.queue_cap, NULL) + q_ptr = self.queue_cap.get_queue_ref() if (q_ptr): self.q = deref(q_ptr) else: @@ -64,7 +63,7 @@ cdef class SyclQueue: cdef class Memory: cdef void* _ptr cdef Py_ssize_t nbytes - cdef object queue_cap + cdef dppl._sycl_core.SyclQueue queue_cap def __cinit__(self, Py_ssize_t nbytes): cdef dppl._sycl_core.SyclQueue q_cap @@ -77,7 +76,7 @@ cdef class Memory: if (nbytes > 0): q_cap = dppl.get_current_queue() - queue_ptr = PyCapsule_GetPointer(q_cap, NULL) + queue_ptr = q_cap.get_queue_ref() p = malloc_shared(nbytes, deref(queue_ptr)) if (p): self._ptr = p @@ -92,7 +91,7 @@ cdef class Memory: cdef void* queue_ptr if (self._ptr): - queue_ptr = PyCapsule_GetPointer(self.queue_cap, NULL) + queue_ptr = self.queue_cap.get_queue_ref() free(self._ptr, deref(queue_ptr)) self._ptr = NULL self.nbytes = 0 @@ -129,9 +128,10 @@ cdef class Memory: def _usm_type(self, qcaps=None): cdef void *q_ptr cdef alloc ptr_type + cdef dppl._sycl_core.SyclQueue _cap _cap = qcaps if (qcaps) else self.queue_cap - q_ptr = PyCapsule_GetPointer(_cap, NULL) + q_ptr = _cap.get_queue_ref() ptr_type = get_pointer_type(self._ptr, deref(q_ptr).get_context()) if (ptr_type == alloc.shared): return "shared" From dc72b03666601c8076bc63f8ddc0fc8a7867d1b2 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 13:40:40 +0300 Subject: [PATCH 16/93] Small style fixes in _memory.pyx. --- dppl/_memory.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 66b3303293..70f0e0b491 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -73,7 +73,7 @@ cdef class Memory: self._ptr = NULL self.queue_cap = None self.nbytes = 0 - + if (nbytes > 0): q_cap = dppl.get_current_queue() queue_ptr = q_cap.get_queue_ref() @@ -96,7 +96,7 @@ cdef class Memory: self._ptr = NULL self.nbytes = 0 self.queue_cap = None - + def __getbuffer__(self, Py_buffer *buffer, int flags): buffer.buf = self._ptr buffer.format = 'B' # byte From b5ab5d600ee150bb5630381f1097c3dceaa3944a Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 17:54:01 +0300 Subject: [PATCH 17/93] Moved functions from _memory.pyx to C-API interface library. --- backends/include/dppl_sycl_types.h | 6 ++ backends/include/dppl_sycl_usm_interface.h | 22 ++++++- backends/source/dppl_sycl_usm_interface.cpp | 26 ++++++++ dppl/_memory.pyx | 70 +++++++++++---------- 4 files changed, 91 insertions(+), 33 deletions(-) diff --git a/backends/include/dppl_sycl_types.h b/backends/include/dppl_sycl_types.h index d1777aae5d..db61bfb677 100644 --- a/backends/include/dppl_sycl_types.h +++ b/backends/include/dppl_sycl_types.h @@ -55,3 +55,9 @@ typedef struct DPPLOpaqueSyclQueue *DPPLSyclQueueRef; * */ typedef struct DPPLOpaqueSyclProgram *DPPLSyclProgramRef; + +/*! + * @brief + * + */ +typedef struct DPPLOpaqueMemoryUSMShared *DPPLMemoryUSMSharedRef; diff --git a/backends/include/dppl_sycl_usm_interface.h b/backends/include/dppl_sycl_usm_interface.h index 94d3ca46be..10783f8a89 100644 --- a/backends/include/dppl_sycl_usm_interface.h +++ b/backends/include/dppl_sycl_usm_interface.h @@ -19,14 +19,34 @@ //===----------------------------------------------------------------------===// /// /// \file -/// TODO +/// This header declares a C interface to sycl::usm interface functions. /// //===----------------------------------------------------------------------===// #pragma once +#include "dppl_data_types.h" +#include "dppl_sycl_types.h" +#include "Support/DllExport.h" #include "Support/ExternC.h" +#include "Support/MemOwnershipAttrs.h" DPPL_C_EXTERN_C_BEGIN +/*! + * @brief Crete USM shared memory. + * + * @return The pointer to USM shared memory. + */ +DPPL_API +__dppl_give DPPLMemoryUSMSharedRef +DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); + +/*! + * @brief Free USM memory. + * + */ +DPPL_API +void DPPLfree (__dppl_take DPPLMemoryUSMSharedRef MRef, __dppl_keep const DPPLSyclQueueRef QRef); + DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_usm_interface.cpp b/backends/source/dppl_sycl_usm_interface.cpp index 974f8c0ad6..06f0d71b3e 100644 --- a/backends/source/dppl_sycl_usm_interface.cpp +++ b/backends/source/dppl_sycl_usm_interface.cpp @@ -22,8 +22,34 @@ /// TODO /// //===----------------------------------------------------------------------===// + #include "dppl_sycl_usm_interface.h" +#include "Support/CBindingWrapping.h" #include /* SYCL headers */ using namespace cl::sycl; + +// TODO: move it to one header for not duplicate in many cpp files +namespace +{ +// Create wrappers for C Binding types (see CBindingWrapping.h). + DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) + DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPPLMemoryUSMSharedRef) + +} /* end of anonymous namespace */ + +__dppl_give DPPLMemoryUSMSharedRef +DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) +{ + auto Q = unwrap(QRef); + auto Ptr = malloc_shared(size, *Q); + return reinterpret_cast(Ptr); +} + +void DPPLfree (DPPLMemoryUSMSharedRef MRef, __dppl_keep const DPPLSyclQueueRef QRef) +{ + auto Ptr = unwrap(MRef); + auto Q = unwrap(QRef); + free(Ptr, *Q); +} diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 70f0e0b491..93267fe290 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -20,19 +20,30 @@ cdef extern from "CL/sycl.hpp" namespace "cl::sycl": context get_context() nogil pass - cdef void* malloc_shared(Py_ssize_t, queue&) nogil - cdef void free(void *, queue&) nogil cdef alloc get_pointer_type(void *, context&) nogil +cdef extern from "dppl_sycl_types.h": + cdef struct DPPLOpaqueSyclQueue + cdef struct DPPLOpaqueMemoryUSMShared + + ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef + ctypedef DPPLOpaqueMemoryUSMShared* DPPLMemoryUSMSharedRef + + +cdef extern from "dppl_sycl_usm_interface.h": + cdef DPPLMemoryUSMSharedRef DPPLmalloc_shared (size_t size, DPPLSyclQueueRef QRef) except + + cdef void DPPLfree (DPPLMemoryUSMSharedRef MRef, DPPLSyclQueueRef QRef) except + + + cdef class SyclQueue: - cdef dppl._sycl_core.SyclQueue queue_cap + cdef dppl._sycl_core.SyclQueue queue cdef queue q def __cinit__(self): cdef void* q_ptr - self.queue_cap = dppl.get_current_queue() - q_ptr = self.queue_cap.get_queue_ref() + self.queue = dppl.get_current_queue() + q_ptr = self.queue.get_queue_ref() if (q_ptr): self.q = deref(q_ptr) else: @@ -54,48 +65,43 @@ cdef class SyclQueue: property get_capsule: def __get__(self): - return self.queue_cap + return self.queue cdef queue get_queue(self): return self.q cdef class Memory: - cdef void* _ptr + cdef DPPLMemoryUSMSharedRef _ptr cdef Py_ssize_t nbytes - cdef dppl._sycl_core.SyclQueue queue_cap + cdef dppl._sycl_core.SyclQueue queue def __cinit__(self, Py_ssize_t nbytes): - cdef dppl._sycl_core.SyclQueue q_cap - cdef void* queue_ptr - cdef void* p + cdef dppl._sycl_core.SyclQueue q + cdef DPPLMemoryUSMSharedRef p self._ptr = NULL - self.queue_cap = None + self.queue = None self.nbytes = 0 if (nbytes > 0): - q_cap = dppl.get_current_queue() - queue_ptr = q_cap.get_queue_ref() - p = malloc_shared(nbytes, deref(queue_ptr)) + q = dppl.get_current_queue() + p = DPPLmalloc_shared(nbytes, q.get_queue_ref()) if (p): self._ptr = p self.nbytes = nbytes - self.queue_cap = q_cap + self.queue = q else: raise RuntimeError("Null memory pointer returned") else: raise ValueError("Non-positive number of bytes found.") def __dealloc__(self): - cdef void* queue_ptr - if (self._ptr): - queue_ptr = self.queue_cap.get_queue_ref() - free(self._ptr, deref(queue_ptr)) + DPPLfree(self._ptr, self.queue.get_queue_ref()) self._ptr = NULL self.nbytes = 0 - self.queue_cap = None + self.queue = None def __getbuffer__(self, Py_buffer *buffer, int flags): buffer.buf = self._ptr @@ -120,7 +126,7 @@ cdef class Memory: property _queue: def __get__(self): - return self.queue_cap + return self.queue def __repr__(self): return "".format(self.nbytes, hex((self._ptr))) @@ -130,7 +136,7 @@ cdef class Memory: cdef alloc ptr_type cdef dppl._sycl_core.SyclQueue _cap - _cap = qcaps if (qcaps) else self.queue_cap + _cap = qcaps if (qcaps) else self.queue q_ptr = _cap.get_queue_ref() ptr_type = get_pointer_type(self._ptr, deref(q_ptr).get_context()) if (ptr_type == alloc.shared): @@ -144,12 +150,12 @@ cdef class Memory: # cdef void* _ptr # cdef Py_ssize_t nbytes -# cdef object queue_cap - - @staticmethod - cdef Memory create(void *p, Py_ssize_t nbytes, object queue_cap): - cdef Memory ret = Memory.__new__() - ret._ptr = p - ret.nbytes = nbytes - ret.q_cap = queue_cap - return ret +# cdef object queue + + # @staticmethod + # cdef Memory create(void *p, Py_ssize_t nbytes, object queue): + # cdef Memory ret = Memory.__new__() + # ret._ptr = p + # ret.nbytes = nbytes + # ret.q_cap = queue + # return ret From c57c05c07dc26158210601334fa9e6f5e9d21e74 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 22:53:48 +0300 Subject: [PATCH 18/93] Move Cython definitions for backend to single pxd file. --- dppl/_memory.pyx | 14 +------ dppl/_sycl_core.pxd | 32 +++++++++++---- dppl/backend.pxd | 99 +++++++++++++++++++++++++++++++++++++++++++++ dppl/sycl_core.pyx | 75 +++------------------------------- 4 files changed, 130 insertions(+), 90 deletions(-) create mode 100644 dppl/backend.pxd diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 93267fe290..4b1a3f9ef6 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -1,4 +1,5 @@ import dppl +from dppl.backend cimport * cimport dppl._sycl_core from cython.operator cimport dereference as deref @@ -23,19 +24,6 @@ cdef extern from "CL/sycl.hpp" namespace "cl::sycl": cdef alloc get_pointer_type(void *, context&) nogil -cdef extern from "dppl_sycl_types.h": - cdef struct DPPLOpaqueSyclQueue - cdef struct DPPLOpaqueMemoryUSMShared - - ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef - ctypedef DPPLOpaqueMemoryUSMShared* DPPLMemoryUSMSharedRef - - -cdef extern from "dppl_sycl_usm_interface.h": - cdef DPPLMemoryUSMSharedRef DPPLmalloc_shared (size_t size, DPPLSyclQueueRef QRef) except + - cdef void DPPLfree (DPPLMemoryUSMSharedRef MRef, DPPLSyclQueueRef QRef) except + - - cdef class SyclQueue: cdef dppl._sycl_core.SyclQueue queue cdef queue q diff --git a/dppl/_sycl_core.pxd b/dppl/_sycl_core.pxd index 9f62a13124..c4b8dd2ab1 100644 --- a/dppl/_sycl_core.pxd +++ b/dppl/_sycl_core.pxd @@ -23,21 +23,39 @@ ## ##===----------------------------------------------------------------------===## -cdef extern from "dppl_sycl_types.h": - cdef struct DPPLOpaqueSyclQueue +from .backend cimport * - ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef + +cdef class SyclContext: + ''' Wrapper class for a Sycl Context + ''' + cdef DPPLSyclContextRef ctxt_ptr + + @staticmethod + cdef SyclContext _create (DPPLSyclContextRef ctxt) + cdef DPPLSyclContextRef get_context_ref (self) + + +cdef class SyclDevice: + ''' Wrapper class for a Sycl Device + ''' + cdef DPPLSyclDeviceRef device_ptr + cdef const char *vendor_name + cdef const char *device_name + cdef const char *driver_version + + @staticmethod + cdef SyclDevice _create (DPPLSyclDeviceRef dref) + cdef DPPLSyclDeviceRef get_device_ptr (self) cdef class SyclQueue: ''' Wrapper class for a Sycl queue. ''' - cdef DPPLSyclQueueRef queue_ptr @staticmethod cdef SyclQueue _create (DPPLSyclQueueRef qref) - cpdef get_sycl_context (self) - cpdef get_sycl_device (self) + cpdef SyclContext get_sycl_context (self) + cpdef SyclDevice get_sycl_device (self) cdef DPPLSyclQueueRef get_queue_ref (self) - diff --git a/dppl/backend.pxd b/dppl/backend.pxd new file mode 100644 index 0000000000..23165c44fa --- /dev/null +++ b/dppl/backend.pxd @@ -0,0 +1,99 @@ +##===------------- backend.pyx - DPPL interface ------*- Cython -*-------===## +## +## Python Data Parallel Processing Library (PyDPPL) +## +## Copyright 2020 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 the Cython interface for the backend API of PyDPPL. +## +##===----------------------------------------------------------------------===## + +from libcpp cimport bool + + +cdef extern from "dppl_utils.h": + cdef void DPPLDeleteCString (const char *str) + + +cdef extern from "dppl_sycl_types.h": + cdef struct DPPLOpaqueSyclContext + cdef struct DPPLOpaqueSyclQueue + cdef struct DPPLOpaqueSyclDevice + cdef struct DPPLOpaqueMemoryUSMShared + + ctypedef DPPLOpaqueSyclContext* DPPLSyclContextRef + ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef + ctypedef DPPLOpaqueSyclDevice* DPPLSyclDeviceRef + ctypedef DPPLOpaqueMemoryUSMShared* DPPLMemoryUSMSharedRef + + +cdef extern from "dppl_sycl_context_interface.h": + cdef void DPPLDeleteSyclContext (DPPLSyclContextRef CtxtRef) except + + + +cdef extern from "dppl_sycl_device_interface.h": + cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + + cdef void DPPLDeleteSyclDevice (DPPLSyclDeviceRef DRef) except + + cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDeviceIsAccelerator (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDeviceIsCPU (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDeviceIsGPU (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDeviceIsHost (const DPPLSyclDeviceRef DRef) except + + cdef const char* DPPLGetDeviceDriverInfo (const DPPLSyclDeviceRef DRef) \ + except + + cdef const char* DPPLGetDeviceName (const DPPLSyclDeviceRef DRef) except + + cdef const char* DPPLGetDeviceVendorName (const DPPLSyclDeviceRef DRef) \ + except + + cdef bool DPPLGetDeviceHostUnifiedMemory (const DPPLSyclDeviceRef DRef) \ + except + + + +cdef extern from "dppl_sycl_platform_interface.h": + cdef size_t DPPLPlatform_GetNumPlatforms () + cdef void DPPLPlatform_DumpInfo () + + +cdef extern from "dppl_sycl_queue_interface.h": + cdef void DPPLDeleteSyclQueue (DPPLSyclQueueRef QRef) except + + cdef DPPLSyclContextRef DPPLGetContextFromQueue (const DPPLSyclQueueRef Q) \ + except+ + cdef DPPLSyclDeviceRef DPPLGetDeviceFromQueue (const DPPLSyclQueueRef Q) \ + except + + + +cdef extern from "dppl_sycl_queue_manager.h": + cdef enum _device_type 'DPPLSyclDeviceType': + _GPU 'DPPL_GPU' + _CPU 'DPPL_CPU' + + cdef DPPLSyclQueueRef DPPLGetCurrentQueue () except + + cdef size_t DPPLGetNumCPUQueues () except + + cdef size_t DPPLGetNumGPUQueues () except + + cdef size_t DPPLGetNumActivatedQueues () except + + cdef DPPLSyclQueueRef DPPLGetQueue (_device_type DTy, + size_t device_num) except + + cdef void DPPLPopSyclQueue () except + + cdef DPPLSyclQueueRef DPPLPushSyclQueue (_device_type DTy, + size_t device_num) except + + cdef void DPPLSetAsDefaultQueue (_device_type DTy, + size_t device_num) except + + + +cdef extern from "dppl_sycl_usm_interface.h": + cdef DPPLMemoryUSMSharedRef DPPLmalloc_shared (size_t size, DPPLSyclQueueRef QRef) except + + cdef void DPPLfree (DPPLMemoryUSMSharedRef MRef, DPPLSyclQueueRef QRef) except + diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index 96993eea05..5df3af4367 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -29,10 +29,12 @@ from __future__ import print_function from enum import Enum, auto import logging -from libcpp cimport bool +from dppl.backend cimport * + _logger = logging.getLogger(__name__) + class device_type(Enum): gpu = auto() cpu = auto() @@ -44,71 +46,8 @@ cdef class UnsupportedDeviceTypeError(Exception): ''' pass -cdef extern from "dppl_utils.h": - cdef void DPPLDeleteCString (const char *str) - -cdef extern from "dppl_sycl_types.h": - cdef struct DPPLOpaqueSyclContext: - pass - cdef struct DPPLOpaqueSyclQueue: - pass - cdef struct DPPLOpaqueSyclDevice: - pass - - ctypedef DPPLOpaqueSyclContext* DPPLSyclContextRef - ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef - ctypedef DPPLOpaqueSyclDevice* DPPLSyclDeviceRef - -cdef extern from "dppl_sycl_context_interface.h": - cdef void DPPLDeleteSyclContext (DPPLSyclContextRef CtxtRef) except + - -cdef extern from "dppl_sycl_device_interface.h": - cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + - cdef void DPPLDeleteSyclDevice (DPPLSyclDeviceRef DRef) except + - cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsAccelerator (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsCPU (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsGPU (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsHost (const DPPLSyclDeviceRef DRef) except + - cdef const char* DPPLGetDeviceDriverInfo (const DPPLSyclDeviceRef DRef) \ - except + - cdef const char* DPPLGetDeviceName (const DPPLSyclDeviceRef DRef) except + - cdef const char* DPPLGetDeviceVendorName (const DPPLSyclDeviceRef DRef) \ - except + - cdef bool DPPLGetDeviceHostUnifiedMemory (const DPPLSyclDeviceRef DRef) \ - except + - -cdef extern from "dppl_sycl_platform_interface.h": - cdef size_t DPPLPlatform_GetNumPlatforms () - cdef void DPPLPlatform_DumpInfo () - -cdef extern from "dppl_sycl_queue_interface.h": - cdef void DPPLDeleteSyclQueue (DPPLSyclQueueRef QRef) except + - cdef DPPLSyclContextRef DPPLGetContextFromQueue (const DPPLSyclQueueRef Q) \ - except+ - cdef DPPLSyclDeviceRef DPPLGetDeviceFromQueue (const DPPLSyclQueueRef Q) \ - except + - -cdef extern from "dppl_sycl_queue_manager.h": - cdef enum _device_type 'DPPLSyclDeviceType': - _GPU 'DPPL_GPU' - _CPU 'DPPL_CPU' - - cdef DPPLSyclQueueRef DPPLGetCurrentQueue () except + - cdef size_t DPPLGetNumCPUQueues () except + - cdef size_t DPPLGetNumGPUQueues () except + - cdef size_t DPPLGetNumActivatedQueues () except + - cdef DPPLSyclQueueRef DPPLGetQueue (_device_type DTy, - size_t device_num) except + - cdef void DPPLPopSyclQueue () except + - cdef DPPLSyclQueueRef DPPLPushSyclQueue (_device_type DTy, - size_t device_num) except + - cdef void DPPLSetAsDefaultQueue (_device_type DTy, - size_t device_num) except + - cdef class SyclContext: - cdef DPPLSyclContextRef ctxt_ptr @staticmethod cdef SyclContext _create (DPPLSyclContextRef ctxt): @@ -126,10 +65,6 @@ cdef class SyclContext: cdef class SyclDevice: ''' Wrapper class for a Sycl Device ''' - cdef DPPLSyclDeviceRef device_ptr - cdef const char *vendor_name - cdef const char *device_name - cdef const char *driver_version @staticmethod cdef SyclDevice _create (DPPLSyclDeviceRef dref): @@ -188,10 +123,10 @@ cdef class SyclQueue: def __dealloc__ (self): DPPLDeleteSyclQueue(self.queue_ptr) - cpdef get_sycl_context (self): + cpdef SyclContext get_sycl_context (self): return SyclContext._create(DPPLGetContextFromQueue(self.queue_ptr)) - cpdef get_sycl_device (self): + cpdef SyclDevice get_sycl_device (self): return SyclDevice._create(DPPLGetDeviceFromQueue(self.queue_ptr)) cdef DPPLSyclQueueRef get_queue_ref (self): From c075f0c9dbae1ae3030cc00b47bd68d03232856a Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 23:06:23 +0300 Subject: [PATCH 19/93] Remove SyclQueue from _memory.pyx --- dppl/_memory.pyx | 35 ------------------- .../dppl_tests/test_sycl_memory_manager.py | 3 -- 2 files changed, 38 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 4b1a3f9ef6..464dcb25e0 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -24,41 +24,6 @@ cdef extern from "CL/sycl.hpp" namespace "cl::sycl": cdef alloc get_pointer_type(void *, context&) nogil -cdef class SyclQueue: - cdef dppl._sycl_core.SyclQueue queue - cdef queue q - - def __cinit__(self): - cdef void* q_ptr - self.queue = dppl.get_current_queue() - q_ptr = self.queue.get_queue_ref() - if (q_ptr): - self.q = deref(q_ptr) - else: - raise ValueError("NULL pointer returned by the Capsule") - - def get_pointer_type(self, Py_ssize_t p): - cdef context ctx = self.q.get_context() - cdef void * p_ptr = p - - ptr_type = get_pointer_type(p_ptr, ctx) - if (ptr_type == alloc.shared): - return "shared" - elif (ptr_type == alloc.host): - return "host" - elif (ptr_type == alloc.device): - return "device" - else: - return "unknown" - - property get_capsule: - def __get__(self): - return self.queue - - cdef queue get_queue(self): - return self.q - - cdef class Memory: cdef DPPLMemoryUSMSharedRef _ptr cdef Py_ssize_t nbytes diff --git a/dppl/tests/dppl_tests/test_sycl_memory_manager.py b/dppl/tests/dppl_tests/test_sycl_memory_manager.py index 95051310db..7d0ea24aaa 100644 --- a/dppl/tests/dppl_tests/test_sycl_memory_manager.py +++ b/dppl/tests/dppl_tests/test_sycl_memory_manager.py @@ -39,7 +39,6 @@ def test_memory_without_context (self): mobj = self._create_memory() # Without context - self.assertEqual(mem.SyclQueue().get_pointer_type(mobj.pointer), 'shared') self.assertEqual(mobj._usm_type(), 'shared') def test_memory_cpu_context (self): @@ -47,7 +46,6 @@ def test_memory_cpu_context (self): # CPU context with dppl.device_context(dppl.device_type.cpu): - self.assertEqual(mem.SyclQueue().get_pointer_type(mobj.pointer), 'unknown') self.assertEqual(mobj._usm_type(), 'shared') def test_memory_gpu_context (self): @@ -55,5 +53,4 @@ def test_memory_gpu_context (self): # GPU context with dppl.device_context(dppl.device_type.gpu): - self.assertEqual(mem.SyclQueue().get_pointer_type(mobj.pointer), 'unknown') self.assertEqual(mobj._usm_type(), 'shared') From 708fd1f4679cac6040431554a128be938ff1dcf5 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 23:10:59 +0300 Subject: [PATCH 20/93] Use SyclQueue from --- dppl/_memory.pyx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 464dcb25e0..0e9cf72475 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -1,6 +1,6 @@ import dppl from dppl.backend cimport * -cimport dppl._sycl_core +from ._sycl_core cimport SyclQueue from cython.operator cimport dereference as deref @@ -27,10 +27,10 @@ cdef extern from "CL/sycl.hpp" namespace "cl::sycl": cdef class Memory: cdef DPPLMemoryUSMSharedRef _ptr cdef Py_ssize_t nbytes - cdef dppl._sycl_core.SyclQueue queue + cdef SyclQueue queue def __cinit__(self, Py_ssize_t nbytes): - cdef dppl._sycl_core.SyclQueue q + cdef SyclQueue q cdef DPPLMemoryUSMSharedRef p self._ptr = NULL @@ -87,7 +87,7 @@ cdef class Memory: def _usm_type(self, qcaps=None): cdef void *q_ptr cdef alloc ptr_type - cdef dppl._sycl_core.SyclQueue _cap + cdef SyclQueue _cap _cap = qcaps if (qcaps) else self.queue q_ptr = _cap.get_queue_ref() From 5d3db207cf981dfcc75fb134b32b3651229266d8 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 23:43:18 +0300 Subject: [PATCH 21/93] Remove cl::sycl::queue from _memory.pyx --- dppl/_memory.pyx | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 0e9cf72475..a9ec79405e 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -14,18 +14,13 @@ cdef extern from "CL/sycl.hpp" namespace "cl::sycl::usm": unknown 'cl::sycl::usm::alloc::unknown' cdef extern from "CL/sycl.hpp" namespace "cl::sycl": - cdef cppclass context nogil: - pass - - cdef cppclass queue nogil: - context get_context() nogil - pass + cdef cppclass context nogil cdef alloc get_pointer_type(void *, context&) nogil cdef class Memory: - cdef DPPLMemoryUSMSharedRef _ptr + cdef DPPLMemoryUSMSharedRef memory_ptr cdef Py_ssize_t nbytes cdef SyclQueue queue @@ -33,7 +28,7 @@ cdef class Memory: cdef SyclQueue q cdef DPPLMemoryUSMSharedRef p - self._ptr = NULL + self.memory_ptr = NULL self.queue = None self.nbytes = 0 @@ -41,7 +36,7 @@ cdef class Memory: q = dppl.get_current_queue() p = DPPLmalloc_shared(nbytes, q.get_queue_ref()) if (p): - self._ptr = p + self.memory_ptr = p self.nbytes = nbytes self.queue = q else: @@ -50,14 +45,14 @@ cdef class Memory: raise ValueError("Non-positive number of bytes found.") def __dealloc__(self): - if (self._ptr): - DPPLfree(self._ptr, self.queue.get_queue_ref()) - self._ptr = NULL + if (self.memory_ptr): + DPPLfree(self.memory_ptr, self.queue.get_queue_ref()) + self.memory_ptr = NULL self.nbytes = 0 self.queue = None def __getbuffer__(self, Py_buffer *buffer, int flags): - buffer.buf = self._ptr + buffer.buf = self.memory_ptr buffer.format = 'B' # byte buffer.internal = NULL # see References buffer.itemsize = 1 @@ -71,7 +66,7 @@ cdef class Memory: property pointer: def __get__(self): - return (self._ptr) + return (self.memory_ptr) property nbytes: def __get__(self): @@ -82,16 +77,14 @@ cdef class Memory: return self.queue def __repr__(self): - return "".format(self.nbytes, hex((self._ptr))) + return "".format(self.nbytes, hex((self.memory_ptr))) - def _usm_type(self, qcaps=None): - cdef void *q_ptr + def _usm_type(self): + cdef DPPLSyclContextRef ctx_ptr cdef alloc ptr_type - cdef SyclQueue _cap - _cap = qcaps if (qcaps) else self.queue - q_ptr = _cap.get_queue_ref() - ptr_type = get_pointer_type(self._ptr, deref(q_ptr).get_context()) + ctx_ptr = self.queue.get_sycl_context().get_context_ref() + ptr_type = get_pointer_type(self.memory_ptr, deref(ctx_ptr)) if (ptr_type == alloc.shared): return "shared" elif (ptr_type == alloc.host): @@ -101,14 +94,10 @@ cdef class Memory: else: return "unknown" -# cdef void* _ptr -# cdef Py_ssize_t nbytes -# cdef object queue - # @staticmethod # cdef Memory create(void *p, Py_ssize_t nbytes, object queue): # cdef Memory ret = Memory.__new__() - # ret._ptr = p + # ret.memory_ptr = p # ret.nbytes = nbytes # ret.q_cap = queue # return ret From 6747080044a62e6d2a3747e585525cdb88c878fd Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Thu, 17 Sep 2020 23:46:36 +0300 Subject: [PATCH 22/93] Removed commented code from _memory.pyx --- dppl/_memory.pyx | 8 -------- 1 file changed, 8 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index a9ec79405e..28b5738008 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -93,11 +93,3 @@ cdef class Memory: return "device" else: return "unknown" - - # @staticmethod - # cdef Memory create(void *p, Py_ssize_t nbytes, object queue): - # cdef Memory ret = Memory.__new__() - # ret.memory_ptr = p - # ret.nbytes = nbytes - # ret.q_cap = queue - # return ret From 56241ef88e352b3347bdcee6c0827a313ec46052 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 00:03:29 +0300 Subject: [PATCH 23/93] Eliminate temporary context object. --- dppl/_memory.pyx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 28b5738008..fb5b3c9c42 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -1,6 +1,6 @@ import dppl from dppl.backend cimport * -from ._sycl_core cimport SyclQueue +from ._sycl_core cimport SyclContext, SyclQueue from cython.operator cimport dereference as deref @@ -80,11 +80,11 @@ cdef class Memory: return "".format(self.nbytes, hex((self.memory_ptr))) def _usm_type(self): - cdef DPPLSyclContextRef ctx_ptr + cdef SyclContext ctxt cdef alloc ptr_type - ctx_ptr = self.queue.get_sycl_context().get_context_ref() - ptr_type = get_pointer_type(self.memory_ptr, deref(ctx_ptr)) + ctxt = self.queue.get_sycl_context() + ptr_type = get_pointer_type(self.memory_ptr, deref(ctxt.get_context_ref())) if (ptr_type == alloc.shared): return "shared" elif (ptr_type == alloc.host): From 7fabbe4a972ed83a2b7cfb9d9db8a93f163c075d Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 00:06:00 +0300 Subject: [PATCH 24/93] Fix style. --- backends/include/dppl_sycl_usm_interface.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backends/include/dppl_sycl_usm_interface.h b/backends/include/dppl_sycl_usm_interface.h index 10783f8a89..26a609e546 100644 --- a/backends/include/dppl_sycl_usm_interface.h +++ b/backends/include/dppl_sycl_usm_interface.h @@ -47,6 +47,7 @@ DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); * */ DPPL_API -void DPPLfree (__dppl_take DPPLMemoryUSMSharedRef MRef, __dppl_keep const DPPLSyclQueueRef QRef); +void DPPLfree (__dppl_take DPPLMemoryUSMSharedRef MRef, + __dppl_keep const DPPLSyclQueueRef QRef); DPPL_C_EXTERN_C_END From abd373bf3b3e3cd389f91dcad289c91ef5d392a4 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 13:02:01 +0300 Subject: [PATCH 25/93] Add MemoryUSM* classes. --- backends/include/dppl_sycl_usm_interface.h | 18 +++++++++++ backends/source/dppl_sycl_usm_interface.cpp | 16 ++++++++++ dppl/_memory.pyx | 29 +++++++++++++++-- dppl/backend.pxd | 2 ++ .../dppl_tests/test_sycl_memory_manager.py | 32 +++++++++++++++++-- 5 files changed, 93 insertions(+), 4 deletions(-) diff --git a/backends/include/dppl_sycl_usm_interface.h b/backends/include/dppl_sycl_usm_interface.h index 26a609e546..4e5004dd6b 100644 --- a/backends/include/dppl_sycl_usm_interface.h +++ b/backends/include/dppl_sycl_usm_interface.h @@ -42,6 +42,24 @@ DPPL_API __dppl_give DPPLMemoryUSMSharedRef DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); +/*! + * @brief Crete USM host memory. + * + * @return The pointer to USM host memory. + */ +DPPL_API +__dppl_give DPPLMemoryUSMSharedRef +DPPLmalloc_host (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); + +/*! + * @brief Crete USM device memory. + * + * @return The pointer to USM device memory. + */ +DPPL_API +__dppl_give DPPLMemoryUSMSharedRef +DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); + /*! * @brief Free USM memory. * diff --git a/backends/source/dppl_sycl_usm_interface.cpp b/backends/source/dppl_sycl_usm_interface.cpp index 06f0d71b3e..74e4707978 100644 --- a/backends/source/dppl_sycl_usm_interface.cpp +++ b/backends/source/dppl_sycl_usm_interface.cpp @@ -47,6 +47,22 @@ DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) return reinterpret_cast(Ptr); } +__dppl_give DPPLMemoryUSMSharedRef +DPPLmalloc_host (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) +{ + auto Q = unwrap(QRef); + auto Ptr = malloc_host(size, *Q); + return reinterpret_cast(Ptr); +} + +__dppl_give DPPLMemoryUSMSharedRef +DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) +{ + auto Q = unwrap(QRef); + auto Ptr = malloc_device(size, *Q); + return reinterpret_cast(Ptr); +} + void DPPLfree (DPPLMemoryUSMSharedRef MRef, __dppl_keep const DPPLSyclQueueRef QRef) { auto Ptr = unwrap(MRef); diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index fb5b3c9c42..a944ee4f1c 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -24,7 +24,7 @@ cdef class Memory: cdef Py_ssize_t nbytes cdef SyclQueue queue - def __cinit__(self, Py_ssize_t nbytes): + cdef _cinit(self, Py_ssize_t nbytes, alloc ptr_type): cdef SyclQueue q cdef DPPLMemoryUSMSharedRef p @@ -34,7 +34,14 @@ cdef class Memory: if (nbytes > 0): q = dppl.get_current_queue() - p = DPPLmalloc_shared(nbytes, q.get_queue_ref()) + + if (ptr_type == alloc.shared): + p = DPPLmalloc_shared(nbytes, q.get_queue_ref()) + if (ptr_type == alloc.host): + p = DPPLmalloc_host(nbytes, q.get_queue_ref()) + if (ptr_type == alloc.device): + p = DPPLmalloc_device(nbytes, q.get_queue_ref()) + if (p): self.memory_ptr = p self.nbytes = nbytes @@ -93,3 +100,21 @@ cdef class Memory: return "device" else: return "unknown" + + +cdef class MemoryUSMShared(Memory): + + def __cinit__(self, Py_ssize_t nbytes): + self._cinit(nbytes, alloc.shared) + + +cdef class MemoryUSMHost(Memory): + + def __cinit__(self, Py_ssize_t nbytes): + self._cinit(nbytes, alloc.host) + + +cdef class MemoryUSMDevice(Memory): + + def __cinit__(self, Py_ssize_t nbytes): + self._cinit(nbytes, alloc.device) diff --git a/dppl/backend.pxd b/dppl/backend.pxd index 23165c44fa..e3d75c6f6f 100644 --- a/dppl/backend.pxd +++ b/dppl/backend.pxd @@ -96,4 +96,6 @@ cdef extern from "dppl_sycl_queue_manager.h": cdef extern from "dppl_sycl_usm_interface.h": cdef DPPLMemoryUSMSharedRef DPPLmalloc_shared (size_t size, DPPLSyclQueueRef QRef) except + + cdef DPPLMemoryUSMSharedRef DPPLmalloc_host (size_t size, DPPLSyclQueueRef QRef) except + + cdef DPPLMemoryUSMSharedRef DPPLmalloc_device (size_t size, DPPLSyclQueueRef QRef) except + cdef void DPPLfree (DPPLMemoryUSMSharedRef MRef, DPPLSyclQueueRef QRef) except + diff --git a/dppl/tests/dppl_tests/test_sycl_memory_manager.py b/dppl/tests/dppl_tests/test_sycl_memory_manager.py index 7d0ea24aaa..3476b30d5b 100644 --- a/dppl/tests/dppl_tests/test_sycl_memory_manager.py +++ b/dppl/tests/dppl_tests/test_sycl_memory_manager.py @@ -21,18 +21,19 @@ import unittest import dppl import dppl._memory as mem +from dppl._memory import MemoryUSMShared, MemoryUSMHost, MemoryUSMDevice class TestMemory (unittest.TestCase): # @unittest.skipIf(not dppl.has_sycl_platforms, "No SYCL platforms available") def test_memory_create (self): nbytes = 1024 - mobj = mem.Memory(nbytes) + mobj = MemoryUSMShared(nbytes) self.assertEqual(mobj.nbytes, nbytes) def _create_memory (self): nbytes = 1024 - mobj = mem.Memory(nbytes) + mobj = MemoryUSMShared(nbytes) return mobj def test_memory_without_context (self): @@ -54,3 +55,30 @@ def test_memory_gpu_context (self): # GPU context with dppl.device_context(dppl.device_type.gpu): self.assertEqual(mobj._usm_type(), 'shared') + + +class TestMemoryUSMShared(unittest.TestCase): + """Tests for MemoryUSMShared + """ + + def test_create (self): + m = MemoryUSMShared(1024) + self.assertEqual(m._usm_type(), 'shared') + + +class TestMemoryUSMHost(unittest.TestCase): + """Tests for MemoryUSMHost + """ + + def test_create (self): + m = MemoryUSMHost(1024) + self.assertEqual(m._usm_type(), 'host') + + +class TestMemoryUSMDevice(unittest.TestCase): + """Tests for MemoryUSMDevice + """ + + def test_create (self): + m = MemoryUSMDevice(1024) + self.assertEqual(m._usm_type(), 'device') From ba3e497268fe3ec69441b455800cbf499500beb3 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 13:09:32 +0300 Subject: [PATCH 26/93] Add __getbuffer__ to Shared and Host MemoryUSM classes. --- dppl/_memory.pyx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index a944ee4f1c..381240213d 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -58,7 +58,7 @@ cdef class Memory: self.nbytes = 0 self.queue = None - def __getbuffer__(self, Py_buffer *buffer, int flags): + cdef _getbuffer(self, Py_buffer *buffer, int flags): buffer.buf = self.memory_ptr buffer.format = 'B' # byte buffer.internal = NULL # see References @@ -107,12 +107,18 @@ cdef class MemoryUSMShared(Memory): def __cinit__(self, Py_ssize_t nbytes): self._cinit(nbytes, alloc.shared) + def __getbuffer__(self, Py_buffer *buffer, int flags): + self._getbuffer(buffer, flags) + cdef class MemoryUSMHost(Memory): def __cinit__(self, Py_ssize_t nbytes): self._cinit(nbytes, alloc.host) + def __getbuffer__(self, Py_buffer *buffer, int flags): + self._getbuffer(buffer, flags) + cdef class MemoryUSMDevice(Memory): From 00d63b05d75897ecb24edcae5f27e5b77d419f08 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 15:47:10 +0300 Subject: [PATCH 27/93] Rename C-API types for USM. --- backends/include/dppl_sycl_types.h | 5 +++-- backends/include/dppl_sycl_usm_interface.h | 8 ++++---- backends/source/dppl_sycl_usm_interface.cpp | 16 ++++++++-------- dppl/_memory.pyx | 4 ++-- dppl/backend.pxd | 12 ++++++------ 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/backends/include/dppl_sycl_types.h b/backends/include/dppl_sycl_types.h index db61bfb677..7d265805bd 100644 --- a/backends/include/dppl_sycl_types.h +++ b/backends/include/dppl_sycl_types.h @@ -57,7 +57,8 @@ typedef struct DPPLOpaqueSyclQueue *DPPLSyclQueueRef; typedef struct DPPLOpaqueSyclProgram *DPPLSyclProgramRef; /*! - * @brief + * @brief Used to pass a sycl::usm memory opaquely through DPPL interfaces. * + * @see sycl::usm */ -typedef struct DPPLOpaqueMemoryUSMShared *DPPLMemoryUSMSharedRef; +typedef struct DPPLOpaqueSyclUSM *DPPLSyclUSMRef; diff --git a/backends/include/dppl_sycl_usm_interface.h b/backends/include/dppl_sycl_usm_interface.h index 4e5004dd6b..117d1ecfcf 100644 --- a/backends/include/dppl_sycl_usm_interface.h +++ b/backends/include/dppl_sycl_usm_interface.h @@ -39,7 +39,7 @@ DPPL_C_EXTERN_C_BEGIN * @return The pointer to USM shared memory. */ DPPL_API -__dppl_give DPPLMemoryUSMSharedRef +__dppl_give DPPLSyclUSMRef DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); /*! @@ -48,7 +48,7 @@ DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); * @return The pointer to USM host memory. */ DPPL_API -__dppl_give DPPLMemoryUSMSharedRef +__dppl_give DPPLSyclUSMRef DPPLmalloc_host (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); /*! @@ -57,7 +57,7 @@ DPPLmalloc_host (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); * @return The pointer to USM device memory. */ DPPL_API -__dppl_give DPPLMemoryUSMSharedRef +__dppl_give DPPLSyclUSMRef DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); /*! @@ -65,7 +65,7 @@ DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); * */ DPPL_API -void DPPLfree (__dppl_take DPPLMemoryUSMSharedRef MRef, +void DPPLfree (__dppl_take DPPLSyclUSMRef MRef, __dppl_keep const DPPLSyclQueueRef QRef); DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_usm_interface.cpp b/backends/source/dppl_sycl_usm_interface.cpp index 74e4707978..8e4351a3c0 100644 --- a/backends/source/dppl_sycl_usm_interface.cpp +++ b/backends/source/dppl_sycl_usm_interface.cpp @@ -35,35 +35,35 @@ namespace { // Create wrappers for C Binding types (see CBindingWrapping.h). DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) - DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPPLMemoryUSMSharedRef) + DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPPLSyclUSMRef) } /* end of anonymous namespace */ -__dppl_give DPPLMemoryUSMSharedRef +__dppl_give DPPLSyclUSMRef DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Ptr = malloc_shared(size, *Q); - return reinterpret_cast(Ptr); + return reinterpret_cast(Ptr); } -__dppl_give DPPLMemoryUSMSharedRef +__dppl_give DPPLSyclUSMRef DPPLmalloc_host (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Ptr = malloc_host(size, *Q); - return reinterpret_cast(Ptr); + return reinterpret_cast(Ptr); } -__dppl_give DPPLMemoryUSMSharedRef +__dppl_give DPPLSyclUSMRef DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Ptr = malloc_device(size, *Q); - return reinterpret_cast(Ptr); + return reinterpret_cast(Ptr); } -void DPPLfree (DPPLMemoryUSMSharedRef MRef, __dppl_keep const DPPLSyclQueueRef QRef) +void DPPLfree (DPPLSyclUSMRef MRef, __dppl_keep const DPPLSyclQueueRef QRef) { auto Ptr = unwrap(MRef); auto Q = unwrap(QRef); diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 381240213d..34c195cc51 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -20,13 +20,13 @@ cdef extern from "CL/sycl.hpp" namespace "cl::sycl": cdef class Memory: - cdef DPPLMemoryUSMSharedRef memory_ptr + cdef DPPLSyclUSMRef memory_ptr cdef Py_ssize_t nbytes cdef SyclQueue queue cdef _cinit(self, Py_ssize_t nbytes, alloc ptr_type): cdef SyclQueue q - cdef DPPLMemoryUSMSharedRef p + cdef DPPLSyclUSMRef p self.memory_ptr = NULL self.queue = None diff --git a/dppl/backend.pxd b/dppl/backend.pxd index e3d75c6f6f..b44af8a9a8 100644 --- a/dppl/backend.pxd +++ b/dppl/backend.pxd @@ -34,12 +34,12 @@ cdef extern from "dppl_sycl_types.h": cdef struct DPPLOpaqueSyclContext cdef struct DPPLOpaqueSyclQueue cdef struct DPPLOpaqueSyclDevice - cdef struct DPPLOpaqueMemoryUSMShared + cdef struct DPPLOpaqueSyclUSM ctypedef DPPLOpaqueSyclContext* DPPLSyclContextRef ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef ctypedef DPPLOpaqueSyclDevice* DPPLSyclDeviceRef - ctypedef DPPLOpaqueMemoryUSMShared* DPPLMemoryUSMSharedRef + ctypedef DPPLOpaqueSyclUSM* DPPLSyclUSMRef cdef extern from "dppl_sycl_context_interface.h": @@ -95,7 +95,7 @@ cdef extern from "dppl_sycl_queue_manager.h": cdef extern from "dppl_sycl_usm_interface.h": - cdef DPPLMemoryUSMSharedRef DPPLmalloc_shared (size_t size, DPPLSyclQueueRef QRef) except + - cdef DPPLMemoryUSMSharedRef DPPLmalloc_host (size_t size, DPPLSyclQueueRef QRef) except + - cdef DPPLMemoryUSMSharedRef DPPLmalloc_device (size_t size, DPPLSyclQueueRef QRef) except + - cdef void DPPLfree (DPPLMemoryUSMSharedRef MRef, DPPLSyclQueueRef QRef) except + + cdef DPPLSyclUSMRef DPPLmalloc_shared (size_t size, DPPLSyclQueueRef QRef) except + + cdef DPPLSyclUSMRef DPPLmalloc_host (size_t size, DPPLSyclQueueRef QRef) except + + cdef DPPLSyclUSMRef DPPLmalloc_device (size_t size, DPPLSyclQueueRef QRef) except + + cdef void DPPLfree (DPPLSyclUSMRef MRef, DPPLSyclQueueRef QRef) except + From 28f0496c21e0c277ac416cf73ffc159110c02ae5 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 16:42:28 +0300 Subject: [PATCH 28/93] Add DPPLUSM_GetPointerType and remove types from CL/sycl.hpp from _memory.pyx --- backends/include/dppl_sycl_usm_interface.h | 11 ++++++ backends/source/dppl_sycl_usm_interface.cpp | 23 ++++++++++- dppl/_memory.pyx | 44 ++++++--------------- dppl/backend.pxd | 1 + 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/backends/include/dppl_sycl_usm_interface.h b/backends/include/dppl_sycl_usm_interface.h index 117d1ecfcf..40e7ca4fd3 100644 --- a/backends/include/dppl_sycl_usm_interface.h +++ b/backends/include/dppl_sycl_usm_interface.h @@ -68,4 +68,15 @@ DPPL_API void DPPLfree (__dppl_take DPPLSyclUSMRef MRef, __dppl_keep const DPPLSyclQueueRef QRef); + +/*! + * @brief Get pointer type. + * + * @return "host", "device", "shared" or "unknown" + */ +DPPL_API +const char * +DPPLUSM_GetPointerType (__dppl_keep DPPLSyclUSMRef MRef, + __dppl_keep const DPPLSyclQueueRef QRef); + DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_usm_interface.cpp b/backends/source/dppl_sycl_usm_interface.cpp index 8e4351a3c0..ff7dcf0324 100644 --- a/backends/source/dppl_sycl_usm_interface.cpp +++ b/backends/source/dppl_sycl_usm_interface.cpp @@ -63,9 +63,30 @@ DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) return reinterpret_cast(Ptr); } -void DPPLfree (DPPLSyclUSMRef MRef, __dppl_keep const DPPLSyclQueueRef QRef) +void DPPLfree (__dppl_take DPPLSyclUSMRef MRef, + __dppl_keep const DPPLSyclQueueRef QRef) { auto Ptr = unwrap(MRef); auto Q = unwrap(QRef); free(Ptr, *Q); } + +const char * +DPPLUSM_GetPointerType (__dppl_keep DPPLSyclUSMRef MRef, + __dppl_keep const DPPLSyclQueueRef QRef) +{ + auto Ptr = unwrap(MRef); + auto Q = unwrap(QRef); + + auto kind = get_pointer_type(Ptr, Q->get_context()); + switch(kind) { + case usm::alloc::host: + return "host"; + case usm::alloc::device: + return "device"; + case usm::alloc::shared: + return "shared"; + default: + return "unknown"; + } +} diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 34c195cc51..f0ac9fcf62 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -6,25 +6,13 @@ from cython.operator cimport dereference as deref from cpython cimport Py_buffer -cdef extern from "CL/sycl.hpp" namespace "cl::sycl::usm": - cdef enum alloc: - host 'cl::sycl::usm::alloc::host' - device 'cl::sycl::usm::alloc::device' - shared 'cl::sycl::usm::alloc::shared' - unknown 'cl::sycl::usm::alloc::unknown' - -cdef extern from "CL/sycl.hpp" namespace "cl::sycl": - cdef cppclass context nogil - - cdef alloc get_pointer_type(void *, context&) nogil - cdef class Memory: cdef DPPLSyclUSMRef memory_ptr cdef Py_ssize_t nbytes cdef SyclQueue queue - cdef _cinit(self, Py_ssize_t nbytes, alloc ptr_type): + cdef _cinit(self, Py_ssize_t nbytes, ptr_type): cdef SyclQueue q cdef DPPLSyclUSMRef p @@ -35,12 +23,14 @@ cdef class Memory: if (nbytes > 0): q = dppl.get_current_queue() - if (ptr_type == alloc.shared): + if (ptr_type == "shared"): p = DPPLmalloc_shared(nbytes, q.get_queue_ref()) - if (ptr_type == alloc.host): + elif (ptr_type == "host"): p = DPPLmalloc_host(nbytes, q.get_queue_ref()) - if (ptr_type == alloc.device): + elif (ptr_type == "device"): p = DPPLmalloc_device(nbytes, q.get_queue_ref()) + else: + raise RuntimeError("Pointer type is unknown: {}".format(ptr_type)) if (p): self.memory_ptr = p @@ -87,25 +77,15 @@ cdef class Memory: return "".format(self.nbytes, hex((self.memory_ptr))) def _usm_type(self): - cdef SyclContext ctxt - cdef alloc ptr_type - - ctxt = self.queue.get_sycl_context() - ptr_type = get_pointer_type(self.memory_ptr, deref(ctxt.get_context_ref())) - if (ptr_type == alloc.shared): - return "shared" - elif (ptr_type == alloc.host): - return "host" - elif (ptr_type == alloc.device): - return "device" - else: - return "unknown" + cdef const char* kind + kind = DPPLUSM_GetPointerType(self.memory_ptr, self.queue.get_queue_ref()) + return kind.decode('UTF-8') cdef class MemoryUSMShared(Memory): def __cinit__(self, Py_ssize_t nbytes): - self._cinit(nbytes, alloc.shared) + self._cinit(nbytes, "shared") def __getbuffer__(self, Py_buffer *buffer, int flags): self._getbuffer(buffer, flags) @@ -114,7 +94,7 @@ cdef class MemoryUSMShared(Memory): cdef class MemoryUSMHost(Memory): def __cinit__(self, Py_ssize_t nbytes): - self._cinit(nbytes, alloc.host) + self._cinit(nbytes, "host") def __getbuffer__(self, Py_buffer *buffer, int flags): self._getbuffer(buffer, flags) @@ -123,4 +103,4 @@ cdef class MemoryUSMHost(Memory): cdef class MemoryUSMDevice(Memory): def __cinit__(self, Py_ssize_t nbytes): - self._cinit(nbytes, alloc.device) + self._cinit(nbytes, "device") diff --git a/dppl/backend.pxd b/dppl/backend.pxd index b44af8a9a8..e810ff97ba 100644 --- a/dppl/backend.pxd +++ b/dppl/backend.pxd @@ -99,3 +99,4 @@ cdef extern from "dppl_sycl_usm_interface.h": cdef DPPLSyclUSMRef DPPLmalloc_host (size_t size, DPPLSyclQueueRef QRef) except + cdef DPPLSyclUSMRef DPPLmalloc_device (size_t size, DPPLSyclQueueRef QRef) except + cdef void DPPLfree (DPPLSyclUSMRef MRef, DPPLSyclQueueRef QRef) except + + cdef const char* DPPLUSM_GetPointerType (DPPLSyclUSMRef MRef, DPPLSyclQueueRef QRef) except + From 80cec2ad07a5ea269d4a669b3372074ab7a32ff7 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 16:47:50 +0300 Subject: [PATCH 29/93] Clean unused code from _memory_.pyx --- dppl/_memory.pyx | 2 -- 1 file changed, 2 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index f0ac9fcf62..cc082d82d3 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -2,8 +2,6 @@ import dppl from dppl.backend cimport * from ._sycl_core cimport SyclContext, SyclQueue -from cython.operator cimport dereference as deref - from cpython cimport Py_buffer From 8ef95df11b3a23f88e3bd670603922e9039c84e4 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 21:55:15 +0300 Subject: [PATCH 30/93] Small fixes. --- backends/include/dppl_sycl_usm_interface.h | 1 - backends/source/dppl_sycl_usm_interface.cpp | 3 ++- dppl/tests/dppl_tests/test_sycl_memory_manager.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backends/include/dppl_sycl_usm_interface.h b/backends/include/dppl_sycl_usm_interface.h index 40e7ca4fd3..e85d124c12 100644 --- a/backends/include/dppl_sycl_usm_interface.h +++ b/backends/include/dppl_sycl_usm_interface.h @@ -68,7 +68,6 @@ DPPL_API void DPPLfree (__dppl_take DPPLSyclUSMRef MRef, __dppl_keep const DPPLSyclQueueRef QRef); - /*! * @brief Get pointer type. * diff --git a/backends/source/dppl_sycl_usm_interface.cpp b/backends/source/dppl_sycl_usm_interface.cpp index ff7dcf0324..428231aad9 100644 --- a/backends/source/dppl_sycl_usm_interface.cpp +++ b/backends/source/dppl_sycl_usm_interface.cpp @@ -19,7 +19,8 @@ //===----------------------------------------------------------------------===// /// /// \file -/// TODO +/// This file implements the data types and functions declared in +/// dppl_sycl_usm_interface.h. /// //===----------------------------------------------------------------------===// diff --git a/dppl/tests/dppl_tests/test_sycl_memory_manager.py b/dppl/tests/dppl_tests/test_sycl_memory_manager.py index 3476b30d5b..9cab6d2de6 100644 --- a/dppl/tests/dppl_tests/test_sycl_memory_manager.py +++ b/dppl/tests/dppl_tests/test_sycl_memory_manager.py @@ -25,7 +25,7 @@ class TestMemory (unittest.TestCase): - # @unittest.skipIf(not dppl.has_sycl_platforms, "No SYCL platforms available") + def test_memory_create (self): nbytes = 1024 mobj = MemoryUSMShared(nbytes) From fa585dab023d80959dc96069ba97b0d77388a64c Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 21:55:59 +0300 Subject: [PATCH 31/93] Remove unused code. --- dppl/tests/dppl_tests/test_sycl_memory_manager.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dppl/tests/dppl_tests/test_sycl_memory_manager.py b/dppl/tests/dppl_tests/test_sycl_memory_manager.py index 9cab6d2de6..fc6cf16aea 100644 --- a/dppl/tests/dppl_tests/test_sycl_memory_manager.py +++ b/dppl/tests/dppl_tests/test_sycl_memory_manager.py @@ -20,7 +20,6 @@ import unittest import dppl -import dppl._memory as mem from dppl._memory import MemoryUSMShared, MemoryUSMHost, MemoryUSMDevice From 533e74b0637f802a27d8f16e95422fca22fb84a0 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 22:04:55 +0300 Subject: [PATCH 32/93] Fix style. --- backends/source/dppl_sycl_usm_interface.cpp | 1 - dppl/_memory.pyx | 9 ++++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/backends/source/dppl_sycl_usm_interface.cpp b/backends/source/dppl_sycl_usm_interface.cpp index 428231aad9..eaebbca107 100644 --- a/backends/source/dppl_sycl_usm_interface.cpp +++ b/backends/source/dppl_sycl_usm_interface.cpp @@ -31,7 +31,6 @@ using namespace cl::sycl; -// TODO: move it to one header for not duplicate in many cpp files namespace { // Create wrappers for C Binding types (see CBindingWrapping.h). diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index cc082d82d3..c20e211ed3 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -28,7 +28,8 @@ cdef class Memory: elif (ptr_type == "device"): p = DPPLmalloc_device(nbytes, q.get_queue_ref()) else: - raise RuntimeError("Pointer type is unknown: {}".format(ptr_type)) + raise RuntimeError("Pointer type is unknown: {}" \ + .format(ptr_type)) if (p): self.memory_ptr = p @@ -72,11 +73,13 @@ cdef class Memory: return self.queue def __repr__(self): - return "".format(self.nbytes, hex((self.memory_ptr))) + return "" \ + .format(self.nbytes, hex((self.memory_ptr))) def _usm_type(self): cdef const char* kind - kind = DPPLUSM_GetPointerType(self.memory_ptr, self.queue.get_queue_ref()) + kind = DPPLUSM_GetPointerType(self.memory_ptr, + self.queue.get_queue_ref()) return kind.decode('UTF-8') From ab7a9ba60cea9a434c18fe493768fb3af9a28d0b Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 18 Sep 2020 23:09:23 +0300 Subject: [PATCH 33/93] Fix style --- dppl/backend.pxd | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/dppl/backend.pxd b/dppl/backend.pxd index e810ff97ba..d19cf02e2a 100644 --- a/dppl/backend.pxd +++ b/dppl/backend.pxd @@ -95,8 +95,12 @@ cdef extern from "dppl_sycl_queue_manager.h": cdef extern from "dppl_sycl_usm_interface.h": - cdef DPPLSyclUSMRef DPPLmalloc_shared (size_t size, DPPLSyclQueueRef QRef) except + - cdef DPPLSyclUSMRef DPPLmalloc_host (size_t size, DPPLSyclQueueRef QRef) except + - cdef DPPLSyclUSMRef DPPLmalloc_device (size_t size, DPPLSyclQueueRef QRef) except + + cdef DPPLSyclUSMRef DPPLmalloc_shared (size_t size, DPPLSyclQueueRef QRef) \ + except + + cdef DPPLSyclUSMRef DPPLmalloc_host (size_t size, DPPLSyclQueueRef QRef) \ + except + + cdef DPPLSyclUSMRef DPPLmalloc_device (size_t size, DPPLSyclQueueRef QRef) \ + except + cdef void DPPLfree (DPPLSyclUSMRef MRef, DPPLSyclQueueRef QRef) except + - cdef const char* DPPLUSM_GetPointerType (DPPLSyclUSMRef MRef, DPPLSyclQueueRef QRef) except + + cdef const char* DPPLUSM_GetPointerType (DPPLSyclUSMRef MRef, + DPPLSyclQueueRef QRef) except + From 0edf5a179111f3f649600ab5f24b72deaf65ce5f Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sat, 19 Sep 2020 12:40:46 -0500 Subject: [PATCH 34/93] Add typedefs for some more sycl data types. --- backends/include/dppl_sycl_types.h | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/backends/include/dppl_sycl_types.h b/backends/include/dppl_sycl_types.h index 7d265805bd..e3ccf3d0a8 100644 --- a/backends/include/dppl_sycl_types.h +++ b/backends/include/dppl_sycl_types.h @@ -26,36 +26,48 @@ #pragma once /*! - * @brief + * @brief Opaque pointer to a sycl::context * */ typedef struct DPPLOpaqueSyclContext *DPPLSyclContextRef; /*! - * @brief + * @brief Opaque pointer to a sycl::device * */ typedef struct DPPLOpaqueSyclDevice *DPPLSyclDeviceRef; /*! - * @brief + * @brief Opaque pointer to a sycl::event + * + */ +typedef struct DPPLOpaqueSyclEvent *DPPLSyclEventRef; + +/*! + * @brief Opaque pointer to a sycl::kernel + * + */ +typedef struct DPPLOpaqueSyclKernel *DPPLSyclKernelRef; + +/*! + * @brief Opaque pointer to a sycl::platform * */ typedef struct DPPLOpaqueSyclPlatform *DPPLSyclPlatformRef; +/*! + * @brief Opaque pointer to a sycl::program + * + */ +typedef struct DPPLOpaqueSyclProgram *DPPLSyclProgramRef; + /*! - * @brief Used to pass a sycl::queue opaquely through DPPL interfaces. + * @brief Opaque pointer to a sycl::queue * * @see sycl::queue */ typedef struct DPPLOpaqueSyclQueue *DPPLSyclQueueRef; -/*! - * @brief Used to pass a sycl::program opaquely through DPPL interfaces. - * - */ -typedef struct DPPLOpaqueSyclProgram *DPPLSyclProgramRef; - /*! * @brief Used to pass a sycl::usm memory opaquely through DPPL interfaces. * From b5674b48304cc2484fed915cc586cf3c38be510e Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Sat, 19 Sep 2020 23:02:37 +0300 Subject: [PATCH 35/93] Use wrap(). --- backends/source/dppl_sycl_usm_interface.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backends/source/dppl_sycl_usm_interface.cpp b/backends/source/dppl_sycl_usm_interface.cpp index eaebbca107..c0bfe2782e 100644 --- a/backends/source/dppl_sycl_usm_interface.cpp +++ b/backends/source/dppl_sycl_usm_interface.cpp @@ -44,7 +44,7 @@ DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Ptr = malloc_shared(size, *Q); - return reinterpret_cast(Ptr); + return wrap(Ptr); } __dppl_give DPPLSyclUSMRef @@ -52,7 +52,7 @@ DPPLmalloc_host (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Ptr = malloc_host(size, *Q); - return reinterpret_cast(Ptr); + return wrap(Ptr); } __dppl_give DPPLSyclUSMRef @@ -60,7 +60,7 @@ DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Ptr = malloc_device(size, *Q); - return reinterpret_cast(Ptr); + return wrap(Ptr); } void DPPLfree (__dppl_take DPPLSyclUSMRef MRef, From 5e93d6e85a1a154a5e51adfaebe618405c1a9632 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Sat, 19 Sep 2020 23:35:56 +0300 Subject: [PATCH 36/93] Store context instead of queue in Memory class. --- backends/include/dppl_sycl_usm_interface.h | 16 ++++++++++++---- backends/source/dppl_sycl_usm_interface.cpp | 21 +++++++++++++++------ dppl/_memory.pyx | 17 +++++++++-------- dppl/backend.pxd | 9 +++++++-- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/backends/include/dppl_sycl_usm_interface.h b/backends/include/dppl_sycl_usm_interface.h index e85d124c12..6a52fcff42 100644 --- a/backends/include/dppl_sycl_usm_interface.h +++ b/backends/include/dppl_sycl_usm_interface.h @@ -65,8 +65,16 @@ DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef); * */ DPPL_API -void DPPLfree (__dppl_take DPPLSyclUSMRef MRef, - __dppl_keep const DPPLSyclQueueRef QRef); +void DPPLfree_with_queue (__dppl_take DPPLSyclUSMRef MRef, + __dppl_keep const DPPLSyclQueueRef QRef); + +/*! + * @brief Free USM memory. + * + */ +DPPL_API +void DPPLfree_with_context (__dppl_take DPPLSyclUSMRef MRef, + __dppl_keep const DPPLSyclContextRef CRef); /*! * @brief Get pointer type. @@ -75,7 +83,7 @@ void DPPLfree (__dppl_take DPPLSyclUSMRef MRef, */ DPPL_API const char * -DPPLUSM_GetPointerType (__dppl_keep DPPLSyclUSMRef MRef, - __dppl_keep const DPPLSyclQueueRef QRef); +DPPLUSM_GetPointerType (__dppl_keep const DPPLSyclUSMRef MRef, + __dppl_keep const DPPLSyclContextRef СRef); DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_usm_interface.cpp b/backends/source/dppl_sycl_usm_interface.cpp index c0bfe2782e..4e1444634b 100644 --- a/backends/source/dppl_sycl_usm_interface.cpp +++ b/backends/source/dppl_sycl_usm_interface.cpp @@ -35,6 +35,7 @@ namespace { // Create wrappers for C Binding types (see CBindingWrapping.h). DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) + DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPPLSyclContextRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPPLSyclUSMRef) } /* end of anonymous namespace */ @@ -63,22 +64,30 @@ DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef) return wrap(Ptr); } -void DPPLfree (__dppl_take DPPLSyclUSMRef MRef, - __dppl_keep const DPPLSyclQueueRef QRef) +void DPPLfree_with_queue (__dppl_take DPPLSyclUSMRef MRef, + __dppl_keep const DPPLSyclQueueRef QRef) { auto Ptr = unwrap(MRef); auto Q = unwrap(QRef); free(Ptr, *Q); } +void DPPLfree_with_context (__dppl_take DPPLSyclUSMRef MRef, + __dppl_keep const DPPLSyclContextRef СRef) +{ + auto Ptr = unwrap(MRef); + auto C = unwrap(СRef); + free(Ptr, *C); +} + const char * -DPPLUSM_GetPointerType (__dppl_keep DPPLSyclUSMRef MRef, - __dppl_keep const DPPLSyclQueueRef QRef) +DPPLUSM_GetPointerType (__dppl_keep const DPPLSyclUSMRef MRef, + __dppl_keep const DPPLSyclContextRef CRef) { auto Ptr = unwrap(MRef); - auto Q = unwrap(QRef); + auto C = unwrap(CRef); - auto kind = get_pointer_type(Ptr, Q->get_context()); + auto kind = get_pointer_type(Ptr, *C); switch(kind) { case usm::alloc::host: return "host"; diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index c20e211ed3..e369f1742b 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -8,15 +8,15 @@ from cpython cimport Py_buffer cdef class Memory: cdef DPPLSyclUSMRef memory_ptr cdef Py_ssize_t nbytes - cdef SyclQueue queue + cdef SyclContext context cdef _cinit(self, Py_ssize_t nbytes, ptr_type): cdef SyclQueue q cdef DPPLSyclUSMRef p self.memory_ptr = NULL - self.queue = None self.nbytes = 0 + self.context = None if (nbytes > 0): q = dppl.get_current_queue() @@ -34,7 +34,7 @@ cdef class Memory: if (p): self.memory_ptr = p self.nbytes = nbytes - self.queue = q + self.context = q.get_sycl_context() else: raise RuntimeError("Null memory pointer returned") else: @@ -42,10 +42,11 @@ cdef class Memory: def __dealloc__(self): if (self.memory_ptr): - DPPLfree(self.memory_ptr, self.queue.get_queue_ref()) + DPPLfree_with_context(self.memory_ptr, + self.context.get_context_ref()) self.memory_ptr = NULL self.nbytes = 0 - self.queue = None + self.context = None cdef _getbuffer(self, Py_buffer *buffer, int flags): buffer.buf = self.memory_ptr @@ -68,9 +69,9 @@ cdef class Memory: def __get__(self): return self.nbytes - property _queue: + property _context: def __get__(self): - return self.queue + return self.context def __repr__(self): return "" \ @@ -79,7 +80,7 @@ cdef class Memory: def _usm_type(self): cdef const char* kind kind = DPPLUSM_GetPointerType(self.memory_ptr, - self.queue.get_queue_ref()) + self.context.get_context_ref()) return kind.decode('UTF-8') diff --git a/dppl/backend.pxd b/dppl/backend.pxd index d19cf02e2a..a427bbda3a 100644 --- a/dppl/backend.pxd +++ b/dppl/backend.pxd @@ -101,6 +101,11 @@ cdef extern from "dppl_sycl_usm_interface.h": except + cdef DPPLSyclUSMRef DPPLmalloc_device (size_t size, DPPLSyclQueueRef QRef) \ except + - cdef void DPPLfree (DPPLSyclUSMRef MRef, DPPLSyclQueueRef QRef) except + + + cdef void DPPLfree_with_queue (DPPLSyclUSMRef MRef, + DPPLSyclQueueRef QRef) except + + cdef void DPPLfree_with_context (DPPLSyclUSMRef MRef, + DPPLSyclContextRef CRef) except + + cdef const char* DPPLUSM_GetPointerType (DPPLSyclUSMRef MRef, - DPPLSyclQueueRef QRef) except + + DPPLSyclContextRef CRef) except + From 6bd1756c1e73131f4f04a46883749ee8fe1cec62 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Sun, 20 Sep 2020 00:03:57 +0300 Subject: [PATCH 37/93] Pass queue as parameter to constructor. If queue is None then get dppl current queue. --- dppl/_memory.pyx | 26 ++++----- .../dppl_tests/test_sycl_memory_manager.py | 54 ++++++++++++------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index e369f1742b..b63211bd01 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -10,8 +10,7 @@ cdef class Memory: cdef Py_ssize_t nbytes cdef SyclContext context - cdef _cinit(self, Py_ssize_t nbytes, ptr_type): - cdef SyclQueue q + cdef _cinit(self, Py_ssize_t nbytes, ptr_type, SyclQueue queue): cdef DPPLSyclUSMRef p self.memory_ptr = NULL @@ -19,14 +18,15 @@ cdef class Memory: self.context = None if (nbytes > 0): - q = dppl.get_current_queue() + if queue is None: + queue = dppl.get_current_queue() if (ptr_type == "shared"): - p = DPPLmalloc_shared(nbytes, q.get_queue_ref()) + p = DPPLmalloc_shared(nbytes, queue.get_queue_ref()) elif (ptr_type == "host"): - p = DPPLmalloc_host(nbytes, q.get_queue_ref()) + p = DPPLmalloc_host(nbytes, queue.get_queue_ref()) elif (ptr_type == "device"): - p = DPPLmalloc_device(nbytes, q.get_queue_ref()) + p = DPPLmalloc_device(nbytes, queue.get_queue_ref()) else: raise RuntimeError("Pointer type is unknown: {}" \ .format(ptr_type)) @@ -34,7 +34,7 @@ cdef class Memory: if (p): self.memory_ptr = p self.nbytes = nbytes - self.context = q.get_sycl_context() + self.context = queue.get_sycl_context() else: raise RuntimeError("Null memory pointer returned") else: @@ -86,8 +86,8 @@ cdef class Memory: cdef class MemoryUSMShared(Memory): - def __cinit__(self, Py_ssize_t nbytes): - self._cinit(nbytes, "shared") + def __cinit__(self, Py_ssize_t nbytes, SyclQueue queue=None): + self._cinit(nbytes, "shared", queue) def __getbuffer__(self, Py_buffer *buffer, int flags): self._getbuffer(buffer, flags) @@ -95,8 +95,8 @@ cdef class MemoryUSMShared(Memory): cdef class MemoryUSMHost(Memory): - def __cinit__(self, Py_ssize_t nbytes): - self._cinit(nbytes, "host") + def __cinit__(self, Py_ssize_t nbytes, SyclQueue queue=None): + self._cinit(nbytes, "host", queue) def __getbuffer__(self, Py_buffer *buffer, int flags): self._getbuffer(buffer, flags) @@ -104,5 +104,5 @@ cdef class MemoryUSMHost(Memory): cdef class MemoryUSMDevice(Memory): - def __cinit__(self, Py_ssize_t nbytes): - self._cinit(nbytes, "device") + def __cinit__(self, Py_ssize_t nbytes, SyclQueue queue=None): + self._cinit(nbytes, "device", queue) diff --git a/dppl/tests/dppl_tests/test_sycl_memory_manager.py b/dppl/tests/dppl_tests/test_sycl_memory_manager.py index fc6cf16aea..15bb397a9f 100644 --- a/dppl/tests/dppl_tests/test_sycl_memory_manager.py +++ b/dppl/tests/dppl_tests/test_sycl_memory_manager.py @@ -27,12 +27,14 @@ class TestMemory (unittest.TestCase): def test_memory_create (self): nbytes = 1024 - mobj = MemoryUSMShared(nbytes) + queue = dppl.get_current_queue() + mobj = MemoryUSMShared(nbytes, queue) self.assertEqual(mobj.nbytes, nbytes) def _create_memory (self): nbytes = 1024 - mobj = MemoryUSMShared(nbytes) + queue = dppl.get_current_queue() + mobj = MemoryUSMShared(nbytes, queue) return mobj def test_memory_without_context (self): @@ -56,28 +58,40 @@ def test_memory_gpu_context (self): self.assertEqual(mobj._usm_type(), 'shared') -class TestMemoryUSMShared(unittest.TestCase): - """Tests for MemoryUSMShared - """ +class TestMemoryUSMBase: + """ Base tests for MemoryUSM* """ - def test_create (self): - m = MemoryUSMShared(1024) - self.assertEqual(m._usm_type(), 'shared') + MemoryUSMClass = None + usm_type = None + def test_create_with_queue (self): + q = dppl.get_current_queue() + m = self.MemoryUSMClass(1024, q) + self.assertEqual(m.nbytes, 1024) + self.assertEqual(m._usm_type(), self.usm_type) -class TestMemoryUSMHost(unittest.TestCase): - """Tests for MemoryUSMHost - """ + def test_create_without_queue (self): + m = self.MemoryUSMClass(1024) + self.assertEqual(m.nbytes, 1024) + self.assertEqual(m._usm_type(), self.usm_type) - def test_create (self): - m = MemoryUSMHost(1024) - self.assertEqual(m._usm_type(), 'host') +class TestMemoryUSMShared(TestMemoryUSMBase, unittest.TestCase): + """ Tests for MemoryUSMShared """ -class TestMemoryUSMDevice(unittest.TestCase): - """Tests for MemoryUSMDevice - """ + MemoryUSMClass = MemoryUSMShared + usm_type = 'shared' - def test_create (self): - m = MemoryUSMDevice(1024) - self.assertEqual(m._usm_type(), 'device') + +class TestMemoryUSMHost(TestMemoryUSMBase, unittest.TestCase): + """ Tests for MemoryUSMHost """ + + MemoryUSMClass = MemoryUSMHost + usm_type = 'host' + + +class TestMemoryUSMDevice(TestMemoryUSMBase, unittest.TestCase): + """ Tests for MemoryUSMDevice """ + + MemoryUSMClass = MemoryUSMDevice + usm_type = 'device' From 1333676aa852aea5a961e0bef129ddbd8dc1adf2 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Sun, 20 Sep 2020 00:08:17 +0300 Subject: [PATCH 38/93] Add comment about casting memory_ptr to char*. --- dppl/_memory.pyx | 1 + 1 file changed, 1 insertion(+) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index b63211bd01..c65e41f327 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -49,6 +49,7 @@ cdef class Memory: self.context = None cdef _getbuffer(self, Py_buffer *buffer, int flags): + # memory_ptr is Ref which is pointer to SYCL type. For USM it is void*. buffer.buf = self.memory_ptr buffer.format = 'B' # byte buffer.internal = NULL # see References From b82eab5ce258df128059c2d6bd19b7589bb5d8f7 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Sun, 20 Sep 2020 00:09:21 +0300 Subject: [PATCH 39/93] Remove pointer property from Memory. --- dppl/_memory.pyx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index c65e41f327..22b9329073 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -62,10 +62,6 @@ cdef class Memory: buffer.strides = &buffer.itemsize buffer.suboffsets = NULL # for pointer arrays only - property pointer: - def __get__(self): - return (self.memory_ptr) - property nbytes: def __get__(self): return self.nbytes From 1b5f804aa2ed1de52acc8767f7a932eb1112bc1f Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Sun, 20 Sep 2020 00:12:28 +0300 Subject: [PATCH 40/93] Rename file with usm tests. --- dppl/tests/dppl_tests/__init__.py | 2 +- .../{test_sycl_memory_manager.py => test_sycl_usm.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename dppl/tests/dppl_tests/{test_sycl_memory_manager.py => test_sycl_usm.py} (100%) diff --git a/dppl/tests/dppl_tests/__init__.py b/dppl/tests/dppl_tests/__init__.py index 7017278251..dd992a0db9 100644 --- a/dppl/tests/dppl_tests/__init__.py +++ b/dppl/tests/dppl_tests/__init__.py @@ -1,2 +1,2 @@ from .test_sycl_queue_manager import * -from .test_sycl_memory_manager import * +from .test_sycl_usm import * diff --git a/dppl/tests/dppl_tests/test_sycl_memory_manager.py b/dppl/tests/dppl_tests/test_sycl_usm.py similarity index 100% rename from dppl/tests/dppl_tests/test_sycl_memory_manager.py rename to dppl/tests/dppl_tests/test_sycl_usm.py From 722c8cf8bab8a7ded6517efc21a27c8706fa0ce1 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sat, 19 Sep 2020 17:08:42 -0500 Subject: [PATCH 41/93] Standardize the names of DPPL functions on DPPLXXX_FuncName. --- .../include/dppl_sycl_context_interface.h | 8 +-- backends/include/dppl_sycl_device_interface.h | 20 +++---- backends/include/dppl_sycl_queue_interface.h | 6 +-- backends/include/dppl_sycl_queue_manager.h | 38 ++++++------- .../source/dppl_sycl_context_interface.cpp | 8 +-- .../source/dppl_sycl_device_interface.cpp | 20 +++---- backends/source/dppl_sycl_queue_interface.cpp | 6 +-- backends/source/dppl_sycl_queue_manager.cpp | 23 ++++---- backends/tests/test_sycl_queue_manager.cpp | 54 +++++++++---------- dppl/backend.pxd | 52 +++++++++--------- dppl/sycl_core.pyx | 38 ++++++------- 11 files changed, 136 insertions(+), 137 deletions(-) diff --git a/backends/include/dppl_sycl_context_interface.h b/backends/include/dppl_sycl_context_interface.h index 1f3506930e..e2164e388e 100644 --- a/backends/include/dppl_sycl_context_interface.h +++ b/backends/include/dppl_sycl_context_interface.h @@ -37,18 +37,18 @@ DPPL_C_EXTERN_C_BEGIN /*! * @brief Returns true if this SYCL context is a host context. * - * @param CtxtRef A opaque pointer to a sycl::context. + * @param CtxRef A opaque pointer to a sycl::context. * @return True if the SYCL context is a host context, else False. */ DPPL_API -bool DPPLIsHostContext (__dppl_keep const DPPLSyclContextRef CtxtRef); +bool DPPLContext_IsHost (__dppl_keep const DPPLSyclContextRef CtxRef); /*! * @brief Delete the pointer after casting it to sycl::context * - * @param CtxtRef The DPPLSyclContextRef pointer to be deleted. + * @param CtxRef The DPPLSyclContextRef pointer to be deleted. */ DPPL_API -void DPPLDeleteSyclContext (__dppl_take DPPLSyclContextRef CtxtRef); +void DPPLContext_Delete (__dppl_take DPPLSyclContextRef CtxRef); DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_device_interface.h b/backends/include/dppl_sycl_device_interface.h index adbc216662..3ebe9a80ef 100644 --- a/backends/include/dppl_sycl_device_interface.h +++ b/backends/include/dppl_sycl_device_interface.h @@ -57,7 +57,7 @@ typedef enum * @param DRef A DPPLSyclDeviceRef pointer. */ DPPL_API -void DPPLDumpDeviceInfo (__dppl_keep const DPPLSyclDeviceRef DRef); +void DPPLDevice_DumpInfo (__dppl_keep const DPPLSyclDeviceRef DRef); /*! * @brief Deletes a DPPLSyclDeviceRef pointer after casting to to sycl::device. @@ -65,7 +65,7 @@ void DPPLDumpDeviceInfo (__dppl_keep const DPPLSyclDeviceRef DRef); * @param DRef The DPPLSyclDeviceRef pointer to be freed. */ DPPL_API -void DPPLDeleteSyclDevice (__dppl_take DPPLSyclDeviceRef DRef); +void DPPLDevice_Delete (__dppl_take DPPLSyclDeviceRef DRef); /*! * @brief Returns true if this SYCL device is an OpenCL device and the device @@ -75,7 +75,7 @@ void DPPLDeleteSyclDevice (__dppl_take DPPLSyclDeviceRef DRef); * @return True if the device type is an accelerator, else False. */ DPPL_API -bool DPPLDeviceIsAccelerator (__dppl_keep const DPPLSyclDeviceRef DRef); +bool DPPLDevice_IsAccelerator (__dppl_keep const DPPLSyclDeviceRef DRef); /*! * @brief Returns true if this SYCL device is an OpenCL device and the device @@ -85,7 +85,7 @@ bool DPPLDeviceIsAccelerator (__dppl_keep const DPPLSyclDeviceRef DRef); * @return True if the device type is a cpu, else False. */ DPPL_API -bool DPPLDeviceIsCPU (__dppl_keep const DPPLSyclDeviceRef DRef); +bool DPPLDevice_IsCPU (__dppl_keep const DPPLSyclDeviceRef DRef); /*! * @brief Returns true if this SYCL device is an OpenCL device and the device @@ -95,7 +95,7 @@ bool DPPLDeviceIsCPU (__dppl_keep const DPPLSyclDeviceRef DRef); * @return True if the device type is a gpu, else False. */ DPPL_API -bool DPPLDeviceIsGPU (__dppl_keep const DPPLSyclDeviceRef DRef); +bool DPPLDevice_IsGPU (__dppl_keep const DPPLSyclDeviceRef DRef); /*! * @brief Returns true if this SYCL device is a host device. @@ -104,7 +104,7 @@ bool DPPLDeviceIsGPU (__dppl_keep const DPPLSyclDeviceRef DRef); * @return True if the device is a host device, else False. */ DPPL_API -bool DPPLDeviceIsHost (__dppl_keep const DPPLSyclDeviceRef DRef); +bool DPPLDevice_IsHost (__dppl_keep const DPPLSyclDeviceRef DRef); /*! * @brief Returns the OpenCL software driver version as a C string. @@ -115,7 +115,7 @@ bool DPPLDeviceIsHost (__dppl_keep const DPPLSyclDeviceRef DRef); */ DPPL_API __dppl_give const char* -DPPLGetDeviceDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef); +DPPLDevice_GetDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef); /*! * @brief Returns a C string for the device name. @@ -125,7 +125,7 @@ DPPLGetDeviceDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef); */ DPPL_API __dppl_give const char* -DPPLGetDeviceName (__dppl_keep const DPPLSyclDeviceRef DRef); +DPPLDevice_GetName (__dppl_keep const DPPLSyclDeviceRef DRef); /*! * @brief Returns a C string corresponding to the vendor name. @@ -135,7 +135,7 @@ DPPLGetDeviceName (__dppl_keep const DPPLSyclDeviceRef DRef); */ DPPL_API __dppl_give const char* -DPPLGetDeviceVendorName (__dppl_keep const DPPLSyclDeviceRef DRef); +DPPLDevice_GetVendorName (__dppl_keep const DPPLSyclDeviceRef DRef); /*! * @brief Returns True if the device and the host share a unified memory @@ -146,6 +146,6 @@ DPPLGetDeviceVendorName (__dppl_keep const DPPLSyclDeviceRef DRef); * with the host. */ DPPL_API -bool DPPLGetDeviceHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef); +bool DPPLDevice_IsHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef); DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index 60d080e3f0..7b9002e8de 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -43,7 +43,7 @@ DPPL_C_EXTERN_C_BEGIN */ DPPL_API __dppl_give DPPLSyclContextRef -DPPLGetContextFromQueue (__dppl_keep const DPPLSyclQueueRef QRef); +DPPLQueue_GetContext (__dppl_keep const DPPLSyclQueueRef QRef); /*! * @brief returns the Sycl device for the queue. @@ -53,7 +53,7 @@ DPPLGetContextFromQueue (__dppl_keep const DPPLSyclQueueRef QRef); */ DPPL_API __dppl_give DPPLSyclDeviceRef -DPPLGetDeviceFromQueue (__dppl_keep const DPPLSyclQueueRef QRef); +DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef); /*! * @brief Delete the pointer after casting it to sycl::queue. @@ -61,6 +61,6 @@ DPPLGetDeviceFromQueue (__dppl_keep const DPPLSyclQueueRef QRef); * @param QRef A DPPLSyclQueueRef pointer that gets deleted. */ DPPL_API -void DPPLDeleteSyclQueue (__dppl_take DPPLSyclQueueRef QRef); +void DPPLQueue_Delete (__dppl_take DPPLSyclQueueRef QRef); DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_queue_manager.h b/backends/include/dppl_sycl_queue_manager.h index 8d550be6b9..33956af07b 100644 --- a/backends/include/dppl_sycl_queue_manager.h +++ b/backends/include/dppl_sycl_queue_manager.h @@ -50,7 +50,7 @@ DPPL_C_EXTERN_C_BEGIN * wrapped inside an opaque DPPLSyclQueueRef pointer. */ DPPL_API -__dppl_give DPPLSyclQueueRef DPPLGetCurrentQueue (); +__dppl_give DPPLSyclQueueRef DPPLQueueMgr_GetCurrentQueue (); /*! * @brief Get a sycl::queue object of the specified type and device id. @@ -63,8 +63,8 @@ __dppl_give DPPLSyclQueueRef DPPLGetCurrentQueue (); * raised if no such device exists. */ DPPL_API -__dppl_give DPPLSyclQueueRef DPPLGetQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum); +__dppl_give DPPLSyclQueueRef DPPLQueueMgr_GetQueue (DPPLSyclDeviceType DeviceTy, + size_t DNum); /*! * @brief Get the number of activated queues not including the global or @@ -73,7 +73,7 @@ __dppl_give DPPLSyclQueueRef DPPLGetQueue (DPPLSyclDeviceType DeviceTy, * @return The number of activated queues. */ DPPL_API -size_t DPPLGetNumActivatedQueues (); +size_t DPPLQueueMgr_GetNumActivatedQueues (); /*! * @brief Get the number of GPU queues available on the system. @@ -81,7 +81,7 @@ size_t DPPLGetNumActivatedQueues (); * @return The number of available GPU queues. */ DPPL_API -size_t DPPLGetNumGPUQueues (); +size_t DPPLQueueMgr_GetNumGPUQueues (); /*! * @brief Get the number of CPU queues available on the system. @@ -89,7 +89,7 @@ size_t DPPLGetNumGPUQueues (); * @return The number of available CPU queues. */ DPPL_API -size_t DPPLGetNumCPUQueues (); +size_t DPPLQueueMgr_GetNumCPUQueues (); /*! * @brief Set the default DPPL queue to the sycl::queue for the given device. @@ -100,8 +100,8 @@ size_t DPPLGetNumCPUQueues (); * @param DNum Device id for the device (defaults to 0) */ DPPL_API -void DPPLSetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum); +void DPPLQueueMgr_SetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, + size_t DNum); /*! * @brief Pushes a new sycl::queue object to the top of DPPL's thread-local @@ -110,11 +110,11 @@ void DPPLSetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, * DPPL maintains a thread-local stack of sycl::queue objects to facilitate * nested parallelism. The sycl::queue at the top of the stack is termed as the * currently activated queue, and is always the one returned by - * DPPLGetCurrentQueue(). DPPLPushSyclQueueToStack creates a new sycl::queue - * corresponding to the specified device and pushes it to the top of the stack. - * A copy of the sycl::queue is returned to the caller wrapped inside the - * opaque DPPLSyclQueueRef pointer. A runtime_error exception is thrown when - * a new sycl::queue could not be created for the specified device. + * DPPLQueueMgr_GetCurrentQueue(). DPPLPushSyclQueueToStack creates a new + * sycl::queue corresponding to the specified device and pushes it to the top + * of the stack. A copy of the sycl::queue is returned to the caller wrapped + * inside the opaque DPPLSyclQueueRef pointer. A runtime_error exception is + * thrown when a new sycl::queue could not be created for the specified device. * * @param DeviceTy The type of Sycl device (sycl_device_type) * @param DNum Device id for the device (defaults to 0) @@ -123,8 +123,8 @@ void DPPLSetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, * stack of sycl::queue objects. */ DPPL_API -__dppl_give DPPLSyclQueueRef DPPLPushSyclQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum); +__dppl_give DPPLSyclQueueRef +DPPLQueueMgr_PushQueue (DPPLSyclDeviceType DeviceTy, size_t DNum); /*! * @brief Pops the top of stack element from DPPL's stack of activated @@ -132,12 +132,12 @@ __dppl_give DPPLSyclQueueRef DPPLPushSyclQueue (DPPLSyclDeviceType DeviceTy, * * DPPLPopSyclQueue only removes the reference from the DPPL stack of * sycl::queue objects. Any instance of the popped queue that were previously - * acquired by calling DPPLPushSyclQueue() or DPPLGetCurrentQueue() needs to be - * freed separately. In addition, a runtime_error is thrown when the stack - * contains only one sycl::queue, i.e., the default queue. + * acquired by calling DPPLPushSyclQueue() or DPPLQueueMgr_GetCurrentQueue() + * needs to be freed separately. In addition, a runtime_error is thrown when + * the stack contains only one sycl::queue, i.e., the default queue. * */ DPPL_API -void DPPLPopSyclQueue (); +void DPPLQueueMgr_PopQueue (); DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_context_interface.cpp b/backends/source/dppl_sycl_context_interface.cpp index d27b150f74..fc3aa26983 100644 --- a/backends/source/dppl_sycl_context_interface.cpp +++ b/backends/source/dppl_sycl_context_interface.cpp @@ -42,9 +42,9 @@ namespace * @param CtxtRef My Param doc * @return {return} My Param doc */ -bool DPPLIsHostContext (__dppl_keep const DPPLSyclContextRef CtxtRef) +bool DPPLContext_IsHost (__dppl_keep const DPPLSyclContextRef CtxRef) { - return unwrap(CtxtRef)->is_host(); + return unwrap(CtxRef)->is_host(); } /*! @@ -52,7 +52,7 @@ bool DPPLIsHostContext (__dppl_keep const DPPLSyclContextRef CtxtRef) * * @param CtxtRef My Param doc */ -void DPPLDeleteSyclContext (__dppl_take DPPLSyclContextRef CtxtRef) +void DPPLContext_Delete (__dppl_take DPPLSyclContextRef CtxRef) { - delete unwrap(CtxtRef); + delete unwrap(CtxRef); } diff --git a/backends/source/dppl_sycl_device_interface.cpp b/backends/source/dppl_sycl_device_interface.cpp index f70f34be1e..1d9d9444c4 100644 --- a/backends/source/dppl_sycl_device_interface.cpp +++ b/backends/source/dppl_sycl_device_interface.cpp @@ -66,41 +66,41 @@ void dump_device_info (const device & Device) * vendor, and device profile are printed out. More attributed may be added * later. */ -void DPPLDumpDeviceInfo (__dppl_keep const DPPLSyclDeviceRef DRef) +void DPPLDevice_DumpInfo (__dppl_keep const DPPLSyclDeviceRef DRef) { auto Device = unwrap(DRef); dump_device_info(*Device); } -void DPPLDeleteSyclDevice (__dppl_take DPPLSyclDeviceRef DRef) +void DPPLDevice_Delete (__dppl_take DPPLSyclDeviceRef DRef) { delete unwrap(DRef); } -bool DPPLDeviceIsAccelerator (__dppl_keep const DPPLSyclDeviceRef DRef) +bool DPPLDevice_IsAccelerator (__dppl_keep const DPPLSyclDeviceRef DRef) { return unwrap(DRef)->is_accelerator(); } -bool DPPLDeviceIsCPU (__dppl_keep const DPPLSyclDeviceRef DRef) +bool DPPLDevice_IsCPU (__dppl_keep const DPPLSyclDeviceRef DRef) { return unwrap(DRef)->is_cpu(); } -bool DPPLDeviceIsGPU (__dppl_keep const DPPLSyclDeviceRef DRef) +bool DPPLDevice_IsGPU (__dppl_keep const DPPLSyclDeviceRef DRef) { return unwrap(DRef)->is_gpu(); } -bool DPPLDeviceIsHost (__dppl_keep const DPPLSyclDeviceRef DRef) +bool DPPLDevice_IsHost (__dppl_keep const DPPLSyclDeviceRef DRef) { return unwrap(DRef)->is_host(); } __dppl_give const char* -DPPLGetDeviceName (__dppl_keep const DPPLSyclDeviceRef DRef) +DPPLDevice_GetName (__dppl_keep const DPPLSyclDeviceRef DRef) { auto name = unwrap(DRef)->get_info(); auto cstr_name = new char [name.length()+1]; @@ -109,7 +109,7 @@ DPPLGetDeviceName (__dppl_keep const DPPLSyclDeviceRef DRef) } __dppl_give const char* -DPPLGetDeviceVendorName (__dppl_keep const DPPLSyclDeviceRef DRef) +DPPLDevice_GetVendorName (__dppl_keep const DPPLSyclDeviceRef DRef) { auto vendor = unwrap(DRef)->get_info(); auto cstr_vendor = new char [vendor.length()+1]; @@ -118,7 +118,7 @@ DPPLGetDeviceVendorName (__dppl_keep const DPPLSyclDeviceRef DRef) } __dppl_give const char* -DPPLGetDeviceDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef) +DPPLDevice_GetDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef) { auto driver = unwrap(DRef)->get_info(); auto cstr_driver = new char [driver.length()+1]; @@ -126,7 +126,7 @@ DPPLGetDeviceDriverInfo (__dppl_keep const DPPLSyclDeviceRef DRef) return cstr_driver; } -bool DPPLGetDeviceHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef) +bool DPPLDevice_IsHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef) { return unwrap(DRef)->get_info(); } diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index e02479d69d..af3fd7a611 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -39,7 +39,7 @@ namespace } /* end of anonymous namespace */ __dppl_give DPPLSyclDeviceRef -DPPLGetDeviceFromQueue (__dppl_keep const DPPLSyclQueueRef QRef) +DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Device = new device(Q->get_device()); @@ -47,7 +47,7 @@ DPPLGetDeviceFromQueue (__dppl_keep const DPPLSyclQueueRef QRef) } __dppl_give DPPLSyclContextRef -DPPLGetContextFromQueue (__dppl_keep const DPPLSyclQueueRef QRef) +DPPLQueue_GetContext (__dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Context = new context(Q->get_context()); @@ -57,7 +57,7 @@ DPPLGetContextFromQueue (__dppl_keep const DPPLSyclQueueRef QRef) /*! * Delete the passed in pointer after verifying it points to a sycl::queue. */ -void DPPLDeleteSyclQueue (__dppl_take DPPLSyclQueueRef QRef) +void DPPLQueue_Delete (__dppl_take DPPLSyclQueueRef QRef) { delete unwrap(QRef); } diff --git a/backends/source/dppl_sycl_queue_manager.cpp b/backends/source/dppl_sycl_queue_manager.cpp index 17776fda11..3fa6033016 100644 --- a/backends/source/dppl_sycl_queue_manager.cpp +++ b/backends/source/dppl_sycl_queue_manager.cpp @@ -138,8 +138,7 @@ DPPLSyclQueueRef QMgrHelper::getCurrentQueue () * purpose. */ DPPLSyclQueueRef -QMgrHelper::getQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum) +QMgrHelper::getQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) { queue *QRef = nullptr; @@ -280,7 +279,7 @@ QMgrHelper::popSyclQueue () * Returns inside the number of activated queues not including the global queue * (QMgrHelper::active_queues[0]). */ -size_t DPPLGetNumActivatedQueues () +size_t DPPLQueueMgr_GetNumActivatedQueues () { if (QMgrHelper::active_queues.empty()) error_reporter("No active contexts"); @@ -290,7 +289,7 @@ size_t DPPLGetNumActivatedQueues () /*! * Returns the number of CPU queues. */ -size_t DPPLGetNumCPUQueues () +size_t DPPLQueueMgr_GetNumCPUQueues () { return QMgrHelper::cpu_queues.size(); } @@ -298,7 +297,7 @@ size_t DPPLGetNumCPUQueues () /*! * Returns the number of GPU queues. */ -size_t DPPLGetNumGPUQueues () +size_t DPPLQueueMgr_GetNumGPUQueues () { return QMgrHelper::gpu_queues.size(); } @@ -306,7 +305,7 @@ size_t DPPLGetNumGPUQueues () /*! * \see QMgrHelper::getCurrentQueue() */ -DPPLSyclQueueRef DPPLGetCurrentQueue () +DPPLSyclQueueRef DPPLQueueMgr_GetCurrentQueue () { return QMgrHelper::getCurrentQueue(); } @@ -315,8 +314,8 @@ DPPLSyclQueueRef DPPLGetCurrentQueue () * Returns a copy of a sycl::queue corresponding to the specified device type * and device number. A runtime_error gets thrown if no such device exists. */ -DPPLSyclQueueRef DPPLGetQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum) +DPPLSyclQueueRef DPPLQueueMgr_GetQueue (DPPLSyclDeviceType DeviceTy, + size_t DNum) { return QMgrHelper::getQueue(DeviceTy, DNum); } @@ -327,7 +326,7 @@ DPPLSyclQueueRef DPPLGetQueue (DPPLSyclDeviceType DeviceTy, * specified device type and id. A runtime_error gets thrown if no such device * exists. */ -void DPPLSetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) +void DPPLQueueMgr_SetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) { QMgrHelper::setAsDefaultQueue(DeviceTy, DNum); } @@ -335,8 +334,8 @@ void DPPLSetAsDefaultQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) /*! * \see QMgrHelper::pushSyclQueue() */ -__dppl_give DPPLSyclQueueRef DPPLPushSyclQueue (DPPLSyclDeviceType DeviceTy, - size_t DNum) +__dppl_give DPPLSyclQueueRef +DPPLQueueMgr_PushQueue (DPPLSyclDeviceType DeviceTy, size_t DNum) { return QMgrHelper::pushSyclQueue(DeviceTy, DNum); } @@ -344,7 +343,7 @@ __dppl_give DPPLSyclQueueRef DPPLPushSyclQueue (DPPLSyclDeviceType DeviceTy, /*! * \see QMgrHelper::popSyclQueue() */ -void DPPLPopSyclQueue () +void DPPLQueueMgr_PopQueue () { QMgrHelper::popSyclQueue(); } diff --git a/backends/tests/test_sycl_queue_manager.cpp b/backends/tests/test_sycl_queue_manager.cpp index d38ddb2a8a..05b04aabf6 100644 --- a/backends/tests/test_sycl_queue_manager.cpp +++ b/backends/tests/test_sycl_queue_manager.cpp @@ -24,8 +24,8 @@ /// //===----------------------------------------------------------------------===// #include "dppl_sycl_device_interface.h" -#include "dppl_sycl_queue_manager.h" #include "dppl_sycl_queue_interface.h" +#include "dppl_sycl_queue_manager.h" #include #include @@ -35,23 +35,23 @@ namespace { void foo (size_t & num) { - auto q1 = DPPLPushSyclQueue(DPPL_CPU, 0); - auto q2 = DPPLPushSyclQueue(DPPL_GPU, 0); + auto q1 = DPPLQueueMgr_PushQueue(DPPL_CPU, 0); + auto q2 = DPPLQueueMgr_PushQueue(DPPL_GPU, 0); // Capture the number of active queues in first - num = DPPLGetNumActivatedQueues(); - DPPLPopSyclQueue(); - DPPLPopSyclQueue(); - DPPLDeleteSyclQueue(q1); - DPPLDeleteSyclQueue(q2); + num = DPPLQueueMgr_GetNumActivatedQueues(); + DPPLQueueMgr_PopQueue(); + DPPLQueueMgr_PopQueue(); + DPPLQueue_Delete(q1); + DPPLQueue_Delete(q2); } void bar (size_t & num) { - auto q1 = DPPLPushSyclQueue(DPPL_GPU, 0); + auto q1 = DPPLQueueMgr_PushQueue(DPPL_GPU, 0); // Capture the number of active queues in second - num = DPPLGetNumActivatedQueues(); - DPPLPopSyclQueue(); - DPPLDeleteSyclQueue(q1); + num = DPPLQueueMgr_GetNumActivatedQueues(); + DPPLQueueMgr_PopQueue(); + DPPLQueue_Delete(q1); } } @@ -62,30 +62,30 @@ struct TestDPPLSyclQueuemanager : public ::testing::Test TEST_F (TestDPPLSyclQueuemanager, CheckDPPLGetCurrentQueue) { DPPLSyclQueueRef q; - ASSERT_NO_THROW(q = DPPLGetCurrentQueue()); + ASSERT_NO_THROW(q = DPPLQueueMgr_GetCurrentQueue()); ASSERT_TRUE(q != nullptr); } TEST_F (TestDPPLSyclQueuemanager, CheckDPPLGetQueue) { - auto numCpuQueues = DPPLGetNumCPUQueues(); - auto numGpuQueues = DPPLGetNumGPUQueues(); + auto numCpuQueues = DPPLQueueMgr_GetNumCPUQueues(); + auto numGpuQueues = DPPLQueueMgr_GetNumGPUQueues(); if(numCpuQueues > 0) { - EXPECT_TRUE(DPPLGetQueue(DPPL_CPU, 0) != nullptr); + EXPECT_TRUE(DPPLQueueMgr_GetQueue(DPPL_CPU, 0) != nullptr); auto non_existent_device_num = numCpuQueues+1; try { - DPPLGetQueue(DPPL_CPU, non_existent_device_num); + DPPLQueueMgr_GetQueue(DPPL_CPU, non_existent_device_num); FAIL() << "SYCL CPU device " << non_existent_device_num << "not found on system."; } catch (...) { } } if(numGpuQueues > 0) { - EXPECT_TRUE(DPPLGetQueue(DPPL_GPU, 0) != nullptr); + EXPECT_TRUE(DPPLQueueMgr_GetQueue(DPPL_GPU, 0) != nullptr); auto non_existent_device_num = numGpuQueues+1; try { - DPPLGetQueue(DPPL_GPU, non_existent_device_num); + DPPLQueueMgr_GetQueue(DPPL_GPU, non_existent_device_num); FAIL() << "SYCL GPU device " << non_existent_device_num << "not found on system."; } @@ -99,7 +99,7 @@ TEST_F (TestDPPLSyclQueuemanager, CheckGetNumActivatedQueues) size_t num0, num1, num2, num4; // Add a queue to main thread - auto q = DPPLPushSyclQueue(DPPL_CPU, 0); + auto q = DPPLQueueMgr_PushQueue(DPPL_CPU, 0); std::thread first (foo, std::ref(num1)); std::thread second (bar, std::ref(num2)); @@ -109,9 +109,9 @@ TEST_F (TestDPPLSyclQueuemanager, CheckGetNumActivatedQueues) second.join(); // Capture the number of active queues in first - num0 = DPPLGetNumActivatedQueues(); - DPPLPopSyclQueue(); - num4 = DPPLGetNumActivatedQueues(); + num0 = DPPLQueueMgr_GetNumActivatedQueues(); + DPPLQueueMgr_PopQueue(); + num4 = DPPLQueueMgr_GetNumActivatedQueues(); // Verify what the expected number of activated queues each time a thread // called getNumActivatedQueues. @@ -120,15 +120,15 @@ TEST_F (TestDPPLSyclQueuemanager, CheckGetNumActivatedQueues) EXPECT_EQ(num2, 1); EXPECT_EQ(num4, 0); - DPPLDeleteSyclQueue(q); + DPPLQueue_Delete(q); } TEST_F (TestDPPLSyclQueuemanager, CheckDPPLDumpDeviceInfo) { - auto q = DPPLGetCurrentQueue(); - EXPECT_NO_FATAL_FAILURE(DPPLDumpDeviceInfo(DPPLGetDeviceFromQueue(q))); - EXPECT_NO_FATAL_FAILURE(DPPLDeleteSyclQueue(q)); + auto q = DPPLQueueMgr_GetCurrentQueue(); + EXPECT_NO_FATAL_FAILURE(DPPLDevice_DumpInfo(DPPLQueue_GetDevice(q))); + EXPECT_NO_FATAL_FAILURE(DPPLQueue_Delete(q)); } diff --git a/dppl/backend.pxd b/dppl/backend.pxd index d19cf02e2a..6b52575d0d 100644 --- a/dppl/backend.pxd +++ b/dppl/backend.pxd @@ -43,23 +43,23 @@ cdef extern from "dppl_sycl_types.h": cdef extern from "dppl_sycl_context_interface.h": - cdef void DPPLDeleteSyclContext (DPPLSyclContextRef CtxtRef) except + + cdef void DPPLContext_Delete (DPPLSyclContextRef CtxtRef) except + cdef extern from "dppl_sycl_device_interface.h": - cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + - cdef void DPPLDeleteSyclDevice (DPPLSyclDeviceRef DRef) except + - cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsAccelerator (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsCPU (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsGPU (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsHost (const DPPLSyclDeviceRef DRef) except + - cdef const char* DPPLGetDeviceDriverInfo (const DPPLSyclDeviceRef DRef) \ + cdef void DPPLDevice_DumpInfo (const DPPLSyclDeviceRef DRef) except + + cdef void DPPLDevice_Delete (DPPLSyclDeviceRef DRef) except + + cdef void DPPLDevice_DumpInfo (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDevice_IsAccelerator (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDevice_IsCPU (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDevice_IsGPU (const DPPLSyclDeviceRef DRef) except + + cdef bool DPPLDevice_IsHost (const DPPLSyclDeviceRef DRef) except + + cdef const char* DPPLDevice_GetDriverInfo (const DPPLSyclDeviceRef DRef) \ except + - cdef const char* DPPLGetDeviceName (const DPPLSyclDeviceRef DRef) except + - cdef const char* DPPLGetDeviceVendorName (const DPPLSyclDeviceRef DRef) \ + cdef const char* DPPLDevice_GetName (const DPPLSyclDeviceRef DRef) except + + cdef const char* DPPLDevice_GetVendorName (const DPPLSyclDeviceRef DRef) \ except + - cdef bool DPPLGetDeviceHostUnifiedMemory (const DPPLSyclDeviceRef DRef) \ + cdef bool DPPLDevice_IsHostUnifiedMemory (const DPPLSyclDeviceRef DRef) \ except + @@ -69,10 +69,10 @@ cdef extern from "dppl_sycl_platform_interface.h": cdef extern from "dppl_sycl_queue_interface.h": - cdef void DPPLDeleteSyclQueue (DPPLSyclQueueRef QRef) except + - cdef DPPLSyclContextRef DPPLGetContextFromQueue (const DPPLSyclQueueRef Q) \ + cdef void DPPLQueue_Delete (DPPLSyclQueueRef QRef) except + + cdef DPPLSyclContextRef DPPLQueue_GetContext (const DPPLSyclQueueRef Q) \ except+ - cdef DPPLSyclDeviceRef DPPLGetDeviceFromQueue (const DPPLSyclQueueRef Q) \ + cdef DPPLSyclDeviceRef DPPLQueue_GetDevice (const DPPLSyclQueueRef Q) \ except + @@ -81,17 +81,17 @@ cdef extern from "dppl_sycl_queue_manager.h": _GPU 'DPPL_GPU' _CPU 'DPPL_CPU' - cdef DPPLSyclQueueRef DPPLGetCurrentQueue () except + - cdef size_t DPPLGetNumCPUQueues () except + - cdef size_t DPPLGetNumGPUQueues () except + - cdef size_t DPPLGetNumActivatedQueues () except + - cdef DPPLSyclQueueRef DPPLGetQueue (_device_type DTy, - size_t device_num) except + - cdef void DPPLPopSyclQueue () except + - cdef DPPLSyclQueueRef DPPLPushSyclQueue (_device_type DTy, - size_t device_num) except + - cdef void DPPLSetAsDefaultQueue (_device_type DTy, - size_t device_num) except + + cdef DPPLSyclQueueRef DPPLQueueMgr_GetCurrentQueue () except + + cdef size_t DPPLQueueMgr_GetNumCPUQueues () except + + cdef size_t DPPLQueueMgr_GetNumGPUQueues () except + + cdef size_t DPPLQueueMgr_GetNumActivatedQueues () except + + cdef DPPLSyclQueueRef DPPLQueueMgr_GetQueue (_device_type DTy, + size_t device_num) except + + cdef void DPPLQueueMgr_PopQueue () except + + cdef DPPLSyclQueueRef DPPLQueueMgr_PushQueue (_device_type DTy, + size_t device_num) except + + cdef void DPPLQueueMgr_SetAsDefaultQueue (_device_type DTy, + size_t device_num) except + cdef extern from "dppl_sycl_usm_interface.h": diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index 5df3af4367..daad6796a7 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -56,7 +56,7 @@ cdef class SyclContext: return ret def __dealloc__ (self): - DPPLDeleteSyclContext(self.ctxt_ptr) + DPPLContext_Delete(self.ctxt_ptr) cdef DPPLSyclContextRef get_context_ref (self): return self.ctxt_ptr @@ -70,13 +70,13 @@ cdef class SyclDevice: cdef SyclDevice _create (DPPLSyclDeviceRef dref): cdef SyclDevice ret = SyclDevice.__new__(SyclDevice) ret.device_ptr = dref - ret.vendor_name = DPPLGetDeviceVendorName(dref) - ret.device_name = DPPLGetDeviceName(dref) - ret.driver_version = DPPLGetDeviceDriverInfo(dref) + ret.vendor_name = DPPLDevice_GetVendorName(dref) + ret.device_name = DPPLDevice_GetName(dref) + ret.driver_version = DPPLDevice_GetDriverInfo(dref) return ret def __dealloc__ (self): - DPPLDeleteSyclDevice(self.device_ptr) + DPPLDevice_Delete(self.device_ptr) DPPLDeleteCString(self.device_name) DPPLDeleteCString(self.vendor_name) DPPLDeleteCString(self.driver_version) @@ -84,7 +84,7 @@ cdef class SyclDevice: def dump_device_info (self): ''' Print information about the SYCL device. ''' - DPPLDumpDeviceInfo(self.device_ptr) + DPPLDevice_DumpInfo(self.device_ptr) def get_device_name (self): ''' Returns the name of the device as a string @@ -121,13 +121,13 @@ cdef class SyclQueue: return ret def __dealloc__ (self): - DPPLDeleteSyclQueue(self.queue_ptr) + DPPLQueue_Delete(self.queue_ptr) cpdef SyclContext get_sycl_context (self): - return SyclContext._create(DPPLGetContextFromQueue(self.queue_ptr)) + return SyclContext._create(DPPLQueue_GetContext(self.queue_ptr)) cpdef SyclDevice get_sycl_device (self): - return SyclDevice._create(DPPLGetDeviceFromQueue(self.queue_ptr)) + return SyclDevice._create(DPPLQueue_GetDevice(self.queue_ptr)) cdef DPPLSyclQueueRef get_queue_ref (self): return self.queue_ptr @@ -137,9 +137,9 @@ cdef class _SyclQueueManager: def _set_as_current_queue (self, device_ty, device_id): cdef DPPLSyclQueueRef queue_ptr if device_ty == device_type.gpu: - queue_ptr = DPPLPushSyclQueue(_device_type._GPU, device_id) + queue_ptr = DPPLQueueMgr_PushQueue(_device_type._GPU, device_id) elif device_ty == device_type.cpu: - queue_ptr = DPPLPushSyclQueue(_device_type._CPU, device_id) + queue_ptr = DPPLQueueMgr_PushQueue(_device_type._CPU, device_id) else: e = UnsupportedDeviceTypeError("Device can only be cpu or gpu") raise e @@ -147,7 +147,7 @@ cdef class _SyclQueueManager: return SyclQueue._create(queue_ptr) def _remove_current_queue (self): - DPPLPopSyclQueue() + DPPLQueueMgr_PopQueue() def has_sycl_platforms (self): cdef size_t num_platforms = DPPLPlatform_GetNumPlatforms() @@ -164,31 +164,31 @@ cdef class _SyclQueueManager: def get_num_activated_queues (self): ''' Return the number of currently activated queues for this thread. ''' - return DPPLGetNumActivatedQueues() + return DPPLQueueMgr_GetNumActivatedQueues() def get_current_queue (self): ''' Returns the activated SYCL queue as a PyCapsule. ''' - return SyclQueue._create(DPPLGetCurrentQueue()) + return SyclQueue._create(DPPLQueueMgr_GetCurrentQueue()) def set_default_queue (self, device_ty, device_id): if device_ty == device_type.gpu: - DPPLSetAsDefaultQueue(_device_type._GPU, device_id) + DPPLQueueMgr_SetAsDefaultQueue(_device_type._GPU, device_id) elif device_ty == device_type.cpu: - DPPLSetAsDefaultQueue(_device_type._CPU, device_id) + DPPLQueueMgr_SetAsDefaultQueue(_device_type._CPU, device_id) else: e = UnsupportedDeviceTypeError("Device can only be cpu or gpu") raise e def has_gpu_queues (self): - cdef size_t num = DPPLGetNumGPUQueues() + cdef size_t num = DPPLQueueMgr_GetNumGPUQueues() if num: return True else: return False def has_cpu_queues (self): - cdef size_t num = DPPLGetNumCPUQueues() + cdef size_t num = DPPLQueueMgr_GetNumCPUQueues() if num: return True else: @@ -200,7 +200,7 @@ cdef class _SyclQueueManager: DPPLPlatform_DumpInfo() def is_in_dppl_ctxt (self): - cdef size_t num = DPPLGetNumActivatedQueues() + cdef size_t num = DPPLQueueMgr_GetNumActivatedQueues() if num: return True else: From 0c67a990a171444e1c1c54511e8975d19c35b74a Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sat, 19 Sep 2020 17:28:56 -0500 Subject: [PATCH 42/93] Fix files. --- dppl/backend.pxd | 41 ----------------------------------------- dppl/sycl_core.pyx | 7 ------- 2 files changed, 48 deletions(-) diff --git a/dppl/backend.pxd b/dppl/backend.pxd index 554acda07c..aebe0dbe59 100644 --- a/dppl/backend.pxd +++ b/dppl/backend.pxd @@ -43,7 +43,6 @@ cdef extern from "dppl_sycl_types.h": cdef extern from "dppl_sycl_context_interface.h": -<<<<<<< HEAD cdef void DPPLContext_Delete (DPPLSyclContextRef CtxtRef) except + @@ -61,25 +60,6 @@ cdef extern from "dppl_sycl_device_interface.h": cdef const char* DPPLDevice_GetVendorName (const DPPLSyclDeviceRef DRef) \ except + cdef bool DPPLDevice_IsHostUnifiedMemory (const DPPLSyclDeviceRef DRef) \ -======= - cdef void DPPLDeleteSyclContext (DPPLSyclContextRef CtxtRef) except + - - -cdef extern from "dppl_sycl_device_interface.h": - cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + - cdef void DPPLDeleteSyclDevice (DPPLSyclDeviceRef DRef) except + - cdef void DPPLDumpDeviceInfo (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsAccelerator (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsCPU (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsGPU (const DPPLSyclDeviceRef DRef) except + - cdef bool DPPLDeviceIsHost (const DPPLSyclDeviceRef DRef) except + - cdef const char* DPPLGetDeviceDriverInfo (const DPPLSyclDeviceRef DRef) \ - except + - cdef const char* DPPLGetDeviceName (const DPPLSyclDeviceRef DRef) except + - cdef const char* DPPLGetDeviceVendorName (const DPPLSyclDeviceRef DRef) \ - except + - cdef bool DPPLGetDeviceHostUnifiedMemory (const DPPLSyclDeviceRef DRef) \ ->>>>>>> master except + @@ -89,17 +69,10 @@ cdef extern from "dppl_sycl_platform_interface.h": cdef extern from "dppl_sycl_queue_interface.h": -<<<<<<< HEAD cdef void DPPLQueue_Delete (DPPLSyclQueueRef QRef) except + cdef DPPLSyclContextRef DPPLQueue_GetContext (const DPPLSyclQueueRef Q) \ except+ cdef DPPLSyclDeviceRef DPPLQueue_GetDevice (const DPPLSyclQueueRef Q) \ -======= - cdef void DPPLDeleteSyclQueue (DPPLSyclQueueRef QRef) except + - cdef DPPLSyclContextRef DPPLGetContextFromQueue (const DPPLSyclQueueRef Q) \ - except+ - cdef DPPLSyclDeviceRef DPPLGetDeviceFromQueue (const DPPLSyclQueueRef Q) \ ->>>>>>> master except + @@ -108,7 +81,6 @@ cdef extern from "dppl_sycl_queue_manager.h": _GPU 'DPPL_GPU' _CPU 'DPPL_CPU' -<<<<<<< HEAD cdef DPPLSyclQueueRef DPPLQueueMgr_GetCurrentQueue () except + cdef size_t DPPLQueueMgr_GetNumCPUQueues () except + cdef size_t DPPLQueueMgr_GetNumGPUQueues () except + @@ -120,19 +92,6 @@ cdef extern from "dppl_sycl_queue_manager.h": size_t device_num) except + cdef void DPPLQueueMgr_SetAsDefaultQueue (_device_type DTy, size_t device_num) except + -======= - cdef DPPLSyclQueueRef DPPLGetCurrentQueue () except + - cdef size_t DPPLGetNumCPUQueues () except + - cdef size_t DPPLGetNumGPUQueues () except + - cdef size_t DPPLGetNumActivatedQueues () except + - cdef DPPLSyclQueueRef DPPLGetQueue (_device_type DTy, - size_t device_num) except + - cdef void DPPLPopSyclQueue () except + - cdef DPPLSyclQueueRef DPPLPushSyclQueue (_device_type DTy, - size_t device_num) except + - cdef void DPPLSetAsDefaultQueue (_device_type DTy, - size_t device_num) except + ->>>>>>> master cdef extern from "dppl_sycl_usm_interface.h": diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index cba8c42c98..daad6796a7 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -124,17 +124,10 @@ cdef class SyclQueue: DPPLQueue_Delete(self.queue_ptr) cpdef SyclContext get_sycl_context (self): -<<<<<<< HEAD return SyclContext._create(DPPLQueue_GetContext(self.queue_ptr)) cpdef SyclDevice get_sycl_device (self): return SyclDevice._create(DPPLQueue_GetDevice(self.queue_ptr)) -======= - return SyclContext._create(DPPLGetContextFromQueue(self.queue_ptr)) - - cpdef SyclDevice get_sycl_device (self): - return SyclDevice._create(DPPLGetDeviceFromQueue(self.queue_ptr)) ->>>>>>> master cdef DPPLSyclQueueRef get_queue_ref (self): return self.queue_ptr From fa2955e77d7a0cfa755da6df2264492e97d95adc Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sat, 19 Sep 2020 17:41:06 -0500 Subject: [PATCH 43/93] Minor changes to scripts. --- backends/dbg_build.sh | 6 ++---- scripts/build_for_develop.sh | 5 +++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/backends/dbg_build.sh b/backends/dbg_build.sh index 6fba1486c9..2ae201b8cb 100755 --- a/backends/dbg_build.sh +++ b/backends/dbg_build.sh @@ -5,6 +5,7 @@ mkdir build pushd build INSTALL_PREFIX=`pwd`/../install +rm -rf ${INSTALL_PREFIX} export ONEAPI_ROOT=/opt/intel/oneapi DPCPP_ROOT=${ONEAPI_ROOT}/compiler/latest/linux PYTHON_INC=`python -c "import distutils.sysconfig; \ @@ -24,9 +25,6 @@ cmake \ -DGTEST_LIB_DIR=${CONDA_PREFIX}/lib \ .. -make V=1 -n -j 4 -make check -make install - +make V=1 -n -j 4 && make check && make install popd diff --git a/scripts/build_for_develop.sh b/scripts/build_for_develop.sh index 30aa387d4d..7c393e5ea8 100755 --- a/scripts/build_for_develop.sh +++ b/scripts/build_for_develop.sh @@ -21,10 +21,11 @@ cmake \ -DCMAKE_CXX_COMPILER:PATH=${DPCPP_ROOT}/bin/dpcpp \ -DPYTHON_INCLUDE_DIR=${PYTHON_INC} \ -DNUMPY_INCLUDE_DIR=${NUMPY_INC} \ + -DGTEST_INCLUDE_DIR=${CONDA_PREFIX}/include/ \ + -DGTEST_LIB_DIR=${CONDA_PREFIX}/lib \ ../backends -make V=1 -n -j 4 && make install -#make check +make V=1 -n -j 4 && make check && make install popd cp install/lib/*.so dppl/ From 503ead4ed7b04c03ccab5c153571d722d465ece3 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sun, 20 Sep 2020 17:12:30 -0500 Subject: [PATCH 44/93] Fix name og tester class. --- backends/tests/test_sycl_queue_manager.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/backends/tests/test_sycl_queue_manager.cpp b/backends/tests/test_sycl_queue_manager.cpp index 05b04aabf6..38faadc217 100644 --- a/backends/tests/test_sycl_queue_manager.cpp +++ b/backends/tests/test_sycl_queue_manager.cpp @@ -55,11 +55,11 @@ namespace } } -struct TestDPPLSyclQueuemanager : public ::testing::Test +struct TestDPPLSyclQueueManager : public ::testing::Test { }; -TEST_F (TestDPPLSyclQueuemanager, CheckDPPLGetCurrentQueue) +TEST_F (TestDPPLSyclQueueManager, CheckDPPLGetCurrentQueue) { DPPLSyclQueueRef q; ASSERT_NO_THROW(q = DPPLQueueMgr_GetCurrentQueue()); @@ -67,7 +67,7 @@ TEST_F (TestDPPLSyclQueuemanager, CheckDPPLGetCurrentQueue) } -TEST_F (TestDPPLSyclQueuemanager, CheckDPPLGetQueue) +TEST_F (TestDPPLSyclQueueManager, CheckDPPLGetQueue) { auto numCpuQueues = DPPLQueueMgr_GetNumCPUQueues(); auto numGpuQueues = DPPLQueueMgr_GetNumGPUQueues(); @@ -94,7 +94,7 @@ TEST_F (TestDPPLSyclQueuemanager, CheckDPPLGetQueue) } -TEST_F (TestDPPLSyclQueuemanager, CheckGetNumActivatedQueues) +TEST_F (TestDPPLSyclQueueManager, CheckGetNumActivatedQueues) { size_t num0, num1, num2, num4; @@ -124,7 +124,7 @@ TEST_F (TestDPPLSyclQueuemanager, CheckGetNumActivatedQueues) } -TEST_F (TestDPPLSyclQueuemanager, CheckDPPLDumpDeviceInfo) +TEST_F (TestDPPLSyclQueueManager, CheckDPPLDumpDeviceInfo) { auto q = DPPLQueueMgr_GetCurrentQueue(); EXPECT_NO_FATAL_FAILURE(DPPLDevice_DumpInfo(DPPLQueue_GetDevice(q))); From 49ea38b35da99233c5a76e488d27a9e7b574cca2 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sun, 20 Sep 2020 19:54:50 -0500 Subject: [PATCH 45/93] Add the C API for program, kernel interfaces. --- backends/CMakeLists.txt | 2 + backends/include/dppl_sycl_kernel_interface.h | 74 +++++++ .../include/dppl_sycl_program_interface.h | 114 +++++++++++ .../source/dppl_sycl_kernel_interface.cpp | 75 ++++++++ .../source/dppl_sycl_program_interface.cpp | 180 ++++++++++++++++++ backends/tests/CMakeLists.txt | 2 + backends/tests/test_sycl_kernel_interface.cpp | 106 +++++++++++ .../tests/test_sycl_program_interface.cpp | 158 +++++++++++++++ 8 files changed, 711 insertions(+) create mode 100644 backends/include/dppl_sycl_kernel_interface.h create mode 100644 backends/include/dppl_sycl_program_interface.h create mode 100644 backends/source/dppl_sycl_kernel_interface.cpp create mode 100644 backends/source/dppl_sycl_program_interface.cpp create mode 100644 backends/tests/test_sycl_kernel_interface.cpp create mode 100644 backends/tests/test_sycl_program_interface.cpp diff --git a/backends/CMakeLists.txt b/backends/CMakeLists.txt index abb8c022ff..ca47176b7e 100644 --- a/backends/CMakeLists.txt +++ b/backends/CMakeLists.txt @@ -98,7 +98,9 @@ add_library( SHARED source/dppl_sycl_context_interface.cpp source/dppl_sycl_device_interface.cpp + source/dppl_sycl_kernel_interface.cpp source/dppl_sycl_platform_interface.cpp + source/dppl_sycl_program_interface.cpp source/dppl_sycl_queue_interface.cpp source/dppl_sycl_queue_manager.cpp source/dppl_sycl_usm_interface.cpp diff --git a/backends/include/dppl_sycl_kernel_interface.h b/backends/include/dppl_sycl_kernel_interface.h new file mode 100644 index 0000000000..65cf646697 --- /dev/null +++ b/backends/include/dppl_sycl_kernel_interface.h @@ -0,0 +1,74 @@ +//===---- dppl_sycl_kernel_interface.h - DPPL-SYCL interface --*--C++ --*--===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 header declares a C API to create Sycl kernels from OpenCL kernels. In +/// future, API to create interoperability kernels from other languages such as +/// Level-0 driver API may be added here. +/// +/// \todo Investigate what we should do when we add support for Level-0 API. +/// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "dppl_data_types.h" +#include "dppl_sycl_types.h" +#include "Support/DllExport.h" +#include "Support/ExternC.h" +#include "Support/MemOwnershipAttrs.h" + +DPPL_C_EXTERN_C_BEGIN + +/*! + * @brief Returns a C string for the kernel name. + * + * @param KRef DPPLSyclKernelRef pointer to an OpenCL + * interoperability kernel. + * @return If a kernel name exists then returns it as a C string, else + * returns a nullptr. + */ +DPPL_API +__dppl_give const char* +DPPLKernel_GetFunctionName (__dppl_keep const DPPLSyclKernelRef KRef); + +/*! + * @brief Returns the number of arguments for the OpenCL kernel. + * + * @param KRef DPPLSyclKernelRef pointer to an OpenCL + * interoperability kernel. + * @return Returns the number of arguments for the OpenCL interoperability + * kernel. + */ +DPPL_API +size_t +DPPLKernel_GetNumArgs (__dppl_keep const DPPLSyclKernelRef KRef); + +/*! + * @brief Deletes the DPPLSyclKernelRef after casting it to a sycl::kernel. + * + * @param KRef DPPLSyclKernelRef pointer to an OpenCL + * interoperability kernel. + */ +DPPL_API +void +DPPLKernel_DeleteKernelRef (__dppl_take DPPLSyclKernelRef KRef); + +DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_program_interface.h b/backends/include/dppl_sycl_program_interface.h new file mode 100644 index 0000000000..c77ee2b213 --- /dev/null +++ b/backends/include/dppl_sycl_program_interface.h @@ -0,0 +1,114 @@ +//===---- dppl_sycl_program_interface.h - DPPL-SYCL interface --*--C++ --*--===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 header declares a C API to create Sycl program an interoperability +/// program defined in OpenCL. In future, API to create interoperability +/// kernels from other languages such as Level-0 driver API may be added here. +/// +/// \todo Investigate what we should do when we add support for Level-0 API. +/// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "dppl_data_types.h" +#include "dppl_sycl_types.h" +#include "Support/DllExport.h" +#include "Support/ExternC.h" +#include "Support/MemOwnershipAttrs.h" + +DPPL_C_EXTERN_C_BEGIN + +/*! + * @brief Create a Sycl program from an OpenCL SPIR-V binary file. + * + * Sycl 1.2 does expose any method to create a sycl::program from a SPIR-V IL + * file. To get around this limitation, we need to use the Sycl feature to + * create an interoperability kernel from an OpenCL kernel. This function first + * creates an OpenCL program and kernel from the SPIR-V binary and then using + * the Sycl-OpenCL interoperability feature creates a Sycl kernel from the + * OpenCL kernel. + * + * The feature to create a Sycl kernel from a SPIR-V IL binary will be available + * in Sycl 2.0, at which point we may deprecate this function. + * + * @param Ctx An opaque pointer to a sycl::context + * @param IL SPIR-V binary + * @return A new SyclProgramRef pointer if the program creation succeeded, + * else returns NULL. + */ +DPPL_API +__dppl_give DPPLSyclProgramRef +DPPLProgram_CreateFromOCLSpirv (__dppl_keep const DPPLSyclContextRef Ctx, + __dppl_keep const void *IL, + size_t Length); + +/*! + * @brief Create a Sycl program from an OpenCL kernel source string. + * + * @param Ctx An opaque pointer to a sycl::context + * @param Source OpenCL source string + * @param CompileOptions Extra compiler flags (refer Sycl spec.) + * @return A new SyclProgramRef pointer if the program creation succeeded, + * else returns NULL. + */ +DPPL_API +__dppl_give DPPLSyclProgramRef +DPPLProgram_CreateFromOCLSource (__dppl_keep const DPPLSyclContextRef Ctx, + __dppl_keep const char *Source, + __dppl_keep const char *CompileOpts = nullptr); + +/*! + * @brief Returns the SyclKernel with given name from the program, if not found + * then return NULL. + * + * @param PRef Opaque pointer to a sycl::program + * @param KernelName Name of kernel + * @return A SyclKernel reference if the kernel exists, else NULL + */ +DPPL_API +__dppl_give DPPLSyclKernelRef +DPPLProgram_GetKernel (__dppl_keep DPPLSyclProgramRef PRef, + __dppl_keep const char *KernelName); + +/*! + * @brief Return True if a SyclKernel with given name exists in the program, if + * not found then returns False. + * + * @param PRef Opaque pointer to a sycl::program + * @param KernelName Name of kernel + * @return True if the kernel exists, else False + */ +DPPL_API +bool +DPPLProgram_HasKernel (__dppl_keep DPPLSyclProgramRef PRef, + __dppl_keep const char *KernelName); + +/*! + * @brief Frees the DPPLSyclProgramRef pointer. + * + * @param PRef Opaque pointer to a sycl::program + */ +DPPL_API +void +DPPLProgram_Delete (__dppl_take DPPLSyclProgramRef PRef); + +DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_kernel_interface.cpp b/backends/source/dppl_sycl_kernel_interface.cpp new file mode 100644 index 0000000000..48f0b5d244 --- /dev/null +++ b/backends/source/dppl_sycl_kernel_interface.cpp @@ -0,0 +1,75 @@ +//===--- dppl_sycl_kernel_interface.cpp - DPPL-SYCL interface --*-- C++ -*-===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 the functions declared in +/// dppl_sycl_kernel_interface.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_sycl_kernel_interface.h" +#include "Support/CBindingWrapping.h" + +#include /* Sycl headers */ + +using namespace cl::sycl; + +namespace +{ + +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(kernel, DPPLSyclKernelRef) + +} /* end of anonymous namespace */ + +__dppl_give const char* +DPPLKernel_GetFunctionName (__dppl_keep const DPPLSyclKernelRef Kernel) +{ + if(!Kernel) { + // \todo record error + return nullptr; + } + + auto SyclKernel = unwrap(Kernel); + auto kernel_name = SyclKernel->get_info(); + if(kernel_name.empty()) + return nullptr; + auto cstr_name = new char [kernel_name.length()+1]; + std::strcpy (cstr_name, kernel_name.c_str()); + return cstr_name; +} + +size_t +DPPLKernel_GetNumArgs (__dppl_keep const DPPLSyclKernelRef Kernel) +{ + if(!Kernel) { + // \todo record error + return -1; + } + + auto SyclKernel = unwrap(Kernel); + auto num_args = SyclKernel->get_info(); + return (size_t)num_args; +} + +void +DPPLKernel_DeleteKernelRef (__dppl_take DPPLSyclKernelRef Kernel) +{ + delete unwrap(Kernel); +} diff --git a/backends/source/dppl_sycl_program_interface.cpp b/backends/source/dppl_sycl_program_interface.cpp new file mode 100644 index 0000000000..01d615a08b --- /dev/null +++ b/backends/source/dppl_sycl_program_interface.cpp @@ -0,0 +1,180 @@ +//===--- dppl_sycl_program_interface.cpp - DPPL-SYCL interface --*-- C++ -*-===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 the functions declared in +/// dppl_sycl_program_interface.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_sycl_program_interface.h" +#include "Support/CBindingWrapping.h" + +#include /* Sycl headers */ +#include /* OpenCL headers */ + +using namespace cl::sycl; + +namespace +{ +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPPLSyclContextRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(program, DPPLSyclProgramRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(kernel, DPPLSyclKernelRef) + +} /* end of anonymous namespace */ + +__dppl_give DPPLSyclProgramRef +DPPLProgram_CreateFromOCLSpirv (__dppl_keep const DPPLSyclContextRef CtxRef, + __dppl_keep const void *IL, + size_t length) +{ + cl_int err; + context *SyclCtx; + if(!CtxRef) { + // \todo handle error + return nullptr; + } + + SyclCtx = unwrap(CtxRef); + auto CLCtx = SyclCtx->get(); + auto CLProgram = clCreateProgramWithIL(CLCtx, IL, length, &err); + if (err) { + // \todo: record the error string and any other information. + std::cerr << "OpenCL program could not be created from the SPIR-V " + "binary. OpenCL Error " << err << ".\n"; + return nullptr; + } + auto SyclDevices = SyclCtx->get_devices(); + + // Get a list of CL Devices from the Sycl devices + auto CLDevices = new cl_device_id[SyclDevices.size()]; + for (auto i = 0ul; i < SyclDevices.size(); ++i) + CLDevices[i] = SyclDevices[i].get(); + + // Build the OpenCL interoperability program + err = clBuildProgram(CLProgram, (cl_uint)(SyclDevices.size()), CLDevices, + nullptr, nullptr, nullptr); + // free the CLDevices array + delete[] CLDevices; + + if (err) { + // \todo: record the error string and any other information. + std::cerr << "OpenCL program could not be built. OpenCL Error " + << err << ".\n"; + return nullptr; + } + + // Create the Sycl program from OpenCL program + try { + auto SyclProgram = new program(*SyclCtx, CLProgram); + return wrap(SyclProgram); + } catch (invalid_object_error) { + // \todo record error + return nullptr; + } +} + +__dppl_give DPPLSyclProgramRef +DPPLProgram_CreateFromOCLSource (__dppl_keep const DPPLSyclContextRef Ctx, + __dppl_keep const char *Source, + __dppl_keep const char *CompileOpts) +{ + cl_int err; + std::string compileOpts; + context *SyclCtx = nullptr; + program *SyclProgram = nullptr; + + if(!Ctx) { + // \todo handle error + return nullptr; + } + + if(!Source) { + // \todo handle error message + return nullptr; + } + + SyclCtx = unwrap(Ctx); + SyclProgram = new program(*SyclCtx); + std::string source = Source; + + if(CompileOpts) { + compileOpts = CompileOpts; + } + + try{ + SyclProgram->build_with_source(source, compileOpts); + return wrap(SyclProgram); + } catch (compile_program_error) { + delete SyclProgram; + // \todo record error + return nullptr; + } catch (feature_not_supported) { + delete SyclProgram; + // \todo record error + return nullptr; + } +} + +__dppl_give DPPLSyclKernelRef +DPPLProgram_GetKernel (__dppl_keep DPPLSyclProgramRef PRef, + __dppl_keep const char *KernelName) +{ + if(!PRef) { + // \todo record error + return nullptr; + } + auto SyclProgram = unwrap(PRef); + if(!KernelName) { + // \todo record error + return nullptr; + } + std::string name = KernelName; + try { + auto SyclKernel = new kernel(SyclProgram->get_kernel(name)); + return wrap(SyclKernel); + } catch (invalid_object_error) { + // \todo record error + return nullptr; + } +} + +bool +DPPLProgram_HasKernel (__dppl_keep DPPLSyclProgramRef PRef, + __dppl_keep const char *KernelName) +{ + if(!PRef) { + // \todo handle error + return false; + } + + auto SyclProgram = unwrap(PRef); + try { + return SyclProgram->has_kernel(KernelName); + } catch (invalid_object_error) { + return false; + } +} + +void +DPPLProgram_Delete (__dppl_take DPPLSyclProgramRef PRef) +{ + delete unwrap(PRef); +} diff --git a/backends/tests/CMakeLists.txt b/backends/tests/CMakeLists.txt index c3add176ba..d608bf903a 100644 --- a/backends/tests/CMakeLists.txt +++ b/backends/tests/CMakeLists.txt @@ -24,7 +24,9 @@ else() set(PYDPPL_BACKEND_TEST_CASES test_sycl_queue_manager + test_sycl_kernel_interface test_sycl_platform_interface + test_sycl_program_interface ) foreach(TEST_CASE ${PYDPPL_BACKEND_TEST_CASES}) diff --git a/backends/tests/test_sycl_kernel_interface.cpp b/backends/tests/test_sycl_kernel_interface.cpp new file mode 100644 index 0000000000..985931c38d --- /dev/null +++ b/backends/tests/test_sycl_kernel_interface.cpp @@ -0,0 +1,106 @@ +//===---- test_sycl_program_interface.cpp - DPPL-SYCL interface -*- C++ -*-===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 has unit test cases for functions defined in +/// dppl_sycl_kernel_interface.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_sycl_context_interface.h" +#include "dppl_sycl_kernel_interface.h" +#include "dppl_sycl_program_interface.h" +#include "dppl_sycl_queue_interface.h" +#include "dppl_sycl_queue_manager.h" +#include "dppl_utils.h" + +#include +#include +#include + +using namespace cl::sycl; + + +struct TestDPPLSyclKernelInterface : public ::testing::Test +{ + const char *CLProgramStr = R"CLC( + kernel void add(global int* a, global int* b, global int* c) { + size_t index = get_global_id(0); + c[index] = a[index] + b[index]; + } + + kernel void axpy(global int* a, global int* b, global int* c, int d) { + size_t index = get_global_id(0); + c[index] = a[index] + d*b[index]; + } + )CLC"; + const char *CompileOpts ="-cl-fast-relaxed-math"; + + DPPLSyclContextRef CurrCtxRef = nullptr; + DPPLSyclQueueRef CurrQueueRef = nullptr; + DPPLSyclProgramRef PRef = nullptr; + DPPLSyclKernelRef AddKernel = nullptr; + DPPLSyclKernelRef AxpyKernel = nullptr; + + TestDPPLSyclKernelInterface () + { + CurrQueueRef = DPPLQueueMgr_GetCurrentQueue(); + CurrCtxRef = DPPLQueue_GetContext(CurrQueueRef); + PRef = DPPLProgram_CreateFromOCLSource(CurrCtxRef, CLProgramStr, + CompileOpts); + AddKernel = DPPLProgram_GetKernel(PRef, "add"); + AxpyKernel = DPPLProgram_GetKernel(PRef, "axpy"); + } + + ~TestDPPLSyclKernelInterface () + { + DPPLQueue_Delete(CurrQueueRef); + DPPLContext_Delete(CurrCtxRef); + DPPLProgram_Delete(PRef); + DPPLKernel_DeleteKernelRef(AddKernel); + DPPLKernel_DeleteKernelRef(AxpyKernel); + } +}; + +TEST_F (TestDPPLSyclKernelInterface, CheckGetFunctionName) +{ + + auto fnName1 = DPPLKernel_GetFunctionName(AddKernel); + auto fnName2 = DPPLKernel_GetFunctionName(AxpyKernel); + ASSERT_STREQ("add", fnName1); + ASSERT_STREQ("axpy", fnName2); + DPPLDeleteCString(fnName1); + DPPLDeleteCString(fnName2); +} + +TEST_F (TestDPPLSyclKernelInterface, CheckGetNumArgs) +{ + + ASSERT_EQ(DPPLKernel_GetNumArgs(AddKernel), 3); + ASSERT_EQ(DPPLKernel_GetNumArgs(AxpyKernel), 4); +} + +int +main (int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + return ret; +} diff --git a/backends/tests/test_sycl_program_interface.cpp b/backends/tests/test_sycl_program_interface.cpp new file mode 100644 index 0000000000..d13a103da6 --- /dev/null +++ b/backends/tests/test_sycl_program_interface.cpp @@ -0,0 +1,158 @@ +//===---- test_sycl_program_interface.cpp - DPPL-SYCL interface -*- C++ -*-===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 has unit test cases for functions defined in +/// dppl_sycl_program_interface.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_sycl_context_interface.h" +#include "dppl_sycl_kernel_interface.h" +#include "dppl_sycl_program_interface.h" +#include "dppl_sycl_queue_interface.h" +#include "dppl_sycl_queue_manager.h" + +#include +#include +#include + +using namespace cl::sycl; + +namespace +{ + const size_t SIZE = 1024; +} + +struct TestDPPLSyclProgramInterface : public ::testing::Test +{ + const char *CLProgramStr = R"CLC( + kernel void add(global int* a, global int* b, global int* c) { + size_t index = get_global_id(0); + c[index] = a[index] + b[index]; + } + + kernel void axpy(global int* a, global int* b, global int* c, int d) { + size_t index = get_global_id(0); + c[index] = a[index] + d*b[index]; + } + )CLC"; + const char *CompileOpts ="-cl-fast-relaxed-math"; + + DPPLSyclContextRef CurrCtxRef = nullptr; + DPPLSyclQueueRef CurrQueueRef = nullptr; + DPPLSyclProgramRef PRef = nullptr; + + TestDPPLSyclProgramInterface () + { + CurrQueueRef = DPPLQueueMgr_GetCurrentQueue(); + CurrCtxRef = DPPLQueue_GetContext(CurrQueueRef); + PRef = DPPLProgram_CreateFromOCLSource(CurrCtxRef, CLProgramStr, + CompileOpts); + } + + ~TestDPPLSyclProgramInterface () + { + DPPLQueue_Delete(CurrQueueRef); + DPPLContext_Delete(CurrCtxRef); + DPPLProgram_Delete(PRef); + } +}; + +TEST_F (TestDPPLSyclProgramInterface, CheckCreateFromOCLSource) +{ + ASSERT_TRUE(PRef != nullptr); +} + + +TEST_F (TestDPPLSyclProgramInterface, CheckHasKernel) +{ + ASSERT_TRUE(DPPLProgram_HasKernel(PRef, "add")); + ASSERT_TRUE(DPPLProgram_HasKernel(PRef, "axpy")); +} + +TEST_F (TestDPPLSyclProgramInterface, CheckGetKernel) +{ + auto AddKernel = DPPLProgram_GetKernel(PRef, "add"); + auto AxpyKernel = DPPLProgram_GetKernel(PRef, "axpy"); + auto syclQueue = reinterpret_cast(CurrQueueRef); + + range<1> a_size{SIZE}; + std::array a, b, c; + + for (int i = 0; i a_device(a.data(), a_size); + buffer b_device(b.data(), a_size); + buffer c_device(c.data(), a_size); + buffer buffs[3] = {a_device, b_device, c_device}; + syclQueue->submit([&](handler& cgh) { + for (auto buff : buffs) { + auto arg = buff.get_access(cgh); + cgh.set_args(arg); + } + auto syclKernel = reinterpret_cast(AddKernel); + cgh.parallel_for(range<1>{SIZE}, *syclKernel); + }); + } + + // Validate the data + for(auto i = 0ul; i < SIZE; ++i) { + EXPECT_EQ(c[i], i + i); + } + + int d = 10; + { + buffer a_device(a.data(), a_size); + buffer b_device(b.data(), a_size); + buffer c_device(c.data(), a_size); + buffer buffs[3] = {a_device, b_device, c_device}; + syclQueue->submit([&](handler& cgh) { + for (auto i = 0ul; i < 3; ++i) { + auto arg = buffs[i].get_access(cgh); + cgh.set_arg(i, arg); + } + cgh.set_arg(3, d); + auto syclKernel = reinterpret_cast(AxpyKernel); + cgh.parallel_for(range<1>{SIZE}, *syclKernel); + }); + } + + // Validate the data + for(auto i = 0ul; i < SIZE; ++i) { + EXPECT_EQ(c[i], i + d*i); + } + + DPPLKernel_DeleteKernelRef(AddKernel); + DPPLKernel_DeleteKernelRef(AxpyKernel); +} + +int +main (int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + return ret; +} From 09c458758d7aab918ffa8d4e696c8c8dd4633f58 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Mon, 21 Sep 2020 18:38:24 -0500 Subject: [PATCH 46/93] Add tests to check program creation from SPIR-V. --- backends/tests/CMakeLists.txt | 8 + backends/tests/multi_kernel.spv | Bin 0 -> 1412 bytes .../tests/test_sycl_program_interface.cpp | 152 ++++++++++++------ 3 files changed, 111 insertions(+), 49 deletions(-) create mode 100644 backends/tests/multi_kernel.spv diff --git a/backends/tests/CMakeLists.txt b/backends/tests/CMakeLists.txt index d608bf903a..c3c4160aa5 100644 --- a/backends/tests/CMakeLists.txt +++ b/backends/tests/CMakeLists.txt @@ -29,6 +29,14 @@ else() test_sycl_program_interface ) + # Copy the spir-v input files to test build directory + set(spirv-test-files + multi_kernel.spv + ) + foreach(tf ${spirv-test-files}) + file(COPY ${tf} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + endforeach() + foreach(TEST_CASE ${PYDPPL_BACKEND_TEST_CASES}) add_executable(${TEST_CASE} EXCLUDE_FROM_ALL ${TEST_CASE}.cpp) target_link_libraries( diff --git a/backends/tests/multi_kernel.spv b/backends/tests/multi_kernel.spv new file mode 100644 index 0000000000000000000000000000000000000000..1fa772d8dfd583970caa6666704fe5d7434f1dda GIT binary patch literal 1412 zcma)*+iDY06oywjX;ZB+)>`Y?Ne>=M3D^r2L@5e}h#)?}Fl~Yar;{+9miEpIAKDwi z|JySw2FZmx?5y*@4tq_sxZN~cu&3hk-*@53S=W=UFnOUld1u&cQ4C@=T+ z|NdFiTJ~(7ukikLR@go{qVO|2Y(>gPHk*F+~}F&bP{^7>SM5prxu zu4dG*+f&;?u-(O`OD*?AJ^y|2RsP5xh_C8x*hBHNwLHderj}UpY%N<;xvFJH^2u2i z^6iTEx}Hq^4y>mhpG!u^Ec<$KWc0|)jGB6?8JV7XdN+9d{%7{=)+bvD{@^itX3jjA z1O0C(ANzKw%R$y6Ta2KyDLE3w2O3Lmh=Ps0E(`mJenph}i0?>yMHCx)za3dO=je%f zQ)7bx{!5_|&;E13d!?*k(U0@=*qo##!R=WXH1%^oom_S$FiNc_B+uhn46iboH=WAcsA517me89&GKIsE~VDR=Pz literal 0 HcmV?d00001 diff --git a/backends/tests/test_sycl_program_interface.cpp b/backends/tests/test_sycl_program_interface.cpp index d13a103da6..82ff21bf52 100644 --- a/backends/tests/test_sycl_program_interface.cpp +++ b/backends/tests/test_sycl_program_interface.cpp @@ -31,6 +31,8 @@ #include "dppl_sycl_queue_manager.h" #include +#include +#include #include #include @@ -39,6 +41,71 @@ using namespace cl::sycl; namespace { const size_t SIZE = 1024; + + void add_kernel_checker (queue *syclQueue, DPPLSyclKernelRef AddKernel) + { + range<1> a_size{SIZE}; + std::array a, b, c; + + for (int i = 0; i a_device(a.data(), a_size); + buffer b_device(b.data(), a_size); + buffer c_device(c.data(), a_size); + buffer buffs[3] = {a_device, b_device, c_device}; + syclQueue->submit([&](handler& cgh) { + for (auto buff : buffs) { + auto arg = buff.get_access(cgh); + cgh.set_args(arg); + } + auto syclKernel = reinterpret_cast(AddKernel); + cgh.parallel_for(range<1>{SIZE}, *syclKernel); + }); + } + + // Validate the data + for(auto i = 0ul; i < SIZE; ++i) { + EXPECT_EQ(c[i], i + i); + } + } + + void axpy_kernel_checker (queue *syclQueue, DPPLSyclKernelRef AxpyKernel) + { + range<1> a_size{SIZE}; + std::array a, b, c; + + for (int i = 0; i a_device(a.data(), a_size); + buffer b_device(b.data(), a_size); + buffer c_device(c.data(), a_size); + buffer buffs[3] = {a_device, b_device, c_device}; + syclQueue->submit([&](handler& cgh) { + for (auto i = 0ul; i < 3; ++i) { + auto arg = buffs[i].get_access(cgh); + cgh.set_arg(i, arg); + } + cgh.set_arg(3, d); + auto syclKernel = reinterpret_cast(AxpyKernel); + cgh.parallel_for(range<1>{SIZE}, *syclKernel); + }); + } + + // Validate the data + for(auto i = 0ul; i < SIZE; ++i) { + EXPECT_EQ(c[i], i + d*i); + } + } } struct TestDPPLSyclProgramInterface : public ::testing::Test @@ -59,6 +126,7 @@ struct TestDPPLSyclProgramInterface : public ::testing::Test DPPLSyclContextRef CurrCtxRef = nullptr; DPPLSyclQueueRef CurrQueueRef = nullptr; DPPLSyclProgramRef PRef = nullptr; + DPPLSyclProgramRef PRef2 = nullptr; TestDPPLSyclProgramInterface () { @@ -66,6 +134,16 @@ struct TestDPPLSyclProgramInterface : public ::testing::Test CurrCtxRef = DPPLQueue_GetContext(CurrQueueRef); PRef = DPPLProgram_CreateFromOCLSource(CurrCtxRef, CLProgramStr, CompileOpts); + + // Create a program from a SPIR-V file + std::ifstream file{"./multi_kernel.spv", + std::ios::binary | std::ios::ate}; + auto fileSize = std::filesystem::file_size("./multi_kernel.spv"); + file.seekg(0, std::ios::beg); + std::vector buffer(fileSize); + file.read(buffer.data(), fileSize); + PRef2 = DPPLProgram_CreateFromOCLSpirv(CurrCtxRef, buffer.data(), + fileSize); } ~TestDPPLSyclProgramInterface () @@ -73,6 +151,7 @@ struct TestDPPLSyclProgramInterface : public ::testing::Test DPPLQueue_Delete(CurrQueueRef); DPPLContext_Delete(CurrCtxRef); DPPLProgram_Delete(PRef); + DPPLProgram_Delete(PRef2); } }; @@ -81,69 +160,44 @@ TEST_F (TestDPPLSyclProgramInterface, CheckCreateFromOCLSource) ASSERT_TRUE(PRef != nullptr); } +TEST_F (TestDPPLSyclProgramInterface, CheckCreateFromOCLSpirv) +{ + ASSERT_TRUE(PRef2 != nullptr); +} -TEST_F (TestDPPLSyclProgramInterface, CheckHasKernel) +TEST_F (TestDPPLSyclProgramInterface, CheckHasKernelOCLSource) { ASSERT_TRUE(DPPLProgram_HasKernel(PRef, "add")); ASSERT_TRUE(DPPLProgram_HasKernel(PRef, "axpy")); } -TEST_F (TestDPPLSyclProgramInterface, CheckGetKernel) +TEST_F (TestDPPLSyclProgramInterface, CheckHasKernelSpirvSource) +{ + ASSERT_TRUE(DPPLProgram_HasKernel(PRef, "add")); + ASSERT_TRUE(DPPLProgram_HasKernel(PRef, "axpy")); +} + +TEST_F (TestDPPLSyclProgramInterface, CheckGetKernelOCLSource) { auto AddKernel = DPPLProgram_GetKernel(PRef, "add"); auto AxpyKernel = DPPLProgram_GetKernel(PRef, "axpy"); auto syclQueue = reinterpret_cast(CurrQueueRef); - range<1> a_size{SIZE}; - std::array a, b, c; - - for (int i = 0; i a_device(a.data(), a_size); - buffer b_device(b.data(), a_size); - buffer c_device(c.data(), a_size); - buffer buffs[3] = {a_device, b_device, c_device}; - syclQueue->submit([&](handler& cgh) { - for (auto buff : buffs) { - auto arg = buff.get_access(cgh); - cgh.set_args(arg); - } - auto syclKernel = reinterpret_cast(AddKernel); - cgh.parallel_for(range<1>{SIZE}, *syclKernel); - }); - } + add_kernel_checker(syclQueue, AddKernel); + axpy_kernel_checker(syclQueue, AxpyKernel); - // Validate the data - for(auto i = 0ul; i < SIZE; ++i) { - EXPECT_EQ(c[i], i + i); - } + DPPLKernel_DeleteKernelRef(AddKernel); + DPPLKernel_DeleteKernelRef(AxpyKernel); +} - int d = 10; - { - buffer a_device(a.data(), a_size); - buffer b_device(b.data(), a_size); - buffer c_device(c.data(), a_size); - buffer buffs[3] = {a_device, b_device, c_device}; - syclQueue->submit([&](handler& cgh) { - for (auto i = 0ul; i < 3; ++i) { - auto arg = buffs[i].get_access(cgh); - cgh.set_arg(i, arg); - } - cgh.set_arg(3, d); - auto syclKernel = reinterpret_cast(AxpyKernel); - cgh.parallel_for(range<1>{SIZE}, *syclKernel); - }); - } +TEST_F (TestDPPLSyclProgramInterface, CheckGetKernelOCLSpirv) +{ + auto AddKernel = DPPLProgram_GetKernel(PRef2, "add"); + auto AxpyKernel = DPPLProgram_GetKernel(PRef2, "axpy"); + auto syclQueue = reinterpret_cast(CurrQueueRef); - // Validate the data - for(auto i = 0ul; i < SIZE; ++i) { - EXPECT_EQ(c[i], i + d*i); - } + add_kernel_checker(syclQueue, AddKernel); + axpy_kernel_checker(syclQueue, AxpyKernel); DPPLKernel_DeleteKernelRef(AddKernel); DPPLKernel_DeleteKernelRef(AxpyKernel); From 140d0aff66c3a651caf4949d4906ec10ae6134dc Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Mon, 21 Sep 2020 22:54:02 -0500 Subject: [PATCH 47/93] Fix name for the DPPLSyclKernelRef deleter. --- backends/include/dppl_sycl_kernel_interface.h | 2 +- backends/source/dppl_sycl_kernel_interface.cpp | 2 +- backends/tests/test_sycl_kernel_interface.cpp | 4 ++-- backends/tests/test_sycl_program_interface.cpp | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/backends/include/dppl_sycl_kernel_interface.h b/backends/include/dppl_sycl_kernel_interface.h index 65cf646697..64d2f97a30 100644 --- a/backends/include/dppl_sycl_kernel_interface.h +++ b/backends/include/dppl_sycl_kernel_interface.h @@ -69,6 +69,6 @@ DPPLKernel_GetNumArgs (__dppl_keep const DPPLSyclKernelRef KRef); */ DPPL_API void -DPPLKernel_DeleteKernelRef (__dppl_take DPPLSyclKernelRef KRef); +DPPLKernel_Delete (__dppl_take DPPLSyclKernelRef KRef); DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_kernel_interface.cpp b/backends/source/dppl_sycl_kernel_interface.cpp index 48f0b5d244..56fa9380cb 100644 --- a/backends/source/dppl_sycl_kernel_interface.cpp +++ b/backends/source/dppl_sycl_kernel_interface.cpp @@ -69,7 +69,7 @@ DPPLKernel_GetNumArgs (__dppl_keep const DPPLSyclKernelRef Kernel) } void -DPPLKernel_DeleteKernelRef (__dppl_take DPPLSyclKernelRef Kernel) +DPPLKernel_Delete (__dppl_take DPPLSyclKernelRef Kernel) { delete unwrap(Kernel); } diff --git a/backends/tests/test_sycl_kernel_interface.cpp b/backends/tests/test_sycl_kernel_interface.cpp index 985931c38d..8bb5f06d31 100644 --- a/backends/tests/test_sycl_kernel_interface.cpp +++ b/backends/tests/test_sycl_kernel_interface.cpp @@ -74,8 +74,8 @@ struct TestDPPLSyclKernelInterface : public ::testing::Test DPPLQueue_Delete(CurrQueueRef); DPPLContext_Delete(CurrCtxRef); DPPLProgram_Delete(PRef); - DPPLKernel_DeleteKernelRef(AddKernel); - DPPLKernel_DeleteKernelRef(AxpyKernel); + DPPLKernel_Delete(AddKernel); + DPPLKernel_Delete(AxpyKernel); } }; diff --git a/backends/tests/test_sycl_program_interface.cpp b/backends/tests/test_sycl_program_interface.cpp index 82ff21bf52..0cc696211e 100644 --- a/backends/tests/test_sycl_program_interface.cpp +++ b/backends/tests/test_sycl_program_interface.cpp @@ -186,8 +186,8 @@ TEST_F (TestDPPLSyclProgramInterface, CheckGetKernelOCLSource) add_kernel_checker(syclQueue, AddKernel); axpy_kernel_checker(syclQueue, AxpyKernel); - DPPLKernel_DeleteKernelRef(AddKernel); - DPPLKernel_DeleteKernelRef(AxpyKernel); + DPPLKernel_Delete(AddKernel); + DPPLKernel_Delete(AxpyKernel); } TEST_F (TestDPPLSyclProgramInterface, CheckGetKernelOCLSpirv) @@ -199,8 +199,8 @@ TEST_F (TestDPPLSyclProgramInterface, CheckGetKernelOCLSpirv) add_kernel_checker(syclQueue, AddKernel); axpy_kernel_checker(syclQueue, AxpyKernel); - DPPLKernel_DeleteKernelRef(AddKernel); - DPPLKernel_DeleteKernelRef(AxpyKernel); + DPPLKernel_Delete(AddKernel); + DPPLKernel_Delete(AxpyKernel); } int From 756d293ed114b01b8dc85f60c109e86e21f0ea9f Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Mon, 21 Sep 2020 22:54:22 -0500 Subject: [PATCH 48/93] Add classes for SyclProgram and SyclDevice. --- dppl/_sycl_core.pxd | 26 +++++++++++++++++++++++++ dppl/backend.pxd | 34 ++++++++++++++++++++++++++++---- dppl/sycl_core.pyx | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/dppl/_sycl_core.pxd b/dppl/_sycl_core.pxd index c4b8dd2ab1..186bd8f803 100644 --- a/dppl/_sycl_core.pxd +++ b/dppl/_sycl_core.pxd @@ -49,6 +49,32 @@ cdef class SyclDevice: cdef DPPLSyclDeviceRef get_device_ptr (self) +cdef class SyclKernel: + ''' Wraps a sycl::kernel object created from an OpenCL interoperability + kernel. + ''' + cdef DPPLSyclKernelRef kernel_ptr + + @staticmethod + cdef SyclKernel _create (DPPLSyclKernelRef kref) + + +cdef class SyclProgram: + ''' Wraps a sycl::program object created from an OpenCL interoperability + program. + + SyclProgram exposes the C API from dppl_sycl_program_interface.h. A + SyclProgram can be created from either a source string or a SPIR-V + binary file. + ''' + cdef DPPLSyclProgramRef program_ptr + + @staticmethod + cdef SyclProgram _create (DPPLSyclProgramRef pref) + cdef DPPLSyclProgramRef get_program_ptr (self) + cpdef SyclKernel get_sycl_kernel(self, kernel_name) + + cdef class SyclQueue: ''' Wrapper class for a Sycl queue. ''' diff --git a/dppl/backend.pxd b/dppl/backend.pxd index aebe0dbe59..e43d228cf2 100644 --- a/dppl/backend.pxd +++ b/dppl/backend.pxd @@ -32,14 +32,18 @@ cdef extern from "dppl_utils.h": cdef extern from "dppl_sycl_types.h": cdef struct DPPLOpaqueSyclContext - cdef struct DPPLOpaqueSyclQueue cdef struct DPPLOpaqueSyclDevice + cdef struct DPPLOpaqueSyclKernel + cdef struct DPPLOpaqueSyclProgram + cdef struct DPPLOpaqueSyclQueue cdef struct DPPLOpaqueSyclUSM ctypedef DPPLOpaqueSyclContext* DPPLSyclContextRef - ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef - ctypedef DPPLOpaqueSyclDevice* DPPLSyclDeviceRef - ctypedef DPPLOpaqueSyclUSM* DPPLSyclUSMRef + ctypedef DPPLOpaqueSyclDevice* DPPLSyclDeviceRef + ctypedef DPPLOpaqueSyclKernel* DPPLSyclKernelRef + ctypedef DPPLOpaqueSyclProgram* DPPLSyclProgramRef + ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef + ctypedef DPPLOpaqueSyclUSM* DPPLSyclUSMRef cdef extern from "dppl_sycl_context_interface.h": @@ -63,11 +67,33 @@ cdef extern from "dppl_sycl_device_interface.h": except + +cdef extern from "dppl_sycl_kernel_interface.h": + cdef const char* DPPLKernel_GetFunctionName (const DPPLSyclKernelRef KRef) + cdef size_t DPPLKernel_GetNumArgs (const DPPLSyclKernelRef KRef) + cdef void DPPLKernel_Delete (DPPLSyclKernelRef KRef) + + cdef extern from "dppl_sycl_platform_interface.h": cdef size_t DPPLPlatform_GetNumPlatforms () cdef void DPPLPlatform_DumpInfo () +cdef extern from "dppl_sycl_program_interface.h": + cdef DPPLSyclProgramRef DPPLProgram_CreateFromOCLSpirv ( \ + const DPPLSyclContextRef Ctx, \ + const void *IL, \ + size_t Length) + cdef DPPLSyclProgramRef DPPLProgram_CreateFromOCLSource ( \ + const DPPLSyclContextRef Ctx, \ + const char* Source, \ + const char* CompileOpts) + cdef DPPLSyclKernelRef DPPLProgram_GetKernel (DPPLSyclProgramRef PRef, \ + const char *KernelName) + cdef bool DPPLProgram_HasKernel (DPPLSyclProgramRef PRef, \ + const char *KernelName) + cdef void DPPLProgram_Delete (DPPLSyclProgramRef PRef) + + cdef extern from "dppl_sycl_queue_interface.h": cdef void DPPLQueue_Delete (DPPLSyclQueueRef QRef) except + cdef DPPLSyclContextRef DPPLQueue_GetContext (const DPPLSyclQueueRef Q) \ diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index daad6796a7..f808c9b845 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -109,6 +109,53 @@ cdef class SyclDevice: ''' return self.device_ptr +cdef class SyclKernel: + ''' Wraps a sycl::kernel object created from an OpenCL interoperability + kernel. + ''' + + @staticmethod + cdef SyclKernel _create (DPPLSyclKernelRef kref): + cdef SyclKernel ret = SyclKernel.__new__(SyclKernel) + ret.kernel_ptr = kref + return ret + + def __dealloc__ (self): + DPPLKernel_Delete(self.kernel_ptr) + + +cdef class SyclProgram: + ''' Wraps a sycl::program object created from an OpenCL interoperability + program. + + SyclProgram exposes the C API from dppl_sycl_program_interface.h. A + SyclProgram can be created from either a source string or a SPIR-V + binary file. + ''' + + @staticmethod + cdef SyclProgram _create (DPPLSyclProgramRef pref): + cdef SyclProgram ret = SyclProgram.__new__(SyclProgram) + ret.program_ptr = pref + return ret + + def __dealloc__ (self): + DPPLProgram_Delete(self.program_ptr) + + cdef DPPLSyclProgramRef get_program_ptr (self): + return self.program_ptr + + cpdef SyclKernel get_sycl_kernel(self, kernel_name): + if isinstance(kernel_name, unicode): + kernel_name = kernel_name + return SyclKernel._create(DPPLProgram_GetKernel(self.program_ptr, + kernel_name)) + else: + TypeError("Expected kernel_name to be a string") + + def has_sycl_kernel(self, kernel_name): + return DPPLProgram_HasKernel(self.program_ptr, kernel_name) + cdef class SyclQueue: ''' Wrapper class for a Sycl queue. From 3802f53f0d0a6defa26cc7f21cb29f99d6e669e2 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 22 Sep 2020 12:52:38 -0500 Subject: [PATCH 49/93] Rename the CString deleter to follow convention in rest of the library. --- backends/include/dppl_utils.h | 2 +- backends/source/dppl_utils.cpp | 2 +- dppl/_sycl_core.pxd | 2 ++ dppl/backend.pxd | 4 +++- dppl/sycl_core.pyx | 6 +++--- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/backends/include/dppl_utils.h b/backends/include/dppl_utils.h index 89071f19f9..3a19bf3e45 100644 --- a/backends/include/dppl_utils.h +++ b/backends/include/dppl_utils.h @@ -36,6 +36,6 @@ DPPL_C_EXTERN_C_BEGIN * @param str C string to be deleted */ DPPL_API -void DPPLDeleteCString (__dppl_take const char* str); +void DPPLCString_Delete (__dppl_take const char* str); DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_utils.cpp b/backends/source/dppl_utils.cpp index 39006153ae..4c0e9857dc 100644 --- a/backends/source/dppl_utils.cpp +++ b/backends/source/dppl_utils.cpp @@ -25,7 +25,7 @@ #include "dppl_utils.h" -void DPPLDeleteCString (__dppl_take const char* str) +void DPPLCString_Delete (__dppl_take const char* str) { delete str; } diff --git a/dppl/_sycl_core.pxd b/dppl/_sycl_core.pxd index c4b8dd2ab1..cb0c238318 100644 --- a/dppl/_sycl_core.pxd +++ b/dppl/_sycl_core.pxd @@ -22,6 +22,8 @@ ## This file defines the Cython interface for the Sycl API of PyDPPL. ## ##===----------------------------------------------------------------------===## +# distutils: language = c++ +# cython: language_level=3 from .backend cimport * diff --git a/dppl/backend.pxd b/dppl/backend.pxd index aebe0dbe59..3584cf2e28 100644 --- a/dppl/backend.pxd +++ b/dppl/backend.pxd @@ -22,12 +22,14 @@ ## This file defines the Cython interface for the backend API of PyDPPL. ## ##===----------------------------------------------------------------------===## +# distutils: language = c++ +# cython: language_level=3 from libcpp cimport bool cdef extern from "dppl_utils.h": - cdef void DPPLDeleteCString (const char *str) + cdef void DPPLCString_Delete (const char *str) cdef extern from "dppl_sycl_types.h": diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index daad6796a7..9b910e6b5d 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -77,9 +77,9 @@ cdef class SyclDevice: def __dealloc__ (self): DPPLDevice_Delete(self.device_ptr) - DPPLDeleteCString(self.device_name) - DPPLDeleteCString(self.vendor_name) - DPPLDeleteCString(self.driver_version) + DPPLCString_Delete(self.device_name) + DPPLCString_Delete(self.vendor_name) + DPPLCString_Delete(self.driver_version) def dump_device_info (self): ''' Print information about the SYCL device. From ead3c7c68ff9343293065cf3dd73235b3147cc9c Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 22 Sep 2020 13:09:42 -0500 Subject: [PATCH 50/93] Update function name. --- backends/tests/test_sycl_kernel_interface.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backends/tests/test_sycl_kernel_interface.cpp b/backends/tests/test_sycl_kernel_interface.cpp index 8bb5f06d31..82f5b583e5 100644 --- a/backends/tests/test_sycl_kernel_interface.cpp +++ b/backends/tests/test_sycl_kernel_interface.cpp @@ -86,8 +86,8 @@ TEST_F (TestDPPLSyclKernelInterface, CheckGetFunctionName) auto fnName2 = DPPLKernel_GetFunctionName(AxpyKernel); ASSERT_STREQ("add", fnName1); ASSERT_STREQ("axpy", fnName2); - DPPLDeleteCString(fnName1); - DPPLDeleteCString(fnName2); + DPPLCString_Delete(fnName1); + DPPLCString_Delete(fnName2); } TEST_F (TestDPPLSyclKernelInterface, CheckGetNumArgs) From eb0de507e5e2f5d87dccd3895a3c4d89959f24db Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 22 Sep 2020 14:10:26 -0500 Subject: [PATCH 51/93] Add Cython wrapper functions to create sycl programs. --- dppl/sycl_core.pyx | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index a7b05918a0..bcce79aaaa 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -268,6 +268,63 @@ has_sycl_platforms = _qmgr.has_sycl_platforms set_default_queue = _qmgr.set_default_queue is_in_dppl_ctxt = _qmgr.is_in_dppl_ctxt + +def create_program_from_source (SyclQueue q, unicode source, unicode copts=""): + ''' Creates a Sycl interoperability program from an OpenCL source string. + + We use the DPPLProgram_CreateFromOCLSource() C API function to create + a Sycl progrma from an OpenCL source program that can contain multiple + kernels. + + Parameters: + q (SyclQueue) : The SyclQueue object wraps the Sycl device for + which the program will be built. + source (unicode): Source string for an OpenCL program. + copts (unicode) : Optional compilation flags that will be used + when compiling the program. + + Returns: + program (SyclProgram): A SyclProgram object wrapping the + syc::program returned by the C API. + ''' + + cdef DPPLSyclProgramRef Pref + + cdef bytes bSrc = source.encode('utf8') + cdef bytes bCOpts = copts.encode('utf8') + cdef const char *Src = bSrc + cdef const char *COpts = bCOpts + cdef DPPLSyclContextRef CRef = q.get_sycl_context().get_context_ref() + Pref = DPPLProgram_CreateFromOCLSource(CRef, Src, COpts) + + return SyclProgram._create(Pref) + + +def create_program_from_spirv (SyclQueue q, char[:] IL): + ''' Creates a Sycl interoperability program from an SPIR-V binary. + + We use the DPPLProgram_CreateFromOCLSpirv() C API function to create + a Sycl progrma from an compiled SPIR-V binary file. + + Parameters: + q (SyclQueue): The SyclQueue object wraps the Sycl device for + which the program will be built. + IL (char[:]) : SPIR-V binary IL file for an OpenCL program. + + Returns: + program (SyclProgram): A SyclProgram object wrapping the + syc::program returned by the C API. + ''' + + cdef DPPLSyclProgramRef Pref + cdef bytes bIL = IL.data + cdef const void *spirvIL = bIL + cdef DPPLSyclContextRef CRef = q.get_sycl_context().get_context_ref() + Pref = DPPLProgram_CreateFromOCLSpirv(CRef, spirvIL, len(IL)) + + return SyclProgram._create(Pref) + + from contextlib import contextmanager @contextmanager From f50cda9aa217b2a41c8b0260a7a82ece571b3b25 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 22 Sep 2020 14:27:43 -0500 Subject: [PATCH 52/93] Expose in Cython some more DPPLKernel functions. --- dppl/_sycl_core.pxd | 1 + dppl/sycl_core.pyx | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/dppl/_sycl_core.pxd b/dppl/_sycl_core.pxd index a0e0afedaf..18e0246dc4 100644 --- a/dppl/_sycl_core.pxd +++ b/dppl/_sycl_core.pxd @@ -56,6 +56,7 @@ cdef class SyclKernel: kernel. ''' cdef DPPLSyclKernelRef kernel_ptr + cdef DPPLSyclKernelRef get_kernel_ptr (self) @staticmethod cdef SyclKernel _create (DPPLSyclKernelRef kref) diff --git a/dppl/sycl_core.pyx b/dppl/sycl_core.pyx index bcce79aaaa..45e546d3d1 100644 --- a/dppl/sycl_core.pyx +++ b/dppl/sycl_core.pyx @@ -122,6 +122,22 @@ cdef class SyclKernel: def __dealloc__ (self): DPPLKernel_Delete(self.kernel_ptr) + DPPLCString_Delete(self.function_name) + + def get_function_name (self): + ''' Returns the name of the Kernel function. + ''' + return self.function_name + + def get_num_args (self): + ''' Returns the number of arguments for this kernel function. + ''' + return DPPLKernel_GetNumArgs(self.kernel_ptr) + + cdef DPPLSyclKernelRef get_kernel_ptr (self): + ''' Returns the DPPLSyclKernelRef pointer for this SyclKernel. + ''' + return self.kernel_ptr cdef class SyclProgram: From ec2be8a40fddf0549d9b80c2a86b4973cc6d2e08 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 22 Sep 2020 16:37:44 -0500 Subject: [PATCH 53/93] Fix leftover PyDPPL use, and add/update copyright notices. --- dpctl/__init__.py | 56 ++++++++++++++++++++++---------------------- dpctl/_memory.pyx | 30 ++++++++++++++++++++++++ dpctl/_sycl_core.pxd | 8 ++++--- dpctl/backend.pxd | 8 ++++--- dpctl/ocldrv.py | 2 +- dpctl/opencl_core.py | 4 ++-- dpctl/sycl_core.pyx | 6 ++--- 7 files changed, 74 insertions(+), 40 deletions(-) diff --git a/dpctl/__init__.py b/dpctl/__init__.py index 3412040d2d..b18ec656c1 100644 --- a/dpctl/__init__.py +++ b/dpctl/__init__.py @@ -1,43 +1,43 @@ +##===--------------- _memory.pyx - dpctl interface ------*- Cython -*------===## +## +## Data Parallel Control (dpctl) +## +## Copyright 2020 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 top-level dpctl module. +## +##===----------------------------------------------------------------------===## ''' - Python Data Parallel Processing Library (PyDPPL) + Data Parallel Control (dpctl) - PyDPPL provides a lightweight Python abstraction over DPC++/SYCL and + Dpctl provides a lightweight Python abstraction over DPC++/SYCL and OpenCL runtime objects. The DPC++ runtime wrapper objects can be accessed by importing dpctl. The OpenCL runtime wrapper objects can be accessed by importing dpctl.ocldrv. The library is in an early-beta stage of development and not yet ready for production usage. - PyDPPL's intended usage is as a common SYCL interoperability layer for + Dpctl's intended usage is as a common SYCL interoperability layer for different Python libraries and applications. The OpenCL support inside PyDPPL is slated to be deprecated and then removed in future releases of the library. Currently, only a small subset of DPC++ runtime objects are exposed - through the dpctl module. The main API classes inside the dpctl module are: - - Runtime: The class stores a global SYCL queue and a stack of - currently activated queues. Runtime provides a special getter - method to retrieve the currently activated SYCL queue - as a Py_capsule. - - A single global thread local instance of the Runtime class - is created on loading the dpctl module for the first time. - - DeviceArray: A DeviceArray object encapsulates a one-dimensional - cl::sycl::buffer object. A DeviceArray object can be - created using a NumPy ndarray. The initial implementation - of DeviceArray follows NumPy's recommended design to create - a custom array container. DeviceArray does not implement - the __array_function__ and the __array_ufunc__ interfaces. - Therefore, DeviceArray does not support NumPy Universal - functions (ufuncs). The design decision to not support - ufuncs can be revisited later if we have a need for such - functionality. For the time being, the class is only meant - as a data exchange format between Python libraries that - use SYCL. - - Global data members: - runtime - An instance of the Runtime class. + through the dpctl module. The main API classes are defined in the _sycl_core.pyx file. Please use `pydoc dpctl._sycl_core` to look at the current API for dpctl. diff --git a/dpctl/_memory.pyx b/dpctl/_memory.pyx index c790ea17ce..cccf9a2705 100644 --- a/dpctl/_memory.pyx +++ b/dpctl/_memory.pyx @@ -1,3 +1,33 @@ +##===--------------- _memory.pyx - dpctl interface ------*- Cython -*------===## +## +## Data Parallel Control (dpctl) +## +## Copyright 2020 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 Python buffer protocol using Sycl USM shared and host +## allocators. The USM device allocator is also exposed through this module for +## use in other Python modules. +## +##===----------------------------------------------------------------------===## + +# distutils: language = c++ +# cython: language_level=3 + import dpctl from dpctl.backend cimport * from ._sycl_core cimport SyclContext, SyclQueue diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index cb0c238318..6f0264e770 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -1,6 +1,6 @@ -##===------------- sycl_core.pxd - DPPL interface ------*- Cython -*-------===## +##===------------- sycl_core.pxd - dpctl interface ------*- Cython -*------===## ## -## Python Data Parallel Processing Library (PyDPPL) +## Data Parallel Control (dpctl) ## ## Copyright 2020 Intel Corporation ## @@ -19,9 +19,11 @@ ##===----------------------------------------------------------------------===## ## ## \file -## This file defines the Cython interface for the Sycl API of PyDPPL. +## This file declares the extension types and functions for the Cython API +## implemented in sycl_core.pyx. ## ##===----------------------------------------------------------------------===## + # distutils: language = c++ # cython: language_level=3 diff --git a/dpctl/backend.pxd b/dpctl/backend.pxd index 3584cf2e28..18667a1ef0 100644 --- a/dpctl/backend.pxd +++ b/dpctl/backend.pxd @@ -1,6 +1,6 @@ -##===------------- backend.pyx - DPPL interface ------*- Cython -*-------===## +##===------------- backend.pyx - dpctl interface ------*- Cython -*--------===## ## -## Python Data Parallel Processing Library (PyDPPL) +## Data Parallel Control (dpctl) ## ## Copyright 2020 Intel Corporation ## @@ -19,9 +19,11 @@ ##===----------------------------------------------------------------------===## ## ## \file -## This file defines the Cython interface for the backend API of PyDPPL. +## This file defines the Cython extern types for the functions and opaque data +## types defined by dpctl's C API. ## ##===----------------------------------------------------------------------===## + # distutils: language = c++ # cython: language_level=3 diff --git a/dpctl/ocldrv.py b/dpctl/ocldrv.py index 904a933568..8c76d2e222 100644 --- a/dpctl/ocldrv.py +++ b/dpctl/ocldrv.py @@ -1,6 +1,6 @@ ##===---------- ocldrv.py - dpctl.ocldrv interface -----*- Python -*-----===## ## -## Python Data Parallel Processing Library (PyDPPL) +## Data Parallel Control (dpctl) ## ## Copyright 2020 Intel Corporation ## diff --git a/dpctl/opencl_core.py b/dpctl/opencl_core.py index 327de08122..cef6b9bcc5 100644 --- a/dpctl/opencl_core.py +++ b/dpctl/opencl_core.py @@ -1,6 +1,6 @@ -##===--------- opencl_core.py - dpctl.ocldrv interface -----*- Python -*----===## +##===--------- opencl_core.py - dpctl.ocldrv interface -----*- Python -*---===## ## -## Python Data Parallel Processing Library (PyDPPL) +## Data paraller Control (dpctl) ## ## Copyright 2020 Intel Corporation ## diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index a74917bb62..f1e01dc7b5 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -1,6 +1,6 @@ -##===------------- sycl_core.pyx - DPPL interface ------*- Cython -*-------===## +##===------------- sycl_core.pyx - dpctl interface ------*- Cython -*------===## ## -## Python Data Parallel Processing Library (PyDPPL) +## Data Parallel Control (dpctl) ## ## Copyright 2020 Intel Corporation ## @@ -19,7 +19,7 @@ ##===----------------------------------------------------------------------===## ## ## \file -## This file implements the Cython interface for the Sycl API of PyDPPL. +## This file implements a sub-set of Sycl's interface using dpctl's CAPI. ## ##===----------------------------------------------------------------------===## From 087104135a1225efd9939d0eb33bfcd93de2088e Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 22 Sep 2020 18:57:41 -0500 Subject: [PATCH 54/93] Reorganize tests and minor fixes to change dppl to dpctl. --- dpctl/sycl_core.pyx | 4 +- dpctl/tests/__init__.py | 27 ++++++++++- dpctl/tests/dppl_tests/__init__.py | 2 - dpctl/tests/test_dump_functions.py | 47 ++++++++++++------- .../test_sycl_queue_manager.py | 31 ++++++------ dpctl/tests/{dppl_tests => }/test_sycl_usm.py | 11 ++++- 6 files changed, 82 insertions(+), 40 deletions(-) delete mode 100644 dpctl/tests/dppl_tests/__init__.py rename dpctl/tests/{dppl_tests => }/test_sycl_queue_manager.py (81%) rename dpctl/tests/{dppl_tests => }/test_sycl_usm.py (92%) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index f1e01dc7b5..6055a777a5 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -199,7 +199,7 @@ cdef class _SyclQueueManager: ''' DPPLPlatform_DumpInfo() - def is_in_dppl_ctxt (self): + def is_in_dpctl_ctxt (self): cdef size_t num = DPPLQueueMgr_GetNumActivatedQueues() if num: return True @@ -219,7 +219,7 @@ has_cpu_queues = _qmgr.has_cpu_queues has_gpu_queues = _qmgr.has_gpu_queues has_sycl_platforms = _qmgr.has_sycl_platforms set_default_queue = _qmgr.set_default_queue -is_in_dppl_ctxt = _qmgr.is_in_dppl_ctxt +is_in_dpctl_ctxt = _qmgr.is_in_dpctl_ctxt from contextlib import contextmanager diff --git a/dpctl/tests/__init__.py b/dpctl/tests/__init__.py index 6bd91b208f..396f0fc154 100644 --- a/dpctl/tests/__init__.py +++ b/dpctl/tests/__init__.py @@ -1,2 +1,27 @@ +##===-------- tests/dpctl_tests/__init__.py - dpctl ------*- Python -*----===## +## +## Data Parallel Control (dpctl) +## +## Copyright 2020 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 +## Top-level module of all dpctl Python unit test cases. +##===----------------------------------------------------------------------===## + +from .test_sycl_queue_manager import * +from .test_sycl_usm import * from .test_dump_functions import * -from .dppl_tests import * diff --git a/dpctl/tests/dppl_tests/__init__.py b/dpctl/tests/dppl_tests/__init__.py deleted file mode 100644 index dd992a0db9..0000000000 --- a/dpctl/tests/dppl_tests/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from .test_sycl_queue_manager import * -from .test_sycl_usm import * diff --git a/dpctl/tests/test_dump_functions.py b/dpctl/tests/test_dump_functions.py index a925a22ba6..b097616122 100644 --- a/dpctl/tests/test_dump_functions.py +++ b/dpctl/tests/test_dump_functions.py @@ -1,18 +1,26 @@ -#******************************************************************************* -# Copyright 2020 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. -#******************************************************************************/ +##===---------- test_sycl_queue_manager.py - dpctl -------*- Python -*----===## +## +## Data Parallel Control (dpctl) +## +## Copyright 2020 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 +### A basic unit test to verify that dpct and dpct.ocldrv exist. +##===----------------------------------------------------------------------===## import unittest @@ -22,19 +30,24 @@ class TestDumpMethods(unittest.TestCase): - def test_dppl_dump (self): + def test_dpctl_dump (self): try: dpctl.dump() except Exception: self.fail("Encountered an exception inside dump().") - def test_dppl_dump_device_info (self): + def test_dpctl_dump_device_info (self): q = dpctl.get_current_queue() try: q.get_sycl_device().dump_device_info() except Exception: self.fail("Encountered an exception inside dump_device_info().") + def test_dpctl_ocldrv_dump (self): + try: + dpctl.ocldrv.runtime.dump() + except Exception: + self.fail("Encountered an exception inside dump_device_info().") if __name__ == '__main__': unittest.main() diff --git a/dpctl/tests/dppl_tests/test_sycl_queue_manager.py b/dpctl/tests/test_sycl_queue_manager.py similarity index 81% rename from dpctl/tests/dppl_tests/test_sycl_queue_manager.py rename to dpctl/tests/test_sycl_queue_manager.py index 5763c18fe7..76df6e3297 100644 --- a/dpctl/tests/dppl_tests/test_sycl_queue_manager.py +++ b/dpctl/tests/test_sycl_queue_manager.py @@ -1,6 +1,6 @@ -##===---------- test_sycl_queue_manager.py - dpctl -------*- Python -*-----===## +##===---------- test_sycl_queue_manager.py - dpctl -------*- Python -*----===## ## -## Python Data Parallel Processing Library (PyDPPL) +## Data Parallel Control (dpctl) ## ## Copyright 2020 Intel Corporation ## @@ -19,8 +19,7 @@ ##===----------------------------------------------------------------------===## ### ### \file -### This file has unit test cases to for the SyclQueueManager class -### in sycl_core.pyx. +### Defines unit test cases for the SyclQueueManager class in sycl_core.pyx. ##===----------------------------------------------------------------------===## import dpctl @@ -28,19 +27,19 @@ class TestGetNumPlatforms (unittest.TestCase): @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") - def test_dppl_get_num_platforms (self): + def test_dpctl_get_num_platforms (self): if(dpctl.has_sycl_platforms): self.assertGreaterEqual(dpctl.get_num_platforms(), 1) @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") class TestDumpMethods (unittest.TestCase): - def test_dppl_dump (self): + def test_dpctl_dump (self): try: dpctl.dump() except Exception: self.fail("Encountered an exception inside dump().") - def test_dppl_dump_device_info (self): + def test_dpctl_dump_device_info (self): q = dpctl.get_current_queue() try: q.get_sycl_device().dump_device_info() @@ -48,22 +47,22 @@ def test_dppl_dump_device_info (self): self.fail("Encountered an exception inside dump_device_info().") @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") -class TestDPPLIsInDPPLCtxt (unittest.TestCase): +class TestDPCTLIsInDPXTLCtxt (unittest.TestCase): - def test_is_in_dppl_ctxt_outside_device_ctxt (self): - self.assertFalse(dpctl.is_in_dppl_ctxt()) + def test_is_in_dpctl_ctxt_outside_device_ctxt (self): + self.assertFalse(dpctl.is_in_dpctl_ctxt()) - def test_is_in_dppl_ctxt_inside_device_ctxt (self): + def test_is_in_dpctl_ctxt_inside_device_ctxt (self): with dpctl.device_context(dpctl.device_type.gpu): - self.assertTrue(dpctl.is_in_dppl_ctxt()) + self.assertTrue(dpctl.is_in_dpctl_ctxt()) @unittest.skipIf(not dpctl.has_cpu_queues(), "No CPU platforms available") - def test_is_in_dppl_ctxt_inside_nested_device_ctxt (self): + def test_is_in_dpctl_ctxt_inside_nested_device_ctxt (self): with dpctl.device_context(dpctl.device_type.cpu): with dpctl.device_context(dpctl.device_type.gpu): - self.assertTrue(dpctl.is_in_dppl_ctxt()) - self.assertTrue(dpctl.is_in_dppl_ctxt()) - self.assertFalse(dpctl.is_in_dppl_ctxt()) + self.assertTrue(dpctl.is_in_dpctl_ctxt()) + self.assertTrue(dpctl.is_in_dpctl_ctxt()) + self.assertFalse(dpctl.is_in_dpctl_ctxt()) @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") class TestGetCurrentQueueInMultipleThreads (unittest.TestCase): diff --git a/dpctl/tests/dppl_tests/test_sycl_usm.py b/dpctl/tests/test_sycl_usm.py similarity index 92% rename from dpctl/tests/dppl_tests/test_sycl_usm.py rename to dpctl/tests/test_sycl_usm.py index cdcc95dcc7..52b70945a5 100644 --- a/dpctl/tests/dppl_tests/test_sycl_usm.py +++ b/dpctl/tests/test_sycl_usm.py @@ -1,6 +1,6 @@ -##===---------- test_sycl_queue_manager.py - dpctl -------*- Python -*-----===## +##===---------- test_sycl_queue_manager.py - dpctl -------*- Python -*----===## ## -## Python Data Parallel Processing Library (PyDPPL) +## Data Parallel Control (dpctl) ## ## Copyright 2020 Intel Corporation ## @@ -17,6 +17,10 @@ ## limitations under the License. ## ##===----------------------------------------------------------------------===## +## +## \file +## Defines unit test cases for the Memory classes in _memory.pyx. +##===----------------------------------------------------------------------===## import unittest import dpctl @@ -111,3 +115,6 @@ class TestMemoryUSMDevice(TestMemoryUSMBase, unittest.TestCase): MemoryUSMClass = MemoryUSMDevice usm_type = 'device' + +if __name__ == '__main__': + unittest.main() From b7f3a7e285dee6a04f451a8bfcdf21d32f8f8068 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 22 Sep 2020 19:05:55 -0500 Subject: [PATCH 55/93] Fix indentation. --- dpctl/tests/test_sycl_queue_manager.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dpctl/tests/test_sycl_queue_manager.py b/dpctl/tests/test_sycl_queue_manager.py index 76df6e3297..3fa177bb18 100644 --- a/dpctl/tests/test_sycl_queue_manager.py +++ b/dpctl/tests/test_sycl_queue_manager.py @@ -26,7 +26,8 @@ import unittest class TestGetNumPlatforms (unittest.TestCase): - @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") + @unittest.skipIf(not dpctl.has_sycl_platforms(), + "No SYCL platforms available") def test_dpctl_get_num_platforms (self): if(dpctl.has_sycl_platforms): self.assertGreaterEqual(dpctl.get_num_platforms(), 1) From 3138fe50d882bb032bfdc278ab74753d1f969a4a Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 22 Sep 2020 19:17:58 -0500 Subject: [PATCH 56/93] Change test class name. --- dpctl/tests/test_sycl_queue_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpctl/tests/test_sycl_queue_manager.py b/dpctl/tests/test_sycl_queue_manager.py index 3fa177bb18..04f6171f68 100644 --- a/dpctl/tests/test_sycl_queue_manager.py +++ b/dpctl/tests/test_sycl_queue_manager.py @@ -48,7 +48,7 @@ def test_dpctl_dump_device_info (self): self.fail("Encountered an exception inside dump_device_info().") @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") -class TestDPCTLIsInDPXTLCtxt (unittest.TestCase): +class TestIsInDPXTLCtxt (unittest.TestCase): def test_is_in_dpctl_ctxt_outside_device_ctxt (self): self.assertFalse(dpctl.is_in_dpctl_ctxt()) From 7f839354731478ae7815950f7e4d7239e2b3ecc9 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 22 Sep 2020 23:18:59 -0500 Subject: [PATCH 57/93] Use wrap/unwrap macros. --- backends/source/dppl_sycl_queue_interface.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index af3fd7a611..d85730690a 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -34,8 +34,9 @@ using namespace cl::sycl; namespace { // Create wrappers for C Binding types (see CBindingWrapping.h). - DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) - +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPPLSyclContextRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPPLSyclDeviceRef) } /* end of anonymous namespace */ __dppl_give DPPLSyclDeviceRef @@ -43,7 +44,7 @@ DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Device = new device(Q->get_device()); - return reinterpret_cast(Device); + return wrap(Device); } __dppl_give DPPLSyclContextRef @@ -51,7 +52,7 @@ DPPLQueue_GetContext (__dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Context = new context(Q->get_context()); - return reinterpret_cast(Context); + return wrap(Context); } /*! From a331de0b39a9f0f78cee358ac6a7387f3a52d110 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 23 Sep 2020 09:31:25 -0500 Subject: [PATCH 58/93] Fix names in docstrings. --- dpctl/__init__.py | 10 +++++----- dpctl/_memory.pyx | 4 ++-- dpctl/_sycl_core.pxd | 4 ++-- dpctl/backend.pxd | 4 ++-- dpctl/ocldrv.py | 6 +++--- dpctl/sycl_core.pyx | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/dpctl/__init__.py b/dpctl/__init__.py index b18ec656c1..0287c9783b 100644 --- a/dpctl/__init__.py +++ b/dpctl/__init__.py @@ -1,6 +1,6 @@ -##===--------------- _memory.pyx - dpctl interface ------*- Cython -*------===## +##===----------------- _memory.pyx - dpctl module -------*- Cython -*------===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## @@ -23,15 +23,15 @@ ## ##===----------------------------------------------------------------------===## ''' - Data Parallel Control (dpctl) + Data Parallel Control (dpCtl) - Dpctl provides a lightweight Python abstraction over DPC++/SYCL and + dpCtl provides a lightweight Python abstraction over DPC++/SYCL and OpenCL runtime objects. The DPC++ runtime wrapper objects can be accessed by importing dpctl. The OpenCL runtime wrapper objects can be accessed by importing dpctl.ocldrv. The library is in an early-beta stage of development and not yet ready for production usage. - Dpctl's intended usage is as a common SYCL interoperability layer for + dpCtl's intended usage is as a common SYCL interoperability layer for different Python libraries and applications. The OpenCL support inside PyDPPL is slated to be deprecated and then removed in future releases of the library. diff --git a/dpctl/_memory.pyx b/dpctl/_memory.pyx index cccf9a2705..3edd8732bc 100644 --- a/dpctl/_memory.pyx +++ b/dpctl/_memory.pyx @@ -1,6 +1,6 @@ -##===--------------- _memory.pyx - dpctl interface ------*- Cython -*------===## +##===--------------- _memory.pyx - dpctl module --------*- Cython -*-------===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index 6f0264e770..caa18814d8 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -1,6 +1,6 @@ -##===------------- sycl_core.pxd - dpctl interface ------*- Cython -*------===## +##===------------- sycl_core.pxd - dpctl module --------*- Cython -*-------===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## diff --git a/dpctl/backend.pxd b/dpctl/backend.pxd index 18667a1ef0..f15f51b529 100644 --- a/dpctl/backend.pxd +++ b/dpctl/backend.pxd @@ -1,6 +1,6 @@ -##===------------- backend.pyx - dpctl interface ------*- Cython -*--------===## +##===------------- backend.pyx - dpctl module -------*- Cython -*----------===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## diff --git a/dpctl/ocldrv.py b/dpctl/ocldrv.py index 8c76d2e222..82461069df 100644 --- a/dpctl/ocldrv.py +++ b/dpctl/ocldrv.py @@ -1,6 +1,6 @@ -##===---------- ocldrv.py - dpctl.ocldrv interface -----*- Python -*-----===## +##===------------- ocldrv.py - dpctl.ocldrv module ------*- Python -*------===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## @@ -20,7 +20,7 @@ ### ### \file ### This file exposes Python classes for different OpenCL classes that are -### exposed by the _dppl_binding CFFI extension module. +### exposed by the _opencl_core CFFI extension module. ##===----------------------------------------------------------------------===## ''' The dpctl.ocldrv module contains a set of Python wrapper classes for OpenCL objects. The module has wrappers for cl_context, cl_device, diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index f1e01dc7b5..5510309fcd 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -1,6 +1,6 @@ -##===------------- sycl_core.pyx - dpctl interface ------*- Cython -*------===## +##===------------- sycl_core.pyx - dpctl module -------*- Cython -*--------===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## From e9e37d08c9fe9404ef795355afc16bc68ca339ce Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 23 Sep 2020 09:41:53 -0500 Subject: [PATCH 59/93] Fix names etc. --- dpctl/tests/__init__.py | 2 +- dpctl/tests/test_dump_functions.py | 4 ++-- dpctl/tests/test_sycl_queue_manager.py | 6 +++--- dpctl/tests/test_sycl_usm.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/dpctl/tests/__init__.py b/dpctl/tests/__init__.py index 396f0fc154..ce80b2561c 100644 --- a/dpctl/tests/__init__.py +++ b/dpctl/tests/__init__.py @@ -1,6 +1,6 @@ ##===-------- tests/dpctl_tests/__init__.py - dpctl ------*- Python -*----===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## diff --git a/dpctl/tests/test_dump_functions.py b/dpctl/tests/test_dump_functions.py index b097616122..52b41edace 100644 --- a/dpctl/tests/test_dump_functions.py +++ b/dpctl/tests/test_dump_functions.py @@ -1,6 +1,6 @@ ##===---------- test_sycl_queue_manager.py - dpctl -------*- Python -*----===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## @@ -19,7 +19,7 @@ ##===----------------------------------------------------------------------===## ### ### \file -### A basic unit test to verify that dpct and dpct.ocldrv exist. +### A basic unit test to verify that dpctl and dpct.ocldrv exist. ##===----------------------------------------------------------------------===## import unittest diff --git a/dpctl/tests/test_sycl_queue_manager.py b/dpctl/tests/test_sycl_queue_manager.py index 04f6171f68..4bd3b0acad 100644 --- a/dpctl/tests/test_sycl_queue_manager.py +++ b/dpctl/tests/test_sycl_queue_manager.py @@ -1,6 +1,6 @@ ##===---------- test_sycl_queue_manager.py - dpctl -------*- Python -*----===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## @@ -27,7 +27,7 @@ class TestGetNumPlatforms (unittest.TestCase): @unittest.skipIf(not dpctl.has_sycl_platforms(), - "No SYCL platforms available") + "No SYCL platforms available") def test_dpctl_get_num_platforms (self): if(dpctl.has_sycl_platforms): self.assertGreaterEqual(dpctl.get_num_platforms(), 1) @@ -48,7 +48,7 @@ def test_dpctl_dump_device_info (self): self.fail("Encountered an exception inside dump_device_info().") @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") -class TestIsInDPXTLCtxt (unittest.TestCase): +class TestIsIndpCtlCtxt (unittest.TestCase): def test_is_in_dpctl_ctxt_outside_device_ctxt (self): self.assertFalse(dpctl.is_in_dpctl_ctxt()) diff --git a/dpctl/tests/test_sycl_usm.py b/dpctl/tests/test_sycl_usm.py index 52b70945a5..3737a01a4c 100644 --- a/dpctl/tests/test_sycl_usm.py +++ b/dpctl/tests/test_sycl_usm.py @@ -1,6 +1,6 @@ ##===---------- test_sycl_queue_manager.py - dpctl -------*- Python -*----===## ## -## Data Parallel Control (dpctl) +## Data Parallel Control (dpCtl) ## ## Copyright 2020 Intel Corporation ## From 5a71d4f15c892cfb908205390d1a1a0105aba9e2 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 23 Sep 2020 09:51:27 -0500 Subject: [PATCH 60/93] Rename function from is_in_dpctl_ctxt to is_in_device_context. --- dpctl/sycl_core.pyx | 4 ++-- dpctl/tests/test_sycl_queue_manager.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index b62e32a797..15f51c4928 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -199,7 +199,7 @@ cdef class _SyclQueueManager: ''' DPPLPlatform_DumpInfo() - def is_in_dpctl_ctxt (self): + def is_in_device_context (self): cdef size_t num = DPPLQueueMgr_GetNumActivatedQueues() if num: return True @@ -219,7 +219,7 @@ has_cpu_queues = _qmgr.has_cpu_queues has_gpu_queues = _qmgr.has_gpu_queues has_sycl_platforms = _qmgr.has_sycl_platforms set_default_queue = _qmgr.set_default_queue -is_in_dpctl_ctxt = _qmgr.is_in_dpctl_ctxt +is_in_device_context = _qmgr.is_in_device_context from contextlib import contextmanager diff --git a/dpctl/tests/test_sycl_queue_manager.py b/dpctl/tests/test_sycl_queue_manager.py index 4bd3b0acad..03ba271854 100644 --- a/dpctl/tests/test_sycl_queue_manager.py +++ b/dpctl/tests/test_sycl_queue_manager.py @@ -48,22 +48,22 @@ def test_dpctl_dump_device_info (self): self.fail("Encountered an exception inside dump_device_info().") @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") -class TestIsIndpCtlCtxt (unittest.TestCase): +class TestIsInDeviceContext (unittest.TestCase): def test_is_in_dpctl_ctxt_outside_device_ctxt (self): - self.assertFalse(dpctl.is_in_dpctl_ctxt()) + self.assertFalse(dpctl.is_in_device_context()) def test_is_in_dpctl_ctxt_inside_device_ctxt (self): with dpctl.device_context(dpctl.device_type.gpu): - self.assertTrue(dpctl.is_in_dpctl_ctxt()) + self.assertTrue(dpctl.is_in_device_context()) @unittest.skipIf(not dpctl.has_cpu_queues(), "No CPU platforms available") def test_is_in_dpctl_ctxt_inside_nested_device_ctxt (self): with dpctl.device_context(dpctl.device_type.cpu): with dpctl.device_context(dpctl.device_type.gpu): - self.assertTrue(dpctl.is_in_dpctl_ctxt()) - self.assertTrue(dpctl.is_in_dpctl_ctxt()) - self.assertFalse(dpctl.is_in_dpctl_ctxt()) + self.assertTrue(dpctl.is_in_device_context()) + self.assertTrue(dpctl.is_in_device_context()) + self.assertFalse(dpctl.is_in_device_context()) @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") class TestGetCurrentQueueInMultipleThreads (unittest.TestCase): From 138f934b30172b49c80a69d4a5fa9115ace8911a Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 23 Sep 2020 10:57:08 -0500 Subject: [PATCH 61/93] Add queue submit and wait API. --- backends/CMakeLists.txt | 1 + backends/include/dppl_sycl_event_interface.h | 56 ++++++++ backends/include/dppl_sycl_queue_interface.h | 49 ++++++- backends/include/dppl_sycl_types.h | 37 +++++ backends/source/dppl_sycl_event_interface.cpp | 52 +++++++ backends/source/dppl_sycl_queue_interface.cpp | 129 +++++++++++++++++- 6 files changed, 315 insertions(+), 9 deletions(-) create mode 100644 backends/include/dppl_sycl_event_interface.h create mode 100644 backends/source/dppl_sycl_event_interface.cpp diff --git a/backends/CMakeLists.txt b/backends/CMakeLists.txt index ca47176b7e..2bfc90b7df 100644 --- a/backends/CMakeLists.txt +++ b/backends/CMakeLists.txt @@ -98,6 +98,7 @@ add_library( SHARED source/dppl_sycl_context_interface.cpp source/dppl_sycl_device_interface.cpp + source/dppl_sycl_event_interface.cpp source/dppl_sycl_kernel_interface.cpp source/dppl_sycl_platform_interface.cpp source/dppl_sycl_program_interface.cpp diff --git a/backends/include/dppl_sycl_event_interface.h b/backends/include/dppl_sycl_event_interface.h new file mode 100644 index 0000000000..e5e34d7f4c --- /dev/null +++ b/backends/include/dppl_sycl_event_interface.h @@ -0,0 +1,56 @@ +//===--- dppl_sycl_event_interface.h - DPPL-SYCL interface ---*---C++ -*---===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 header declares a C API to a sub-set of the sycl::event interface. +/// +//===----------------------------------------------------------------------===// + +#pragma once + +#include "dppl_data_types.h" +#include "dppl_sycl_types.h" +#include "Support/DllExport.h" +#include "Support/ExternC.h" +#include "Support/MemOwnershipAttrs.h" + + +DPPL_C_EXTERN_C_BEGIN + +/*! + * @brief + * + * @param ERef My Param doc + * @return {return} My Param doc + */ +DPPL_API +void DPPLEvent_Wait (__dppl_keep DPPLSyclEventRef ERef); + +/*! + * @brief Deletes the DPPLSyclEventRef after casting it to a sycl::event. + * + * @param ERef An opaque DPPLSyclEventRef pointer that would be + * freed. + */ +DPPL_API +void +DPPLEvent_Delete (__dppl_take DPPLSyclEventRef ERef); + +DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index 7b9002e8de..22f6f0a974 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -35,6 +35,14 @@ DPPL_C_EXTERN_C_BEGIN +/*! + * @brief Delete the pointer after casting it to sycl::queue. + * + * @param QRef A DPPLSyclQueueRef pointer that gets deleted. + */ +DPPL_API +void DPPLQueue_Delete (__dppl_take DPPLSyclQueueRef QRef); + /*! * @brief Returns the Sycl context for the queue. * @@ -56,11 +64,46 @@ __dppl_give DPPLSyclDeviceRef DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef); /*! - * @brief Delete the pointer after casting it to sycl::queue. + * @brief Submits the kernel to the specified queue using give arguments. * - * @param QRef A DPPLSyclQueueRef pointer that gets deleted. + * A wrapper over sycl::queue.submit(). The function takes an OpenCL + * interoperability kernel, the kernel arguments, and a sycl queue as input + * arguments. The kernel arguments are passed in as an array of the + * DPPLKernelArg tagged union. + * + * \todo sycl::buffer arguments are not supported yet. + * + * @param KRef Opaque pointer to a OpenCL interoperability kernel + * wrapped inside a sycl::kernel. + * @param QRef Opaque pointer to the sycl::queue where the kernel + * will be enqueued. + * @param Args An array of the DPPLKernelArg tagged union type that + * represents the kernel arguments for the kernel. + * @param NArgs The number of kernel arguments (size of Args array). + * @param Range Array storing the range dimensions that can have a + * maximum size of three. Note the number of values + * in the array depends on the number of dimensions. + * @param NDims Number of dimensions in the range (size of Range). + * @return A opaque pointer to the sycl::event returned by the + * sycl::queue.submit() function. */ DPPL_API -void DPPLQueue_Delete (__dppl_take DPPLSyclQueueRef QRef); +DPPLSyclEventRef +DPPLQueue_Submit (__dppl_keep DPPLSyclKernelRef KRef, + __dppl_keep DPPLSyclQueueRef QRef, + __dppl_keep DPPLKernelArg *Args, + size_t NArgs, + size_t Range[3], + size_t NDims); + +/*! + * @brief Calls the sycl::queue.submit function to do a blocking wait on all + * enqueued tasks in the queue. + * + * @param QRef Opaque pointer to a sycl::queue. + */ +DPPL_API +void +DPPLQueue_Wait (__dppl_keep DPPLSyclQueueRef QRef); DPPL_C_EXTERN_C_END diff --git a/backends/include/dppl_sycl_types.h b/backends/include/dppl_sycl_types.h index e3ccf3d0a8..1af9574771 100644 --- a/backends/include/dppl_sycl_types.h +++ b/backends/include/dppl_sycl_types.h @@ -74,3 +74,40 @@ typedef struct DPPLOpaqueSyclQueue *DPPLSyclQueueRef; * @see sycl::usm */ typedef struct DPPLOpaqueSyclUSM *DPPLSyclUSMRef; + +/*! + * @brief Enum of currently support types for kernel arguments. + * + * \todo Add support for sycl::buffer + * + */ +enum DPPLArgTypes +{ + CHAR, + SIGNED_CHAR, + UNSIGNED_CHAR, + SHORT, + INT, + UNSIGNED_INT, + LONG, + UNSIGNED_LONG, + LONG_LONG, + UNSIGNED_LONG_LONG, + SIZE_T, + FLOAT, + DOUBLE, + LONG_DOUBLE, + VOID_PTR +}; + +/*! + * @brief Represents an abstract kernel argument that is passed through to an + * OpenCL kernel via Sycl. + * + */ +struct DPPLKernelArg +{ + enum DPPLArgTypes argType; + void *argVal; +}; + diff --git a/backends/source/dppl_sycl_event_interface.cpp b/backends/source/dppl_sycl_event_interface.cpp new file mode 100644 index 0000000000..69a739483f --- /dev/null +++ b/backends/source/dppl_sycl_event_interface.cpp @@ -0,0 +1,52 @@ +//===--- dppl_sycl_event_interface.cpp - DPPL-SYCL interface --*- C++ -*---===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 the data types and functions declared in +/// dppl_sycl_event_interface.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_sycl_event_interface.h" +#include "Support/CBindingWrapping.h" + +#include /* SYCL headers */ + +using namespace cl::sycl; + +namespace +{ +// Create wrappers for C Binding types (see CBindingWrapping.h) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(event, DPPLSyclEventRef) +} /* end of anonymous namespace */ + + +void DPPLEvent_Wait (__dppl_keep DPPLSyclEventRef ERef) +{ + // \todo How to handle errors? E.g. when ERef is null or not a valid event. + auto SyclEvent = unwrap(ERef); + SyclEvent->wait(); +} + +void +DPPLEvent_Delete (__dppl_take DPPLSyclEventRef ERef) +{ + delete unwrap(ERef); +} diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index d85730690a..beefd9a0d4 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -34,11 +34,88 @@ using namespace cl::sycl; namespace { // Create wrappers for C Binding types (see CBindingWrapping.h). -DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPPLSyclContextRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPPLSyclDeviceRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(event, DPPLSyclEventRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(kernel, DPPLSyclKernelRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) + +/*! + * @brief Set the kernel arg object + * + * @param cgh My Param doc + * @param Arg My Param doc + */ +bool set_kernel_arg (handler &cgh, __dppl_keep DPPLKernelArg Arg, size_t idx) +{ + bool arg_set = true; + + switch (Arg.argType) + { + case CHAR: + cgh.set_arg(idx, *(char*)Arg.argVal); + break; + case SIGNED_CHAR: + cgh.set_arg(idx, *(signed char*)Arg.argVal); + break; + case UNSIGNED_CHAR: + cgh.set_arg(idx, *(unsigned char*)Arg.argVal); + break; + case SHORT: + cgh.set_arg(idx, *(short*)Arg.argVal); + break; + case INT: + cgh.set_arg(idx, *(int*)Arg.argVal); + break; + case UNSIGNED_INT: + cgh.set_arg(idx, *(unsigned int*)Arg.argVal); + break; + case LONG: + cgh.set_arg(idx, *(long*)Arg.argVal); + break; + case UNSIGNED_LONG: + cgh.set_arg(idx, *(unsigned long*)Arg.argVal); + break; + case LONG_LONG: + cgh.set_arg(idx, *(long long*)Arg.argVal); + break; + case UNSIGNED_LONG_LONG: + cgh.set_arg(idx, *(unsigned long long*)Arg.argVal); + break; + case SIZE_T: + cgh.set_arg(idx, *(size_t*)Arg.argVal); + break; + case FLOAT: + cgh.set_arg(idx, *(float*)Arg.argVal); + break; + case DOUBLE: + cgh.set_arg(idx, *(double*)Arg.argVal); + break; + case LONG_DOUBLE: + cgh.set_arg(idx, *(long double*)Arg.argVal); + break; + case VOID_PTR: + cgh.set_arg(idx, Arg.argVal); + break; + default: + // \todo handle errors + arg_set = false; + std::cerr << "Kernel argument could not be created.\n"; + break; + } + return arg_set; +} + } /* end of anonymous namespace */ +/*! + * Delete the passed in pointer after verifying it points to a sycl::queue. + */ +void DPPLQueue_Delete (__dppl_take DPPLSyclQueueRef QRef) +{ + delete unwrap(QRef); +} + __dppl_give DPPLSyclDeviceRef DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef) { @@ -55,10 +132,50 @@ DPPLQueue_GetContext (__dppl_keep const DPPLSyclQueueRef QRef) return wrap(Context); } -/*! - * Delete the passed in pointer after verifying it points to a sycl::queue. - */ -void DPPLQueue_Delete (__dppl_take DPPLSyclQueueRef QRef) +__dppl_give DPPLSyclEventRef +DPPLQueue_Submit (__dppl_keep DPPLSyclKernelRef KRef, + __dppl_keep DPPLSyclQueueRef QRef, + __dppl_keep DPPLKernelArg *Args, + size_t NArgs, + size_t Range[3], + size_t NDims) { - delete unwrap(QRef); + auto Kernel = unwrap(KRef); + auto Queue = unwrap(QRef); + event e; + + e = Queue->submit([&](handler& cgh) { + for (auto i = 0ul; i < 4; ++i) { + // \todo add support for Sycl buffers + // \todo handle errors properly + if(!set_kernel_arg(cgh, Args[i], i)) + exit(1); + } + switch(NDims) + { + case 1: + cgh.parallel_for(range<1>{Range[0]}, *Kernel); + break; + case 2: + cgh.parallel_for(range<2>{Range[0], Range[1]}, *Kernel); + break; + case 3: + cgh.parallel_for(range<3>{Range[0], Range[1], Range[2]}, *Kernel); + break; + default: + // \todo handle the error + std::cerr << "Range cannot be greater than three dimensions.\n"; + exit(1); + } + }); + + return wrap(new event(e)); +} + +void +DPPLQueue_Wait (__dppl_keep DPPLSyclQueueRef QRef) +{ + // \todo what happens if the QRef is null or a pointer to a valid sycl queue + auto SyclQueue = unwrap(QRef); + SyclQueue->wait(); } From ada1d014ee83f94bc117aa9706d07e96e3801da4 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 23 Sep 2020 13:54:15 -0500 Subject: [PATCH 62/93] Add some temporary prints to instrument. --- .../source/dppl_sycl_program_interface.cpp | 8 ++- backends/source/dppl_sycl_queue_interface.cpp | 3 ++ dpctl/sycl_core.pyx | 2 + dpctl/tests/test_sycl_program.py | 51 +++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 dpctl/tests/test_sycl_program.py diff --git a/backends/source/dppl_sycl_program_interface.cpp b/backends/source/dppl_sycl_program_interface.cpp index 01d615a08b..f99134c879 100644 --- a/backends/source/dppl_sycl_program_interface.cpp +++ b/backends/source/dppl_sycl_program_interface.cpp @@ -110,9 +110,15 @@ DPPLProgram_CreateFromOCLSource (__dppl_keep const DPPLSyclContextRef Ctx, // \todo handle error message return nullptr; } + std::cout << "CTX address when passed to program creator: " << Ctx << std::endl; + std::cout << "Ref count in program create : " << + unwrap(Ctx)->get_info() << std::endl; + + queue q{default_selector()}; SyclCtx = unwrap(Ctx); - SyclProgram = new program(*SyclCtx); + auto syclContext = q.get_context(); + SyclProgram = new program(syclContext); std::string source = Source; if(CompileOpts) { diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index beefd9a0d4..68761c8af4 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -129,6 +129,9 @@ DPPLQueue_GetContext (__dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Context = new context(Q->get_context()); + std::cout << "Context Addr when created in DPPLQueue_GetContext " << Context << '\n'; + std::cout << "Ref count when first created : " << + Context->get_info() << '\n'; return wrap(Context); } diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 4db401152c..20557e2364 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -30,6 +30,7 @@ from __future__ import print_function from enum import Enum, auto import logging from dpctl.backend cimport * +from libc.stdio cimport printf _logger = logging.getLogger(__name__) @@ -59,6 +60,7 @@ cdef class SyclContext: DPPLContext_Delete(self.ctxt_ptr) cdef DPPLSyclContextRef get_context_ref (self): + printf("Address inside SyclContext %p\n",self.ctxt_ptr) return self.ctxt_ptr diff --git a/dpctl/tests/test_sycl_program.py b/dpctl/tests/test_sycl_program.py new file mode 100644 index 0000000000..fe45a196a3 --- /dev/null +++ b/dpctl/tests/test_sycl_program.py @@ -0,0 +1,51 @@ +##===------------- test_sycl_program.py - dpctl -------*- Python -*-------===## +## +## Data Parallel Control (dpctl) +## +## Copyright 2020 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 +## Defines unit test cases for the SyclProgram and SyclKernel classes defined +## in sycl_core.pyx. +##===----------------------------------------------------------------------===## + +import dpctl +import unittest + + +@unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") +class TestProgramFromOCSource (unittest.TestCase): + + def test_create_program_from_source (self): + q = dpctl.get_current_queue() + oclSrc = " \ + kernel void add(global int* a, global int* b, global int* c) { \ + size_t index = get_global_id(0); \ + c[index] = a[index] + b[index]; \ + } \ + kernel void axpy(global int* a, global int* b, global int* c, int d) { \ + size_t index = get_global_id(0); \ + c[index] = a[index] + d*b[index]; \ + }" + prog = dpctl.create_program_from_source(q,oclSrc) + # self.assertIsNotNone(prog) + + # self.assertTrue(prog.has_sycl_kernel("add")) + # self.assertTrue(prog.has_sycl_kernel("axpy")) + +if __name__ == '__main__': + unittest.main() From f96dc2648eba488b654e87aeda21bdcef02af238 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 23 Sep 2020 15:59:52 -0500 Subject: [PATCH 63/93] Fix program creation from source and add a test case. --- .../source/dppl_sycl_program_interface.cpp | 8 +- backends/source/dppl_sycl_queue_interface.cpp | 3 - dpctl/_sycl_core.pxd | 21 +++-- dpctl/sycl_core.pyx | 83 +++++++++---------- dpctl/tests/__init__.py | 4 +- dpctl/tests/test_sycl_program.py | 15 +++- 6 files changed, 68 insertions(+), 66 deletions(-) diff --git a/backends/source/dppl_sycl_program_interface.cpp b/backends/source/dppl_sycl_program_interface.cpp index f99134c879..01d615a08b 100644 --- a/backends/source/dppl_sycl_program_interface.cpp +++ b/backends/source/dppl_sycl_program_interface.cpp @@ -110,15 +110,9 @@ DPPLProgram_CreateFromOCLSource (__dppl_keep const DPPLSyclContextRef Ctx, // \todo handle error message return nullptr; } - std::cout << "CTX address when passed to program creator: " << Ctx << std::endl; - std::cout << "Ref count in program create : " << - unwrap(Ctx)->get_info() << std::endl; - - queue q{default_selector()}; SyclCtx = unwrap(Ctx); - auto syclContext = q.get_context(); - SyclProgram = new program(syclContext); + SyclProgram = new program(*SyclCtx); std::string source = Source; if(CompileOpts) { diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index 68761c8af4..beefd9a0d4 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -129,9 +129,6 @@ DPPLQueue_GetContext (__dppl_keep const DPPLSyclQueueRef QRef) { auto Q = unwrap(QRef); auto Context = new context(Q->get_context()); - std::cout << "Context Addr when created in DPPLQueue_GetContext " << Context << '\n'; - std::cout << "Ref count when first created : " << - Context->get_info() << '\n'; return wrap(Context); } diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index ca15d56430..4c07200dc4 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -33,7 +33,7 @@ from .backend cimport * cdef class SyclContext: ''' Wrapper class for a Sycl Context ''' - cdef DPPLSyclContextRef ctxt_ptr + cdef DPPLSyclContextRef _ctxt_ptr @staticmethod cdef SyclContext _create (DPPLSyclContextRef ctxt) @@ -43,10 +43,10 @@ cdef class SyclContext: cdef class SyclDevice: ''' Wrapper class for a Sycl Device ''' - cdef DPPLSyclDeviceRef device_ptr - cdef const char *vendor_name - cdef const char *device_name - cdef const char *driver_version + cdef DPPLSyclDeviceRef _device_ptr + cdef const char *_vendor_name + cdef const char *_device_name + cdef const char *_driver_version @staticmethod cdef SyclDevice _create (DPPLSyclDeviceRef dref) @@ -57,7 +57,8 @@ cdef class SyclKernel: ''' Wraps a sycl::kernel object created from an OpenCL interoperability kernel. ''' - cdef DPPLSyclKernelRef kernel_ptr + cdef DPPLSyclKernelRef _kernel_ptr + cdef const char *_function_name cdef DPPLSyclKernelRef get_kernel_ptr (self) @staticmethod @@ -72,18 +73,20 @@ cdef class SyclProgram: SyclProgram can be created from either a source string or a SPIR-V binary file. ''' - cdef DPPLSyclProgramRef program_ptr + cdef DPPLSyclProgramRef _program_ptr @staticmethod cdef SyclProgram _create (DPPLSyclProgramRef pref) cdef DPPLSyclProgramRef get_program_ptr (self) - cpdef SyclKernel get_sycl_kernel(self, kernel_name) + cpdef SyclKernel get_sycl_kernel(self, str kernel_name) cdef class SyclQueue: ''' Wrapper class for a Sycl queue. ''' - cdef DPPLSyclQueueRef queue_ptr + cdef DPPLSyclQueueRef _queue_ptr + cdef SyclContext _context + cdef SyclDevice _device @staticmethod cdef SyclQueue _create (DPPLSyclQueueRef qref) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 20557e2364..721778ffab 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -30,12 +30,9 @@ from __future__ import print_function from enum import Enum, auto import logging from dpctl.backend cimport * -from libc.stdio cimport printf - _logger = logging.getLogger(__name__) - class device_type(Enum): gpu = auto() cpu = auto() @@ -53,15 +50,14 @@ cdef class SyclContext: @staticmethod cdef SyclContext _create (DPPLSyclContextRef ctxt): cdef SyclContext ret = SyclContext.__new__(SyclContext) - ret.ctxt_ptr = ctxt + ret._ctxt_ptr = ctxt return ret def __dealloc__ (self): - DPPLContext_Delete(self.ctxt_ptr) + DPPLContext_Delete(self._ctxt_ptr) cdef DPPLSyclContextRef get_context_ref (self): - printf("Address inside SyclContext %p\n",self.ctxt_ptr) - return self.ctxt_ptr + return self._ctxt_ptr cdef class SyclDevice: @@ -71,32 +67,32 @@ cdef class SyclDevice: @staticmethod cdef SyclDevice _create (DPPLSyclDeviceRef dref): cdef SyclDevice ret = SyclDevice.__new__(SyclDevice) - ret.device_ptr = dref - ret.vendor_name = DPPLDevice_GetVendorName(dref) - ret.device_name = DPPLDevice_GetName(dref) - ret.driver_version = DPPLDevice_GetDriverInfo(dref) + ret._device_ptr = dref + ret._vendor_name = DPPLDevice_GetVendorName(dref) + ret._device_name = DPPLDevice_GetName(dref) + ret._driver_version = DPPLDevice_GetDriverInfo(dref) return ret def __dealloc__ (self): - DPPLDevice_Delete(self.device_ptr) - DPPLCString_Delete(self.device_name) - DPPLCString_Delete(self.vendor_name) - DPPLCString_Delete(self.driver_version) + DPPLDevice_Delete(self._device_ptr) + DPPLCString_Delete(self._device_name) + DPPLCString_Delete(self._vendor_name) + DPPLCString_Delete(self._driver_version) def dump_device_info (self): ''' Print information about the SYCL device. ''' - DPPLDevice_DumpInfo(self.device_ptr) + DPPLDevice_DumpInfo(self._device_ptr) def get_device_name (self): ''' Returns the name of the device as a string ''' - return self.device_name + return self._device_name.decode() def get_vendor_name (self): ''' Returns the device vendor name as a string ''' - return self.vendor_name + return self._vendor_name.decode() def get_driver_version (self): ''' Returns the OpenCL software driver version as a string @@ -104,12 +100,12 @@ cdef class SyclDevice: device is an OpenCL device. Returns a string class with the value "1.2" if this SYCL device is a host device. ''' - return self.driver_version + return self._driver_version.decode() cdef DPPLSyclDeviceRef get_device_ptr (self): ''' Returns the DPPLSyclDeviceRef pointer for this class. ''' - return self.device_ptr + return self._device_ptr cdef class SyclKernel: ''' Wraps a sycl::kernel object created from an OpenCL interoperability @@ -119,27 +115,28 @@ cdef class SyclKernel: @staticmethod cdef SyclKernel _create (DPPLSyclKernelRef kref): cdef SyclKernel ret = SyclKernel.__new__(SyclKernel) - ret.kernel_ptr = kref + ret._kernel_ptr = kref + ret._function_name = DPPLKernel_GetFunctionName(kref) return ret def __dealloc__ (self): - DPPLKernel_Delete(self.kernel_ptr) - DPPLCString_Delete(self.function_name) + DPPLKernel_Delete(self._kernel_ptr) + DPPLCString_Delete(self._function_name) def get_function_name (self): ''' Returns the name of the Kernel function. ''' - return self.function_name + return self._function_name.decode() def get_num_args (self): ''' Returns the number of arguments for this kernel function. ''' - return DPPLKernel_GetNumArgs(self.kernel_ptr) + return DPPLKernel_GetNumArgs(self._kernel_ptr) cdef DPPLSyclKernelRef get_kernel_ptr (self): ''' Returns the DPPLSyclKernelRef pointer for this SyclKernel. ''' - return self.kernel_ptr + return self._kernel_ptr cdef class SyclProgram: @@ -154,25 +151,23 @@ cdef class SyclProgram: @staticmethod cdef SyclProgram _create (DPPLSyclProgramRef pref): cdef SyclProgram ret = SyclProgram.__new__(SyclProgram) - ret.program_ptr = pref + ret._program_ptr = pref return ret def __dealloc__ (self): - DPPLProgram_Delete(self.program_ptr) + DPPLProgram_Delete(self._program_ptr) cdef DPPLSyclProgramRef get_program_ptr (self): - return self.program_ptr + return self._program_ptr - cpdef SyclKernel get_sycl_kernel(self, kernel_name): - if isinstance(kernel_name, unicode): - kernel_name = kernel_name - return SyclKernel._create(DPPLProgram_GetKernel(self.program_ptr, - kernel_name)) - else: - TypeError("Expected kernel_name to be a string") + cpdef SyclKernel get_sycl_kernel(self, str kernel_name): + name = kernel_name.encode('utf8') + return SyclKernel._create(DPPLProgram_GetKernel(self._program_ptr, + name)) - def has_sycl_kernel(self, kernel_name): - return DPPLProgram_HasKernel(self.program_ptr, kernel_name) + def has_sycl_kernel(self, str kernel_name): + name = kernel_name.encode('utf8') + return DPPLProgram_HasKernel(self._program_ptr, name) cdef class SyclQueue: @@ -182,20 +177,22 @@ cdef class SyclQueue: @staticmethod cdef SyclQueue _create (DPPLSyclQueueRef qref): cdef SyclQueue ret = SyclQueue.__new__(SyclQueue) - ret.queue_ptr = qref + ret._context = SyclContext._create(DPPLQueue_GetContext(qref)) + ret._device = SyclDevice._create(DPPLQueue_GetDevice(qref)) + ret._queue_ptr = qref return ret def __dealloc__ (self): - DPPLQueue_Delete(self.queue_ptr) + DPPLQueue_Delete(self._queue_ptr) cpdef SyclContext get_sycl_context (self): - return SyclContext._create(DPPLQueue_GetContext(self.queue_ptr)) + return self._context cpdef SyclDevice get_sycl_device (self): - return SyclDevice._create(DPPLQueue_GetDevice(self.queue_ptr)) + return self._device cdef DPPLSyclQueueRef get_queue_ref (self): - return self.queue_ptr + return self._queue_ptr cdef class _SyclQueueManager: diff --git a/dpctl/tests/__init__.py b/dpctl/tests/__init__.py index ce80b2561c..9aa00a8d40 100644 --- a/dpctl/tests/__init__.py +++ b/dpctl/tests/__init__.py @@ -22,6 +22,8 @@ ## Top-level module of all dpctl Python unit test cases. ##===----------------------------------------------------------------------===## +from .test_dump_functions import * +from .test_sycl_program import * from .test_sycl_queue_manager import * from .test_sycl_usm import * -from .test_dump_functions import * + diff --git a/dpctl/tests/test_sycl_program.py b/dpctl/tests/test_sycl_program.py index fe45a196a3..a0a3ed960c 100644 --- a/dpctl/tests/test_sycl_program.py +++ b/dpctl/tests/test_sycl_program.py @@ -42,10 +42,19 @@ def test_create_program_from_source (self): c[index] = a[index] + d*b[index]; \ }" prog = dpctl.create_program_from_source(q,oclSrc) - # self.assertIsNotNone(prog) + self.assertIsNotNone(prog) + + self.assertTrue(prog.has_sycl_kernel("add")) + self.assertTrue(prog.has_sycl_kernel("axpy")) + + addKernel = prog.get_sycl_kernel('add') + axpyKernel = prog.get_sycl_kernel('axpy') + + self.assertEqual(addKernel.get_function_name(),"add") + self.assertEqual(axpyKernel.get_function_name(),"axpy") + self.assertEqual(addKernel.get_num_args(), 3) + self.assertEqual(axpyKernel.get_num_args(), 4) - # self.assertTrue(prog.has_sycl_kernel("add")) - # self.assertTrue(prog.has_sycl_kernel("axpy")) if __name__ == '__main__': unittest.main() From 9e72b98edc0857e44870a4b1060b5ca9a412b7ed Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 23 Sep 2020 23:28:16 -0500 Subject: [PATCH 64/93] WIP compile spirv program... --- dpctl/sycl_core.pyx | 31 ++++++++++++++++------- dpctl/tests/input_files/multi_kernel.spv | Bin 0 -> 1412 bytes dpctl/tests/test_sycl_program.py | 27 ++++++++++++++++++-- 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 dpctl/tests/input_files/multi_kernel.spv diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 721778ffab..ffd9bd359d 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -44,6 +44,11 @@ cdef class UnsupportedDeviceTypeError(Exception): ''' pass +cdef class SyclProgramCompilationError(Exception): + '''This exception is raised when a sycl program could not be built from + either a spirv binary file or a string source. + ''' + pass cdef class SyclContext: @@ -107,6 +112,7 @@ cdef class SyclDevice: ''' return self._device_ptr + cdef class SyclKernel: ''' Wraps a sycl::kernel object created from an OpenCL interoperability kernel. @@ -268,6 +274,7 @@ cdef class _SyclQueueManager: else: return False + # This private instance of the _SyclQueueManager should not be directly # accessed outside the module. _qmgr = _SyclQueueManager() @@ -312,30 +319,36 @@ def create_program_from_source (SyclQueue q, unicode source, unicode copts=""): cdef DPPLSyclContextRef CRef = q.get_sycl_context().get_context_ref() Pref = DPPLProgram_CreateFromOCLSource(CRef, Src, COpts) + if Pref is NULL: + raise SyclProgramCompilationError() + return SyclProgram._create(Pref) -def create_program_from_spirv (SyclQueue q, char[:] IL): +def create_program_from_spirv (SyclQueue q, const unsigned char[:] IL): ''' Creates a Sycl interoperability program from an SPIR-V binary. We use the DPPLProgram_CreateFromOCLSpirv() C API function to create a Sycl progrma from an compiled SPIR-V binary file. Parameters: - q (SyclQueue): The SyclQueue object wraps the Sycl device for - which the program will be built. - IL (char[:]) : SPIR-V binary IL file for an OpenCL program. + q (SyclQueue): The SyclQueue object wraps the Sycl device for + which the program will be built. + IL (const char[:]) : SPIR-V binary IL file for an OpenCL program. - Returns: - program (SyclProgram): A SyclProgram object wrapping the - syc::program returned by the C API. + Returns: + program (SyclProgram): A SyclProgram object wrapping the + syc::program returned by the C API. ''' cdef DPPLSyclProgramRef Pref - cdef bytes bIL = IL.data + cdef bytes bIL = bytes(IL) cdef const void *spirvIL = bIL cdef DPPLSyclContextRef CRef = q.get_sycl_context().get_context_ref() - Pref = DPPLProgram_CreateFromOCLSpirv(CRef, spirvIL, len(IL)) + cdef size_t length = IL.shape[0] + Pref = DPPLProgram_CreateFromOCLSpirv(CRef, spirvIL, length) + if Pref is NULL: + raise SyclProgramCompilationError() return SyclProgram._create(Pref) diff --git a/dpctl/tests/input_files/multi_kernel.spv b/dpctl/tests/input_files/multi_kernel.spv new file mode 100644 index 0000000000000000000000000000000000000000..1fa772d8dfd583970caa6666704fe5d7434f1dda GIT binary patch literal 1412 zcma)*+iDY06oywjX;ZB+)>`Y?Ne>=M3D^r2L@5e}h#)?}Fl~Yar;{+9miEpIAKDwi z|JySw2FZmx?5y*@4tq_sxZN~cu&3hk-*@53S=W=UFnOUld1u&cQ4C@=T+ z|NdFiTJ~(7ukikLR@go{qVO|2Y(>gPHk*F+~}F&bP{^7>SM5prxu zu4dG*+f&;?u-(O`OD*?AJ^y|2RsP5xh_C8x*hBHNwLHderj}UpY%N<;xvFJH^2u2i z^6iTEx}Hq^4y>mhpG!u^Ec<$KWc0|)jGB6?8JV7XdN+9d{%7{=)+bvD{@^itX3jjA z1O0C(ANzKw%R$y6Ta2KyDLE3w2O3Lmh=Ps0E(`mJenph}i0?>yMHCx)za3dO=je%f zQ)7bx{!5_|&;E13d!?*k(U0@=*qo##!R=WXH1%^oom_S$FiNc_B+uhn46iboH=WAcsA517me89&GKIsE~VDR=Pz literal 0 HcmV?d00001 diff --git a/dpctl/tests/test_sycl_program.py b/dpctl/tests/test_sycl_program.py index a0a3ed960c..af8ccfba3f 100644 --- a/dpctl/tests/test_sycl_program.py +++ b/dpctl/tests/test_sycl_program.py @@ -25,10 +25,10 @@ import dpctl import unittest - +import os @unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") -class TestProgramFromOCSource (unittest.TestCase): +class TestProgramFromOCLSource (unittest.TestCase): def test_create_program_from_source (self): q = dpctl.get_current_queue() @@ -56,5 +56,28 @@ def test_create_program_from_source (self): self.assertEqual(axpyKernel.get_num_args(), 4) +@unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") +class TestProgramFromSPRIV (unittest.TestCase): + + def test_create_program_from_spirv(self): + q = dpctl.get_current_queue() + CURR_DIR = os.path.dirname(os.path.abspath(__file__)) + spirv_file = os.path.join(CURR_DIR, 'input_files/multi_kernel.spv') + with open(spirv_file, 'rb') as fin: + spirv = fin.read() + prog = dpctl.create_program_from_spirv(q,spirv) + self.assertIsNotNone(prog) + self.assertTrue(prog.has_sycl_kernel("add")) + self.assertTrue(prog.has_sycl_kernel("axpy")) + + addKernel = prog.get_sycl_kernel('add') + axpyKernel = prog.get_sycl_kernel('axpy') + + self.assertEqual(addKernel.get_function_name(),"add") + self.assertEqual(axpyKernel.get_function_name(),"axpy") + self.assertEqual(addKernel.get_num_args(), 3) + self.assertEqual(axpyKernel.get_num_args(), 4) + + if __name__ == '__main__': unittest.main() From ad8014329d586eaba909ffc71caa66ecc25beb12 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Thu, 24 Sep 2020 14:00:48 -0500 Subject: [PATCH 65/93] Fix program build from spirv. --- dpctl/sycl_core.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index ffd9bd359d..cd202fb52f 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -324,6 +324,7 @@ def create_program_from_source (SyclQueue q, unicode source, unicode copts=""): return SyclProgram._create(Pref) +cimport cython.array def create_program_from_spirv (SyclQueue q, const unsigned char[:] IL): ''' Creates a Sycl interoperability program from an SPIR-V binary. @@ -342,11 +343,10 @@ def create_program_from_spirv (SyclQueue q, const unsigned char[:] IL): ''' cdef DPPLSyclProgramRef Pref - cdef bytes bIL = bytes(IL) - cdef const void *spirvIL = bIL + cdef const unsigned char *dIL = &IL[0] cdef DPPLSyclContextRef CRef = q.get_sycl_context().get_context_ref() cdef size_t length = IL.shape[0] - Pref = DPPLProgram_CreateFromOCLSpirv(CRef, spirvIL, length) + Pref = DPPLProgram_CreateFromOCLSpirv(CRef, dIL, length) if Pref is NULL: raise SyclProgramCompilationError() From 814a2901855dfe9cda04a446bbb60a2767b64f44 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Thu, 24 Sep 2020 14:04:50 -0500 Subject: [PATCH 66/93] Store a reference to the ffi, lib object so they are removed aonly once Runtime is cleaned. Also fix the singleton implementation. --- dpctl/ocldrv.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/dpctl/ocldrv.py b/dpctl/ocldrv.py index 889f7c9a73..49601475e7 100644 --- a/dpctl/ocldrv.py +++ b/dpctl/ocldrv.py @@ -503,17 +503,18 @@ class Runtime(): OpenCL devices found on the system. ''' - _runtime = None - _cpu_device = None - _gpu_device = None - _curr_device = None + _singleton = None def __new__(cls): - obj = cls._runtime + obj = cls._singleton if obj is not None: return obj else: obj = object.__new__(cls) + + cls._lib = lib + cls._ffi = ffi + ffiobj = ffi.new("runtime_t *") retval = (lib.create_dp_runtime(ffiobj)) if(retval): @@ -533,6 +534,7 @@ def __new__(cls): _logger.warning("No GPU device") cls._curr_device = DeviceEnv(cls._runtime[0][0].curr_env) + cls._singleton = obj return obj @@ -540,20 +542,20 @@ def __init__(self): pass def __del__(self): - if Runtime._runtime: - retval = (lib.destroy_dp_runtime(Runtime._runtime)) + if self._runtime: + retval = (self._lib.destroy_dp_runtime(self._runtime)) if(retval): _raise_driver_error("destroy_dp_runtime", -1) def has_cpu_device(self): ''' Returns True is the system has an OpenCL driver for the CPU.''' - return Runtime._cpu_device is not None + return self._cpu_device is not None def has_gpu_device(self): ''' Returns True is the system has an OpenCL driver for the GPU.''' - return Runtime._gpu_device is not None + return self._gpu_device is not None def get_cpu_device(self): @@ -561,39 +563,39 @@ def get_cpu_device(self): CPU context. ''' - if(Runtime._cpu_device is None): + if(self._cpu_device is None): _raise_device_not_found_error("get_cpu_device") - return Runtime._cpu_device + return self._cpu_device def get_gpu_device(self): ''' Returns a cdata wrapper for the first available OpenCL GPU context. ''' - if(Runtime._gpu_device is None): + if(self._gpu_device is None): _raise_device_not_found_error("get_gpu_device") - return Runtime._gpu_device + return self._gpu_device def get_current_device(self): ''' Returns a cdata wrapper for the first available OpenCL CPU context. ''' - return Runtime._curr_device + return self._curr_device def get_runtime_ptr(self): ''' Returns a reference to the runtime object. ''' - return Runtime._runtime[0] + return self._runtime[0] def dump(self): ''' Prints OpenCL metadata about the available devices and contexts. ''' - retval = Runtime._runtime[0].dump_fn(Runtime._runtime[0]) + retval = self._runtime[0].dump_fn(Runtime._runtime[0]) if retval == -1: _raise_driver_error("runtime dump_fn", -1) return retval From 36e69dbf81d14183b7a1bb6653bee98434a6aea2 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Thu, 24 Sep 2020 22:12:30 -0500 Subject: [PATCH 67/93] Add C unit test for the queue submit and wait calls. --- backends/include/dppl_sycl_queue_interface.h | 28 ++- backends/include/dppl_sycl_types.h | 37 ---- backends/source/dppl_sycl_queue_interface.cpp | 42 ++-- backends/tests/CMakeLists.txt | 3 +- backends/tests/test_sycl_queue_interface.cpp | 186 ++++++++++++++++++ 5 files changed, 237 insertions(+), 59 deletions(-) create mode 100644 backends/tests/test_sycl_queue_interface.cpp diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index 22f6f0a974..1b4a4c3345 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -35,6 +35,31 @@ DPPL_C_EXTERN_C_BEGIN +/*! + * @brief Supported types for kernel arguments to be passed to a Sycl kernel. + * + * \todo Add support for sycl::buffer + * + */ +typedef enum +{ + CHAR, + SIGNED_CHAR, + UNSIGNED_CHAR, + SHORT, + INT, + UNSIGNED_INT, + LONG, + UNSIGNED_LONG, + LONG_LONG, + UNSIGNED_LONG_LONG, + SIZE_T, + FLOAT, + DOUBLE, + LONG_DOUBLE, + VOID_PTR +} DPPLKernelArgType; + /*! * @brief Delete the pointer after casting it to sycl::queue. * @@ -91,7 +116,8 @@ DPPL_API DPPLSyclEventRef DPPLQueue_Submit (__dppl_keep DPPLSyclKernelRef KRef, __dppl_keep DPPLSyclQueueRef QRef, - __dppl_keep DPPLKernelArg *Args, + __dppl_keep void **Args, + __dppl_keep DPPLKernelArgType *ArgTypes, size_t NArgs, size_t Range[3], size_t NDims); diff --git a/backends/include/dppl_sycl_types.h b/backends/include/dppl_sycl_types.h index 1af9574771..e3ccf3d0a8 100644 --- a/backends/include/dppl_sycl_types.h +++ b/backends/include/dppl_sycl_types.h @@ -74,40 +74,3 @@ typedef struct DPPLOpaqueSyclQueue *DPPLSyclQueueRef; * @see sycl::usm */ typedef struct DPPLOpaqueSyclUSM *DPPLSyclUSMRef; - -/*! - * @brief Enum of currently support types for kernel arguments. - * - * \todo Add support for sycl::buffer - * - */ -enum DPPLArgTypes -{ - CHAR, - SIGNED_CHAR, - UNSIGNED_CHAR, - SHORT, - INT, - UNSIGNED_INT, - LONG, - UNSIGNED_LONG, - LONG_LONG, - UNSIGNED_LONG_LONG, - SIZE_T, - FLOAT, - DOUBLE, - LONG_DOUBLE, - VOID_PTR -}; - -/*! - * @brief Represents an abstract kernel argument that is passed through to an - * OpenCL kernel via Sycl. - * - */ -struct DPPLKernelArg -{ - enum DPPLArgTypes argType; - void *argVal; -}; - diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index beefd9a0d4..7c8da01a6c 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -46,56 +46,57 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef) * @param cgh My Param doc * @param Arg My Param doc */ -bool set_kernel_arg (handler &cgh, __dppl_keep DPPLKernelArg Arg, size_t idx) +bool set_kernel_arg (handler &cgh, size_t idx, __dppl_keep void *Arg, + DPPLKernelArgType ArgTy) { bool arg_set = true; - switch (Arg.argType) + switch (ArgTy) { case CHAR: - cgh.set_arg(idx, *(char*)Arg.argVal); + cgh.set_arg(idx, *(char*)Arg); break; case SIGNED_CHAR: - cgh.set_arg(idx, *(signed char*)Arg.argVal); + cgh.set_arg(idx, *(signed char*)Arg); break; case UNSIGNED_CHAR: - cgh.set_arg(idx, *(unsigned char*)Arg.argVal); + cgh.set_arg(idx, *(unsigned char*)Arg); break; case SHORT: - cgh.set_arg(idx, *(short*)Arg.argVal); + cgh.set_arg(idx, *(short*)Arg); break; case INT: - cgh.set_arg(idx, *(int*)Arg.argVal); + cgh.set_arg(idx, *(int*)Arg); break; case UNSIGNED_INT: - cgh.set_arg(idx, *(unsigned int*)Arg.argVal); + cgh.set_arg(idx, *(unsigned int*)Arg); break; case LONG: - cgh.set_arg(idx, *(long*)Arg.argVal); + cgh.set_arg(idx, *(long*)Arg); break; case UNSIGNED_LONG: - cgh.set_arg(idx, *(unsigned long*)Arg.argVal); + cgh.set_arg(idx, *(unsigned long*)Arg); break; case LONG_LONG: - cgh.set_arg(idx, *(long long*)Arg.argVal); + cgh.set_arg(idx, *(long long*)Arg); break; case UNSIGNED_LONG_LONG: - cgh.set_arg(idx, *(unsigned long long*)Arg.argVal); + cgh.set_arg(idx, *(unsigned long long*)Arg); break; case SIZE_T: - cgh.set_arg(idx, *(size_t*)Arg.argVal); + cgh.set_arg(idx, *(size_t*)Arg); break; case FLOAT: - cgh.set_arg(idx, *(float*)Arg.argVal); + cgh.set_arg(idx, *(float*)Arg); break; case DOUBLE: - cgh.set_arg(idx, *(double*)Arg.argVal); + cgh.set_arg(idx, *(double*)Arg); break; case LONG_DOUBLE: - cgh.set_arg(idx, *(long double*)Arg.argVal); + cgh.set_arg(idx, *(long double*)Arg); break; case VOID_PTR: - cgh.set_arg(idx, Arg.argVal); + cgh.set_arg(idx, Arg); break; default: // \todo handle errors @@ -135,7 +136,8 @@ DPPLQueue_GetContext (__dppl_keep const DPPLSyclQueueRef QRef) __dppl_give DPPLSyclEventRef DPPLQueue_Submit (__dppl_keep DPPLSyclKernelRef KRef, __dppl_keep DPPLSyclQueueRef QRef, - __dppl_keep DPPLKernelArg *Args, + __dppl_keep void **Args, + __dppl_keep DPPLKernelArgType *ArgTypes, size_t NArgs, size_t Range[3], size_t NDims) @@ -145,10 +147,10 @@ DPPLQueue_Submit (__dppl_keep DPPLSyclKernelRef KRef, event e; e = Queue->submit([&](handler& cgh) { - for (auto i = 0ul; i < 4; ++i) { + for (auto i = 0ul; i < NArgs; ++i) { // \todo add support for Sycl buffers // \todo handle errors properly - if(!set_kernel_arg(cgh, Args[i], i)) + if(!set_kernel_arg(cgh, i, Args[i], ArgTypes[i])) exit(1); } switch(NDims) diff --git a/backends/tests/CMakeLists.txt b/backends/tests/CMakeLists.txt index c3c4160aa5..08e7f9f9ff 100644 --- a/backends/tests/CMakeLists.txt +++ b/backends/tests/CMakeLists.txt @@ -23,10 +23,11 @@ else() link_directories(${GTEST_LIB_DIR}) set(PYDPPL_BACKEND_TEST_CASES - test_sycl_queue_manager test_sycl_kernel_interface test_sycl_platform_interface test_sycl_program_interface + test_sycl_queue_interface + test_sycl_queue_manager ) # Copy the spir-v input files to test build directory diff --git a/backends/tests/test_sycl_queue_interface.cpp b/backends/tests/test_sycl_queue_interface.cpp new file mode 100644 index 0000000000..0d98b9e850 --- /dev/null +++ b/backends/tests/test_sycl_queue_interface.cpp @@ -0,0 +1,186 @@ +//===---- test_sycl_queue_interface.cpp - DPPL-SYCL interface -*- C++ --*--===// +// +// Python Data Parallel Processing Library (PyDPPL) +// +// Copyright 2020 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 has unit test cases for functions defined in +/// dppl_sycl_queue_interface.h. +/// +//===----------------------------------------------------------------------===// + +#include "dppl_sycl_context_interface.h" +#include "dppl_sycl_event_interface.h" +#include "dppl_sycl_kernel_interface.h" +#include "dppl_sycl_program_interface.h" +#include "dppl_sycl_queue_interface.h" +#include "dppl_sycl_queue_manager.h" +#include "dppl_sycl_usm_interface.h" + +#include "Support/CBindingWrapping.h" + +#include + +namespace +{ + constexpr size_t SIZE = 1024; + + DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPPLSyclUSMRef); + + void add_kernel_checker (const float *a, const float *b, const float *c) + { + // Validate the data + for(auto i = 0ul; i < SIZE; ++i) { + EXPECT_EQ(c[i], a[i] + b[i]); + } + } + + void axpy_kernel_checker (const float *a, const float *b, const float *c, + float d) + { + for(auto i = 0ul; i < SIZE; ++i) { + EXPECT_EQ(c[i], a[i] + d*b[i]); + } + } +} + +struct TestDPPLSyclQueueInterface : public ::testing::Test +{ + const char *CLProgramStr = R"CLC( + kernel void init_arr (global float *a) { + size_t index = get_global_id(0); + a[index] = (float)index; + } + + kernel void add (global float* a, global float* b, global float* c) { + size_t index = get_global_id(0); + c[index] = a[index] + b[index]; + } + + kernel void axpy (global float* a, global float* b, + global float* c, float d) { + size_t index = get_global_id(0); + c[index] = a[index] + d*b[index]; + } + )CLC"; + const char *CompileOpts ="-cl-fast-relaxed-math"; + + DPPLSyclContextRef CurrCtxRef = nullptr; + DPPLSyclQueueRef CurrQueue = nullptr; + DPPLSyclProgramRef PRef = nullptr; + DPPLSyclProgramRef PRef2 = nullptr; + TestDPPLSyclQueueInterface () + { + CurrQueue = DPPLQueueMgr_GetCurrentQueue(); + CurrCtxRef = DPPLQueue_GetContext(CurrQueue); + PRef = DPPLProgram_CreateFromOCLSource(CurrCtxRef, CLProgramStr, + CompileOpts); + } + + ~TestDPPLSyclQueueInterface () + { + DPPLQueue_Delete(CurrQueue); + DPPLContext_Delete(CurrCtxRef); + DPPLProgram_Delete(PRef); + } +}; + +TEST_F (TestDPPLSyclQueueInterface, CheckSubmit) +{ + ASSERT_TRUE(PRef != nullptr); + ASSERT_TRUE(DPPLProgram_HasKernel(PRef, "init_arr")); + ASSERT_TRUE(DPPLProgram_HasKernel(PRef, "add")); + ASSERT_TRUE(DPPLProgram_HasKernel(PRef, "axpy")); + + auto InitKernel = DPPLProgram_GetKernel(PRef, "init_arr"); + auto AddKernel = DPPLProgram_GetKernel(PRef, "add"); + auto AxpyKernel = DPPLProgram_GetKernel(PRef, "axpy"); + + // Create the input args + auto a = DPPLmalloc_shared(SIZE, CurrQueue); + ASSERT_TRUE(a != nullptr); + auto b = DPPLmalloc_shared(SIZE, CurrQueue); + ASSERT_TRUE(b != nullptr); + auto c = DPPLmalloc_shared(SIZE, CurrQueue); + ASSERT_TRUE(c != nullptr); + + // Initialize a,b + DPPLKernelArgType argTypes[] = {VOID_PTR}; + size_t Range[] = {SIZE}; + void *arg1[1] = { unwrap(a) }; + void *arg2[1] = { unwrap(b) }; + + auto E1 = DPPLQueue_Submit(InitKernel, CurrQueue, arg1, argTypes, 1, + Range, 1); + auto E2 = DPPLQueue_Submit(InitKernel, CurrQueue, arg2, argTypes, 1, + Range, 1); + ASSERT_TRUE(E1 != nullptr); + ASSERT_TRUE(E2 != nullptr); + + DPPLQueue_Wait(CurrQueue); + + // Submit the add kernel + void *args[3] = { unwrap(a), unwrap(b), unwrap(c) }; + DPPLKernelArgType addKernelArgTypes[] = {VOID_PTR, VOID_PTR, VOID_PTR}; + + auto E3 = DPPLQueue_Submit(AddKernel, CurrQueue, args, addKernelArgTypes, 3, + Range, 1); + ASSERT_TRUE(E3 != nullptr); + DPPLQueue_Wait(CurrQueue); + + // Verify the result of "add" + add_kernel_checker((float*)a, (float*)b, (float*)c); + + // Create kernel args for axpy + float d = 10.0; + void *args2[4] = { unwrap(a), unwrap(b), unwrap(c) , (void*)&d}; + DPPLKernelArgType addKernelArgTypes2[] = { VOID_PTR, + VOID_PTR, + VOID_PTR, + FLOAT + }; + auto E4 = DPPLQueue_Submit(AxpyKernel, CurrQueue, args2, + addKernelArgTypes2, 4, Range, 1); + ASSERT_TRUE(E4 != nullptr); + DPPLQueue_Wait(CurrQueue); + + // Verify the result of "axpy" + axpy_kernel_checker((float*)a, (float*)b, (float*)c, d); + + // clean ups + DPPLEvent_Delete(E1); + DPPLEvent_Delete(E2); + DPPLEvent_Delete(E3); + DPPLEvent_Delete(E4); + + DPPLKernel_Delete(AddKernel); + DPPLKernel_Delete(AxpyKernel); + DPPLKernel_Delete(InitKernel); + + DPPLfree_with_queue((DPPLSyclUSMRef)a, CurrQueue); + DPPLfree_with_queue((DPPLSyclUSMRef)b, CurrQueue); + DPPLfree_with_queue((DPPLSyclUSMRef)c, CurrQueue); +} + +int +main (int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + return ret; +} From f6e05fbb01ee2b3192aa425dbc458e7a201ee8ad Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Fri, 25 Sep 2020 01:06:51 -0500 Subject: [PATCH 68/93] WIP submit function. --- backends/include/dppl_sycl_queue_interface.h | 40 ++++++++-------- backends/source/dppl_sycl_queue_interface.cpp | 38 +++++++-------- backends/tests/test_sycl_queue_interface.cpp | 15 +++--- dpctl/_sycl_core.pxd | 23 +++++++-- dpctl/backend.pxd | 47 +++++++++++++++---- dpctl/sycl_core.pyx | 39 +++++++++++++++ 6 files changed, 143 insertions(+), 59 deletions(-) diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index 1b4a4c3345..46f7fbe93c 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -43,21 +43,21 @@ DPPL_C_EXTERN_C_BEGIN */ typedef enum { - CHAR, - SIGNED_CHAR, - UNSIGNED_CHAR, - SHORT, - INT, - UNSIGNED_INT, - LONG, - UNSIGNED_LONG, - LONG_LONG, - UNSIGNED_LONG_LONG, - SIZE_T, - FLOAT, - DOUBLE, - LONG_DOUBLE, - VOID_PTR + DPPL_CHAR, + DPPL_SIGNED_CHAR, + DPPL_UNSIGNED_CHAR, + DPPL_SHORT, + DPPL_INT, + DPPL_UNSIGNED_INT, + DPPL_LONG, + DPPL_UNSIGNED_LONG, + DPPL_LONG_LONG, + DPPL_UNSIGNED_LONG_LONG, + DPPL_SIZE_T, + DPPL_FLOAT, + DPPL_DOUBLE, + DPPL_LONG_DOUBLE, + DPPL_VOID_PTR } DPPLKernelArgType; /*! @@ -114,12 +114,12 @@ DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef); */ DPPL_API DPPLSyclEventRef -DPPLQueue_Submit (__dppl_keep DPPLSyclKernelRef KRef, - __dppl_keep DPPLSyclQueueRef QRef, +DPPLQueue_Submit (__dppl_keep const DPPLSyclKernelRef KRef, + __dppl_keep const DPPLSyclQueueRef QRef, __dppl_keep void **Args, - __dppl_keep DPPLKernelArgType *ArgTypes, + __dppl_keep const DPPLKernelArgType *ArgTypes, size_t NArgs, - size_t Range[3], + size_t const Range[3], size_t NDims); /*! @@ -130,6 +130,6 @@ DPPLQueue_Submit (__dppl_keep DPPLSyclKernelRef KRef, */ DPPL_API void -DPPLQueue_Wait (__dppl_keep DPPLSyclQueueRef QRef); +DPPLQueue_Wait (__dppl_keep const DPPLSyclQueueRef QRef); DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index 7c8da01a6c..e02bc4b714 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -53,49 +53,49 @@ bool set_kernel_arg (handler &cgh, size_t idx, __dppl_keep void *Arg, switch (ArgTy) { - case CHAR: + case DPPL_CHAR: cgh.set_arg(idx, *(char*)Arg); break; - case SIGNED_CHAR: + case DPPL_SIGNED_CHAR: cgh.set_arg(idx, *(signed char*)Arg); break; - case UNSIGNED_CHAR: + case DPPL_UNSIGNED_CHAR: cgh.set_arg(idx, *(unsigned char*)Arg); break; - case SHORT: + case DPPL_SHORT: cgh.set_arg(idx, *(short*)Arg); break; - case INT: + case DPPL_INT: cgh.set_arg(idx, *(int*)Arg); break; - case UNSIGNED_INT: + case DPPL_UNSIGNED_INT: cgh.set_arg(idx, *(unsigned int*)Arg); break; - case LONG: + case DPPL_LONG: cgh.set_arg(idx, *(long*)Arg); break; - case UNSIGNED_LONG: + case DPPL_UNSIGNED_LONG: cgh.set_arg(idx, *(unsigned long*)Arg); break; - case LONG_LONG: + case DPPL_LONG_LONG: cgh.set_arg(idx, *(long long*)Arg); break; - case UNSIGNED_LONG_LONG: + case DPPL_UNSIGNED_LONG_LONG: cgh.set_arg(idx, *(unsigned long long*)Arg); break; - case SIZE_T: + case DPPL_SIZE_T: cgh.set_arg(idx, *(size_t*)Arg); break; - case FLOAT: + case DPPL_FLOAT: cgh.set_arg(idx, *(float*)Arg); break; - case DOUBLE: + case DPPL_DOUBLE: cgh.set_arg(idx, *(double*)Arg); break; - case LONG_DOUBLE: + case DPPL_LONG_DOUBLE: cgh.set_arg(idx, *(long double*)Arg); break; - case VOID_PTR: + case DPPL_VOID_PTR: cgh.set_arg(idx, Arg); break; default: @@ -134,12 +134,12 @@ DPPLQueue_GetContext (__dppl_keep const DPPLSyclQueueRef QRef) } __dppl_give DPPLSyclEventRef -DPPLQueue_Submit (__dppl_keep DPPLSyclKernelRef KRef, - __dppl_keep DPPLSyclQueueRef QRef, +DPPLQueue_Submit (__dppl_keep const DPPLSyclKernelRef KRef, + __dppl_keep const DPPLSyclQueueRef QRef, __dppl_keep void **Args, - __dppl_keep DPPLKernelArgType *ArgTypes, + __dppl_keep const DPPLKernelArgType *ArgTypes, size_t NArgs, - size_t Range[3], + size_t const Range[3], size_t NDims) { auto Kernel = unwrap(KRef); diff --git a/backends/tests/test_sycl_queue_interface.cpp b/backends/tests/test_sycl_queue_interface.cpp index 0d98b9e850..fd9dda8688 100644 --- a/backends/tests/test_sycl_queue_interface.cpp +++ b/backends/tests/test_sycl_queue_interface.cpp @@ -120,7 +120,7 @@ TEST_F (TestDPPLSyclQueueInterface, CheckSubmit) ASSERT_TRUE(c != nullptr); // Initialize a,b - DPPLKernelArgType argTypes[] = {VOID_PTR}; + DPPLKernelArgType argTypes[] = {DPPL_VOID_PTR}; size_t Range[] = {SIZE}; void *arg1[1] = { unwrap(a) }; void *arg2[1] = { unwrap(b) }; @@ -136,7 +136,10 @@ TEST_F (TestDPPLSyclQueueInterface, CheckSubmit) // Submit the add kernel void *args[3] = { unwrap(a), unwrap(b), unwrap(c) }; - DPPLKernelArgType addKernelArgTypes[] = {VOID_PTR, VOID_PTR, VOID_PTR}; + DPPLKernelArgType addKernelArgTypes[] = { DPPL_VOID_PTR, + DPPL_VOID_PTR, + DPPL_VOID_PTR + }; auto E3 = DPPLQueue_Submit(AddKernel, CurrQueue, args, addKernelArgTypes, 3, Range, 1); @@ -149,10 +152,10 @@ TEST_F (TestDPPLSyclQueueInterface, CheckSubmit) // Create kernel args for axpy float d = 10.0; void *args2[4] = { unwrap(a), unwrap(b), unwrap(c) , (void*)&d}; - DPPLKernelArgType addKernelArgTypes2[] = { VOID_PTR, - VOID_PTR, - VOID_PTR, - FLOAT + DPPLKernelArgType addKernelArgTypes2[] = { DPPL_VOID_PTR, + DPPL_VOID_PTR, + DPPL_VOID_PTR, + DPPL_FLOAT }; auto E4 = DPPLQueue_Submit(AxpyKernel, CurrQueue, args2, addKernelArgTypes2, 4, Range, 1); diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index 4c07200dc4..d4b0d125c0 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -29,7 +29,6 @@ from .backend cimport * - cdef class SyclContext: ''' Wrapper class for a Sycl Context ''' @@ -53,6 +52,16 @@ cdef class SyclDevice: cdef DPPLSyclDeviceRef get_device_ptr (self) +cdef class SyclEvent: + ''' Wrapper class for a Sycl Event + ''' + cdef DPPLSyclEventRef _event_ptr + + @staticmethod + cdef SyclEvent _create (DPPLSyclEventRef e) + cpdef void wait (self) + + cdef class SyclKernel: ''' Wraps a sycl::kernel object created from an OpenCL interoperability kernel. @@ -76,8 +85,8 @@ cdef class SyclProgram: cdef DPPLSyclProgramRef _program_ptr @staticmethod - cdef SyclProgram _create (DPPLSyclProgramRef pref) - cdef DPPLSyclProgramRef get_program_ptr (self) + cdef SyclProgram _create (DPPLSyclProgramRef pref) + cdef DPPLSyclProgramRef get_program_ptr (self) cpdef SyclKernel get_sycl_kernel(self, str kernel_name) @@ -87,9 +96,13 @@ cdef class SyclQueue: cdef DPPLSyclQueueRef _queue_ptr cdef SyclContext _context cdef SyclDevice _device + cdef list _events @staticmethod - cdef SyclQueue _create (DPPLSyclQueueRef qref) + cdef SyclQueue _create (DPPLSyclQueueRef qref) cpdef SyclContext get_sycl_context (self) cpdef SyclDevice get_sycl_device (self) - cdef DPPLSyclQueueRef get_queue_ref (self) + cdef DPPLSyclQueueRef get_queue_ref (self) + cpdef SyclEvent submit (self, SyclKernel kernel, list args, \ + const size_t [:]range, size_t ndims) + cpdef void wait (self) diff --git a/dpctl/backend.pxd b/dpctl/backend.pxd index c6cebeb415..5a96be6ed7 100644 --- a/dpctl/backend.pxd +++ b/dpctl/backend.pxd @@ -37,6 +37,7 @@ cdef extern from "dppl_utils.h": cdef extern from "dppl_sycl_types.h": cdef struct DPPLOpaqueSyclContext cdef struct DPPLOpaqueSyclDevice + cdef struct DPPLOpaqueSyclEvent cdef struct DPPLOpaqueSyclKernel cdef struct DPPLOpaqueSyclProgram cdef struct DPPLOpaqueSyclQueue @@ -44,6 +45,7 @@ cdef extern from "dppl_sycl_types.h": ctypedef DPPLOpaqueSyclContext* DPPLSyclContextRef ctypedef DPPLOpaqueSyclDevice* DPPLSyclDeviceRef + ctypedef DPPLOpaqueSyclEvent* DPPLSyclEventRef ctypedef DPPLOpaqueSyclKernel* DPPLSyclKernelRef ctypedef DPPLOpaqueSyclProgram* DPPLSyclProgramRef ctypedef DPPLOpaqueSyclQueue* DPPLSyclQueueRef @@ -55,6 +57,9 @@ cdef extern from "dppl_sycl_context_interface.h": cdef extern from "dppl_sycl_device_interface.h": + cdef enum _device_type 'DPPLSyclDeviceType': + _GPU 'DPPL_GPU' + _CPU 'DPPL_CPU' cdef void DPPLDevice_DumpInfo (const DPPLSyclDeviceRef DRef) except + cdef void DPPLDevice_Delete (DPPLSyclDeviceRef DRef) except + cdef void DPPLDevice_DumpInfo (const DPPLSyclDeviceRef DRef) except + @@ -71,6 +76,11 @@ cdef extern from "dppl_sycl_device_interface.h": except + +cdef extern from "dppl_sycl_event_interface.h": + cdef void DPPLEvent_Wait (DPPLSyclEventRef ERef) + cdef void DPPLEvent_Delete (DPPLSyclEventRef ERef) + + cdef extern from "dppl_sycl_kernel_interface.h": cdef const char* DPPLKernel_GetFunctionName (const DPPLSyclKernelRef KRef) cdef size_t DPPLKernel_GetNumArgs (const DPPLSyclKernelRef KRef) @@ -99,18 +109,37 @@ cdef extern from "dppl_sycl_program_interface.h": cdef extern from "dppl_sycl_queue_interface.h": - cdef void DPPLQueue_Delete (DPPLSyclQueueRef QRef) except + - cdef DPPLSyclContextRef DPPLQueue_GetContext (const DPPLSyclQueueRef Q) \ - except+ - cdef DPPLSyclDeviceRef DPPLQueue_GetDevice (const DPPLSyclQueueRef Q) \ - except + + cdef enum _arg_data_type 'DPPLKernelArgType': + _CHAR 'DPPL_CHAR', + _SIGNED_CHAR 'DPPL_SIGNED_CHAR', + _UNSIGNED_CHAR 'DPPL_UNSIGNED_CHAR', + _SHORT 'DPPL_SHORT', + _INT 'DPPL_INT', + _UNSIGNED_INT 'DPPL_INT', + _LONG 'DPPL_LONG', + _UNSIGNED_LONG 'DPPL_UNSIGNED_LONG', + _LONG_LONG 'LONG_LONG', + _UNSIGNED_LONG_LONG 'UNSIGNED_LONG_LONG', + _SIZE_T 'DPPL_SIZE_T', + _FLOAT 'DPPL_FLOAT', + _DOUBLE 'DPPL_DOUBLE', + _LONG_DOUBLE 'DPPL_DOUBLE', + _VOID_PTR 'DPPL_VOID_PTR' + ctypedef _arg_data_type DPPLKernelArgType + cdef void DPPLQueue_Delete (DPPLSyclQueueRef QRef) + cdef DPPLSyclContextRef DPPLQueue_GetContext (const DPPLSyclQueueRef Q) + cdef DPPLSyclDeviceRef DPPLQueue_GetDevice (const DPPLSyclQueueRef Q) + cdef DPPLSyclEventRef DPPLQueue_Submit (const DPPLSyclKernelRef Ref, \ + const DPPLSyclQueueRef QRef, \ + void **Args, \ + const DPPLKernelArgType *ArgTypes,\ + size_t NArgs, \ + const size_t Range[3], \ + size_t NDims) + cdef void DPPLQueue_Wait (const DPPLSyclQueueRef QRef) cdef extern from "dppl_sycl_queue_manager.h": - cdef enum _device_type 'DPPLSyclDeviceType': - _GPU 'DPPL_GPU' - _CPU 'DPPL_CPU' - cdef DPPLSyclQueueRef DPPLQueueMgr_GetCurrentQueue () except + cdef size_t DPPLQueueMgr_GetNumCPUQueues () except + cdef size_t DPPLQueueMgr_GetNumGPUQueues () except + diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index cd202fb52f..f2c53fe148 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -30,6 +30,7 @@ from __future__ import print_function from enum import Enum, auto import logging from dpctl.backend cimport * +from libc.stdlib cimport malloc, free _logger = logging.getLogger(__name__) @@ -113,6 +114,23 @@ cdef class SyclDevice: return self._device_ptr +cdef class SyclEvent: + ''' Wrapper class for a Sycl Event + ''' + + @staticmethod + cdef SyclEvent _create (DPPLSyclEventRef eref): + cdef SyclEvent ret = SyclEvent.__new__(SyclEvent) + ret._event_ptr = eref + return ret + + def __dealloc__ (self): + DPPLEvent_Delete(self._event_ptr) + + cpdef void wait (self): + DPPLEvent_Wait(self._event_ptr) + + cdef class SyclKernel: ''' Wraps a sycl::kernel object created from an OpenCL interoperability kernel. @@ -200,8 +218,29 @@ cdef class SyclQueue: cdef DPPLSyclQueueRef get_queue_ref (self): return self._queue_ptr + cpdef SyclEvent submit (self, SyclKernel kernel, list args, \ + const size_t[:] range, size_t ndims): + cdef llist = args + cdef void *kargs = malloc(len(args) * sizeof(void*)) + cdef DPPLKernelArgType *ktypes = malloc( + len(args) * sizeof(DPPLKernelArgType) + ) + for idx, arg in enumerate(args): + if isinstance(arg, int): + #kargs = ... + ktypes[idx] = _arg_data_type._INT + free(kargs) + free(ktypes) + return None + + cpdef void wait (self): + DPPLQueue_Wait(self._queue_ptr) + cdef class _SyclQueueManager: + ''' Wrapper for the C API's sycl queue manager interface. + ''' + def _set_as_current_queue (self, device_ty, device_id): cdef DPPLSyclQueueRef queue_ptr if device_ty == device_type.gpu: From a9d8cf7e7eb08a43b32709c588d83a6823592184 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Fri, 25 Sep 2020 16:35:09 -0500 Subject: [PATCH 69/93] WIP stuff.. --- backends/source/dppl_sycl_queue_interface.cpp | 2 ++ backends/tests/test_sycl_queue_interface.cpp | 8 +++++--- dpctl/_sycl_core.pxd | 1 - 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index e02bc4b714..993c504243 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -147,6 +147,8 @@ DPPLQueue_Submit (__dppl_keep const DPPLSyclKernelRef KRef, event e; e = Queue->submit([&](handler& cgh) { + // \todo cgh.depends_on + for (auto i = 0ul; i < NArgs; ++i) { // \todo add support for Sycl buffers // \todo handle errors properly diff --git a/backends/tests/test_sycl_queue_interface.cpp b/backends/tests/test_sycl_queue_interface.cpp index fd9dda8688..2c5938a65a 100644 --- a/backends/tests/test_sycl_queue_interface.cpp +++ b/backends/tests/test_sycl_queue_interface.cpp @@ -136,7 +136,8 @@ TEST_F (TestDPPLSyclQueueInterface, CheckSubmit) // Submit the add kernel void *args[3] = { unwrap(a), unwrap(b), unwrap(c) }; - DPPLKernelArgType addKernelArgTypes[] = { DPPL_VOID_PTR, + DPPLKernelArgType addKernelArgTypes[] = { + DPPL_VOID_PTR, DPPL_VOID_PTR, DPPL_VOID_PTR }; @@ -151,8 +152,9 @@ TEST_F (TestDPPLSyclQueueInterface, CheckSubmit) // Create kernel args for axpy float d = 10.0; - void *args2[4] = { unwrap(a), unwrap(b), unwrap(c) , (void*)&d}; - DPPLKernelArgType addKernelArgTypes2[] = { DPPL_VOID_PTR, + void *args2[4] = { unwrap(a), unwrap(b), unwrap(c) , (void*)&d }; + DPPLKernelArgType addKernelArgTypes2[] = { + DPPL_VOID_PTR, DPPL_VOID_PTR, DPPL_VOID_PTR, DPPL_FLOAT diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index d4b0d125c0..c8c9b19095 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -96,7 +96,6 @@ cdef class SyclQueue: cdef DPPLSyclQueueRef _queue_ptr cdef SyclContext _context cdef SyclDevice _device - cdef list _events @staticmethod cdef SyclQueue _create (DPPLSyclQueueRef qref) From 3d21faae34faaa9999e0cf61198924ba4dddf5bd Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 25 Sep 2020 18:58:58 -0500 Subject: [PATCH 70/93] Add wrapper for SYCL queue::memcpy(). --- backends/include/dppl_sycl_queue_interface.h | 9 +++ backends/source/dppl_sycl_queue_interface.cpp | 8 +++ dpctl/_memory.pxd | 46 ++++++++++++ dpctl/_memory.pyx | 3 - dpctl/_sycl_core.pxd | 1 + dpctl/backend.pxd | 3 + dpctl/sycl_core.pyx | 18 ++++- dpctl/tests/test_sycl_queue_memcpy.py | 70 +++++++++++++++++++ dpctl/tests/test_sycl_usm.py | 8 +++ 9 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 dpctl/_memory.pxd create mode 100644 dpctl/tests/test_sycl_queue_memcpy.py diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index 7b9002e8de..653cf8a0d3 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -63,4 +63,13 @@ DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef); DPPL_API void DPPLQueue_Delete (__dppl_take DPPLSyclQueueRef QRef); +/*! + * @brief C-API wrapper for sycl::queue::memcpy. It waits an event. + * + * @param QRef A DPPLSyclQueueRef pointer that gets deleted. + */ +DPPL_API +void DPPLQueue_memcpy (__dppl_keep const DPPLSyclQueueRef QRef, + void *Dest, const void *Src, size_t Count); + DPPL_C_EXTERN_C_END diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index af3fd7a611..3f933294c2 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -61,3 +61,11 @@ void DPPLQueue_Delete (__dppl_take DPPLSyclQueueRef QRef) { delete unwrap(QRef); } + +void DPPLQueue_memcpy (__dppl_take const DPPLSyclQueueRef QRef, + void *Dest, const void *Src, size_t Count) +{ + auto Q = unwrap(QRef); + auto event = Q->memcpy(Dest, Src, Count); + event.wait(); +} diff --git a/dpctl/_memory.pxd b/dpctl/_memory.pxd new file mode 100644 index 0000000000..3c868125ca --- /dev/null +++ b/dpctl/_memory.pxd @@ -0,0 +1,46 @@ +##===--------------- _memory.pxd - dpctl module --------*- Cython -*-------===## +## +## Data Parallel Control (dpCtl) +## +## Copyright 2020 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. +## +##===----------------------------------------------------------------------===## + +# distutils: language = c++ +# cython: language_level=3 + +from .backend cimport DPPLSyclUSMRef +from ._sycl_core cimport SyclQueue + + +cdef class Memory: + cdef DPPLSyclUSMRef memory_ptr + cdef Py_ssize_t nbytes + cdef SyclQueue queue + + cdef _cinit(self, Py_ssize_t nbytes, ptr_type, SyclQueue queue) + cdef _getbuffer(self, Py_buffer *buffer, int flags) + + +cdef class MemoryUSMShared(Memory): + pass + + +cdef class MemoryUSMHost(Memory): + pass + + +cdef class MemoryUSMDevice(Memory): + pass diff --git a/dpctl/_memory.pyx b/dpctl/_memory.pyx index 97ff0dcdc5..1671d3bb7c 100644 --- a/dpctl/_memory.pyx +++ b/dpctl/_memory.pyx @@ -36,9 +36,6 @@ from cpython cimport Py_buffer cdef class Memory: - cdef DPPLSyclUSMRef memory_ptr - cdef Py_ssize_t nbytes - cdef SyclQueue queue cdef _cinit(self, Py_ssize_t nbytes, ptr_type, SyclQueue queue): cdef DPPLSyclUSMRef p diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index caa18814d8..b10afa787e 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -63,3 +63,4 @@ cdef class SyclQueue: cpdef SyclContext get_sycl_context (self) cpdef SyclDevice get_sycl_device (self) cdef DPPLSyclQueueRef get_queue_ref (self) + cpdef memcpy (self, dest, src, int count) diff --git a/dpctl/backend.pxd b/dpctl/backend.pxd index f15f51b529..11fbf5d189 100644 --- a/dpctl/backend.pxd +++ b/dpctl/backend.pxd @@ -78,6 +78,9 @@ cdef extern from "dppl_sycl_queue_interface.h": except+ cdef DPPLSyclDeviceRef DPPLQueue_GetDevice (const DPPLSyclQueueRef Q) \ except + + cdef void DPPLQueue_memcpy (const DPPLSyclQueueRef Q, + void *Dest, const void *Src, size_t Count) \ + except + cdef extern from "dppl_sycl_queue_manager.h": diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 15f51c4928..de3ba846f5 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -29,7 +29,8 @@ from __future__ import print_function from enum import Enum, auto import logging -from dpctl.backend cimport * +from .backend cimport * +from ._memory cimport Memory _logger = logging.getLogger(__name__) @@ -132,6 +133,21 @@ cdef class SyclQueue: cdef DPPLSyclQueueRef get_queue_ref (self): return self.queue_ptr + cpdef memcpy (self, dest, src, int count): + cdef void *c_dest, *c_src + + if isinstance(dest, Memory): + c_dest = (dest).memory_ptr + else: + raise TypeError("Parameter dest should be Memory.") + + if isinstance(src, Memory): + c_src = (src).memory_ptr + else: + raise TypeError("Parameter src should be Memory.") + + DPPLQueue_memcpy(self.queue_ptr, c_dest, c_src, count) + cdef class _SyclQueueManager: def _set_as_current_queue (self, device_ty, device_id): diff --git a/dpctl/tests/test_sycl_queue_memcpy.py b/dpctl/tests/test_sycl_queue_memcpy.py new file mode 100644 index 0000000000..c0056fbd52 --- /dev/null +++ b/dpctl/tests/test_sycl_queue_memcpy.py @@ -0,0 +1,70 @@ +##===---------- test_sycl_queue_manager.py - dpctl -------*- Python -*----===## +## +## Data Parallel Control (dpCtl) +## +## Copyright 2020 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 +### Defines unit test cases for the SyclQueueManager class in sycl_core.pyx. +##===----------------------------------------------------------------------===## + +import dpctl +import unittest + + + +class TestQueueMemcpy (unittest.TestCase): + + def _create_memory (self): + nbytes = 1024 + queue = dpctl.get_current_queue() + mobj = dpctl._memory.MemoryUSMShared(nbytes, queue) + return mobj + + def test_memcpy_copy_usm_to_usm (self): + mobj1 = self._create_memory() + mobj2 = self._create_memory() + q = dpctl.get_current_queue() + + mv1 = memoryview(mobj1) + mv2 = memoryview(mobj2) + + mv1[:3] = b'123' + + q.memcpy(mobj2, mobj1, 3) + + self.assertEqual(mv2[:3], b'123') + + def test_memcpy_type_error (self): + mobj = self._create_memory() + q = dpctl.get_current_queue() + + with self.assertRaises(TypeError) as cm: + q.memcpy(None, mobj, 3) + + self.assertEqual(type(cm.exception), TypeError) + self.assertEqual(str(cm.exception), "Parameter dest should be Memory.") + + with self.assertRaises(TypeError) as cm: + q.memcpy(mobj, None, 3) + + self.assertEqual(type(cm.exception), TypeError) + self.assertEqual(str(cm.exception), "Parameter src should be Memory.") + + +if __name__ == '__main__': + unittest.main() diff --git a/dpctl/tests/test_sycl_usm.py b/dpctl/tests/test_sycl_usm.py index 3737a01a4c..895a9f5180 100644 --- a/dpctl/tests/test_sycl_usm.py +++ b/dpctl/tests/test_sycl_usm.py @@ -78,6 +78,13 @@ def test_memory_gpu_context (self): self.assertTrue(usm_type in ['unknown', 'shared']) + def test_buffer_protocol (self): + mobj = self._create_memory() + mv1 = memoryview(mobj) + mv2 = memoryview(mobj) + self.assertEqual(mv1, mv2) + + class TestMemoryUSMBase: """ Base tests for MemoryUSM* """ @@ -116,5 +123,6 @@ class TestMemoryUSMDevice(TestMemoryUSMBase, unittest.TestCase): MemoryUSMClass = MemoryUSMDevice usm_type = 'device' + if __name__ == '__main__': unittest.main() From 558458875b9e08bff7e7b95e746edca6c8da5ea7 Mon Sep 17 00:00:00 2001 From: Sergey Pokhodenko Date: Fri, 25 Sep 2020 19:03:55 -0500 Subject: [PATCH 71/93] Fix comment. --- dpctl/tests/test_sycl_queue_memcpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpctl/tests/test_sycl_queue_memcpy.py b/dpctl/tests/test_sycl_queue_memcpy.py index c0056fbd52..6cb95ea63e 100644 --- a/dpctl/tests/test_sycl_queue_memcpy.py +++ b/dpctl/tests/test_sycl_queue_memcpy.py @@ -19,7 +19,7 @@ ##===----------------------------------------------------------------------===## ### ### \file -### Defines unit test cases for the SyclQueueManager class in sycl_core.pyx. +### Defines unit test cases for the SyclQueue.memcpy in sycl_core.pyx. ##===----------------------------------------------------------------------===## import dpctl From 2f2cde1306ee74737e4db6b6e72cab856fb1ed95 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sat, 26 Sep 2020 13:36:50 -0500 Subject: [PATCH 72/93] WIP stuff... --- dpctl/sycl_core.pyx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index f2c53fe148..acabd97579 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -193,6 +193,7 @@ cdef class SyclProgram: name = kernel_name.encode('utf8') return DPPLProgram_HasKernel(self._program_ptr, name) +#include ctypes cdef class SyclQueue: ''' Wrapper class for a Sycl queue. @@ -226,8 +227,8 @@ cdef class SyclQueue: len(args) * sizeof(DPPLKernelArgType) ) for idx, arg in enumerate(args): - if isinstance(arg, int): - #kargs = ... + if isinstance(arg, ctypes.c_int): + kargs[idx] = (ctypes.addressof(arg)) ktypes[idx] = _arg_data_type._INT free(kargs) free(ktypes) From 8d99c84dc0aceec118a34764cb3b2f57fd21aa48 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sun, 27 Sep 2020 19:01:22 -0500 Subject: [PATCH 73/93] WIP changes to queue submit. --- dpctl/.gitignore | 3 +- dpctl/_sycl_core.pxd | 26 +++--- dpctl/backend.pxd | 4 +- dpctl/ocldrv.py | 2 +- dpctl/sycl_core.pyx | 167 ++++++++++++++++++++++++++--------- scripts/build_for_develop.sh | 2 +- 6 files changed, 145 insertions(+), 59 deletions(-) diff --git a/dpctl/.gitignore b/dpctl/.gitignore index 1edc29f09d..dc4316f4bd 100644 --- a/dpctl/.gitignore +++ b/dpctl/.gitignore @@ -1,3 +1,4 @@ *.so *.cpp -*.c \ No newline at end of file +*.c +include/* \ No newline at end of file diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index c8c9b19095..4f0aba7109 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -32,7 +32,7 @@ from .backend cimport * cdef class SyclContext: ''' Wrapper class for a Sycl Context ''' - cdef DPPLSyclContextRef _ctxt_ptr + cdef DPPLSyclContextRef _ctxt_ref @staticmethod cdef SyclContext _create (DPPLSyclContextRef ctxt) @@ -42,23 +42,25 @@ cdef class SyclContext: cdef class SyclDevice: ''' Wrapper class for a Sycl Device ''' - cdef DPPLSyclDeviceRef _device_ptr + cdef DPPLSyclDeviceRef _device_ref cdef const char *_vendor_name cdef const char *_device_name cdef const char *_driver_version @staticmethod cdef SyclDevice _create (DPPLSyclDeviceRef dref) - cdef DPPLSyclDeviceRef get_device_ptr (self) + cdef DPPLSyclDeviceRef get_device_ref (self) cdef class SyclEvent: ''' Wrapper class for a Sycl Event ''' - cdef DPPLSyclEventRef _event_ptr + cdef DPPLSyclEventRef _event_ref + cdef list _args @staticmethod - cdef SyclEvent _create (DPPLSyclEventRef e) + cdef SyclEvent _create (DPPLSyclEventRef e, list args) + cdef DPPLSyclEventRef get_event_ref (self) cpdef void wait (self) @@ -66,9 +68,9 @@ cdef class SyclKernel: ''' Wraps a sycl::kernel object created from an OpenCL interoperability kernel. ''' - cdef DPPLSyclKernelRef _kernel_ptr + cdef DPPLSyclKernelRef _kernel_ref cdef const char *_function_name - cdef DPPLSyclKernelRef get_kernel_ptr (self) + cdef DPPLSyclKernelRef get_kernel_ref (self) @staticmethod cdef SyclKernel _create (DPPLSyclKernelRef kref) @@ -82,26 +84,28 @@ cdef class SyclProgram: SyclProgram can be created from either a source string or a SPIR-V binary file. ''' - cdef DPPLSyclProgramRef _program_ptr + cdef DPPLSyclProgramRef _program_ref @staticmethod cdef SyclProgram _create (DPPLSyclProgramRef pref) - cdef DPPLSyclProgramRef get_program_ptr (self) + cdef DPPLSyclProgramRef get_program_ref (self) cpdef SyclKernel get_sycl_kernel(self, str kernel_name) cdef class SyclQueue: ''' Wrapper class for a Sycl queue. ''' - cdef DPPLSyclQueueRef _queue_ptr + cdef DPPLSyclQueueRef _queue_ref cdef SyclContext _context cdef SyclDevice _device + cdef _raise_queue_submit_error (self, fname, errcode) + @staticmethod cdef SyclQueue _create (DPPLSyclQueueRef qref) cpdef SyclContext get_sycl_context (self) cpdef SyclDevice get_sycl_device (self) cdef DPPLSyclQueueRef get_queue_ref (self) cpdef SyclEvent submit (self, SyclKernel kernel, list args, \ - const size_t [:]range, size_t ndims) + list gSize, list lSize) cpdef void wait (self) diff --git a/dpctl/backend.pxd b/dpctl/backend.pxd index 5a96be6ed7..fe56271468 100644 --- a/dpctl/backend.pxd +++ b/dpctl/backend.pxd @@ -118,8 +118,8 @@ cdef extern from "dppl_sycl_queue_interface.h": _UNSIGNED_INT 'DPPL_INT', _LONG 'DPPL_LONG', _UNSIGNED_LONG 'DPPL_UNSIGNED_LONG', - _LONG_LONG 'LONG_LONG', - _UNSIGNED_LONG_LONG 'UNSIGNED_LONG_LONG', + _LONG_LONG 'DPPL_LONG_LONG', + _UNSIGNED_LONG_LONG 'DPPL_UNSIGNED_LONG_LONG', _SIZE_T 'DPPL_SIZE_T', _FLOAT 'DPPL_FLOAT', _DOUBLE 'DPPL_DOUBLE', diff --git a/dpctl/ocldrv.py b/dpctl/ocldrv.py index 49601475e7..e4d82887fa 100644 --- a/dpctl/ocldrv.py +++ b/dpctl/ocldrv.py @@ -342,7 +342,7 @@ def __del__(self): pass def retain_context(self): - ''' Increment the refrence count of the OpenCL context object. + ''' Increment the reference count of the OpenCL context object. ''' retval = (lib.retain_dp_context(self._env_ptr.context)) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index acabd97579..2f5243e8bb 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -39,13 +39,19 @@ class device_type(Enum): cpu = auto() -cdef class UnsupportedDeviceTypeError(Exception): +cdef class UnsupportedDeviceTypeError (Exception): '''This exception is raised when a device type other than CPU or GPU is encountered. ''' pass -cdef class SyclProgramCompilationError(Exception): +cdef class SyclProgramCompilationError (Exception): + '''This exception is raised when a sycl program could not be built from + either a spirv binary file or a string source. + ''' + pass + +cdef class SyclKernelSubmitError (Exception): '''This exception is raised when a sycl program could not be built from either a spirv binary file or a string source. ''' @@ -56,14 +62,14 @@ cdef class SyclContext: @staticmethod cdef SyclContext _create (DPPLSyclContextRef ctxt): cdef SyclContext ret = SyclContext.__new__(SyclContext) - ret._ctxt_ptr = ctxt + ret._ctxt_ref = ctxt return ret def __dealloc__ (self): - DPPLContext_Delete(self._ctxt_ptr) + DPPLContext_Delete(self._ctxt_ref) cdef DPPLSyclContextRef get_context_ref (self): - return self._ctxt_ptr + return self._ctxt_ref cdef class SyclDevice: @@ -73,14 +79,14 @@ cdef class SyclDevice: @staticmethod cdef SyclDevice _create (DPPLSyclDeviceRef dref): cdef SyclDevice ret = SyclDevice.__new__(SyclDevice) - ret._device_ptr = dref + ret._device_ref = dref ret._vendor_name = DPPLDevice_GetVendorName(dref) ret._device_name = DPPLDevice_GetName(dref) ret._driver_version = DPPLDevice_GetDriverInfo(dref) return ret def __dealloc__ (self): - DPPLDevice_Delete(self._device_ptr) + DPPLDevice_Delete(self._device_ref) DPPLCString_Delete(self._device_name) DPPLCString_Delete(self._vendor_name) DPPLCString_Delete(self._driver_version) @@ -88,7 +94,7 @@ cdef class SyclDevice: def dump_device_info (self): ''' Print information about the SYCL device. ''' - DPPLDevice_DumpInfo(self._device_ptr) + DPPLDevice_DumpInfo(self._device_ref) def get_device_name (self): ''' Returns the name of the device as a string @@ -108,10 +114,10 @@ cdef class SyclDevice: ''' return self._driver_version.decode() - cdef DPPLSyclDeviceRef get_device_ptr (self): + cdef DPPLSyclDeviceRef get_device_ref (self): ''' Returns the DPPLSyclDeviceRef pointer for this class. ''' - return self._device_ptr + return self._device_ref cdef class SyclEvent: @@ -119,16 +125,22 @@ cdef class SyclEvent: ''' @staticmethod - cdef SyclEvent _create (DPPLSyclEventRef eref): + cdef SyclEvent _create (DPPLSyclEventRef eref, list args): cdef SyclEvent ret = SyclEvent.__new__(SyclEvent) - ret._event_ptr = eref + ret._event_ref = eref + ret._args = args return ret def __dealloc__ (self): - DPPLEvent_Delete(self._event_ptr) + DPPLEvent_Delete(self._event_ref) + + cdef DPPLSyclEventRef get_event_ref (self): + ''' Returns the DPPLSyclEventRef pointer for this class. + ''' + return self._event_ref cpdef void wait (self): - DPPLEvent_Wait(self._event_ptr) + DPPLEvent_Wait(self._event_ref) cdef class SyclKernel: @@ -139,12 +151,12 @@ cdef class SyclKernel: @staticmethod cdef SyclKernel _create (DPPLSyclKernelRef kref): cdef SyclKernel ret = SyclKernel.__new__(SyclKernel) - ret._kernel_ptr = kref + ret._kernel_ref = kref ret._function_name = DPPLKernel_GetFunctionName(kref) return ret def __dealloc__ (self): - DPPLKernel_Delete(self._kernel_ptr) + DPPLKernel_Delete(self._kernel_ref) DPPLCString_Delete(self._function_name) def get_function_name (self): @@ -155,12 +167,12 @@ cdef class SyclKernel: def get_num_args (self): ''' Returns the number of arguments for this kernel function. ''' - return DPPLKernel_GetNumArgs(self._kernel_ptr) + return DPPLKernel_GetNumArgs(self._kernel_ref) - cdef DPPLSyclKernelRef get_kernel_ptr (self): + cdef DPPLSyclKernelRef get_kernel_ref (self): ''' Returns the DPPLSyclKernelRef pointer for this SyclKernel. ''' - return self._kernel_ptr + return self._kernel_ref cdef class SyclProgram: @@ -175,25 +187,26 @@ cdef class SyclProgram: @staticmethod cdef SyclProgram _create (DPPLSyclProgramRef pref): cdef SyclProgram ret = SyclProgram.__new__(SyclProgram) - ret._program_ptr = pref + ret._program_ref = pref return ret def __dealloc__ (self): - DPPLProgram_Delete(self._program_ptr) + DPPLProgram_Delete(self._program_ref) - cdef DPPLSyclProgramRef get_program_ptr (self): - return self._program_ptr + cdef DPPLSyclProgramRef get_program_ref (self): + return self._program_ref cpdef SyclKernel get_sycl_kernel(self, str kernel_name): name = kernel_name.encode('utf8') - return SyclKernel._create(DPPLProgram_GetKernel(self._program_ptr, + return SyclKernel._create(DPPLProgram_GetKernel(self._program_ref, name)) def has_sycl_kernel(self, str kernel_name): name = kernel_name.encode('utf8') - return DPPLProgram_HasKernel(self._program_ptr, name) + return DPPLProgram_HasKernel(self._program_ref, name) -#include ctypes +import ctypes +from libc.stdio cimport printf cdef class SyclQueue: ''' Wrapper class for a Sycl queue. @@ -204,11 +217,17 @@ cdef class SyclQueue: cdef SyclQueue ret = SyclQueue.__new__(SyclQueue) ret._context = SyclContext._create(DPPLQueue_GetContext(qref)) ret._device = SyclDevice._create(DPPLQueue_GetDevice(qref)) - ret._queue_ptr = qref + ret._queue_ref = qref return ret def __dealloc__ (self): - DPPLQueue_Delete(self._queue_ptr) + DPPLQueue_Delete(self._queue_ref) + + cdef _raise_queue_submit_error (self, fname, errcode): + e = SyclKernelSubmitError("Kernel submission to Sycl queue failed.") + e.fname = fname + e.code = errcode + raise e cpdef SyclContext get_sycl_context (self): return self._context @@ -217,25 +236,87 @@ cdef class SyclQueue: return self._device cdef DPPLSyclQueueRef get_queue_ref (self): - return self._queue_ptr + return self._queue_ref cpdef SyclEvent submit (self, SyclKernel kernel, list args, \ - const size_t[:] range, size_t ndims): - cdef llist = args - cdef void *kargs = malloc(len(args) * sizeof(void*)) - cdef DPPLKernelArgType *ktypes = malloc( + list gSize, list lSize): + + cdef void **kargs = malloc(len(args) * sizeof(void*)) + cdef DPPLKernelArgType *kargty = malloc( len(args) * sizeof(DPPLKernelArgType) ) + cdef size_t Range[3] + Range[0] = 1024 + Range[1] = 1 + Range[2] = 2 + for idx, arg in enumerate(args): - if isinstance(arg, ctypes.c_int): - kargs[idx] = (ctypes.addressof(arg)) - ktypes[idx] = _arg_data_type._INT + if isinstance(arg, ctypes.c_char): + charval = (arg.value) + kargs[idx]= (&charval) + kargty[idx] = _arg_data_type._CHAR + elif isinstance(arg, ctypes.c_int): + intval = (arg.value) + kargs[idx]= (&intval) + kargty[idx] = _arg_data_type._INT + elif isinstance(arg, ctypes.c_uint): + unintval = (arg.value) + kargs[idx]= (&unintval) + kargty[idx] = _arg_data_type._UNSIGNED_INT + elif isinstance(arg, ctypes.c_long): + longval = (arg.value) + kargs[idx]= (&longval) + kargty[idx] = _arg_data_type._LONG + elif isinstance(arg, ctypes.c_longlong): + longlongval = (arg.value) + kargs[idx]= (&longlongval) + kargty[idx] = _arg_data_type._LONG_LONG + elif isinstance(arg, ctypes.c_ulonglong): + ulonglongval = (arg.value) + kargs[idx]= (&ulonglongval) + kargty[idx] = _arg_data_type._UNSIGNED_LONG_LONG + elif isinstance(arg, ctypes.c_short): + shortval = (arg.value) + kargs[idx]= (&shortval) + kargty[idx] = _arg_data_type._SHORT + elif isinstance(arg, ctypes.c_size_t): + sizetval = (arg.value) + kargs[idx]= (&sizetval) + kargty[idx] = _arg_data_type._SIZE_T + #elif isinstance(arg, ctypes.c_float): + # floatval = (arg.value) + # kargs[idx]= (&floatval) + # kargty[idx] = _arg_data_type._FLOAT + elif isinstance(arg, ctypes.c_double): + doubleval = (arg.value) + kargs[idx]= (&doubleval) + kargty[idx] = _arg_data_type._DOUBLE + elif isinstance(arg, memoryview): + print(arg) + kargs[idx]= arg + kargty[idx] = _arg_data_type._VOID_PTR + else: + raise TypeError("Unsupported type for a kernel argument") + + cdef DPPLSyclEventRef Eref = DPPLQueue_Submit(kernel.get_kernel_ref(), + self.get_queue_ref(), + kargs, + kargty, + len(args), + Range, + 1) + free(kargs) - free(ktypes) - return None + free(kargty) + + if Eref is NULL: + # \todo get the error number from dpctl-capi + self._raise_queue_submit_error("DPPLQueue_Submit", -1) + + return SyclEvent._create(Eref, args) cpdef void wait (self): - DPPLQueue_Wait(self._queue_ptr) + DPPLQueue_Wait(self._queue_ref) cdef class _SyclQueueManager: @@ -243,16 +324,16 @@ cdef class _SyclQueueManager: ''' def _set_as_current_queue (self, device_ty, device_id): - cdef DPPLSyclQueueRef queue_ptr + cdef DPPLSyclQueueRef queue_ref if device_ty == device_type.gpu: - queue_ptr = DPPLQueueMgr_PushQueue(_device_type._GPU, device_id) + queue_ref = DPPLQueueMgr_PushQueue(_device_type._GPU, device_id) elif device_ty == device_type.cpu: - queue_ptr = DPPLQueueMgr_PushQueue(_device_type._CPU, device_id) + queue_ref = DPPLQueueMgr_PushQueue(_device_type._CPU, device_id) else: e = UnsupportedDeviceTypeError("Device can only be cpu or gpu") raise e - return SyclQueue._create(queue_ptr) + return SyclQueue._create(queue_ref) def _remove_current_queue (self): DPPLQueueMgr_PopQueue() diff --git a/scripts/build_for_develop.sh b/scripts/build_for_develop.sh index e035ff6962..bb5c92e907 100755 --- a/scripts/build_for_develop.sh +++ b/scripts/build_for_develop.sh @@ -26,7 +26,7 @@ cmake \ -DGTEST_LIB_DIR=${CONDA_PREFIX}/lib \ ../backends -make V=1 -n -j 4 && make check && make install +make V=1 -n -j 4 && make install popd cp install/lib/*.so dpctl/ From cd354234e75860d8140f31455c96e6e3eab575b7 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Mon, 28 Sep 2020 09:07:05 -0500 Subject: [PATCH 74/93] Handle exception. --- backends/source/dppl_sycl_queue_interface.cpp | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index 993c504243..9aa77a15ea 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -146,32 +146,38 @@ DPPLQueue_Submit (__dppl_keep const DPPLSyclKernelRef KRef, auto Queue = unwrap(QRef); event e; - e = Queue->submit([&](handler& cgh) { - // \todo cgh.depends_on + try { + e = Queue->submit([&](handler& cgh) { + // \todo cgh.depends_on - for (auto i = 0ul; i < NArgs; ++i) { - // \todo add support for Sycl buffers - // \todo handle errors properly - if(!set_kernel_arg(cgh, i, Args[i], ArgTypes[i])) + for (auto i = 0ul; i < NArgs; ++i) { + // \todo add support for Sycl buffers + // \todo handle errors properly + if(!set_kernel_arg(cgh, i, Args[i], ArgTypes[i])) + exit(1); + } + switch(NDims) + { + case 1: + cgh.parallel_for(range<1>{Range[0]}, *Kernel); + break; + case 2: + cgh.parallel_for(range<2>{Range[0], Range[1]}, *Kernel); + break; + case 3: + cgh.parallel_for(range<3>{Range[0], Range[1], Range[2]}, *Kernel); + break; + default: + // \todo handle the error + std::cerr << "Range cannot be greater than three dimensions.\n"; exit(1); - } - switch(NDims) - { - case 1: - cgh.parallel_for(range<1>{Range[0]}, *Kernel); - break; - case 2: - cgh.parallel_for(range<2>{Range[0], Range[1]}, *Kernel); - break; - case 3: - cgh.parallel_for(range<3>{Range[0], Range[1], Range[2]}, *Kernel); - break; - default: - // \todo handle the error - std::cerr << "Range cannot be greater than three dimensions.\n"; - exit(1); - } - }); + } + }); + } catch (runtime_error re) { + // \todo fix error handling + std::cerr << re.what() << '\n'; + return nullptr; + } return wrap(new event(e)); } From ec7a0dd41162b1951558fe473a05f4326b79f259 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 28 Sep 2020 12:41:42 -0500 Subject: [PATCH 75/93] added processing of ctype.c_float --- dpctl/sycl_core.pyx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 2f5243e8bb..b99cd08b76 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -246,6 +246,16 @@ cdef class SyclQueue: len(args) * sizeof(DPPLKernelArgType) ) cdef size_t Range[3] + cdef char charval + cdef int intval + cdef unsigned int uintval + cdef long longval + cdef long long longlongval + cdef unsigned long long ulonglongval + cdef short shortval + cdef size_t sizetval + cdef double doubleval + cdef float floatval Range[0] = 1024 Range[1] = 1 Range[2] = 2 @@ -283,10 +293,10 @@ cdef class SyclQueue: sizetval = (arg.value) kargs[idx]= (&sizetval) kargty[idx] = _arg_data_type._SIZE_T - #elif isinstance(arg, ctypes.c_float): - # floatval = (arg.value) - # kargs[idx]= (&floatval) - # kargty[idx] = _arg_data_type._FLOAT + elif isinstance(arg, ctypes.c_float): + floatval = (arg.value) + kargs[idx]= (&floatval) + kargty[idx] = _arg_data_type._FLOAT elif isinstance(arg, ctypes.c_double): doubleval = (arg.value) kargs[idx]= (&doubleval) From 860d2e16c70390a7e6be160b5acd54f05d0b0d9c Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 28 Sep 2020 13:57:56 -0500 Subject: [PATCH 76/93] added processing of gSize --- dpctl/sycl_core.pyx | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index b99cd08b76..e4ae93a30a 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -256,9 +256,26 @@ cdef class SyclQueue: cdef size_t sizetval cdef double doubleval cdef float floatval - Range[0] = 1024 - Range[1] = 1 - Range[2] = 2 + cdef int gs_len = len(gSize) + cdef int ls_len = len(lSize) + + if (gs_len != ls_len): + raise ValueError("") + + if (gs_len == 1): + Range[0] = gSize[0] + Range[1] = 1 + Range[2] = 1 + elif (gs_len == 2): + Range[0] = gSize[0] + Range[1] = gSize[1] + Range[2] = 1 + elif (gs_len == 3): + Range[0] = gSize[0] + Range[1] = gSize[1] + Range[2] = gSize[2] + else: + raise ValueError("") for idx, arg in enumerate(args): if isinstance(arg, ctypes.c_char): From 5f86e2f4ab05e6087b8b4e43d80ee6a5f832d77d Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 28 Sep 2020 14:20:26 -0500 Subject: [PATCH 77/93] WIP towards passing USM memory to kernel --- dpctl/sycl_core.pyx | 5 ++- dpctl/tests/test_kernel_submit.py | 53 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 dpctl/tests/test_kernel_submit.py diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index e4ae93a30a..d1e53bd203 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -318,9 +318,8 @@ cdef class SyclQueue: doubleval = (arg.value) kargs[idx]= (&doubleval) kargty[idx] = _arg_data_type._DOUBLE - elif isinstance(arg, memoryview): - print(arg) - kargs[idx]= arg + elif isinstance(arg, Memory): + kargs[idx]= (arg._pointer) kargty[idx] = _arg_data_type._VOID_PTR else: raise TypeError("Unsupported type for a kernel argument") diff --git a/dpctl/tests/test_kernel_submit.py b/dpctl/tests/test_kernel_submit.py new file mode 100644 index 0000000000..75c4f2cda7 --- /dev/null +++ b/dpctl/tests/test_kernel_submit.py @@ -0,0 +1,53 @@ +import ctypes +import dpctl +import array +import numpy as np + +def test_create_program_from_source (): + q = dpctl.get_current_queue() + q.get_sycl_device().dump_device_info() + ctx = q.get_sycl_context() + oclSrc = " \ + kernel void add(global int* a, global int* b, global int* c) { \ + size_t index = get_global_id(0); \ + c[index] = a[index] + b[index]; \ + } \ + kernel void axpy(global int* a, global int* b, global int* c, int d) { \ + size_t index = get_global_id(0); \ + c[index] = a[index] + d*b[index]; \ + }" + prog = dpctl.create_program_from_source(q,oclSrc) + print(prog) + print(prog.has_sycl_kernel('add')) + print(prog.has_sycl_kernel('axpy')) + addKernel = prog.get_sycl_kernel('add') + print(addKernel) + print(addKernel.get_function_name()) + print(addKernel.get_num_args()) + axpyKernel = prog.get_sycl_kernel('axpy') + print(axpyKernel) + print(axpyKernel.get_function_name()) + print(axpyKernel.get_num_args()) + + a = np.arange(1024) + b = np.arange(1024) + c = np.zeros(1024) + d = 2 + print(a) + args = [] + + print(type(a.data)) + args.append(a.data) + args.append(b.data) + args.append(c.data) + args.append(ctypes.c_int(d)) + + r = [ 1024, 1, 1 ] + + q.submit(axpyKernel, args, r, r) + # self.assertIsNotNone(prog) + + # self.assertTrue() + # self.assertTrue(prog.has_sycl_kernel("axpy")) + +test_create_program_from_source() From 872dd8fb84575803fe78e92bbfa4db8870c289de Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 28 Sep 2020 14:50:49 -0500 Subject: [PATCH 78/93] fixed Cython errors --- dpctl/sycl_core.pyx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 9d40b91582..b345f96611 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -348,7 +348,8 @@ cdef class SyclQueue: DPPLQueue_Wait(self._queue_ref) cpdef memcpy (self, dest, src, int count): - cdef void *c_dest, *c_src + cdef void *c_dest + cdef void *c_src if isinstance(dest, Memory): c_dest = (dest).memory_ptr @@ -360,7 +361,7 @@ cdef class SyclQueue: else: raise TypeError("Parameter src should be Memory.") - DPPLQueue_memcpy(self.queue_ptr, c_dest, c_src, count) + DPPLQueue_memcpy(self._queue_ref, c_dest, c_src, count) cdef class _SyclQueueManager: From fa4a3baa9aba86c24e86563a6630c09ad89e9439 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 28 Sep 2020 14:51:05 -0500 Subject: [PATCH 79/93] fixed test_kernel_sybmit --- dpctl/tests/test_kernel_submit.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/dpctl/tests/test_kernel_submit.py b/dpctl/tests/test_kernel_submit.py index 75c4f2cda7..7dd6d146f4 100644 --- a/dpctl/tests/test_kernel_submit.py +++ b/dpctl/tests/test_kernel_submit.py @@ -1,5 +1,6 @@ import ctypes import dpctl +import dpctl._memory as dpctl_mem import array import numpy as np @@ -28,23 +29,31 @@ def test_create_program_from_source (): print(axpyKernel) print(axpyKernel.get_function_name()) print(axpyKernel.get_num_args()) - - a = np.arange(1024) - b = np.arange(1024) - c = np.zeros(1024) + + abuf = dpctl_mem.MemoryUSMShared(1024*np.dtype('i').itemsize) + bbuf = dpctl_mem.MemoryUSMShared(1024*np.dtype('i').itemsize) + cbuf = dpctl_mem.MemoryUSMShared(1024*np.dtype('i').itemsize) + a = np.ndarray((1024), buffer=abuf, dtype='i') + b = np.ndarray((1024), buffer=bbuf, dtype='i') + c = np.ndarray((1024), buffer=cbuf, dtype='i') + a[:] = np.arange(1024) + b[:] = np.arange(1024, 0, -1) + c[:] = 0 d = 2 - print(a) args = [] - print(type(a.data)) - args.append(a.data) - args.append(b.data) - args.append(c.data) + args.append(a.base) + args.append(b.base) + args.append(c.base) args.append(ctypes.c_int(d)) r = [ 1024, 1, 1 ] - q.submit(axpyKernel, args, r, r) + e = q.submit(axpyKernel, args, r, r) + e.wait() + + print(c) + print(a + d * b) # self.assertIsNotNone(prog) # self.assertTrue() From b6c68fb8f4dfa78cf5b939474ef7469aeddbacd9 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Mon, 28 Sep 2020 15:39:50 -0500 Subject: [PATCH 80/93] Minor change. --- dpctl/_sycl_core.pxd | 4 ++-- dpctl/sycl_core.pyx | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index 776f0c8394..6f814cba5a 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -106,7 +106,7 @@ cdef class SyclQueue: cpdef SyclContext get_sycl_context (self) cpdef SyclDevice get_sycl_device (self) cdef DPPLSyclQueueRef get_queue_ref (self) - cpdef SyclEvent submit (self, SyclKernel kernel, list args, \ - list gSize, list lSize) + cpdef SyclEvent submit (self, SyclKernel kernel, list args, list gS, \ + list lS=*) cpdef void wait (self) cpdef memcpy (self, dest, src, int count) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index b345f96611..6f9c293803 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -241,8 +241,8 @@ cdef class SyclQueue: cdef DPPLSyclQueueRef get_queue_ref (self): return self._queue_ref - cpdef SyclEvent submit (self, SyclKernel kernel, list args, \ - list gSize, list lSize): + cpdef SyclEvent submit (self, SyclKernel kernel, list args, list gS, \ + list lS = None): cdef void **kargs = malloc(len(args) * sizeof(void*)) cdef DPPLKernelArgType *kargty = malloc( @@ -259,24 +259,24 @@ cdef class SyclQueue: cdef size_t sizetval cdef double doubleval cdef float floatval - cdef int gs_len = len(gSize) - cdef int ls_len = len(lSize) + cdef int gs_len = len(gS) + cdef int ls_len = len(lS) if (gs_len != ls_len): raise ValueError("") if (gs_len == 1): - Range[0] = gSize[0] + Range[0] = gS[0] Range[1] = 1 Range[2] = 1 elif (gs_len == 2): - Range[0] = gSize[0] - Range[1] = gSize[1] + Range[0] = gS[0] + Range[1] = gS[1] Range[2] = 1 elif (gs_len == 3): - Range[0] = gSize[0] - Range[1] = gSize[1] - Range[2] = gSize[2] + Range[0] = gS[0] + Range[1] = gS[1] + Range[2] = gS[2] else: raise ValueError("") From 3f0b5567922e2f191f0528320f994ee02d4b81c5 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 29 Sep 2020 08:56:38 -0500 Subject: [PATCH 81/93] Remove printf --- dpctl/sycl_core.pyx | 1 - 1 file changed, 1 deletion(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 6f9c293803..e484a54c8d 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -209,7 +209,6 @@ cdef class SyclProgram: return DPPLProgram_HasKernel(self._program_ref, name) import ctypes -from libc.stdio cimport printf cdef class SyclQueue: ''' Wrapper class for a Sycl queue. From 4890e616fd3cfee0d91c49a9a0b66a7a86c7d5db Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 29 Sep 2020 09:08:29 -0500 Subject: [PATCH 82/93] Rename DPPLQueue_Submit and allow DepEvents. --- backends/include/dppl_sycl_queue_interface.h | 21 ++++++++++++------- backends/source/dppl_sycl_queue_interface.cpp | 21 ++++++++++++------- backends/tests/test_sycl_queue_interface.cpp | 17 ++++++++------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index de2fe66783..466fa08b39 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -97,6 +97,7 @@ DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef); * DPPLKernelArg tagged union. * * \todo sycl::buffer arguments are not supported yet. + * \todo Add support for id WorkItemOffset * * @param KRef Opaque pointer to a OpenCL interoperability kernel * wrapped inside a sycl::kernel. @@ -109,18 +110,24 @@ DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef); * maximum size of three. Note the number of values * in the array depends on the number of dimensions. * @param NDims Number of dimensions in the range (size of Range). + * @param DepEvents List of dependent DPPLSyclEventRef objects (events) + * for the kernel. We call sycl::handler.depends_on for + * each of the provided events. + * @param NDepEvents Size of the DepEvents list. * @return A opaque pointer to the sycl::event returned by the * sycl::queue.submit() function. */ DPPL_API DPPLSyclEventRef -DPPLQueue_Submit (__dppl_keep const DPPLSyclKernelRef KRef, - __dppl_keep const DPPLSyclQueueRef QRef, - __dppl_keep void **Args, - __dppl_keep const DPPLKernelArgType *ArgTypes, - size_t NArgs, - size_t const Range[3], - size_t NDims); +DPPLQueue_SubmitRange (__dppl_keep const DPPLSyclKernelRef KRef, + __dppl_keep const DPPLSyclQueueRef QRef, + __dppl_keep void **Args, + __dppl_keep const DPPLKernelArgType *ArgTypes, + size_t NArgs, + size_t const Range[3], + size_t NDims, + __dppl_keep const DPPLSyclEventRef *DepEvents, + size_t NDepEvents); /*! * @brief Calls the sycl::queue.submit function to do a blocking wait on all diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index 3584be6928..881f0785c6 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -134,13 +134,15 @@ DPPLQueue_GetContext (__dppl_keep const DPPLSyclQueueRef QRef) } __dppl_give DPPLSyclEventRef -DPPLQueue_Submit (__dppl_keep const DPPLSyclKernelRef KRef, - __dppl_keep const DPPLSyclQueueRef QRef, - __dppl_keep void **Args, - __dppl_keep const DPPLKernelArgType *ArgTypes, - size_t NArgs, - size_t const Range[3], - size_t NDims) +DPPLQueue_SubmitRange (__dppl_keep const DPPLSyclKernelRef KRef, + __dppl_keep const DPPLSyclQueueRef QRef, + __dppl_keep void **Args, + __dppl_keep const DPPLKernelArgType *ArgTypes, + size_t NArgs, + size_t const Range[3], + size_t NDims, + __dppl_keep const DPPLSyclEventRef *DepEvents, + size_t NDepEvents) { auto Kernel = unwrap(KRef); auto Queue = unwrap(QRef); @@ -148,7 +150,10 @@ DPPLQueue_Submit (__dppl_keep const DPPLSyclKernelRef KRef, try { e = Queue->submit([&](handler& cgh) { - // \todo cgh.depends_on + // Depend on any event that was specified by the caller. + if(NDepEvents) + for(auto i = 0ul; i < NDepEvents; ++i) + cgh.depends_on(*unwrap(DepEvents[i])); for (auto i = 0ul; i < NArgs; ++i) { // \todo add support for Sycl buffers diff --git a/backends/tests/test_sycl_queue_interface.cpp b/backends/tests/test_sycl_queue_interface.cpp index 2c5938a65a..7dee344f73 100644 --- a/backends/tests/test_sycl_queue_interface.cpp +++ b/backends/tests/test_sycl_queue_interface.cpp @@ -125,10 +125,10 @@ TEST_F (TestDPPLSyclQueueInterface, CheckSubmit) void *arg1[1] = { unwrap(a) }; void *arg2[1] = { unwrap(b) }; - auto E1 = DPPLQueue_Submit(InitKernel, CurrQueue, arg1, argTypes, 1, - Range, 1); - auto E2 = DPPLQueue_Submit(InitKernel, CurrQueue, arg2, argTypes, 1, - Range, 1); + auto E1 = DPPLQueue_SubmitRange(InitKernel, CurrQueue, arg1, argTypes, 1, + Range, 1, nullptr, 0); + auto E2 = DPPLQueue_SubmitRange(InitKernel, CurrQueue, arg2, argTypes, 1, + Range, 1, nullptr, 0); ASSERT_TRUE(E1 != nullptr); ASSERT_TRUE(E2 != nullptr); @@ -142,8 +142,8 @@ TEST_F (TestDPPLSyclQueueInterface, CheckSubmit) DPPL_VOID_PTR }; - auto E3 = DPPLQueue_Submit(AddKernel, CurrQueue, args, addKernelArgTypes, 3, - Range, 1); + auto E3 = DPPLQueue_SubmitRange(AddKernel, CurrQueue, args, + addKernelArgTypes, 3, Range, 1, nullptr, 0); ASSERT_TRUE(E3 != nullptr); DPPLQueue_Wait(CurrQueue); @@ -159,8 +159,9 @@ TEST_F (TestDPPLSyclQueueInterface, CheckSubmit) DPPL_VOID_PTR, DPPL_FLOAT }; - auto E4 = DPPLQueue_Submit(AxpyKernel, CurrQueue, args2, - addKernelArgTypes2, 4, Range, 1); + auto E4 = DPPLQueue_SubmitRange(AxpyKernel, CurrQueue, args2, + addKernelArgTypes2, 4, Range, 1, + nullptr, 0); ASSERT_TRUE(E4 != nullptr); DPPLQueue_Wait(CurrQueue); From c0f6521ced1fad3971af2171b6687985fe7c6cf6 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 29 Sep 2020 09:43:00 -0500 Subject: [PATCH 83/93] Update Cython to use latest DubmitRange function. --- backends/include/dppl_sycl_queue_interface.h | 2 +- backends/source/dppl_sycl_queue_interface.cpp | 2 +- dpctl/backend.pxd | 17 ++++++++------ dpctl/sycl_core.pyx | 22 +++++++++++-------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index 466fa08b39..a041d0e28e 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -124,7 +124,7 @@ DPPLQueue_SubmitRange (__dppl_keep const DPPLSyclKernelRef KRef, __dppl_keep void **Args, __dppl_keep const DPPLKernelArgType *ArgTypes, size_t NArgs, - size_t const Range[3], + const size_t Range[3], size_t NDims, __dppl_keep const DPPLSyclEventRef *DepEvents, size_t NDepEvents); diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index 881f0785c6..4aad5fb122 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -139,7 +139,7 @@ DPPLQueue_SubmitRange (__dppl_keep const DPPLSyclKernelRef KRef, __dppl_keep void **Args, __dppl_keep const DPPLKernelArgType *ArgTypes, size_t NArgs, - size_t const Range[3], + const size_t Range[3], size_t NDims, __dppl_keep const DPPLSyclEventRef *DepEvents, size_t NDepEvents) diff --git a/dpctl/backend.pxd b/dpctl/backend.pxd index dd867c3a40..6249435d44 100644 --- a/dpctl/backend.pxd +++ b/dpctl/backend.pxd @@ -129,13 +129,16 @@ cdef extern from "dppl_sycl_queue_interface.h": cdef void DPPLQueue_Delete (DPPLSyclQueueRef QRef) cdef DPPLSyclContextRef DPPLQueue_GetContext (const DPPLSyclQueueRef Q) cdef DPPLSyclDeviceRef DPPLQueue_GetDevice (const DPPLSyclQueueRef Q) - cdef DPPLSyclEventRef DPPLQueue_Submit (const DPPLSyclKernelRef Ref, \ - const DPPLSyclQueueRef QRef, \ - void **Args, \ - const DPPLKernelArgType *ArgTypes,\ - size_t NArgs, \ - const size_t Range[3], \ - size_t NDims) + cdef DPPLSyclEventRef DPPLQueue_SubmitRange ( \ + const DPPLSyclKernelRef Ref, \ + const DPPLSyclQueueRef QRef, \ + void **Args, \ + const DPPLKernelArgType *ArgTypes, \ + size_t NArgs, \ + const size_t Range[3], \ + size_t NDims, \ + const DPPLSyclEventRef *DepEvents, \ + size_t NDepEvents) cdef void DPPLQueue_Wait (const DPPLSyclQueueRef QRef) cdef void DPPLQueue_memcpy (const DPPLSyclQueueRef Q, void *Dest, const void *Src, size_t Count) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index e484a54c8d..ad63d27fd9 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -135,6 +135,7 @@ cdef class SyclEvent: return ret def __dealloc__ (self): + self.wait() DPPLEvent_Delete(self._event_ref) cdef DPPLSyclEventRef get_event_ref (self): @@ -258,8 +259,8 @@ cdef class SyclQueue: cdef size_t sizetval cdef double doubleval cdef float floatval - cdef int gs_len = len(gS) - cdef int ls_len = len(lS) + cdef size_t gs_len = len(gS) + cdef size_t ls_len = len(lS) if (gs_len != ls_len): raise ValueError("") @@ -326,13 +327,16 @@ cdef class SyclQueue: else: raise TypeError("Unsupported type for a kernel argument") - cdef DPPLSyclEventRef Eref = DPPLQueue_Submit(kernel.get_kernel_ref(), - self.get_queue_ref(), - kargs, - kargty, - len(args), - Range, - 1) + cdef DPPLSyclEventRef Eref = DPPLQueue_SubmitRange( + kernel.get_kernel_ref(), + self.get_queue_ref(), + kargs, + kargty, + len(args), + Range, + gs_len, + NULL, + 0) free(kargs) free(kargty) From c185789717c730d9754369187a951c81d3dc1b7c Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 29 Sep 2020 11:02:45 -0500 Subject: [PATCH 84/93] Refactoring --- dpctl/_sycl_core.pxd | 2 ++ dpctl/sycl_core.pyx | 81 ++++++++++++++++++++++++-------------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index 6f814cba5a..4feb027453 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -100,6 +100,8 @@ cdef class SyclQueue: cdef SyclDevice _device cdef _raise_queue_submit_error (self, fname, errcode) + cdef _populate_args (self, list args, void **kargs, \ + DPPLKernelArgType *kargty) @staticmethod cdef SyclQueue _create (DPPLSyclQueueRef qref) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index ad63d27fd9..4d910e779b 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -232,23 +232,9 @@ cdef class SyclQueue: e.code = errcode raise e - cpdef SyclContext get_sycl_context (self): - return self._context - - cpdef SyclDevice get_sycl_device (self): - return self._device - - cdef DPPLSyclQueueRef get_queue_ref (self): - return self._queue_ref - - cpdef SyclEvent submit (self, SyclKernel kernel, list args, list gS, \ - list lS = None): + cdef _populate_args (self, list args, void **kargs, \ + DPPLKernelArgType *kargty): - cdef void **kargs = malloc(len(args) * sizeof(void*)) - cdef DPPLKernelArgType *kargty = malloc( - len(args) * sizeof(DPPLKernelArgType) - ) - cdef size_t Range[3] cdef char charval cdef int intval cdef unsigned int uintval @@ -259,26 +245,6 @@ cdef class SyclQueue: cdef size_t sizetval cdef double doubleval cdef float floatval - cdef size_t gs_len = len(gS) - cdef size_t ls_len = len(lS) - - if (gs_len != ls_len): - raise ValueError("") - - if (gs_len == 1): - Range[0] = gS[0] - Range[1] = 1 - Range[2] = 1 - elif (gs_len == 2): - Range[0] = gS[0] - Range[1] = gS[1] - Range[2] = 1 - elif (gs_len == 3): - Range[0] = gS[0] - Range[1] = gS[1] - Range[2] = gS[2] - else: - raise ValueError("") for idx, arg in enumerate(args): if isinstance(arg, ctypes.c_char): @@ -327,6 +293,49 @@ cdef class SyclQueue: else: raise TypeError("Unsupported type for a kernel argument") + cpdef SyclContext get_sycl_context (self): + return self._context + + cpdef SyclDevice get_sycl_device (self): + return self._device + + cdef DPPLSyclQueueRef get_queue_ref (self): + return self._queue_ref + + cpdef SyclEvent submit (self, SyclKernel kernel, list args, list gS, \ + list lS = None): + + cdef void **kargs = malloc(len(args) * sizeof(void*)) + cdef DPPLKernelArgType *kargty = malloc( + len(args) * sizeof(DPPLKernelArgType) + ) + cdef size_t Range[3] + cdef size_t gs_len = len(gS) + cdef size_t ls_len = len(lS) + + # populate the args and argstype + self._populate_args(args, kargs, kargty) + + if (gs_len != ls_len): + raise ValueError("") + + if (gs_len == 1): + Range[0] = gS[0] + Range[1] = 1 + Range[2] = 1 + elif (gs_len == 2): + Range[0] = gS[0] + Range[1] = gS[1] + Range[2] = 1 + elif (gs_len == 3): + Range[0] = gS[0] + Range[1] = gS[1] + Range[2] = gS[2] + else: + raise ValueError("") + + + cdef DPPLSyclEventRef Eref = DPPLQueue_SubmitRange( kernel.get_kernel_ref(), self.get_queue_ref(), From 3b19998a9f0f8d0c3e87d33596711d6964d53492 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 29 Sep 2020 17:40:44 -0500 Subject: [PATCH 85/93] Fix indentation. --- backends/source/dppl_sycl_queue_interface.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index 4aad5fb122..1ec98d5097 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -170,7 +170,8 @@ DPPLQueue_SubmitRange (__dppl_keep const DPPLSyclKernelRef KRef, cgh.parallel_for(range<2>{Range[0], Range[1]}, *Kernel); break; case 3: - cgh.parallel_for(range<3>{Range[0], Range[1], Range[2]}, *Kernel); + cgh.parallel_for(range<3>{Range[0], Range[1], Range[2]}, + *Kernel); break; default: // \todo handle the error From 1f7a0b1ed645bee938142c94beceb3c763099e99 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 29 Sep 2020 18:25:16 -0500 Subject: [PATCH 86/93] Fix test case. --- dpctl/sycl_core.pyx | 43 +++++++------------------------ dpctl/tests/test_kernel_submit.py | 12 ++++----- 2 files changed, 16 insertions(+), 39 deletions(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 4d910e779b..241b2b8f80 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -235,57 +235,36 @@ cdef class SyclQueue: cdef _populate_args (self, list args, void **kargs, \ DPPLKernelArgType *kargty): - cdef char charval - cdef int intval - cdef unsigned int uintval - cdef long longval - cdef long long longlongval - cdef unsigned long long ulonglongval - cdef short shortval - cdef size_t sizetval - cdef double doubleval - cdef float floatval - for idx, arg in enumerate(args): if isinstance(arg, ctypes.c_char): - charval = (arg.value) - kargs[idx]= (&charval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._CHAR elif isinstance(arg, ctypes.c_int): - intval = (arg.value) - kargs[idx]= (&intval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._INT elif isinstance(arg, ctypes.c_uint): - unintval = (arg.value) - kargs[idx]= (&unintval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._UNSIGNED_INT elif isinstance(arg, ctypes.c_long): - longval = (arg.value) - kargs[idx]= (&longval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._LONG elif isinstance(arg, ctypes.c_longlong): - longlongval = (arg.value) - kargs[idx]= (&longlongval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._LONG_LONG elif isinstance(arg, ctypes.c_ulonglong): - ulonglongval = (arg.value) - kargs[idx]= (&ulonglongval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._UNSIGNED_LONG_LONG elif isinstance(arg, ctypes.c_short): - shortval = (arg.value) - kargs[idx]= (&shortval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._SHORT elif isinstance(arg, ctypes.c_size_t): - sizetval = (arg.value) - kargs[idx]= (&sizetval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._SIZE_T elif isinstance(arg, ctypes.c_float): - floatval = (arg.value) - kargs[idx]= (&floatval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._FLOAT elif isinstance(arg, ctypes.c_double): - doubleval = (arg.value) - kargs[idx]= (&doubleval) + kargs[idx] = (ctypes.addressof(arg)) kargty[idx] = _arg_data_type._DOUBLE elif isinstance(arg, Memory): kargs[idx]= (arg._pointer) @@ -334,8 +313,6 @@ cdef class SyclQueue: else: raise ValueError("") - - cdef DPPLSyclEventRef Eref = DPPLQueue_SubmitRange( kernel.get_kernel_ref(), self.get_queue_ref(), diff --git a/dpctl/tests/test_kernel_submit.py b/dpctl/tests/test_kernel_submit.py index 7dd6d146f4..9898864f46 100644 --- a/dpctl/tests/test_kernel_submit.py +++ b/dpctl/tests/test_kernel_submit.py @@ -41,22 +41,22 @@ def test_create_program_from_source (): c[:] = 0 d = 2 args = [] - + args.append(a.base) args.append(b.base) args.append(c.base) args.append(ctypes.c_int(d)) - - r = [ 1024, 1, 1 ] - + + r = [ 1, 1, 1024 ] + e = q.submit(axpyKernel, args, r, r) e.wait() print(c) print(a + d * b) # self.assertIsNotNone(prog) - + # self.assertTrue() # self.assertTrue(prog.has_sycl_kernel("axpy")) - + test_create_program_from_source() From 2b3b62ae47d5c6952248fdda51d8ef6b2761c314 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 29 Sep 2020 20:39:39 -0500 Subject: [PATCH 87/93] Minor refactoring... --- dpctl/_sycl_core.pxd | 7 +- dpctl/sycl_core.pyx | 115 +++++++++++++++++++----------- dpctl/tests/test_kernel_submit.py | 2 +- 3 files changed, 79 insertions(+), 45 deletions(-) diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index 4feb027453..dfac54dc83 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -100,8 +100,9 @@ cdef class SyclQueue: cdef SyclDevice _device cdef _raise_queue_submit_error (self, fname, errcode) - cdef _populate_args (self, list args, void **kargs, \ - DPPLKernelArgType *kargty) + cdef _raise_invalid_range_error (self, fname, ndims, errcode) + cdef int _populate_args (self, list args, void **kargs, \ + DPPLKernelArgType *kargty) @staticmethod cdef SyclQueue _create (DPPLSyclQueueRef qref) @@ -109,6 +110,6 @@ cdef class SyclQueue: cpdef SyclDevice get_sycl_device (self) cdef DPPLSyclQueueRef get_queue_ref (self) cpdef SyclEvent submit (self, SyclKernel kernel, list args, list gS, \ - list lS=*) + list lS=*, list dEvents=*) cpdef void wait (self) cpdef memcpy (self, dest, src, int count) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 241b2b8f80..99c72f87f3 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -60,6 +60,13 @@ cdef class SyclKernelSubmitError (Exception): ''' pass +cdef class SyclKernelInvalidRangeError (Exception): + '''This exception is raised when a range that has more than three + dimensions or less than one dimension. + ''' + pass + + cdef class SyclContext: @staticmethod @@ -232,9 +239,17 @@ cdef class SyclQueue: e.code = errcode raise e - cdef _populate_args (self, list args, void **kargs, \ - DPPLKernelArgType *kargty): + cdef _raise_invalid_range_error (self, fname, ndims, errcode): + e = SyclKernelInvalidRangeError("Range with ", ndims, " not allowed. " + "Range should have between one and " + "three dimensions.") + e.fname = fname + e.code = errcode + raise e + cdef int _populate_args (self, list args, void **kargs, \ + DPPLKernelArgType *kargty): + cdef int ret = 0 for idx, arg in enumerate(args): if isinstance(arg, ctypes.c_char): kargs[idx] = (ctypes.addressof(arg)) @@ -270,7 +285,8 @@ cdef class SyclQueue: kargs[idx]= (arg._pointer) kargty[idx] = _arg_data_type._VOID_PTR else: - raise TypeError("Unsupported type for a kernel argument") + ret = -1 + return ret cpdef SyclContext get_sycl_context (self): return self._context @@ -282,47 +298,64 @@ cdef class SyclQueue: return self._queue_ref cpdef SyclEvent submit (self, SyclKernel kernel, list args, list gS, \ - list lS = None): + list lS = None, list dEvents = None): - cdef void **kargs = malloc(len(args) * sizeof(void*)) - cdef DPPLKernelArgType *kargty = malloc( - len(args) * sizeof(DPPLKernelArgType) - ) + cdef void **kargs = NULL + cdef DPPLKernelArgType *kargty = NULL + cdef DPPLSyclEventRef Eref = NULL cdef size_t Range[3] - cdef size_t gs_len = len(gS) - cdef size_t ls_len = len(lS) - - # populate the args and argstype - self._populate_args(args, kargs, kargty) - - if (gs_len != ls_len): - raise ValueError("") - - if (gs_len == 1): - Range[0] = gS[0] - Range[1] = 1 - Range[2] = 1 - elif (gs_len == 2): - Range[0] = gS[0] - Range[1] = gS[1] - Range[2] = 1 - elif (gs_len == 3): - Range[0] = gS[0] - Range[1] = gS[1] - Range[2] = gS[2] + cdef size_t nGS = len(gS) + cdef size_t nLS = len(lS) if lS is not None else 0 + + # Allocate the arrays to be sent to DPPLQueue_Submit + kargs = malloc(len(args) * sizeof(void*)) + if not kargs: + raise MemoryError() + kargty = malloc(len(args)*sizeof(DPPLKernelArgType)) + if not kargty: + free(kargs) + raise MemoryError() + + # populate the args and argstype arrays + cdef int ret = self._populate_args(args, kargs, kargty) + if ret == -1: + free(kargs) + free(kargty) + raise TypeError("Unsupported type for a kernel argument") + + if nGS == 0 or nGS > 3: + raise self._raise_invalid_range_error("SyclEvent.submit", nGS, -1) + + if lS is None: + if (nGS == 1): + Range[0] = gS[0] + Range[1] = 1 + Range[2] = 1 + elif (nGS == 2): + Range[0] = gS[0] + Range[1] = gS[1] + Range[2] = 1 + elif (nGS == 3): + Range[0] = gS[0] + Range[1] = gS[1] + Range[2] = gS[2] + else: + raise ValueError("") + + Eref = DPPLQueue_SubmitRange(kernel.get_kernel_ref(), + self.get_queue_ref(), + kargs, + kargty, + len(args), + Range, + nGS, + NULL, + 0) else: - raise ValueError("") - - cdef DPPLSyclEventRef Eref = DPPLQueue_SubmitRange( - kernel.get_kernel_ref(), - self.get_queue_ref(), - kargs, - kargty, - len(args), - Range, - gs_len, - NULL, - 0) + if (nGS != nLS): + raise ValueError("") + if nLS == 0 or nLS > 3: + raise self._raise_invalid_range_error("SyclEvent.submit",nLS,-1) free(kargs) free(kargty) diff --git a/dpctl/tests/test_kernel_submit.py b/dpctl/tests/test_kernel_submit.py index 9898864f46..cc72329b64 100644 --- a/dpctl/tests/test_kernel_submit.py +++ b/dpctl/tests/test_kernel_submit.py @@ -49,7 +49,7 @@ def test_create_program_from_source (): r = [ 1, 1, 1024 ] - e = q.submit(axpyKernel, args, r, r) + e = q.submit(axpyKernel, args, r) e.wait() print(c) From 1e1b0306124c0f07103e76bdca4342d53238165f Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 29 Sep 2020 22:35:05 -0500 Subject: [PATCH 88/93] Add nd_range support. --- backends/include/dppl_sycl_queue_interface.h | 78 ++++++++++++++--- backends/source/dppl_sycl_queue_interface.cpp | 70 ++++++++++++++- dpctl/_sycl_core.pxd | 1 + dpctl/backend.pxd | 11 +++ dpctl/sycl_core.pyx | 85 +++++++++++++------ scripts/build_for_develop.sh | 3 +- 6 files changed, 205 insertions(+), 43 deletions(-) diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index a041d0e28e..1bb2cdbe71 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -89,12 +89,12 @@ __dppl_give DPPLSyclDeviceRef DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef); /*! - * @brief Submits the kernel to the specified queue using give arguments. + * @brief Submits the kernel to the specified queue with the provided range + * argument. * - * A wrapper over sycl::queue.submit(). The function takes an OpenCL - * interoperability kernel, the kernel arguments, and a sycl queue as input - * arguments. The kernel arguments are passed in as an array of the - * DPPLKernelArg tagged union. + * A wrapper over sycl::queue.submit(). The function takes an interoperability + * kernel, the kernel arguments, and a Sycl queue as input. The kernel is + * submitted as parallel_for(range, *unwrap(KRef)). * * \todo sycl::buffer arguments are not supported yet. * \todo Add support for id WorkItemOffset @@ -103,13 +103,15 @@ DPPLQueue_GetDevice (__dppl_keep const DPPLSyclQueueRef QRef); * wrapped inside a sycl::kernel. * @param QRef Opaque pointer to the sycl::queue where the kernel * will be enqueued. - * @param Args An array of the DPPLKernelArg tagged union type that - * represents the kernel arguments for the kernel. - * @param NArgs The number of kernel arguments (size of Args array). - * @param Range Array storing the range dimensions that can have a - * maximum size of three. Note the number of values - * in the array depends on the number of dimensions. - * @param NDims Number of dimensions in the range (size of Range). + * @param Args An array of void* pointers that represent the + * kernel arguments for the kernel. + * @param ArgTypes An array of DPPLKernelArgType enum values that + * represent the type of each kernel argument. + * @param NArgs Size of Args and ArgTypes. + * @param Range Defines the overall dimension of the dispatch for + * the kernel. The array can have up to three + * dimensions. + * @param NRange Size of the gRange array. * @param DepEvents List of dependent DPPLSyclEventRef objects (events) * for the kernel. We call sycl::handler.depends_on for * each of the provided events. @@ -124,11 +126,59 @@ DPPLQueue_SubmitRange (__dppl_keep const DPPLSyclKernelRef KRef, __dppl_keep void **Args, __dppl_keep const DPPLKernelArgType *ArgTypes, size_t NArgs, - const size_t Range[3], - size_t NDims, + __dppl_keep const size_t Range[3], + size_t NRange, __dppl_keep const DPPLSyclEventRef *DepEvents, size_t NDepEvents); +/*! + * @brief Submits the kernel to the specified queue with the provided nd_range + * argument. + * + * A wrapper over sycl::queue.submit(). The function takes an interoperability + * kernel, the kernel arguments, and a Sycl queue as input. The kernel is + * submitted as parallel_for(nd_range, *unwrap(KRef)). + * + * \todo sycl::buffer arguments are not supported yet. + * \todo Add support for id WorkItemOffset + * + * @param KRef Opaque pointer to a OpenCL interoperability kernel + * wrapped inside a sycl::kernel. + * @param QRef Opaque pointer to the sycl::queue where the kernel + * will be enqueued. + * @param Args An array of void* pointers that represent the + * kernel arguments for the kernel. + * @param ArgTypes An array of DPPLKernelArgType enum values that + * represent the type of each kernel argument. + * @param NArgs Size of Args. + * @param gRange Defines the overall dimension of the dispatch for + * the kernel. The array can have up to three + * dimensions. + * @param lRange Defines the iteration domain of a single work-group + * in a parallel dispatch. The array can have up to + * three dimensions. + * @param NDims The number of dimensions for both local and global + * ranges. + * @param DepEvents List of dependent DPPLSyclEventRef objects (events) + * for the kernel. We call sycl::handler.depends_on for + * each of the provided events. + * @param NDepEvents Size of the DepEvents list. + * @return A opaque pointer to the sycl::event returned by the + * sycl::queue.submit() function. + */ +DPPL_API +DPPLSyclEventRef +DPPLQueue_SubmitNDRange(__dppl_keep const DPPLSyclKernelRef KRef, + __dppl_keep const DPPLSyclQueueRef QRef, + __dppl_keep void **Args, + __dppl_keep const DPPLKernelArgType *ArgTypes, + size_t NArgs, + __dppl_keep const size_t gRange[3], + __dppl_keep const size_t lRange[3], + size_t NDims, + __dppl_keep const DPPLSyclEventRef *DepEvents, + size_t NDepEvents); + /*! * @brief Calls the sycl::queue.submit function to do a blocking wait on all * enqueued tasks in the queue. diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index 1ec98d5097..eba56cd810 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -139,7 +139,7 @@ DPPLQueue_SubmitRange (__dppl_keep const DPPLSyclKernelRef KRef, __dppl_keep void **Args, __dppl_keep const DPPLKernelArgType *ArgTypes, size_t NArgs, - const size_t Range[3], + __dppl_keep const size_t Range[3], size_t NDims, __dppl_keep const DPPLSyclEventRef *DepEvents, size_t NDepEvents) @@ -175,14 +175,78 @@ DPPLQueue_SubmitRange (__dppl_keep const DPPLSyclKernelRef KRef, break; default: // \todo handle the error - std::cerr << "Range cannot be greater than three dimensions.\n"; - exit(1); + throw std::runtime_error("Range cannot be greater than three " + "dimensions."); } }); } catch (runtime_error re) { // \todo fix error handling std::cerr << re.what() << '\n'; return nullptr; + } catch (std::runtime_error sre) { + std::cerr << sre.what() << '\n'; + return nullptr; + } + + return wrap(new event(e)); +} + +DPPLSyclEventRef +DPPLQueue_SubmitNDRange(__dppl_keep const DPPLSyclKernelRef KRef, + __dppl_keep const DPPLSyclQueueRef QRef, + __dppl_keep void **Args, + __dppl_keep const DPPLKernelArgType *ArgTypes, + size_t NArgs, + __dppl_keep const size_t gRange[3], + __dppl_keep const size_t lRange[3], + size_t NDims, + __dppl_keep const DPPLSyclEventRef *DepEvents, + size_t NDepEvents) +{ + auto Kernel = unwrap(KRef); + auto Queue = unwrap(QRef); + event e; + + try { + e = Queue->submit([&](handler& cgh) { + // Depend on any event that was specified by the caller. + if(NDepEvents) + for(auto i = 0ul; i < NDepEvents; ++i) + cgh.depends_on(*unwrap(DepEvents[i])); + + for (auto i = 0ul; i < NArgs; ++i) { + // \todo add support for Sycl buffers + // \todo handle errors properly + if(!set_kernel_arg(cgh, i, Args[i], ArgTypes[i])) + exit(1); + } + switch(NDims) + { + case 1: + cgh.parallel_for(nd_range<1>{{gRange[0]},{lRange[0]}}, *Kernel); + break; + case 2: + cgh.parallel_for(nd_range<2>{{gRange[0], gRange[1]}, + {lRange[0], lRange[1]}}, *Kernel); + break; + case 3: + cgh.parallel_for(nd_range<3>{{gRange[0], gRange[1], gRange[2]}, + {lRange[0], lRange[1], lRange[3]}}, + *Kernel); + break; + default: + // \todo handle the error + throw std::runtime_error("Range cannot be greater than three " + "dimensions."); + } + }); + } catch (runtime_error re) { + // \todo fix error handling + std::cerr << re.what() << '\n'; + return nullptr; + } catch (std::runtime_error sre) { + std::cerr << sre.what() << '\n'; + return nullptr; } return wrap(new event(e)); diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index dfac54dc83..5178b5b252 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -103,6 +103,7 @@ cdef class SyclQueue: cdef _raise_invalid_range_error (self, fname, ndims, errcode) cdef int _populate_args (self, list args, void **kargs, \ DPPLKernelArgType *kargty) + cdef int _populate_range (self, size_t Range[3], list gS, size_t nGS) @staticmethod cdef SyclQueue _create (DPPLSyclQueueRef qref) diff --git a/dpctl/backend.pxd b/dpctl/backend.pxd index 6249435d44..45ef857b2c 100644 --- a/dpctl/backend.pxd +++ b/dpctl/backend.pxd @@ -139,6 +139,17 @@ cdef extern from "dppl_sycl_queue_interface.h": size_t NDims, \ const DPPLSyclEventRef *DepEvents, \ size_t NDepEvents) + cdef DPPLSyclEventRef DPPLQueue_SubmitNDRange( \ + const DPPLSyclKernelRef Ref, \ + const DPPLSyclQueueRef QRef, \ + void **Args, \ + const DPPLKernelArgType *ArgTypes, \ + size_t NArgs, \ + const size_t gRange[3], \ + const size_t lRange[3], \ + size_t NDims, \ + const DPPLSyclEventRef *DepEvents, \ + size_t NDepEvents) cdef void DPPLQueue_Wait (const DPPLSyclQueueRef QRef) cdef void DPPLQueue_memcpy (const DPPLSyclQueueRef Q, void *Dest, const void *Src, size_t Count) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 99c72f87f3..8c521780cc 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -288,6 +288,29 @@ cdef class SyclQueue: ret = -1 return ret + + cdef int _populate_range (self, size_t Range[3], list S, size_t nS): + + cdef int ret = 0 + + if nS == 1: + Range[0] = S[0] + Range[1] = 1 + Range[2] = 1 + elif nS == 2: + Range[0] = S[0] + Range[1] = S[1] + Range[2] = 1 + elif nS == 3: + Range[0] = S[0] + Range[1] = S[1] + Range[2] = S[2] + else: + ret = -1 + + return ret + + cpdef SyclContext get_sycl_context (self): return self._context @@ -303,7 +326,9 @@ cdef class SyclQueue: cdef void **kargs = NULL cdef DPPLKernelArgType *kargty = NULL cdef DPPLSyclEventRef Eref = NULL - cdef size_t Range[3] + cdef int ret + cdef size_t gRange[3] + cdef size_t lRange[3] cdef size_t nGS = len(gS) cdef size_t nLS = len(lS) if lS is not None else 0 @@ -317,46 +342,56 @@ cdef class SyclQueue: raise MemoryError() # populate the args and argstype arrays - cdef int ret = self._populate_args(args, kargs, kargty) + ret = self._populate_args(args, kargs, kargty) if ret == -1: free(kargs) free(kargty) raise TypeError("Unsupported type for a kernel argument") - if nGS == 0 or nGS > 3: - raise self._raise_invalid_range_error("SyclEvent.submit", nGS, -1) - if lS is None: - if (nGS == 1): - Range[0] = gS[0] - Range[1] = 1 - Range[2] = 1 - elif (nGS == 2): - Range[0] = gS[0] - Range[1] = gS[1] - Range[2] = 1 - elif (nGS == 3): - Range[0] = gS[0] - Range[1] = gS[1] - Range[2] = gS[2] - else: - raise ValueError("") + ret = self._populate_range (gRange, gS, nGS) + if ret == -1: + free(kargs) + free(kargty) + self._raise_invalid_range_error("SyclQueue.submit", nGS, -1) Eref = DPPLQueue_SubmitRange(kernel.get_kernel_ref(), self.get_queue_ref(), kargs, kargty, len(args), - Range, + gRange, nGS, NULL, 0) else: - if (nGS != nLS): - raise ValueError("") - if nLS == 0 or nLS > 3: - raise self._raise_invalid_range_error("SyclEvent.submit",nLS,-1) - + ret = self._populate_range (gRange, gS, nGS) + if ret == -1: + free(kargs) + free(kargty) + self._raise_invalid_range_error("SyclQueue.submit", nGS, -1) + ret = self._populate_range (lRange, lS, nLS) + if ret == -1: + free(kargs) + free(kargty) + self._raise_invalid_range_error("SyclQueue.submit", nLS, -1) + + if nGS != nLS: + free(kargs) + free(kargty) + raise ValueError("Local and global ranges need to have same " + "number of dimensions.") + + Eref = DPPLQueue_SubmitNDRange(kernel.get_kernel_ref(), + self.get_queue_ref(), + kargs, + kargty, + len(args), + gRange, + lRange, + nGS, + NULL, + 0) free(kargs) free(kargty) diff --git a/scripts/build_for_develop.sh b/scripts/build_for_develop.sh index bb5c92e907..63057d8099 100755 --- a/scripts/build_for_develop.sh +++ b/scripts/build_for_develop.sh @@ -26,7 +26,7 @@ cmake \ -DGTEST_LIB_DIR=${CONDA_PREFIX}/lib \ ../backends -make V=1 -n -j 4 && make install +make V=1 -n -j 4 && make check && make install popd cp install/lib/*.so dpctl/ @@ -47,3 +47,4 @@ export CXX=dpcpp export CFLAGS=-fPIC python setup.py clean --all python setup.py build develop +python -m unittest dpctl.tests From 7a5b4d416b2ea47c67fe0357e8cf674762f29348 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 29 Sep 2020 22:49:23 -0500 Subject: [PATCH 89/93] add support for dependent events. --- dpctl/sycl_core.pyx | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 8c521780cc..7750b66534 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -325,12 +325,14 @@ cdef class SyclQueue: cdef void **kargs = NULL cdef DPPLKernelArgType *kargty = NULL + cdef DPPLSyclEventRef *depEvents = NULL cdef DPPLSyclEventRef Eref = NULL cdef int ret cdef size_t gRange[3] cdef size_t lRange[3] cdef size_t nGS = len(gS) cdef size_t nLS = len(lS) if lS is not None else 0 + cdef size_t nDE = len(dEvents) if dEvents is not None else 0 # Allocate the arrays to be sent to DPPLQueue_Submit kargs = malloc(len(args) * sizeof(void*)) @@ -340,12 +342,24 @@ cdef class SyclQueue: if not kargty: free(kargs) raise MemoryError() + # Create the array of dependent events if any + if dEvents is not None and nDE > 0: + depEvents = malloc(nDE*sizeof(DPPLSyclEventRef)) + if not depEvents: + free(kargs) + free(kargty) + raise MemoryError() + else: + for idx, de in enumerate(dEvents): + + depEvents[idx] = (de).get_event_ref() # populate the args and argstype arrays ret = self._populate_args(args, kargs, kargty) if ret == -1: free(kargs) free(kargty) + free(depEvents) raise TypeError("Unsupported type for a kernel argument") if lS is None: @@ -353,6 +367,7 @@ cdef class SyclQueue: if ret == -1: free(kargs) free(kargty) + free(depEvents) self._raise_invalid_range_error("SyclQueue.submit", nGS, -1) Eref = DPPLQueue_SubmitRange(kernel.get_kernel_ref(), @@ -362,23 +377,26 @@ cdef class SyclQueue: len(args), gRange, nGS, - NULL, - 0) + depEvents, + nDE) else: ret = self._populate_range (gRange, gS, nGS) if ret == -1: free(kargs) free(kargty) + free(depEvents) self._raise_invalid_range_error("SyclQueue.submit", nGS, -1) ret = self._populate_range (lRange, lS, nLS) if ret == -1: free(kargs) free(kargty) + free(depEvents) self._raise_invalid_range_error("SyclQueue.submit", nLS, -1) if nGS != nLS: free(kargs) free(kargty) + free(depEvents) raise ValueError("Local and global ranges need to have same " "number of dimensions.") @@ -390,10 +408,11 @@ cdef class SyclQueue: gRange, lRange, nGS, - NULL, - 0) + depEvents, + nDE) free(kargs) free(kargty) + free(depEvents) if Eref is NULL: # \todo get the error number from dpctl-capi From c70c55f663c3a6c0de93a04bbebd5ea25dafe85c Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 30 Sep 2020 00:23:31 -0500 Subject: [PATCH 90/93] Add a basic unit test to test kernel submission. --- dpctl/sycl_core.pyx | 15 +++-- dpctl/tests/test_kernel_submit.py | 96 +++++++++++++------------------ 2 files changed, 46 insertions(+), 65 deletions(-) diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 7750b66534..350ba22170 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -345,14 +345,13 @@ cdef class SyclQueue: # Create the array of dependent events if any if dEvents is not None and nDE > 0: depEvents = malloc(nDE*sizeof(DPPLSyclEventRef)) - if not depEvents: - free(kargs) - free(kargty) - raise MemoryError() - else: - for idx, de in enumerate(dEvents): - - depEvents[idx] = (de).get_event_ref() + if not depEvents: + free(kargs) + free(kargty) + raise MemoryError() + else: + for idx, de in enumerate(dEvents): + depEvents[idx] = (de).get_event_ref() # populate the args and argstype arrays ret = self._populate_args(args, kargs, kargty) diff --git a/dpctl/tests/test_kernel_submit.py b/dpctl/tests/test_kernel_submit.py index cc72329b64..1a6e1a87d2 100644 --- a/dpctl/tests/test_kernel_submit.py +++ b/dpctl/tests/test_kernel_submit.py @@ -1,62 +1,44 @@ import ctypes import dpctl +import unittest import dpctl._memory as dpctl_mem -import array import numpy as np -def test_create_program_from_source (): - q = dpctl.get_current_queue() - q.get_sycl_device().dump_device_info() - ctx = q.get_sycl_context() - oclSrc = " \ - kernel void add(global int* a, global int* b, global int* c) { \ - size_t index = get_global_id(0); \ - c[index] = a[index] + b[index]; \ - } \ - kernel void axpy(global int* a, global int* b, global int* c, int d) { \ - size_t index = get_global_id(0); \ - c[index] = a[index] + d*b[index]; \ - }" - prog = dpctl.create_program_from_source(q,oclSrc) - print(prog) - print(prog.has_sycl_kernel('add')) - print(prog.has_sycl_kernel('axpy')) - addKernel = prog.get_sycl_kernel('add') - print(addKernel) - print(addKernel.get_function_name()) - print(addKernel.get_num_args()) - axpyKernel = prog.get_sycl_kernel('axpy') - print(axpyKernel) - print(axpyKernel.get_function_name()) - print(axpyKernel.get_num_args()) - - abuf = dpctl_mem.MemoryUSMShared(1024*np.dtype('i').itemsize) - bbuf = dpctl_mem.MemoryUSMShared(1024*np.dtype('i').itemsize) - cbuf = dpctl_mem.MemoryUSMShared(1024*np.dtype('i').itemsize) - a = np.ndarray((1024), buffer=abuf, dtype='i') - b = np.ndarray((1024), buffer=bbuf, dtype='i') - c = np.ndarray((1024), buffer=cbuf, dtype='i') - a[:] = np.arange(1024) - b[:] = np.arange(1024, 0, -1) - c[:] = 0 - d = 2 - args = [] - - args.append(a.base) - args.append(b.base) - args.append(c.base) - args.append(ctypes.c_int(d)) - - r = [ 1, 1, 1024 ] - - e = q.submit(axpyKernel, args, r) - e.wait() - - print(c) - print(a + d * b) - # self.assertIsNotNone(prog) - - # self.assertTrue() - # self.assertTrue(prog.has_sycl_kernel("axpy")) - -test_create_program_from_source() +@unittest.skipIf(not dpctl.has_sycl_platforms(), "No SYCL platforms available") +class Test1DKernelSubmit (unittest.TestCase): + + def test_create_program_from_source (self): + q = dpctl.get_current_queue() + ctx = q.get_sycl_context() + oclSrc = " \ + kernel void axpy(global int* a, global int* b, global int* c, int d) { \ + size_t index = get_global_id(0); \ + c[index] = d*a[index] + b[index]; \ + }" + prog = dpctl.create_program_from_source(q,oclSrc) + axpyKernel = prog.get_sycl_kernel('axpy') + + abuf = dpctl_mem.MemoryUSMShared(1024*np.dtype('i').itemsize) + bbuf = dpctl_mem.MemoryUSMShared(1024*np.dtype('i').itemsize) + cbuf = dpctl_mem.MemoryUSMShared(1024*np.dtype('i').itemsize) + a = np.ndarray((1024), buffer=abuf, dtype='i') + b = np.ndarray((1024), buffer=bbuf, dtype='i') + c = np.ndarray((1024), buffer=cbuf, dtype='i') + a[:] = np.arange(1024) + b[:] = np.arange(1024, 0, -1) + c[:] = 0 + d = 2 + args = [] + + args.append(a.base) + args.append(b.base) + args.append(c.base) + args.append(ctypes.c_int(d)) + + r = [1024] + + q.submit(axpyKernel, args, r) + self.assertTrue(np.allclose(c, a*d + b)) + +if __name__ == '__main__': + unittest.main() From bdef26c86d5a92c208655ffd1c09398a11cf604a Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 30 Sep 2020 00:28:02 -0500 Subject: [PATCH 91/93] Add a basic unit test. --- dpctl/tests/__init__.py | 1 + ...l_submit.py => test_sycl_kernel_submit.py} | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) rename dpctl/tests/{test_kernel_submit.py => test_sycl_kernel_submit.py} (61%) diff --git a/dpctl/tests/__init__.py b/dpctl/tests/__init__.py index 2d1bf3a8e7..f13b6d01d9 100644 --- a/dpctl/tests/__init__.py +++ b/dpctl/tests/__init__.py @@ -23,6 +23,7 @@ ##===----------------------------------------------------------------------===## from .test_dump_functions import * +from .test_sycl_kernel_submit import * from .test_sycl_program import * from .test_sycl_queue_manager import * from .test_sycl_usm import * diff --git a/dpctl/tests/test_kernel_submit.py b/dpctl/tests/test_sycl_kernel_submit.py similarity index 61% rename from dpctl/tests/test_kernel_submit.py rename to dpctl/tests/test_sycl_kernel_submit.py index 1a6e1a87d2..b1371fa1b3 100644 --- a/dpctl/tests/test_kernel_submit.py +++ b/dpctl/tests/test_sycl_kernel_submit.py @@ -1,3 +1,27 @@ +##===------------- test_sycl_kernel_submit.py - dpctl -----*- Python -*---===## +## +## Data Parallel Control (dpctl) +## +## Copyright 2020 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 +## Defines unit test cases for kernel submission to a sycl::queue. +## +##===----------------------------------------------------------------------===## import ctypes import dpctl import unittest From bd2c1715d3f5fbba7c36ab3260e1bcd4b278e524 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 30 Sep 2020 09:37:12 -0500 Subject: [PATCH 92/93] minor clean ups left over by merge. --- backends/include/dppl_sycl_event_interface.h | 5 ++--- backends/include/dppl_sycl_queue_interface.h | 12 ++---------- backends/source/dppl_sycl_queue_interface.cpp | 8 -------- dpctl/sycl_core.pyx | 1 + 4 files changed, 5 insertions(+), 21 deletions(-) diff --git a/backends/include/dppl_sycl_event_interface.h b/backends/include/dppl_sycl_event_interface.h index e5e34d7f4c..c97eaf08f3 100644 --- a/backends/include/dppl_sycl_event_interface.h +++ b/backends/include/dppl_sycl_event_interface.h @@ -35,10 +35,9 @@ DPPL_C_EXTERN_C_BEGIN /*! - * @brief + * @brief C-API wrapper for sycl::event.wait. * - * @param ERef My Param doc - * @return {return} My Param doc + * @param ERef An opaque DPPLSyclEventRef pointer on which to wait. */ DPPL_API void DPPLEvent_Wait (__dppl_keep DPPLSyclEventRef ERef); diff --git a/backends/include/dppl_sycl_queue_interface.h b/backends/include/dppl_sycl_queue_interface.h index 91be0314bc..e1c452108d 100644 --- a/backends/include/dppl_sycl_queue_interface.h +++ b/backends/include/dppl_sycl_queue_interface.h @@ -190,16 +190,8 @@ void DPPLQueue_Wait (__dppl_keep const DPPLSyclQueueRef QRef); /*! - * @brief C-API wrapper for sycl::queue::memcpy. It waits an event. - * - * @param QRef A DPPLSyclQueueRef pointer that gets deleted. - */ -DPPL_API -void DPPLQueue_memcpy (__dppl_keep const DPPLSyclQueueRef QRef, - void *Dest, const void *Src, size_t Count); - -/*! - * @brief C-API wrapper for sycl::queue::memcpy. It waits an event. + * @brief C-API wrapper for sycl::queue::memcpy, the function waits on an event + * till the memcpy operation completes. * * @param QRef An opaque pointer to the sycl queue. * @param Dest An USM pointer to the destination memory. diff --git a/backends/source/dppl_sycl_queue_interface.cpp b/backends/source/dppl_sycl_queue_interface.cpp index 1d3793ef60..7ddaece07e 100644 --- a/backends/source/dppl_sycl_queue_interface.cpp +++ b/backends/source/dppl_sycl_queue_interface.cpp @@ -260,14 +260,6 @@ DPPLQueue_Wait (__dppl_keep DPPLSyclQueueRef QRef) SyclQueue->wait(); } -void DPPLQueue_memcpy (__dppl_take const DPPLSyclQueueRef QRef, - void *Dest, const void *Src, size_t Count) -{ - auto Q = unwrap(QRef); - auto event = Q->memcpy(Dest, Src, Count); - event.wait(); -} - void DPPLQueue_Memcpy (__dppl_take const DPPLSyclQueueRef QRef, void *Dest, const void *Src, size_t Count) { diff --git a/dpctl/sycl_core.pyx b/dpctl/sycl_core.pyx index 84791aeee4..347a25d29d 100644 --- a/dpctl/sycl_core.pyx +++ b/dpctl/sycl_core.pyx @@ -36,6 +36,7 @@ from libc.stdlib cimport malloc, free _logger = logging.getLogger(__name__) + class device_type(Enum): gpu = auto() cpu = auto() From 39c54ae07264b4ec1c4f9bd7410ee746f0d54844 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Wed, 30 Sep 2020 10:21:19 -0500 Subject: [PATCH 93/93] Add extra new line. --- dpctl/_sycl_core.pxd | 1 + 1 file changed, 1 insertion(+) diff --git a/dpctl/_sycl_core.pxd b/dpctl/_sycl_core.pxd index ad21d278a3..c8860b3c6b 100644 --- a/dpctl/_sycl_core.pxd +++ b/dpctl/_sycl_core.pxd @@ -29,6 +29,7 @@ from .backend cimport * + cdef class SyclContext: ''' Wrapper class for a Sycl Context '''