Skip to content

Commit d22acb4

Browse files
Merge pull request #683 from vlad-perevezentsev/error_handler
Implement error_handler function in libsyclinterface/helper
2 parents 476c804 + 3e0c061 commit d22acb4

22 files changed

+515
-577
lines changed

dpctl/_backend.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ cdef extern from "dpctl_sycl_platform_interface.h":
281281
cdef extern from "dpctl_sycl_context_interface.h":
282282
cdef DPCTLSyclContextRef DPCTLContext_Create(
283283
const DPCTLSyclDeviceRef DRef,
284-
error_handler_callback *error_handler,
284+
error_handler_callback *handler,
285285
int properties)
286286
cdef DPCTLSyclContextRef DPCTLContext_CreateFromDevices(
287287
const DPCTLDeviceVectorRef DVRef,
288-
error_handler_callback *error_handler,
288+
error_handler_callback *handler,
289289
int properties)
290290
cdef DPCTLSyclContextRef DPCTLContext_Copy(
291291
const DPCTLSyclContextRef CRef)
@@ -323,7 +323,7 @@ cdef extern from "dpctl_sycl_queue_interface.h":
323323
cdef DPCTLSyclQueueRef DPCTLQueue_Create(
324324
const DPCTLSyclContextRef CRef,
325325
const DPCTLSyclDeviceRef DRef,
326-
error_handler_callback *error_handler,
326+
error_handler_callback *handler,
327327
int properties)
328328
cdef DPCTLSyclQueueRef DPCTLQueue_CreateForDevice(
329329
const DPCTLSyclDeviceRef dRef,

libsyclinterface/helper/include/dpctl_async_error_handler.h renamed to libsyclinterface/helper/include/dpctl_error_handlers.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,22 @@ class DPCTL_API DPCTL_AsyncErrorHandler
4444

4545
void operator()(const cl::sycl::exception_list &exceptions);
4646
};
47+
48+
enum error_level : int
49+
{
50+
none = 0,
51+
error = 1,
52+
warning = 2
53+
};
54+
55+
void error_handler(const std::exception &e,
56+
const char *file_name,
57+
const char *func_name,
58+
int line_num,
59+
error_level error_type = error_level::error);
60+
61+
void error_handler(const std::string &what,
62+
const char *file_name,
63+
const char *func_name,
64+
int line_num,
65+
error_level error_type = error_level::warning);

libsyclinterface/helper/include/dpctl_string_utils.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/// \file
2222
/// Helper function to convert a C++ string to a C string.
2323
//===----------------------------------------------------------------------===//
24+
#include "../helper/include/dpctl_error_handlers.h"
2425
#include <cstring>
2526
#include <iostream>
2627
#include <string>
@@ -55,9 +56,8 @@ cstring_from_string(const std::string &str)
5556
// to be null-terminated and the copy function is asked to
5657
// copy enough characters to include that null-character.
5758
cstr[cstr_len - 1] = '\0';
58-
} catch (std::bad_alloc const &ba) {
59-
// \todo log error
60-
std::cerr << ba.what() << '\n';
59+
} catch (std::exception const &e) {
60+
error_handler(e, __FILE__, __func__, __LINE__);
6161
}
6262

6363
return cstr;

libsyclinterface/helper/source/dpctl_async_error_handler.cpp

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===-- dpctl_async_error_handler.h - An async error handler -*-C++-*- ===//
2+
//
3+
// Data Parallel Control (dpctl)
4+
//
5+
// Copyright 2020-2021 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// A functor to use for passing an error handler callback function to sycl
23+
/// context and queue contructors.
24+
//===----------------------------------------------------------------------===//
25+
26+
#include "dpctl_error_handlers.h"
27+
#include <cstring>
28+
29+
void DPCTL_AsyncErrorHandler::operator()(
30+
const cl::sycl::exception_list &exceptions)
31+
{
32+
for (std::exception_ptr const &e : exceptions) {
33+
try {
34+
std::rethrow_exception(e);
35+
} catch (sycl::exception const &e) {
36+
error_handler(e, __FILE__, __func__, __LINE__);
37+
auto err_code = e.get_cl_code();
38+
handler_(static_cast<int>(err_code));
39+
}
40+
}
41+
}
42+
43+
inline int get_requested_level(void)
44+
{
45+
int requested_level = 0;
46+
47+
const char *verbose = std::getenv("DPCTL_VERBOSITY");
48+
49+
if (verbose) {
50+
if (!strncmp(verbose, "none", 4))
51+
requested_level = error_level::none;
52+
else if (!strncmp(verbose, "error", 5))
53+
requested_level = error_level::error;
54+
else if (!strncmp(verbose, "warning", 7))
55+
requested_level = error_level::warning;
56+
}
57+
58+
return requested_level;
59+
}
60+
61+
void error_handler(const std::exception &e,
62+
const char *file_name,
63+
const char *func_name,
64+
int line_num,
65+
error_level error_type)
66+
{
67+
int requested_level = get_requested_level();
68+
int error_level = static_cast<int>(error_type);
69+
70+
if (requested_level <= error_level) {
71+
std::cerr << e.what() << " in " << func_name << " at " << file_name
72+
<< ":" << line_num << std::endl;
73+
}
74+
}
75+
76+
void error_handler(const std::string &what,
77+
const char *file_name,
78+
const char *func_name,
79+
int line_num,
80+
error_level error_type)
81+
{
82+
int error_level = static_cast<int>(error_type);
83+
int requested_level = get_requested_level();
84+
85+
if (requested_level <= error_level) {
86+
std::cerr << what << " In " << func_name << " at " << file_name << ":"
87+
<< line_num << std::endl;
88+
}
89+
}

