|
| 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