diff --git a/dpctl/_backend.pxd b/dpctl/_backend.pxd index eefdd4da85..8a74222b06 100644 --- a/dpctl/_backend.pxd +++ b/dpctl/_backend.pxd @@ -281,11 +281,11 @@ cdef extern from "dpctl_sycl_platform_interface.h": cdef extern from "dpctl_sycl_context_interface.h": cdef DPCTLSyclContextRef DPCTLContext_Create( const DPCTLSyclDeviceRef DRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int properties) cdef DPCTLSyclContextRef DPCTLContext_CreateFromDevices( const DPCTLDeviceVectorRef DVRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int properties) cdef DPCTLSyclContextRef DPCTLContext_Copy( const DPCTLSyclContextRef CRef) @@ -323,7 +323,7 @@ cdef extern from "dpctl_sycl_queue_interface.h": cdef DPCTLSyclQueueRef DPCTLQueue_Create( const DPCTLSyclContextRef CRef, const DPCTLSyclDeviceRef DRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int properties) cdef DPCTLSyclQueueRef DPCTLQueue_CreateForDevice( const DPCTLSyclDeviceRef dRef, diff --git a/libsyclinterface/helper/include/dpctl_async_error_handler.h b/libsyclinterface/helper/include/dpctl_error_handlers.h similarity index 73% rename from libsyclinterface/helper/include/dpctl_async_error_handler.h rename to libsyclinterface/helper/include/dpctl_error_handlers.h index f409f8646c..9bd93eec66 100644 --- a/libsyclinterface/helper/include/dpctl_async_error_handler.h +++ b/libsyclinterface/helper/include/dpctl_error_handlers.h @@ -44,3 +44,22 @@ class DPCTL_API DPCTL_AsyncErrorHandler void operator()(const cl::sycl::exception_list &exceptions); }; + +enum error_level : int +{ + none = 0, + error = 1, + warning = 2 +}; + +void error_handler(const std::exception &e, + const char *file_name, + const char *func_name, + int line_num, + error_level error_type = error_level::error); + +void error_handler(const std::string &what, + const char *file_name, + const char *func_name, + int line_num, + error_level error_type = error_level::warning); diff --git a/libsyclinterface/helper/include/dpctl_string_utils.hpp b/libsyclinterface/helper/include/dpctl_string_utils.hpp index aa1a8a9535..735f972128 100644 --- a/libsyclinterface/helper/include/dpctl_string_utils.hpp +++ b/libsyclinterface/helper/include/dpctl_string_utils.hpp @@ -21,6 +21,7 @@ /// \file /// Helper function to convert a C++ string to a C string. //===----------------------------------------------------------------------===// +#include "../helper/include/dpctl_error_handlers.h" #include #include #include @@ -55,9 +56,8 @@ cstring_from_string(const std::string &str) // to be null-terminated and the copy function is asked to // copy enough characters to include that null-character. cstr[cstr_len - 1] = '\0'; - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } return cstr; diff --git a/libsyclinterface/helper/source/dpctl_async_error_handler.cpp b/libsyclinterface/helper/source/dpctl_async_error_handler.cpp deleted file mode 100644 index 6b5d6db191..0000000000 --- a/libsyclinterface/helper/source/dpctl_async_error_handler.cpp +++ /dev/null @@ -1,42 +0,0 @@ -//===-- dpctl_async_error_handler.h - An async error handler -*-C++-*- ===// -// -// Data Parallel Control (dpctl) -// -// Copyright 2020-2021 Intel Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//===----------------------------------------------------------------------===// -/// -/// \file -/// A functor to use for passing an error handler callback function to sycl -/// context and queue contructors. -//===----------------------------------------------------------------------===// - -#include "dpctl_async_error_handler.h" - -void DPCTL_AsyncErrorHandler::operator()( - const cl::sycl::exception_list &exceptions) -{ - for (std::exception_ptr const &e : exceptions) { - try { - std::rethrow_exception(e); - } catch (cl::sycl::exception const &e) { - std::cerr << "Caught asynchronous SYCL exception:\n" - << e.what() << std::endl; - // FIXME: Change get_cl_code() to code() once DPCPP supports it. - auto err_code = e.get_cl_code(); - handler_(err_code); - } - } -} diff --git a/libsyclinterface/helper/source/dpctl_error_handlers.cpp b/libsyclinterface/helper/source/dpctl_error_handlers.cpp new file mode 100644 index 0000000000..33beb7bb9f --- /dev/null +++ b/libsyclinterface/helper/source/dpctl_error_handlers.cpp @@ -0,0 +1,89 @@ +//===-- dpctl_async_error_handler.h - An async error handler -*-C++-*- ===// +// +// Data Parallel Control (dpctl) +// +// Copyright 2020-2021 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// A functor to use for passing an error handler callback function to sycl +/// context and queue contructors. +//===----------------------------------------------------------------------===// + +#include "dpctl_error_handlers.h" +#include + +void DPCTL_AsyncErrorHandler::operator()( + const cl::sycl::exception_list &exceptions) +{ + for (std::exception_ptr const &e : exceptions) { + try { + std::rethrow_exception(e); + } catch (sycl::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); + auto err_code = e.get_cl_code(); + handler_(static_cast(err_code)); + } + } +} + +inline int get_requested_level(void) +{ + int requested_level = 0; + + const char *verbose = std::getenv("DPCTL_VERBOSITY"); + + if (verbose) { + if (!strncmp(verbose, "none", 4)) + requested_level = error_level::none; + else if (!strncmp(verbose, "error", 5)) + requested_level = error_level::error; + else if (!strncmp(verbose, "warning", 7)) + requested_level = error_level::warning; + } + + return requested_level; +} + +void error_handler(const std::exception &e, + const char *file_name, + const char *func_name, + int line_num, + error_level error_type) +{ + int requested_level = get_requested_level(); + int error_level = static_cast(error_type); + + if (requested_level <= error_level) { + std::cerr << e.what() << " in " << func_name << " at " << file_name + << ":" << line_num << std::endl; + } +} + +void error_handler(const std::string &what, + const char *file_name, + const char *func_name, + int line_num, + error_level error_type) +{ + int error_level = static_cast(error_type); + int requested_level = get_requested_level(); + + if (requested_level <= error_level) { + std::cerr << what << " In " << func_name << " at " << file_name << ":" + << line_num << std::endl; + } +} diff --git a/libsyclinterface/include/dpctl_sycl_context_interface.h b/libsyclinterface/include/dpctl_sycl_context_interface.h index f93125092d..77434c576a 100644 --- a/libsyclinterface/include/dpctl_sycl_context_interface.h +++ b/libsyclinterface/include/dpctl_sycl_context_interface.h @@ -46,7 +46,7 @@ DPCTL_C_EXTERN_C_BEGIN * optional async error handler and properties bit flags. * * @param DRef Opaque pointer to a SYCL device. - * @param error_handler A callback function that will be invoked by the + * @param handler A callback function that will be invoked by the * async_handler used during context creation. Can be * NULL if no async_handler is needed. * @param properties An optional combination of bit flags to define @@ -58,7 +58,7 @@ DPCTL_C_EXTERN_C_BEGIN DPCTL_API __dpctl_give DPCTLSyclContextRef DPCTLContext_Create(__dpctl_keep const DPCTLSyclDeviceRef DRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int properties); /*! @@ -67,7 +67,7 @@ DPCTLContext_Create(__dpctl_keep const DPCTLSyclDeviceRef DRef, * * @param DVRef An opaque pointer to a std::vector of * DPCTLSyclDeviceRef opaque pointers. - * @param error_handler A callback function that will be invoked by the + * @param handler A callback function that will be invoked by the * async_handler used during context creation. Can be * NULL if no async_handler is needed. * @param properties An optional combination of bit flags to define @@ -79,7 +79,7 @@ DPCTLContext_Create(__dpctl_keep const DPCTLSyclDeviceRef DRef, DPCTL_API __dpctl_give DPCTLSyclContextRef DPCTLContext_CreateFromDevices(__dpctl_keep const DPCTLDeviceVectorRef DVRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int properties); /*! diff --git a/libsyclinterface/include/dpctl_sycl_queue_interface.h b/libsyclinterface/include/dpctl_sycl_queue_interface.h index 9f46a83e50..228604785e 100644 --- a/libsyclinterface/include/dpctl_sycl_queue_interface.h +++ b/libsyclinterface/include/dpctl_sycl_queue_interface.h @@ -47,7 +47,7 @@ DPCTL_C_EXTERN_C_BEGIN * * @param CRef An opaque pointer to a sycl::context. * @param DRef An opaque pointer to a sycl::device - * @param error_handler A callback function that will be invoked by the + * @param handler A callback function that will be invoked by the * async_handler used during queue creation. Can be * NULL if no async_handler is needed. * @param properties A combination of bit flags using the values defined @@ -62,7 +62,7 @@ DPCTL_API __dpctl_give DPCTLSyclQueueRef DPCTLQueue_Create(__dpctl_keep const DPCTLSyclContextRef CRef, __dpctl_keep const DPCTLSyclDeviceRef DRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int properties); /*! @@ -86,7 +86,7 @@ DPCTLQueue_Create(__dpctl_keep const DPCTLSyclContextRef CRef, * function begaves the same way as the SYCL constructor. * * @param dRef An opaque pointer to a ``sycl::device``. - * @param error_handler A callback function that will be invoked by the + * @param handler A callback function that will be invoked by the * async_handler used during queue creation. Can be * NULL if no async_handler is needed. * @param properties A combination of bit flags using the values defined @@ -101,7 +101,7 @@ DPCTLQueue_Create(__dpctl_keep const DPCTLSyclContextRef CRef, DPCTL_API __dpctl_give DPCTLSyclQueueRef DPCTLQueue_CreateForDevice(__dpctl_keep const DPCTLSyclDeviceRef dRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int properties); /*! diff --git a/libsyclinterface/source/dpctl_sycl_context_interface.cpp b/libsyclinterface/source/dpctl_sycl_context_interface.cpp index e26f6ed49c..d6a74833dd 100644 --- a/libsyclinterface/source/dpctl_sycl_context_interface.cpp +++ b/libsyclinterface/source/dpctl_sycl_context_interface.cpp @@ -25,7 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_context_interface.h" -#include "../helper/include/dpctl_async_error_handler.h" +#include "../helper/include/dpctl_error_handlers.h" #include "Support/CBindingWrapping.h" #include #include @@ -43,20 +43,21 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector, __dpctl_give DPCTLSyclContextRef DPCTLContext_Create(__dpctl_keep const DPCTLSyclDeviceRef DRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int /**/) { DPCTLSyclContextRef CRef = nullptr; auto Device = unwrap(DRef); - if (!Device) - return CRef; + if (!Device) { + error_handler("Cannot create device from DPCTLSyclDeviceRef" + "as input is a nullptr.", + __FILE__, __func__, __LINE__); + return nullptr; + } try { - CRef = - wrap(new context(*Device, DPCTL_AsyncErrorHandler(error_handler))); - } catch (const std::bad_alloc &ba) { - std::cerr << ba.what() << '\n'; - } catch (const runtime_error &re) { - std::cerr << re.what() << '\n'; + CRef = wrap(new context(*Device, DPCTL_AsyncErrorHandler(handler))); + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } return CRef; @@ -64,14 +65,18 @@ DPCTLContext_Create(__dpctl_keep const DPCTLSyclDeviceRef DRef, __dpctl_give DPCTLSyclContextRef DPCTLContext_CreateFromDevices(__dpctl_keep const DPCTLDeviceVectorRef DVRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int /**/) { DPCTLSyclContextRef CRef = nullptr; std::vector Devices; auto DeviceRefs = unwrap(DVRef); - if (!DeviceRefs) + if (!DeviceRefs) { + error_handler("Cannot create device reference from DPCTLDeviceVectorRef" + "as input is a nullptr.", + __FILE__, __func__, __LINE__); return CRef; + } Devices.reserve(DeviceRefs->size()); for (auto const &DRef : *DeviceRefs) { @@ -79,12 +84,9 @@ DPCTLContext_CreateFromDevices(__dpctl_keep const DPCTLDeviceVectorRef DVRef, } try { - CRef = - wrap(new context(Devices, DPCTL_AsyncErrorHandler(error_handler))); - } catch (const std::bad_alloc &ba) { - std::cerr << ba.what() << '\n'; - } catch (const runtime_error &re) { - std::cerr << re.what() << '\n'; + CRef = wrap(new context(Devices, DPCTL_AsyncErrorHandler(handler))); + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } return CRef; @@ -93,9 +95,11 @@ DPCTLContext_CreateFromDevices(__dpctl_keep const DPCTLDeviceVectorRef DVRef, bool DPCTLContext_AreEq(__dpctl_keep const DPCTLSyclContextRef CtxRef1, __dpctl_keep const DPCTLSyclContextRef CtxRef2) { - if (!(CtxRef1 && CtxRef2)) - // \todo handle error + if (!(CtxRef1 && CtxRef2)) { + error_handler("DPCTLSyclContextRefs are nullptr.", __FILE__, __func__, + __LINE__); return false; + } return (*unwrap(CtxRef1) == *unwrap(CtxRef2)); } @@ -104,15 +108,15 @@ DPCTLContext_Copy(__dpctl_keep const DPCTLSyclContextRef CRef) { auto Context = unwrap(CRef); if (!Context) { - std::cerr << "Cannot copy DPCTLSyclContextRef as input is a nullptr\n"; + error_handler("Cannot copy DPCTLSyclContextRef as input is a nullptr.", + __FILE__, __func__, __LINE__); return nullptr; } try { auto CopiedContext = new context(*Context); return wrap(CopiedContext); - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -122,16 +126,17 @@ DPCTLContext_GetDevices(__dpctl_keep const DPCTLSyclContextRef CRef) { auto Context = unwrap(CRef); if (!Context) { - std::cerr << "Can not retrieve devices from DPCTLSyclContextRef as " - "input is a nullptr\n"; + error_handler("Cannot retrieve devices from DPCTLSyclContextRef as " + "input is a nullptr.", + __FILE__, __func__, __LINE__); return nullptr; } std::vector *DevicesVectorPtr = nullptr; try { DevicesVectorPtr = new std::vector(); - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + delete DevicesVectorPtr; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } try { @@ -141,15 +146,9 @@ DPCTLContext_GetDevices(__dpctl_keep const DPCTLSyclContextRef CRef) DevicesVectorPtr->emplace_back(wrap(new device(Dev))); } return wrap(DevicesVectorPtr); - } catch (std::bad_alloc const &ba) { - delete DevicesVectorPtr; - // \todo log error - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (const runtime_error &re) { + } catch (std::exception const &e) { delete DevicesVectorPtr; - // \todo log error - std::cerr << re.what() << '\n'; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -158,8 +157,9 @@ size_t DPCTLContext_DeviceCount(__dpctl_keep const DPCTLSyclContextRef CRef) { auto Context = unwrap(CRef); if (!Context) { - std::cerr << "Can not retrieve devices from DPCTLSyclContextRef as " - "input is a nullptr\n"; + error_handler("Cannot retrieve devices from DPCTLSyclContextRef as " + "input is a nullptr.", + __FILE__, __func__, __LINE__); return 0; } const auto Devices = Context->get_devices(); @@ -207,8 +207,7 @@ size_t DPCTLContext_Hash(__dpctl_keep const DPCTLSyclContextRef CtxRef) return hash_fn(*C); } else { - std::cerr << "Argument CtxRef is null" - << "/n"; + error_handler("Argument CtxRef is null.", __FILE__, __func__, __LINE__); return 0; } } diff --git a/libsyclinterface/source/dpctl_sycl_device_interface.cpp b/libsyclinterface/source/dpctl_sycl_device_interface.cpp index dbe42e764b..804998cf19 100644 --- a/libsyclinterface/source/dpctl_sycl_device_interface.cpp +++ b/libsyclinterface/source/dpctl_sycl_device_interface.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_device_interface.h" +#include "../helper/include/dpctl_error_handlers.h" #include "../helper/include/dpctl_string_utils.hpp" #include "../helper/include/dpctl_utils_helper.h" #include "Support/CBindingWrapping.h" @@ -52,15 +53,15 @@ DPCTLDevice_Copy(__dpctl_keep const DPCTLSyclDeviceRef DRef) { auto Device = unwrap(DRef); if (!Device) { - std::cerr << "Cannot copy DPCTLSyclDeviceRef as input is a nullptr\n"; + error_handler("Cannot copy DPCTLSyclDeviceRef as input is a nullptr", + __FILE__, __func__, __LINE__); return nullptr; } try { auto CopiedDevice = new device(*Device); return wrap(CopiedDevice); - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -70,13 +71,8 @@ __dpctl_give DPCTLSyclDeviceRef DPCTLDevice_Create() try { auto Device = new device(); return wrap(Device); - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -85,19 +81,17 @@ __dpctl_give DPCTLSyclDeviceRef DPCTLDevice_CreateFromSelector( __dpctl_keep const DPCTLSyclDeviceSelectorRef DSRef) { auto Selector = unwrap(DSRef); - if (!Selector) - // \todo : Log error + if (!Selector) { + error_handler("Cannot difine device selector for DPCTLSyclDeviceRef " + "as input is a nullptr.", + __FILE__, __func__, __LINE__); return nullptr; + } try { auto Device = new device(*Selector); return wrap(Device); - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -116,9 +110,8 @@ DPCTLDevice_GetDeviceType(__dpctl_keep const DPCTLSyclDeviceRef DRef) try { auto SyclDTy = D->get_info(); DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(SyclDTy); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return DTy; @@ -180,9 +173,8 @@ DPCTLDevice_GetMaxComputeUnits(__dpctl_keep const DPCTLSyclDeviceRef DRef) if (D) { try { nComputeUnits = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return nComputeUnits; @@ -196,9 +188,8 @@ DPCTLDevice_GetGlobalMemSize(__dpctl_keep const DPCTLSyclDeviceRef DRef) if (D) { try { GlobalMemSize = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return GlobalMemSize; @@ -211,9 +202,8 @@ uint64_t DPCTLDevice_GetLocalMemSize(__dpctl_keep const DPCTLSyclDeviceRef DRef) if (D) { try { LocalMemSize = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return LocalMemSize; @@ -228,9 +218,8 @@ DPCTLDevice_GetMaxWorkItemDims(__dpctl_keep const DPCTLSyclDeviceRef DRef) try { maxWorkItemDims = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return maxWorkItemDims; @@ -248,12 +237,8 @@ DPCTLDevice_GetMaxWorkItemSizes(__dpctl_keep const DPCTLSyclDeviceRef DRef) for (auto i = 0ul; i < 3; ++i) { sizes[i] = id_sizes[i]; } - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return sizes; @@ -267,9 +252,8 @@ DPCTLDevice_GetMaxWorkGroupSize(__dpctl_keep const DPCTLSyclDeviceRef DRef) if (D) { try { max_wg_size = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return max_wg_size; @@ -283,9 +267,8 @@ DPCTLDevice_GetMaxNumSubGroups(__dpctl_keep const DPCTLSyclDeviceRef DRef) if (D) { try { max_nsubgroups = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return max_nsubgroups; @@ -299,8 +282,8 @@ DPCTLDevice_GetPlatform(__dpctl_keep const DPCTLSyclDeviceRef DRef) if (D) { try { PRef = wrap(new platform(D->get_platform())); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return PRef; @@ -315,9 +298,8 @@ DPCTLDevice_GetName(__dpctl_keep const DPCTLSyclDeviceRef DRef) try { auto name = D->get_info(); cstr_name = dpctl::helper::cstring_from_string(name); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return cstr_name; @@ -332,9 +314,8 @@ DPCTLDevice_GetVendor(__dpctl_keep const DPCTLSyclDeviceRef DRef) try { auto vendor = D->get_info(); cstr_vendor = dpctl::helper::cstring_from_string(vendor); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return cstr_vendor; @@ -349,9 +330,8 @@ DPCTLDevice_GetDriverVersion(__dpctl_keep const DPCTLSyclDeviceRef DRef) try { auto driver = D->get_info(); cstr_driver = dpctl::helper::cstring_from_string(driver); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return cstr_driver; @@ -364,9 +344,8 @@ bool DPCTLDevice_IsHostUnifiedMemory(__dpctl_keep const DPCTLSyclDeviceRef DRef) if (D) { try { ret = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return ret; @@ -391,9 +370,8 @@ bool DPCTLDevice_HasAspect(__dpctl_keep const DPCTLSyclDeviceRef DRef, if (D) { try { hasAspect = D->has(DPCTL_DPCTLAspectTypeToSyclAspect(AT)); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return hasAspect; @@ -407,8 +385,8 @@ bool DPCTLDevice_HasAspect(__dpctl_keep const DPCTLSyclDeviceRef DRef, if (D) { \ try { \ result = D->get_info(); \ - } catch (runtime_error const &re) { \ - std::cerr << re.what() << '\n'; \ + } catch (std::exception const &e) { \ + error_handler(e, __FILE__, __func__, __LINE__); \ } \ } \ return result; \ @@ -431,9 +409,8 @@ bool DPCTLDevice_GetSubGroupIndependentForwardProgress( try { SubGroupProgress = D->get_info< info::device::sub_group_independent_forward_progress>(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return SubGroupProgress; @@ -448,9 +425,8 @@ uint32_t DPCTLDevice_GetPreferredVectorWidthChar( try { vector_width_char = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return vector_width_char; @@ -465,9 +441,8 @@ uint32_t DPCTLDevice_GetPreferredVectorWidthShort( try { vector_width_short = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return vector_width_short; @@ -482,9 +457,8 @@ uint32_t DPCTLDevice_GetPreferredVectorWidthInt( try { vector_width_int = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return vector_width_int; @@ -499,9 +473,8 @@ uint32_t DPCTLDevice_GetPreferredVectorWidthLong( try { vector_width_long = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return vector_width_long; @@ -516,9 +489,8 @@ uint32_t DPCTLDevice_GetPreferredVectorWidthFloat( try { vector_width_float = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return vector_width_float; @@ -533,9 +505,8 @@ uint32_t DPCTLDevice_GetPreferredVectorWidthDouble( try { vector_width_double = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return vector_width_double; @@ -550,9 +521,8 @@ uint32_t DPCTLDevice_GetPreferredVectorWidthHalf( try { vector_width_half = D->get_info(); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return vector_width_half; @@ -566,12 +536,8 @@ DPCTLDevice_GetParentDevice(__dpctl_keep const DPCTLSyclDeviceRef DRef) try { auto parent_D = D->get_info(); return wrap(new device(parent_D)); - } catch (invalid_object_error const &ioe) { - // not a sub device - return nullptr; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -586,8 +552,8 @@ DPCTLDevice_CreateSubDevicesEqually(__dpctl_keep const DPCTLSyclDeviceRef DRef, std::vector *Devices = nullptr; if (DRef) { if (count == 0) { - std::cerr << "Can not create sub-devices with zero compute units" - << '\n'; + error_handler("Cannot create sub-devices with zero compute units", + __FILE__, __func__, __LINE__); return nullptr; } auto D = unwrap(DRef); @@ -598,18 +564,9 @@ DPCTLDevice_CreateSubDevicesEqually(__dpctl_keep const DPCTLSyclDeviceRef DRef, for (const auto &sd : subDevices) { Devices->emplace_back(wrap(new device(sd))); } - } catch (std::bad_alloc const &ba) { - delete Devices; - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (feature_not_supported const &fnse) { - delete Devices; - std::cerr << fnse.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { + } catch (std::exception const &e) { delete Devices; - // \todo log error - std::cerr << re.what() << '\n'; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -626,8 +583,8 @@ DPCTLDevice_CreateSubDevicesByCounts(__dpctl_keep const DPCTLSyclDeviceRef DRef, vcounts.assign(counts, counts + ncounts); size_t min_elem = *std::min_element(vcounts.begin(), vcounts.end()); if (min_elem == 0) { - std::cerr << "Can not create sub-devices with zero compute units" - << '\n'; + error_handler("Cannot create sub-devices with zero compute units", + __FILE__, __func__, __LINE__); return nullptr; } if (DRef) { @@ -636,12 +593,8 @@ DPCTLDevice_CreateSubDevicesByCounts(__dpctl_keep const DPCTLSyclDeviceRef DRef, try { subDevices = D->create_sub_devices< info::partition_property::partition_by_counts>(vcounts); - } catch (feature_not_supported const &fnse) { - std::cerr << fnse.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } try { @@ -649,14 +602,9 @@ DPCTLDevice_CreateSubDevicesByCounts(__dpctl_keep const DPCTLSyclDeviceRef DRef, for (const auto &sd : subDevices) { Devices->emplace_back(wrap(new device(sd))); } - } catch (std::bad_alloc const &ba) { - delete Devices; - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { + } catch (std::exception const &e) { delete Devices; - // \todo log error - std::cerr << re.what() << '\n'; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -679,18 +627,9 @@ __dpctl_give DPCTLDeviceVectorRef DPCTLDevice_CreateSubDevicesByAffinity( for (const auto &sd : subDevices) { Devices->emplace_back(wrap(new device(sd))); } - } catch (std::bad_alloc const &ba) { - delete Devices; - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (feature_not_supported const &fnse) { - delete Devices; - std::cerr << fnse.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { + } catch (std::exception const &e) { delete Devices; - // \todo log error - std::cerr << re.what() << '\n'; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -705,9 +644,7 @@ size_t DPCTLDevice_Hash(__dpctl_keep const DPCTLSyclDeviceRef DRef) return hash_fn(*D); } else { - // todo: log error - std::cerr << "Argument DRef is null" - << "/n"; + error_handler("Argument DRef is null", __FILE__, __func__, __LINE__); return 0; } } diff --git a/libsyclinterface/source/dpctl_sycl_device_manager.cpp b/libsyclinterface/source/dpctl_sycl_device_manager.cpp index 67446d6350..06d90c039c 100644 --- a/libsyclinterface/source/dpctl_sycl_device_manager.cpp +++ b/libsyclinterface/source/dpctl_sycl_device_manager.cpp @@ -24,6 +24,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_device_manager.h" +#include "../helper/include/dpctl_error_handlers.h" #include "../helper/include/dpctl_string_utils.hpp" #include "../helper/include/dpctl_utils_helper.h" #include "Support/CBindingWrapping.h" @@ -134,8 +135,9 @@ struct DeviceCacheBuilder auto entry = cache_l.emplace(D, Ctx); if (!entry.second) { - std::cerr << "Fatal Error during device cache " - "construction.\n"; + error_handler("Fatal Error during device cache " + "construction.", + __FILE__, __func__, __LINE__); std::terminate(); } } @@ -160,9 +162,12 @@ DPCTLDeviceMgr_GetCachedContext(__dpctl_keep const DPCTLSyclDeviceRef DRef) DPCTLSyclContextRef CRef = nullptr; auto Device = unwrap(DRef); - if (!Device) + if (!Device) { + error_handler("Cannot create device from DPCTLSyclDeviceRef" + "as input is a nullptr.", + __FILE__, __func__, __LINE__); return CRef; - + } auto &cache = DeviceCacheBuilder::getDeviceCache(); auto entry = cache.find(*Device); if (entry != cache.end()) { @@ -170,14 +175,15 @@ DPCTLDeviceMgr_GetCachedContext(__dpctl_keep const DPCTLSyclDeviceRef DRef) try { ContextPtr = new context(entry->second); CRef = wrap(ContextPtr); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << std::endl; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); delete ContextPtr; CRef = nullptr; } } else { - std::cerr << "No cached default context for device" << std::endl; + error_handler("No cached default context for device.", __FILE__, + __func__, __LINE__); } return CRef; } @@ -191,8 +197,9 @@ DPCTLDeviceMgr_GetDevices(int device_identifier) try { Devices = new std::vector(); - } catch (std::bad_alloc const &ba) { + } catch (std::exception const &e) { delete Devices; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } @@ -226,9 +233,8 @@ DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef) try { auto infostr = get_device_info_str(*D); cstr_info = dpctl::helper::cstring_from_string(infostr); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return cstr_info; @@ -303,7 +309,8 @@ void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef) if (Device) std::cout << get_device_info_str(*Device); else - std::cout << "Device is not valid (NULL). Cannot print device info.\n"; + error_handler("Device is not valid (NULL). Cannot print device info.", + __FILE__, __func__, __LINE__); } int64_t DPCTLDeviceMgr_GetRelativeId(__dpctl_keep const DPCTLSyclDeviceRef DRef) diff --git a/libsyclinterface/source/dpctl_sycl_device_selector_interface.cpp b/libsyclinterface/source/dpctl_sycl_device_selector_interface.cpp index 1193e11cee..8793775806 100644 --- a/libsyclinterface/source/dpctl_sycl_device_selector_interface.cpp +++ b/libsyclinterface/source/dpctl_sycl_device_selector_interface.cpp @@ -24,6 +24,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_device_selector_interface.h" +#include "../helper/include/dpctl_error_handlers.h" #include "Support/CBindingWrapping.h" #include /* SYCL headers */ @@ -42,11 +43,8 @@ __dpctl_give DPCTLSyclDeviceSelectorRef DPCTLAcceleratorSelector_Create() try { auto Selector = new accelerator_selector(); return wrap(Selector); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -56,11 +54,8 @@ __dpctl_give DPCTLSyclDeviceSelectorRef DPCTLDefaultSelector_Create() try { auto Selector = new default_selector(); return wrap(Selector); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -70,11 +65,8 @@ __dpctl_give DPCTLSyclDeviceSelectorRef DPCTLCPUSelector_Create() try { auto Selector = new cpu_selector(); return wrap(Selector); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -90,11 +82,8 @@ DPCTLFilterSelector_Create(__dpctl_keep const char *filter_str) try { auto Selector = new filter_selector_t(filter_str); return wrap(Selector); - } catch (std::bad_alloc &ba) { - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error &re) { - std::cerr << "Device: " << filter_str << " " << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -104,11 +93,8 @@ __dpctl_give DPCTLSyclDeviceSelectorRef DPCTLGPUSelector_Create() try { auto Selector = new gpu_selector(); return wrap(Selector); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -118,11 +104,8 @@ __dpctl_give DPCTLSyclDeviceSelectorRef DPCTLHostSelector_Create() try { auto Selector = new host_selector(); return wrap(Selector); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } diff --git a/libsyclinterface/source/dpctl_sycl_event_interface.cpp b/libsyclinterface/source/dpctl_sycl_event_interface.cpp index 5d36ef3dee..2ba4b28011 100644 --- a/libsyclinterface/source/dpctl_sycl_event_interface.cpp +++ b/libsyclinterface/source/dpctl_sycl_event_interface.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_event_interface.h" +#include "../helper/include/dpctl_error_handlers.h" #include "../helper/include/dpctl_utils_helper.h" #include "Support/CBindingWrapping.h" #include /* SYCL headers */ @@ -49,23 +50,23 @@ __dpctl_give DPCTLSyclEventRef DPCTLEvent_Create() try { auto E = new event(); ERef = wrap(E); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } return ERef; } void DPCTLEvent_Wait(__dpctl_keep DPCTLSyclEventRef ERef) { - // \todo How to handle errors? E.g. when ERef is null or not a valid event. if (ERef) { auto SyclEvent = unwrap(ERef); if (SyclEvent) SyclEvent->wait(); } else { - std::cerr << "Cannot wait for the event. DPCTLSyclEventRef as input is " - "a nullptr\n"; + error_handler("Cannot wait for the event. DPCTLSyclEventRef as " + "input is a nullptr.", + __FILE__, __func__, __LINE__); } } @@ -77,9 +78,9 @@ void DPCTLEvent_WaitAndThrow(__dpctl_keep DPCTLSyclEventRef ERef) SyclEvent->wait_and_throw(); } else { - std::cerr << "Cannot wait_and_throw for the event. DPCTLSyclEventRef " - "as input is " - "a nullptr\n"; + error_handler("Cannot wait_and_throw for the event. DPCTLSyclEventRef " + "as input is a nullptr.", + __FILE__, __func__, __LINE__); } } @@ -93,15 +94,15 @@ DPCTLEvent_Copy(__dpctl_keep DPCTLSyclEventRef ERef) { auto SyclEvent = unwrap(ERef); if (!SyclEvent) { - std::cerr << "Cannot copy DPCTLSyclEventRef as input is a nullptr\n"; + error_handler("Cannot copy DPCTLSyclEventRef as input is a nullptr.", + __FILE__, __func__, __LINE__); return nullptr; } try { auto CopiedSyclEvent = new event(*SyclEvent); return wrap(CopiedSyclEvent); - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -114,7 +115,8 @@ DPCTLSyclBackendType DPCTLEvent_GetBackend(__dpctl_keep DPCTLSyclEventRef ERef) BTy = DPCTL_SyclBackendToDPCTLBackendType(E->get_backend()); } else { - std::cerr << "Backend cannot be looked up for a NULL event\n"; + error_handler("Backend cannot be looked up for a NULL event.", __FILE__, + __func__, __LINE__); } return BTy; } @@ -130,9 +132,8 @@ DPCTLEvent_GetCommandExecutionStatus(__dpctl_keep DPCTLSyclEventRef ERef) auto SyclESTy = E->get_info(); ESTy = DPCTL_SyclEventStatusToDPCTLEventStatusType(SyclESTy); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return ESTy; @@ -147,9 +148,8 @@ uint64_t DPCTLEvent_GetProfilingInfoSubmit(__dpctl_keep DPCTLSyclEventRef ERef) E->wait(); profilingInfoSubmit = E->get_profiling_info< sycl::info::event_profiling::command_submit>(); - } catch (invalid_object_error &e) { - // \todo log error - std::cerr << e.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return profilingInfoSubmit; @@ -164,9 +164,8 @@ uint64_t DPCTLEvent_GetProfilingInfoStart(__dpctl_keep DPCTLSyclEventRef ERef) E->wait(); profilingInfoStart = E->get_profiling_info< sycl::info::event_profiling::command_start>(); - } catch (invalid_object_error &e) { - // \todo log error - std::cerr << e.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return profilingInfoStart; @@ -181,9 +180,8 @@ uint64_t DPCTLEvent_GetProfilingInfoEnd(__dpctl_keep DPCTLSyclEventRef ERef) E->wait(); profilingInfoEnd = E->get_profiling_info< sycl::info::event_profiling::command_end>(); - } catch (invalid_object_error &e) { - // \todo log error - std::cerr << e.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } return profilingInfoEnd; @@ -194,15 +192,15 @@ DPCTLEvent_GetWaitList(__dpctl_keep DPCTLSyclEventRef ERef) { auto E = unwrap(ERef); if (!E) { - std::cerr << "Cannot get wait list as input is a nullptr\n"; + error_handler("Cannot get wait list as input is a nullptr.", __FILE__, + __func__, __LINE__); return nullptr; } std::vector *EventsVectorPtr = nullptr; try { EventsVectorPtr = new std::vector(); - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } try { @@ -212,15 +210,9 @@ DPCTLEvent_GetWaitList(__dpctl_keep DPCTLSyclEventRef ERef) EventsVectorPtr->emplace_back(wrap(new event(Ev))); } return wrap(EventsVectorPtr); - } catch (std::bad_alloc const &ba) { + } catch (std::exception const &e) { delete EventsVectorPtr; - // \todo log error - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (const runtime_error &re) { - delete EventsVectorPtr; - // \todo log error - std::cerr << re.what() << '\n'; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } diff --git a/libsyclinterface/source/dpctl_sycl_kernel_interface.cpp b/libsyclinterface/source/dpctl_sycl_kernel_interface.cpp index b25d73ada0..a1a7e38e39 100644 --- a/libsyclinterface/source/dpctl_sycl_kernel_interface.cpp +++ b/libsyclinterface/source/dpctl_sycl_kernel_interface.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_kernel_interface.h" +#include "../helper/include/dpctl_error_handlers.h" #include "../helper/include/dpctl_string_utils.hpp" #include "Support/CBindingWrapping.h" #include /* Sycl headers */ @@ -42,7 +43,9 @@ __dpctl_give const char * DPCTLKernel_GetFunctionName(__dpctl_keep const DPCTLSyclKernelRef Kernel) { if (!Kernel) { - // \todo record error + error_handler("Cannot get the number of arguments " + "as input is a nullptr.", + __FILE__, __func__, __LINE__); return nullptr; } @@ -56,7 +59,9 @@ DPCTLKernel_GetFunctionName(__dpctl_keep const DPCTLSyclKernelRef Kernel) size_t DPCTLKernel_GetNumArgs(__dpctl_keep const DPCTLSyclKernelRef Kernel) { if (!Kernel) { - // \todo record error + error_handler("Cannot get the number of arguments from " + "DPCTLSyclKernelRef as input is a nullptr.", + __FILE__, __func__, __LINE__); return -1; } diff --git a/libsyclinterface/source/dpctl_sycl_platform_interface.cpp b/libsyclinterface/source/dpctl_sycl_platform_interface.cpp index 03004433cb..1ae656b08f 100644 --- a/libsyclinterface/source/dpctl_sycl_platform_interface.cpp +++ b/libsyclinterface/source/dpctl_sycl_platform_interface.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_platform_interface.h" +#include "../helper/include/dpctl_error_handlers.h" #include "../helper/include/dpctl_string_utils.hpp" #include "../helper/include/dpctl_utils_helper.h" #include "Support/CBindingWrapping.h" @@ -50,15 +51,15 @@ DPCTLPlatform_Copy(__dpctl_keep const DPCTLSyclPlatformRef PRef) { auto Platform = unwrap(PRef); if (!Platform) { - std::cerr << "Cannot copy DPCTLSyclPlatformRef as input is a nullptr\n"; + error_handler("Cannot copy DPCTLSyclPlatformRef as input is a nullptr.", + __FILE__, __func__, __LINE__); return nullptr; } try { auto CopiedPlatform = new platform(*Platform); return wrap(CopiedPlatform); - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -69,8 +70,8 @@ __dpctl_give DPCTLSyclPlatformRef DPCTLPlatform_Create() try { auto P = new platform(); PRef = wrap(P); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } return PRef; } @@ -84,17 +85,15 @@ __dpctl_give DPCTLSyclPlatformRef DPCTLPlatform_CreateFromSelector( try { P = new platform(*DS); return wrap(P); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - return nullptr; - } catch (runtime_error const &re) { + } catch (std::exception const &e) { delete P; - std::cerr << re.what() << '\n'; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } else { - std::cerr << "Device selector pointer cannot be NULL\n"; + error_handler("Device selector pointer cannot be NULL.", __FILE__, + __func__, __LINE__); } return nullptr; @@ -115,7 +114,8 @@ DPCTLPlatform_GetBackend(__dpctl_keep const DPCTLSyclPlatformRef PRef) BTy = DPCTL_SyclBackendToDPCTLBackendType(P->get_backend()); } else { - std::cerr << "Backend cannot be looked up for a NULL platform\n"; + error_handler("Backend cannot be looked up for a NULL platform.", + __FILE__, __func__, __LINE__); } return BTy; } @@ -128,14 +128,14 @@ DPCTLPlatform_GetName(__dpctl_keep const DPCTLSyclPlatformRef PRef) try { auto name = P->get_info(); return dpctl::helper::cstring_from_string(name); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } else { - std::cerr << "Name cannot be looked up for a NULL platform\n"; + error_handler("Name cannot be looked up for a NULL platform.", __FILE__, + __func__, __LINE__); return nullptr; } } @@ -148,14 +148,14 @@ DPCTLPlatform_GetVendor(__dpctl_keep const DPCTLSyclPlatformRef PRef) try { auto vendor = P->get_info(); return dpctl::helper::cstring_from_string(vendor); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } else { - std::cerr << "Vendor cannot be looked up for a NULL platform\n"; + error_handler("Vendor cannot be looked up for a NULL platform.", + __FILE__, __func__, __LINE__); return nullptr; } } @@ -168,14 +168,14 @@ DPCTLPlatform_GetVersion(__dpctl_keep const DPCTLSyclPlatformRef PRef) try { auto driver = P->get_info(); return dpctl::helper::cstring_from_string(driver); - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } else { - std::cerr << "Driver version cannot be looked up for a NULL platform\n"; + error_handler("Driver version cannot be looked up for a NULL platform.", + __FILE__, __func__, __LINE__); return nullptr; } } @@ -189,7 +189,8 @@ __dpctl_give DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms() try { Platforms = new std::vector(); Platforms->reserve(platforms.size()); - } catch (std::bad_alloc const &ba) { + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } diff --git a/libsyclinterface/source/dpctl_sycl_platform_manager.cpp b/libsyclinterface/source/dpctl_sycl_platform_manager.cpp index 11a4b65cf2..679b0c6350 100644 --- a/libsyclinterface/source/dpctl_sycl_platform_manager.cpp +++ b/libsyclinterface/source/dpctl_sycl_platform_manager.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_platform_manager.h" +#include "../helper/include/dpctl_error_handlers.h" #include "../helper/include/dpctl_utils_helper.h" #include "Support/CBindingWrapping.h" #include "dpctl_sycl_platform_interface.h" @@ -45,8 +46,9 @@ void platform_print_info_impl(const platform &p, size_t verbosity) std::stringstream ss; if (verbosity > 2) { - std::cerr << "Illegal verbosity level. Accepted values are 0, 1, or 2. " - "Defaulting to verbosity level 0.\n"; + error_handler("Illegal verbosity level. Accepted values are 0, 1, or 2." + "Defaulting to verbosity level 0.", + __FILE__, __func__, __LINE__); verbosity = 0; } @@ -112,6 +114,7 @@ void DPCTLPlatformMgr_PrintInfo(__dpctl_keep const DPCTLSyclPlatformRef PRef, platform_print_info_impl(*p, verbosity); } else { - std::cout << "Platform reference is NULL.\n"; + error_handler("Platform reference is NULL.", __FILE__, __func__, + __LINE__); } } diff --git a/libsyclinterface/source/dpctl_sycl_program_interface.cpp b/libsyclinterface/source/dpctl_sycl_program_interface.cpp index fc2f443beb..7e24d3fd34 100644 --- a/libsyclinterface/source/dpctl_sycl_program_interface.cpp +++ b/libsyclinterface/source/dpctl_sycl_program_interface.cpp @@ -30,11 +30,13 @@ #endif #include "dpctl_sycl_program_interface.h" +#include "../helper/include/dpctl_error_handlers.h" #include "Config/dpctl_config.h" #include "Support/CBindingWrapping.h" #include /* OpenCL headers */ #include /* Sycl headers */ #include +#include #ifdef DPCTL_ENABLE_LO_PROGRAM_CREATION #include "../helper/include/dpctl_dynamic_lib_helper.h" @@ -84,10 +86,11 @@ createOpenCLInterOpProgram(const context &SyclCtx, auto CLCtx = get_native(SyclCtx); 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"; + std::stringstream ss; + ss << "OpenCL program could not be created from the SPIR-V " + "binary. OpenCL Error " + << err << "."; + error_handler(ss.str(), __FILE__, __func__, __LINE__); return nullptr; } auto SyclDevices = SyclCtx.get_devices(); @@ -104,9 +107,9 @@ createOpenCLInterOpProgram(const context &SyclCtx, 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"; + std::stringstream ss; + ss << "OpenCL program could not be built. OpenCL Error " << err << "."; + error_handler(ss.str(), __FILE__, __func__, __LINE__); return nullptr; } @@ -114,9 +117,8 @@ createOpenCLInterOpProgram(const context &SyclCtx, try { auto SyclProgram = new program(SyclCtx, CLProgram); return wrap(SyclProgram); - } catch (invalid_object_error &e) { - // \todo record error - std::cerr << e.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -127,9 +129,9 @@ zeModuleCreateFT getZeModuleCreateFn() { static dpctl::DynamicLibHelper zeLib(zeLoaderName, libLoadFlags); if (!zeLib.opened()) { - // TODO: handle error - std::cerr << "The level zero loader dynamic library could not " - "be opened.\n"; + error_handler("The level zero loader dynamic library could not " + "be opened.", + __FILE__, __func__, __LINE__); return nullptr; } static auto stZeModuleCreateF = @@ -147,8 +149,8 @@ createLevelZeroInterOpProgram(const context &SyclCtx, auto ZeCtx = get_native(SyclCtx); auto SyclDevices = SyclCtx.get_devices(); if (SyclDevices.size() > 1) { - std::cerr << "Level zero program can be created for only one device.\n"; - // TODO: handle error + error_handler("Level zero program can be created for only one device.", + __FILE__, __func__, __LINE__); return nullptr; } @@ -172,15 +174,16 @@ createLevelZeroInterOpProgram(const context &SyclCtx, auto stZeModuleCreateF = getZeModuleCreateFn(); if (!stZeModuleCreateF) { - std::cerr << "ZeModuleCreateFn is invalid.\n"; + error_handler("ZeModuleCreateFn is invalid.", __FILE__, __func__, + __LINE__); return nullptr; } auto ret = stZeModuleCreateF(ZeCtx, ZeDevice, &ZeModuleDesc, &ZeModule, nullptr); if (ret != ZE_RESULT_SUCCESS) { - // TODO: handle error - std::cerr << "ZeModule creation failed.\n"; + error_handler("ZeModule creation failed.", __FILE__, __func__, + __LINE__); return nullptr; } @@ -189,9 +192,8 @@ createLevelZeroInterOpProgram(const context &SyclCtx, auto ZeProgram = new program(sycl::level_zero::make_program( SyclCtx, reinterpret_cast(ZeModule))); return wrap(ZeProgram); - } catch (invalid_object_error &e) { - // \todo record error - std::cerr << e.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -208,9 +210,9 @@ DPCTLProgram_CreateFromSpirv(__dpctl_keep const DPCTLSyclContextRef CtxRef, DPCTLSyclProgramRef Pref = nullptr; context *SyclCtx = nullptr; if (!CtxRef) { - // \todo handle error - std::cerr << "Cannot create program from SPIR-V as the supplied SYCL " - "context is NULL.\n"; + error_handler("Cannot create program from SPIR-V as the supplied SYCL " + "context is NULL.", + __FILE__, __func__, __LINE__); return Pref; } SyclCtx = unwrap(CtxRef); @@ -241,12 +243,12 @@ DPCTLProgram_CreateFromOCLSource(__dpctl_keep const DPCTLSyclContextRef Ctx, program *SyclProgram = nullptr; if (!Ctx) { - // \todo handle error + error_handler("Input Ctx is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } if (!Source) { - // \todo handle error message + error_handler("Input Source is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } @@ -265,29 +267,20 @@ DPCTLProgram_CreateFromOCLSource(__dpctl_keep const DPCTLSyclContextRef Ctx, try { SyclProgram->build_with_source(source, compileOpts); return wrap(SyclProgram); - } catch (compile_program_error &e) { - std::cerr << e.what() << '\n'; + } catch (std::exception const &e) { delete SyclProgram; - // \todo record error - return nullptr; - } catch (feature_not_supported &e) { - std::cerr << e.what() << '\n'; - delete SyclProgram; - // \todo record error - return nullptr; - } catch (runtime_error &e) { - std::cerr << e.what() << '\n'; - delete SyclProgram; - // \todo record error + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } break; case backend::level_zero: - std::cerr << "CreateFromSource is not supported in Level Zero.\n"; + error_handler("CreateFromSource is not supported in Level Zero.", + __FILE__, __func__, __LINE__); delete SyclProgram; return nullptr; default: - std::cerr << "CreateFromSource is not supported in unknown backend.\n"; + error_handler("CreateFromSource is not supported in unknown backend.", + __FILE__, __func__, __LINE__); delete SyclProgram; return nullptr; } @@ -298,21 +291,21 @@ DPCTLProgram_GetKernel(__dpctl_keep DPCTLSyclProgramRef PRef, __dpctl_keep const char *KernelName) { if (!PRef) { - // \todo record error + error_handler("Input PRef is nullptr", __FILE__, __func__, __LINE__); return nullptr; } auto SyclProgram = unwrap(PRef); if (!KernelName) { - // \todo record error + error_handler("Input KernelName is nullptr", __FILE__, __func__, + __LINE__); return nullptr; } std::string name = KernelName; try { auto SyclKernel = new kernel(SyclProgram->get_kernel(name)); return wrap(SyclKernel); - } catch (invalid_object_error &e) { - // \todo record error - std::cerr << e.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -321,15 +314,15 @@ bool DPCTLProgram_HasKernel(__dpctl_keep DPCTLSyclProgramRef PRef, __dpctl_keep const char *KernelName) { if (!PRef) { - // \todo handle error + error_handler("Input PRef is nullptr", __FILE__, __func__, __LINE__); return false; } auto SyclProgram = unwrap(PRef); try { return SyclProgram->has_kernel(KernelName); - } catch (invalid_object_error &e) { - std::cerr << e.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return false; } } diff --git a/libsyclinterface/source/dpctl_sycl_queue_interface.cpp b/libsyclinterface/source/dpctl_sycl_queue_interface.cpp index 46bbd0d10f..152bec562d 100644 --- a/libsyclinterface/source/dpctl_sycl_queue_interface.cpp +++ b/libsyclinterface/source/dpctl_sycl_queue_interface.cpp @@ -25,7 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_queue_interface.h" -#include "../helper/include/dpctl_async_error_handler.h" +#include "../helper/include/dpctl_error_handlers.h" #include "Support/CBindingWrapping.h" #include "dpctl_sycl_context_interface.h" #include "dpctl_sycl_device_interface.h" @@ -108,9 +108,9 @@ bool set_kernel_arg(handler &cgh, cgh.set_arg(idx, Arg); break; default: - // \todo handle errors arg_set = false; - std::cerr << "Kernel argument could not be created.\n"; + error_handler("Kernel argument could not be created.", __FILE__, + __func__, __LINE__); break; } return arg_set; @@ -140,10 +140,10 @@ std::unique_ptr create_property_list(int properties) } if (_prop) { - // todo: log error - std::cerr << "Invalid queue property argument (" << std::hex - << properties << "), interpreted as (" << (properties ^ _prop) - << ")" << '\n'; + std::stringstream ss; + ss << "Invalid queue property argument (" << std::hex << properties + << "), interpreted as (" << (properties ^ _prop) << ")."; + error_handler(ss.str(), __FILE__, __func__, __LINE__); } return propList; } @@ -165,7 +165,7 @@ DPCTL_API __dpctl_give DPCTLSyclQueueRef DPCTLQueue_Create(__dpctl_keep const DPCTLSyclContextRef CRef, __dpctl_keep const DPCTLSyclDeviceRef DRef, - error_handler_callback *error_handler, + error_handler_callback *handler, int properties) { DPCTLSyclQueueRef q = nullptr; @@ -173,51 +173,45 @@ DPCTLQueue_Create(__dpctl_keep const DPCTLSyclContextRef CRef, auto ctx = unwrap(CRef); if (!(dev && ctx)) { - /* \todo handle error */ + error_handler("Cannot create queue from DPCTLSyclContextRef and " + "DPCTLSyclDeviceRef as input is a nullptr.", + __FILE__, __func__, __LINE__); return q; } auto propList = create_property_list(properties); - if (propList && error_handler) { + if (propList && handler) { try { - auto Queue = new queue( - *ctx, *dev, DPCTL_AsyncErrorHandler(error_handler), *propList); + auto Queue = new queue(*ctx, *dev, DPCTL_AsyncErrorHandler(handler), + *propList); q = wrap(Queue); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (runtime_error &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } else if (properties) { try { auto Queue = new queue(*ctx, *dev, *propList); q = wrap(Queue); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (runtime_error &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } - else if (error_handler) { + else if (handler) { try { auto Queue = - new queue(*ctx, *dev, DPCTL_AsyncErrorHandler(error_handler)); + new queue(*ctx, *dev, DPCTL_AsyncErrorHandler(handler)); q = wrap(Queue); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (runtime_error &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } else { try { auto Queue = new queue(*ctx, *dev); q = wrap(Queue); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (runtime_error &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } @@ -234,7 +228,8 @@ DPCTLQueue_CreateForDevice(__dpctl_keep const DPCTLSyclDeviceRef DRef, auto Device = unwrap(DRef); if (!Device) { - std::cerr << "Cannot create queue from NULL device reference.\n"; + error_handler("Cannot create queue from NULL device reference.", + __FILE__, __func__, __LINE__); return QRef; } // Check if a cached default context exists for the device. @@ -248,8 +243,8 @@ DPCTLQueue_CreateForDevice(__dpctl_keep const DPCTLSyclDeviceRef DRef, try { ContextPtr = new context(*Device); CRef = wrap(ContextPtr); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << std::endl; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); delete ContextPtr; return QRef; } @@ -280,14 +275,14 @@ DPCTLQueue_Copy(__dpctl_keep const DPCTLSyclQueueRef QRef) try { auto CopiedQueue = new queue(*Queue); return wrap(CopiedQueue); - } catch (std::bad_alloc &ba) { - std::cerr << ba.what() << std::endl; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } else { - std::cerr << "Can not copy DPCTLSyclQueueRef as input is a nullptr" - << std::endl; + error_handler("Cannot copy DPCTLSyclQueueRef as input is a nullptr", + __FILE__, __func__, __LINE__); return nullptr; } } @@ -295,9 +290,11 @@ DPCTLQueue_Copy(__dpctl_keep const DPCTLSyclQueueRef QRef) bool DPCTLQueue_AreEq(__dpctl_keep const DPCTLSyclQueueRef QRef1, __dpctl_keep const DPCTLSyclQueueRef QRef2) { - if (!(QRef1 && QRef2)) - // \todo handle error + if (!(QRef1 && QRef2)) { + error_handler("DPCTLSyclQueueRefs are nullptr.", __FILE__, __func__, + __LINE__); return false; + } return (*unwrap(QRef1) == *unwrap(QRef2)); } @@ -308,9 +305,8 @@ DPCTLSyclBackendType DPCTLQueue_GetBackend(__dpctl_keep DPCTLSyclQueueRef QRef) try { auto C = Q->get_context(); return DPCTLContext_GetBackend(wrap(&C)); - } catch (runtime_error &re) { - std::cerr << re.what() << '\n'; - // store error message + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return DPCTL_UNKNOWN_BACKEND; } } @@ -327,12 +323,13 @@ DPCTLQueue_GetDevice(__dpctl_keep const DPCTLSyclQueueRef QRef) try { auto Device = new device(Q->get_device()); DRef = wrap(Device); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } } else { - std::cerr << "Could not get the device for this queue.\n"; + error_handler("Could not get the device for this queue.", __FILE__, + __func__, __LINE__); } return DRef; } @@ -345,7 +342,8 @@ DPCTLQueue_GetContext(__dpctl_keep const DPCTLSyclQueueRef QRef) if (Q) CRef = wrap(new context(Q->get_context())); else { - std::cerr << "Could not get the context for this queue.\n"; + error_handler("Could not get the context for this queue.", __FILE__, + __func__, __LINE__); } return CRef; } @@ -374,7 +372,6 @@ DPCTLQueue_SubmitRange(__dpctl_keep const DPCTLSyclKernelRef KRef, 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); } @@ -390,17 +387,12 @@ DPCTLQueue_SubmitRange(__dpctl_keep const DPCTLSyclKernelRef KRef, *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'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } @@ -432,7 +424,6 @@ DPCTLQueue_SubmitNDRange(__dpctl_keep const DPCTLSyclKernelRef KRef, 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); } @@ -452,17 +443,12 @@ DPCTLQueue_SubmitNDRange(__dpctl_keep const DPCTLSyclKernelRef KRef, *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'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } @@ -479,8 +465,7 @@ void DPCTLQueue_Wait(__dpctl_keep DPCTLSyclQueueRef QRef) SyclQueue->wait(); } else { - // todo: log error - std::cerr << "Argument QRef is NULL" << '\n'; + error_handler("Argument QRef is NULL.", __FILE__, __func__, __LINE__); } } @@ -494,16 +479,15 @@ DPCTLSyclEventRef DPCTLQueue_Memcpy(__dpctl_keep const DPCTLSyclQueueRef QRef, sycl::event ev; try { ev = Q->memcpy(Dest, Src, Count); - } catch (const sycl::runtime_error &re) { - // todo: log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } return wrap(new event(ev)); } else { - // todo: log error - std::cerr << "QRef passed to memcpy was NULL" << '\n'; + error_handler("QRef passed to memcpy was NULL.", __FILE__, __func__, + __LINE__); return nullptr; } } @@ -518,23 +502,21 @@ DPCTLSyclEventRef DPCTLQueue_Prefetch(__dpctl_keep DPCTLSyclQueueRef QRef, sycl::event ev; try { ev = Q->prefetch(Ptr, Count); - } catch (sycl::runtime_error &re) { - // todo: log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } return wrap(new event(ev)); } else { - // todo: log error - std::cerr << "Attempt to prefetch USM-allocation at nullptr" - << '\n'; + error_handler("Attempt to prefetch USM-allocation at nullptr.", + __FILE__, __func__, __LINE__); return nullptr; } } else { - // todo: log error - std::cerr << "QRef passed to prefetch was NULL" << '\n'; + error_handler("QRef passed to prefetch was NULL.", __FILE__, __func__, + __LINE__); return nullptr; } } @@ -549,16 +531,15 @@ DPCTLSyclEventRef DPCTLQueue_MemAdvise(__dpctl_keep DPCTLSyclQueueRef QRef, sycl::event ev; try { ev = Q->mem_advise(Ptr, Count, static_cast(Advice)); - } catch (const sycl::runtime_error &re) { - // todo: log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } return wrap(new event(ev)); } else { - // todo: log error - std::cerr << "QRef passed to prefetch was NULL" << '\n'; + error_handler("QRef passed to prefetch was NULL.", __FILE__, __func__, + __LINE__); return nullptr; } } @@ -591,8 +572,7 @@ size_t DPCTLQueue_Hash(__dpctl_keep const DPCTLSyclQueueRef QRef) return hash_fn(*Q); } else { - // todo: log error - std::cerr << "Argument QRef is null" << '\n'; + error_handler("Argument QRef is NULL.", __FILE__, __func__, __LINE__); return 0; } } @@ -614,20 +594,15 @@ __dpctl_give DPCTLSyclEventRef DPCTLQueue_SubmitBarrierForEvents( cgh.barrier(); }); - } 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'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } return wrap(new event(e)); } else { - // todo: log error - std::cerr << "Argument QRef is null" << '\n'; + error_handler("Argument QRef is NULL", __FILE__, __func__, __LINE__); return nullptr; } } diff --git a/libsyclinterface/source/dpctl_sycl_queue_manager.cpp b/libsyclinterface/source/dpctl_sycl_queue_manager.cpp index c43f364cf9..4bd7a7d3ea 100644 --- a/libsyclinterface/source/dpctl_sycl_queue_manager.cpp +++ b/libsyclinterface/source/dpctl_sycl_queue_manager.cpp @@ -24,6 +24,7 @@ /// //===----------------------------------------------------------------------===// #include "dpctl_sycl_queue_manager.h" +#include "../helper/include/dpctl_error_handlers.h" #include "Support/CBindingWrapping.h" #include "dpctl_sycl_device_manager.h" #include /* SYCL headers */ @@ -56,16 +57,15 @@ struct QueueManager qs.emplace_back(*unwrap(CRef), *unwrap(DRef)); } else { - std::cerr << "Fatal Error: No cached context for default " - "device.\n"; + error_handler("Fatal Error: No cached context for default " + "device.", + __FILE__, __func__, __LINE__); std::terminate(); } delete unwrap(DRef); delete unwrap(CRef); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (runtime_error const &re) { - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); } return qs; @@ -85,8 +85,8 @@ bool DPCTLQueueMgr_GlobalQueueIsCurrent() { auto &qs = QueueManager::getQueueStack(); if (qs.empty()) { - // \todo handle error - std::cerr << "Error: No global queue found.\n"; + error_handler("Error: No global queue found.", __FILE__, __func__, + __LINE__); return false; } // The first entry of the QueueStack is always the global queue. If there @@ -104,8 +104,8 @@ DPCTLSyclQueueRef DPCTLQueueMgr_GetCurrentQueue() { auto &qs = QueueManager::getQueueStack(); if (qs.empty()) { - // \todo handle error - std::cerr << "No currently active queues.\n"; + error_handler("No currently active queues.", __FILE__, __func__, + __LINE__); return nullptr; } auto last = qs.size() - 1; @@ -117,8 +117,8 @@ bool DPCTLQueueMgr_IsCurrentQueue(__dpctl_keep const DPCTLSyclQueueRef QRef) { auto &qs = QueueManager::getQueueStack(); if (qs.empty()) { - // \todo handle error - std::cerr << "No currently active queues.\n"; + error_handler("No currently active queues.", __FILE__, __func__, + __LINE__); return false; } auto last = qs.size() - 1; @@ -135,8 +135,9 @@ void DPCTLQueueMgr_SetGlobalQueue(__dpctl_keep const DPCTLSyclQueueRef qRef) qs[0] = *unwrap(qRef); } else { - // TODO: This should be an error and we should not fail silently. - std::cerr << "Error: Failed to set the global queue.\n"; + error_handler("Error: Failed to set the global queue.", __FILE__, + __func__, __LINE__); + std::terminate(); } } @@ -148,8 +149,9 @@ void DPCTLQueueMgr_PushQueue(__dpctl_keep const DPCTLSyclQueueRef qRef) qs.emplace_back(*unwrap(qRef)); } else { - // TODO: This should be an error and we should not fail silently. - std::cerr << "Error: Failed to set the current queue.\n"; + error_handler("Error: Failed to set the current queue.", __FILE__, + __func__, __LINE__); + std::terminate(); } } @@ -162,7 +164,7 @@ void DPCTLQueueMgr_PopQueue() // The first entry in the QueueStack is the global queue, and should not be // removed. if (qs.size() <= 1) { - std::cerr << "No queue to pop.\n"; + error_handler("No queue to pop.", __FILE__, __func__, __LINE__); return; } qs.pop_back(); @@ -172,8 +174,8 @@ size_t DPCTLQueueMgr_GetQueueStackSize() { auto &qs = QueueManager::getQueueStack(); if (qs.empty()) { - // \todo handle error - std::cerr << "Error: No global queue found.\n"; + error_handler("Error: No global queue found.", __FILE__, __func__, + __LINE__); return -1; } // The first entry of the QueueStack is always the global queue. If there diff --git a/libsyclinterface/source/dpctl_sycl_usm_interface.cpp b/libsyclinterface/source/dpctl_sycl_usm_interface.cpp index be1becb1f1..2347898c36 100644 --- a/libsyclinterface/source/dpctl_sycl_usm_interface.cpp +++ b/libsyclinterface/source/dpctl_sycl_usm_interface.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_usm_interface.h" +#include "../helper/include/dpctl_error_handlers.h" #include "Support/CBindingWrapping.h" #include "dpctl_sycl_device_interface.h" #include /* SYCL headers */ @@ -45,15 +46,15 @@ __dpctl_give DPCTLSyclUSMRef DPCTLmalloc_shared(size_t size, __dpctl_keep const DPCTLSyclQueueRef QRef) { if (!QRef) { - std::cerr << "Input QRef is nullptr\n"; + error_handler("Input QRef is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } try { auto Q = unwrap(QRef); auto Ptr = malloc_shared(size, *Q); return wrap(Ptr); - } catch (feature_not_supported const &fns) { - std::cerr << fns.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -64,15 +65,15 @@ DPCTLaligned_alloc_shared(size_t alignment, __dpctl_keep const DPCTLSyclQueueRef QRef) { if (!QRef) { - std::cerr << "Input QRef is nullptr\n"; + error_handler("Input QRef is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } try { auto Q = unwrap(QRef); auto Ptr = aligned_alloc_shared(alignment, size, *Q); return wrap(Ptr); - } catch (feature_not_supported const &fns) { - std::cerr << fns.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -81,7 +82,7 @@ __dpctl_give DPCTLSyclUSMRef DPCTLmalloc_host(size_t size, __dpctl_keep const DPCTLSyclQueueRef QRef) { if (!QRef) { - std::cerr << "Input QRef is nullptr\n"; + error_handler("Input QRef is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } // SYCL 2020 spec: for devices without aspect::usm_host_allocations: @@ -97,7 +98,7 @@ DPCTLaligned_alloc_host(size_t alignment, __dpctl_keep const DPCTLSyclQueueRef QRef) { if (!QRef) { - std::cerr << "Input QRef is nullptr\n"; + error_handler("Input QRef is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } // SYCL 2020 spec: for devices without aspect::usm_host_allocations: @@ -111,15 +112,15 @@ __dpctl_give DPCTLSyclUSMRef DPCTLmalloc_device(size_t size, __dpctl_keep const DPCTLSyclQueueRef QRef) { if (!QRef) { - std::cerr << "Input QRef is nullptr\n"; + error_handler("Input QRef is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } try { auto Q = unwrap(QRef); auto Ptr = malloc_device(size, *Q); return wrap(Ptr); - } catch (feature_not_supported const &fns) { - std::cerr << fns.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -130,15 +131,15 @@ DPCTLaligned_alloc_device(size_t alignment, __dpctl_keep const DPCTLSyclQueueRef QRef) { if (!QRef) { - std::cerr << "Input QRef is nullptr\n"; + error_handler("Input QRef is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } try { auto Q = unwrap(QRef); auto Ptr = aligned_alloc_device(alignment, size, *Q); return wrap(Ptr); - } catch (feature_not_supported const &fns) { - std::cerr << fns.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -147,11 +148,12 @@ void DPCTLfree_with_queue(__dpctl_take DPCTLSyclUSMRef MRef, __dpctl_keep const DPCTLSyclQueueRef QRef) { if (!QRef) { - std::cerr << "Input QRef is nullptr\n"; + error_handler("Input QRef is nullptr.", __FILE__, __func__, __LINE__); return; } if (!MRef) { - std::cerr << "Input MRef is nullptr, nothing to free\n"; + error_handler("Input MRef is nullptr, nothing to free.", __FILE__, + __func__, __LINE__); return; } auto Ptr = unwrap(MRef); @@ -163,11 +165,12 @@ void DPCTLfree_with_context(__dpctl_take DPCTLSyclUSMRef MRef, __dpctl_keep const DPCTLSyclContextRef CRef) { if (!CRef) { - std::cerr << "Input CRef is nullptr\n"; + error_handler("Input CRef is nullptr.", __FILE__, __func__, __LINE__); return; } if (!MRef) { - std::cerr << "Input MRef is nullptr, nothing to free\n"; + error_handler("Input MRef is nullptr, nothing to free.", __FILE__, + __func__, __LINE__); return; } auto Ptr = unwrap(MRef); @@ -179,11 +182,11 @@ const char *DPCTLUSM_GetPointerType(__dpctl_keep const DPCTLSyclUSMRef MRef, __dpctl_keep const DPCTLSyclContextRef CRef) { if (!CRef) { - std::cerr << "Input CRef is nullptr\n"; + error_handler("Input CRef is nullptr.", __FILE__, __func__, __LINE__); return "unknown"; } if (!MRef) { - std::cerr << "Input MRef is nullptr\n"; + error_handler("Input MRef is nullptr.", __FILE__, __func__, __LINE__); return "unknown"; } auto Ptr = unwrap(MRef); @@ -207,11 +210,11 @@ DPCTLUSM_GetPointerDevice(__dpctl_keep const DPCTLSyclUSMRef MRef, __dpctl_keep const DPCTLSyclContextRef CRef) { if (!CRef) { - std::cerr << "Input CRef is nullptr\n"; + error_handler("Input CRef is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } if (!MRef) { - std::cerr << "Input MRef is nullptr\n"; + error_handler("Input MRef is nullptr.", __FILE__, __func__, __LINE__); return nullptr; } diff --git a/libsyclinterface/source/dpctl_vector_templ.cpp b/libsyclinterface/source/dpctl_vector_templ.cpp index 36e1bd4e65..a2f9e8def9 100644 --- a/libsyclinterface/source/dpctl_vector_templ.cpp +++ b/libsyclinterface/source/dpctl_vector_templ.cpp @@ -23,6 +23,7 @@ /// the wrapper functions for vector operations. /// //===----------------------------------------------------------------------===// +#include "../helper/include/dpctl_error_handlers.h" #include "../helper/include/dpctl_vector_macros.h" #include "Support/MemOwnershipAttrs.h" #include @@ -44,8 +45,9 @@ __dpctl_give VECTOR(EL) FN(EL, Create)() try { Vec = new std::vector(); return wrap(Vec); - } catch (std::bad_alloc const &ba) { + } catch (std::exception const &e) { delete Vec; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -68,8 +70,9 @@ __dpctl_give VECTOR(EL) wrap(new std::remove_pointer::type(*Ref))); } return wrap(Vec); - } catch (std::bad_alloc const &ba) { + } catch (std::exception const &e) { delete Vec; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } @@ -133,8 +136,8 @@ SYCLREF(EL) FN(EL, GetAt)(__dpctl_keep VECTOR(EL) VRef, size_t index) SYCLREF(EL) ret; try { ret = Vec->at(index); - } catch (std::out_of_range const &oor) { - std::cerr << oor.what() << '\n'; + } catch (std::exception const &e) { + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } auto Ref = unwrap(ret); @@ -142,10 +145,9 @@ SYCLREF(EL) FN(EL, GetAt)(__dpctl_keep VECTOR(EL) VRef, size_t index) try { elPtr = new std::remove_pointer::type(*Ref); copy = wrap(elPtr); - } catch (std::bad_alloc const &ba) { + } catch (std::exception const &e) { delete elPtr; - // \todo log error - std::cerr << ba.what() << '\n'; + error_handler(e, __FILE__, __func__, __LINE__); return nullptr; } } diff --git a/libsyclinterface/tests/CMakeLists.txt b/libsyclinterface/tests/CMakeLists.txt index 1000634794..1ecb4e847d 100644 --- a/libsyclinterface/tests/CMakeLists.txt +++ b/libsyclinterface/tests/CMakeLists.txt @@ -59,7 +59,7 @@ if(DPCTL_GENERATE_COVERAGE) ) add_custom_target(llvm-cov COMMAND ${CMAKE_MAKE_PROGRAM} dpctl_c_api_tests - COMMAND ${CMAKE_CURRENT_BINARY_DIR}/dpctl_c_api_tests + COMMAND ${CMAKE_COMMAND} -E env DPCTL_VERBOSITY=error ${CMAKE_CURRENT_BINARY_DIR}/dpctl_c_api_tests COMMAND ${LLVMProfdata_EXE} merge -sparse default.profraw diff --git a/libsyclinterface/tests/test_sycl_device_subdevices.cpp b/libsyclinterface/tests/test_sycl_device_subdevices.cpp index 1979826bfd..14252bdc88 100644 --- a/libsyclinterface/tests/test_sycl_device_subdevices.cpp +++ b/libsyclinterface/tests/test_sycl_device_subdevices.cpp @@ -174,13 +174,8 @@ TEST_P(TestDPCTLSyclDeviceInterface, ChkCreateSubDevicesByAffinityNuma) auto subDevices = D->create_sub_devices< info::partition_property::partition_by_affinity_domain>(domain); expected_size = subDevices.size(); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (feature_not_supported const &fnse) { - std::cerr << fnse.what() << '\n'; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + std::cerr << e.what() << std::endl; } if (DVRef && expected_size) { @@ -210,13 +205,8 @@ TEST_P(TestDPCTLSyclDeviceInterface, ChkCreateSubDevicesByAffinityL4Cache) auto subDevices = D->create_sub_devices< info::partition_property::partition_by_affinity_domain>(domain); expected_size = subDevices.size(); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (feature_not_supported const &fnse) { - std::cerr << fnse.what() << '\n'; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + std::cerr << e.what() << std::endl; } if (DVRef && expected_size) { @@ -246,13 +236,8 @@ TEST_P(TestDPCTLSyclDeviceInterface, ChkCreateSubDevicesByAffinityL3Cache) auto subDevices = D->create_sub_devices< info::partition_property::partition_by_affinity_domain>(domain); expected_size = subDevices.size(); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (feature_not_supported const &fnse) { - std::cerr << fnse.what() << '\n'; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + std::cerr << e.what() << std::endl; } if (DVRef && expected_size) { @@ -282,13 +267,8 @@ TEST_P(TestDPCTLSyclDeviceInterface, ChkCreateSubDevicesByAffinityL2Cache) auto subDevices = D->create_sub_devices< info::partition_property::partition_by_affinity_domain>(domain); expected_size = subDevices.size(); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (feature_not_supported const &fnse) { - std::cerr << fnse.what() << '\n'; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + std::cerr << e.what() << std::endl; } if (DVRef && expected_size) { @@ -318,13 +298,8 @@ TEST_P(TestDPCTLSyclDeviceInterface, ChkCreateSubDevicesByAffinityL1Cache) auto subDevices = D->create_sub_devices< info::partition_property::partition_by_affinity_domain>(domain); expected_size = subDevices.size(); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (feature_not_supported const &fnse) { - std::cerr << fnse.what() << '\n'; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + std::cerr << e.what() << std::endl; } if (DVRef && expected_size) { @@ -355,13 +330,8 @@ TEST_P(TestDPCTLSyclDeviceInterface, auto subDevices = D->create_sub_devices< info::partition_property::partition_by_affinity_domain>(domain); expected_size = subDevices.size(); - } catch (std::bad_alloc const &ba) { - std::cerr << ba.what() << '\n'; - } catch (feature_not_supported const &fnse) { - std::cerr << fnse.what() << '\n'; - } catch (runtime_error const &re) { - // \todo log error - std::cerr << re.what() << '\n'; + } catch (std::exception const &e) { + std::cerr << e.what() << std::endl; } if (DVRef && expected_size) {