libsyclinterface/include/dpctl_sycl_context_interface.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ DPCTL_C_EXTERN_C_BEGIN
4646
* optional async error handler and properties bit flags.
4747
*
4848
* @param DRef Opaque pointer to a SYCL device.
49-
* @param error_handler A callback function that will be invoked by the
49+
* @param handler A callback function that will be invoked by the
5050
* async_handler used during context creation. Can be
5151
* NULL if no async_handler is needed.
5252
* @param properties An optional combination of bit flags to define
@@ -58,7 +58,7 @@ DPCTL_C_EXTERN_C_BEGIN
5858
DPCTL_API
5959
__dpctl_give DPCTLSyclContextRef
6060
DPCTLContext_Create(__dpctl_keep const DPCTLSyclDeviceRef DRef,
61-
error_handler_callback *error_handler,
61+
error_handler_callback *handler,
6262
int properties);
6363

6464
/*!
@@ -67,7 +67,7 @@ DPCTLContext_Create(__dpctl_keep const DPCTLSyclDeviceRef DRef,
6767
*
6868
* @param DVRef An opaque pointer to a std::vector of
6969
* DPCTLSyclDeviceRef opaque pointers.
70-
* @param error_handler A callback function that will be invoked by the
70+
* @param handler A callback function that will be invoked by the
7171
* async_handler used during context creation. Can be
7272
* NULL if no async_handler is needed.
7373
* @param properties An optional combination of bit flags to define
@@ -79,7 +79,7 @@ DPCTLContext_Create(__dpctl_keep const DPCTLSyclDeviceRef DRef,
7979
DPCTL_API
8080
__dpctl_give DPCTLSyclContextRef
8181
DPCTLContext_CreateFromDevices(__dpctl_keep const DPCTLDeviceVectorRef DVRef,
82-
error_handler_callback *error_handler,
82+
error_handler_callback *handler,
8383
int properties);
8484

8585
/*!

libsyclinterface/include/dpctl_sycl_queue_interface.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ DPCTL_C_EXTERN_C_BEGIN
4747
*
4848
* @param CRef An opaque pointer to a sycl::context.
4949
* @param DRef An opaque pointer to a sycl::device
50-
* @param error_handler A callback function that will be invoked by the
50+
* @param handler A callback function that will be invoked by the
5151
* async_handler used during queue creation. Can be
5252
* NULL if no async_handler is needed.
5353
* @param properties A combination of bit flags using the values defined
@@ -62,7 +62,7 @@ DPCTL_API
6262
__dpctl_give DPCTLSyclQueueRef
6363
DPCTLQueue_Create(__dpctl_keep const DPCTLSyclContextRef CRef,
6464
__dpctl_keep const DPCTLSyclDeviceRef DRef,
65-
error_handler_callback *error_handler,
65+
error_handler_callback *handler,
6666
int properties);
6767

6868
/*!
@@ -86,7 +86,7 @@ DPCTLQueue_Create(__dpctl_keep const DPCTLSyclContextRef CRef,
8686
* function begaves the same way as the SYCL constructor.
8787
*
8888
* @param dRef An opaque pointer to a ``sycl::device``.
89-
* @param error_handler A callback function that will be invoked by the
89+
* @param handler A callback function that will be invoked by the
9090
* async_handler used during queue creation. Can be
9191
* NULL if no async_handler is needed.
9292
* @param properties A combination of bit flags using the values defined
@@ -101,7 +101,7 @@ DPCTLQueue_Create(__dpctl_keep const DPCTLSyclContextRef CRef,
101101
DPCTL_API
102102
__dpctl_give DPCTLSyclQueueRef
103103
DPCTLQueue_CreateForDevice(__dpctl_keep const DPCTLSyclDeviceRef dRef,
104-
error_handler_callback *error_handler,
104+
error_handler_callback *handler,
105105
int properties);
106106

107107
/*!

0 commit comments

Comments
 (0)