Skip to content

Commit c384bdb

Browse files
author
Diptorup Deb
committed
Add a new DPCTLExecState type to improve error handling.
- A new DPCTLExecState type and associated constructors and destructors are added. - The error_handler_callback is depracated and is superseded by the new error_handler_callback_fn type. The new function type allows passing in both an error code and an error message. - A default error handler function is defined that will print out an error code and associated error message to std::cerr.
1 parent 49ded41 commit c384bdb

File tree

4 files changed

+199
-2
lines changed

4 files changed

+199
-2
lines changed

dpctl-capi/include/dpctl_error_handler_type.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@
3232
* @param err_code Error code extracted from an SYCL asynchronous
3333
* error.
3434
*/
35-
typedef void error_handler_callback(int err_code);
35+
typedef void error_handler_callback(int err_code) __attribute__((
36+
deprecated("the function does not allow passing in an error string, use "
37+
"'error_handler_callback_fn' instead!!!")));
38+
39+
/*!
40+
* @brief Type signature required for an error handler callback function.
41+
*
42+
* @param err_code My Param doc
43+
* @param err_msg My Param doc
44+
*/
45+
typedef void (*error_handler_callback_fn)(int err_code, const char *err_msg);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===- dpctl_exec_state.h - C API for service functions -*-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+
/// The file declares a struct to store dpctl's error handler and other
23+
/// execution state configurations.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
#pragma once
28+
29+
#include "Support/DllExport.h"
30+
#include "Support/ExternC.h"
31+
#include "Support/MemOwnershipAttrs.h"
32+
#include "dpctl_error_handler_type.h"
33+
34+
DPCTL_C_EXTERN_C_BEGIN
35+
36+
/*!
37+
* @brief An opaque type to represent an "execution state" for the dpctl
38+
* sycl interface library.
39+
*
40+
* The execution state controls the behavior of functions exposed by
41+
* libDPCTLSyclInterface. For now, only error handling is controlled by an
42+
* execution state. Using a custom execution state, users can define how
43+
* exceptions in the C++ code are handled and propagated to callers. A default
44+
* execution state where all exceptions are caught and the error messages
45+
* printed to ``std::cerr`` is included for convenience.
46+
*
47+
*/
48+
typedef struct DpctlExecutionState *DPCTLExecState;
49+
50+
/*!
51+
* @brief Create a new ``DPCTLExecState`` object.
52+
*
53+
* @param handler An error handler function.
54+
* @return A ``DPCTLExecState`` opaque pointer.
55+
*/
56+
__dpctl_give DPCTLExecState
57+
dpctl_exec_state_create(const error_handler_callback_fn handler);
58+
59+
/*!
60+
* @brief Create a default execution state that prints the error message to
61+
* ``std::cerr``.
62+
*
63+
* @return A ``DPCTLExecState`` opaque pointer.
64+
*/
65+
__dpctl_give DPCTLExecState dpctl_exec_state_create_default();
66+
67+
/*!
68+
* @brief Delete an ``DPCTLExecState`` opaque pointer.
69+
*
70+
* @param DPCTLExecState A ``DPCTLExecState`` opaque pointer to be freed.
71+
*/
72+
void dpctl_exec_state_delete(__dpctl_take DPCTLExecState state);
73+
74+
DPCTL_C_EXTERN_C_END

dpctl-capi/include/dpctl_service.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//===----------------------------------------------------------------------===//
2020
///
2121
/// \file
22-
/// This header defines dpctl service functions.
22+
/// This header declares dpctl service functions.
2323
///
2424
//===----------------------------------------------------------------------===//
2525

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//===- dpctl_exec_state.cpp - Implements C API for sycl::context ---==//
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+
/// This file implements the data types and functions declared in
23+
/// dpctl_exec_state.h.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
#include "dpctl_exec_state.h"
28+
#include "Support/CBindingWrapping.h"
29+
#include <iostream>
30+
31+
namespace
32+
{
33+
34+
/*!
35+
* @brief A default error handler function that prints out the error code and
36+
* the error message to ``std::cerr``.
37+
*
38+
* @param err_code My Param doc
39+
* @param err_msg My Param doc
40+
*/
41+
void default_handler(int err_code, const char *err_msg)
42+
{
43+
std::cerr << "DPCTL-ERROR (" << err_code << ") " << err_msg;
44+
}
45+
46+
/*!
47+
* @brief The execution state that is passed to every libDPCTLSyclInterface
48+
* function.
49+
*
50+
* The ``ExecutionState`` class is a concrete implementation of the
51+
* `DPCTLExecState`` opaque type.
52+
*
53+
*/
54+
class ExecutionState
55+
{
56+
const error_handler_callback_fn handler_;
57+
58+
public:
59+
/*!
60+
* @brief Construct a new ``ExecutionState`` object using the default error
61+
* handler object.
62+
*
63+
*/
64+
ExecutionState() : handler_(default_handler){};
65+
/*!
66+
* @brief Construct a new ``ExecutionState`` object with the provided error
67+
* handler function.
68+
*
69+
* @param handler Error handler function to be used by the
70+
* instance of ``ExecutionState``.
71+
*/
72+
explicit ExecutionState(const error_handler_callback_fn handler)
73+
: handler_(handler)
74+
{
75+
}
76+
77+
void operator()(int err_code, const char *err_msg) const
78+
{
79+
handler_(err_code, err_msg);
80+
}
81+
};
82+
83+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionState, DPCTLExecState);
84+
85+
} // namespace
86+
87+
__dpctl_give DPCTLExecState
88+
dpctl_exec_state_create(const error_handler_callback_fn handler)
89+
{
90+
try {
91+
ExecutionState *state = new ExecutionState(handler);
92+
return wrap(state);
93+
} catch (std::bad_alloc const &ba) {
94+
std::cerr << ba.what() << '\n';
95+
std::terminate();
96+
}
97+
}
98+
99+
__dpctl_give DPCTLExecState dpctl_exec_state_create_default()
100+
{
101+
try {
102+
ExecutionState *state = new ExecutionState();
103+
return wrap(state);
104+
} catch (std::bad_alloc const &ba) {
105+
std::cerr << ba.what() << '\n';
106+
std::terminate();
107+
}
108+
}
109+
110+
void dpctl_exec_state_delete(__dpctl_take DPCTLExecState state)
111+
{
112+
delete unwrap(state);
113+
}

0 commit comments

Comments
 (0)