17 #ifndef EXAMPLE_UTILS_HPP 18 #define EXAMPLE_UTILS_HPP 27 #include <initializer_list> 35 struct example_allows_unimplemented :
public std::exception {
36 example_allows_unimplemented(
const char *message) noexcept
38 virtual const char *what() const noexcept
override {
return message; }
46 inline int handle_example_errors(
47 std::initializer_list<dnnl::engine::kind> engine_kinds,
48 std::function<
void()> example) {
53 }
catch (example_allows_unimplemented &e) {
54 std::cout << e.message << std::endl;
57 std::cout <<
"DNNL error caught: " << std::endl
58 <<
"\tStatus: " << dnnl_status2str(e.status) << std::endl
59 <<
"\tMessage: " << e.
what() << std::endl;
61 }
catch (std::exception &e) {
62 std::cout <<
"Error in the example: " << e.what() <<
"." << std::endl;
66 std::string engine_kind_str;
67 for (
auto it = engine_kinds.begin(); it != engine_kinds.end(); ++it) {
68 if (it != engine_kinds.begin()) engine_kind_str +=
"/";
69 engine_kind_str += engine_kind2str_upper(*it);
72 std::cout <<
"Example " << (exit_code ?
"failed" :
"passed") <<
" on " 73 << engine_kind_str <<
"." << std::endl;
79 inline int handle_example_errors(
82 return handle_example_errors(
83 {engine_kind}, [&]() { example(engine_kind, argc, argv); });
87 inline int handle_example_errors(
90 return handle_example_errors(
91 {engine_kind}, [&]() { example(engine_kind); });
95 int argc,
char **argv,
int extra_args = 0) {
99 }
else if (argc <= extra_args + 2) {
100 std::string engine_kind_str = argv[1];
102 if (engine_kind_str ==
"cpu") {
104 }
else if (engine_kind_str ==
"gpu") {
107 std::cout <<
"Could not find compatible GPU" << std::endl
108 <<
"Please run the example with CPU instead" 117 std::cout <<
"Inappropriate engine kind." << std::endl
118 <<
"Please run the example like this: " << argv[0] <<
" [cpu|gpu]" 119 << (extra_args ?
" [extra arguments]" :
"") <<
"." << std::endl;
126 assert(!
"not expected");
127 return "<Unknown engine>";
131 inline void read_from_dnnl_memory(
void *handle,
dnnl::memory &mem) {
137 for (
size_t i = 0; i < bytes; ++i)
138 ((uint8_t *)handle)[i] = src[i];
140 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL 143 cl_command_queue q = s.get_ocl_command_queue();
146 cl_int ret = clEnqueueReadBuffer(
147 q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
148 if (ret != CL_SUCCESS)
149 throw std::runtime_error(
"clEnqueueReadBuffer failed.");
155 inline void write_to_dnnl_memory(
void *handle,
dnnl::memory &mem) {
161 for (
size_t i = 0; i < bytes; ++i)
162 dst[i] = ((uint8_t *)handle)[i];
164 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL 167 cl_command_queue q = s.get_ocl_command_queue();
171 cl_int ret = clEnqueueWriteBuffer(
172 q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
173 if (ret != CL_SUCCESS)
174 throw std::runtime_error(
"clEnqueueWriteBuffer failed.");
void * get_data_handle() const
Returns the underlying memory buffer.
Definition: dnnl.hpp:1919
An execution engine.
Definition: dnnl.hpp:837
static size_t get_count(kind kind)
Returns the number of engines of a certain kind.
Definition: dnnl.hpp:861
DNNL exception class.
Definition: dnnl.hpp:87
const char * what() const noexcept override
Returns the explanatory string.
Definition: dnnl.hpp:99
kind
Kinds of engines.
Definition: dnnl.hpp:842
engine get_engine() const
Returns the associated engine.
Definition: dnnl.hpp:1909
Memory object.
Definition: dnnl.hpp:1119
desc get_desc() const
Returns the associated memory descriptor.
Definition: dnnl.hpp:1901
cl_mem get_ocl_mem_object() const
Returns the OpenCL memory object associated with the memory.
Definition: dnnl.hpp:2002
size_t get_size() const
Returns size of the memory descriptor in bytes.
Definition: dnnl.hpp:1836
An execution stream.
Definition: dnnl.hpp:981