17 #ifndef EXAMPLE_UTILS_HPP
18 #define EXAMPLE_UTILS_HPP
27 #include <initializer_list>
36 std::cout <<
"Application couldn't find GPU, please run with CPU "
47 struct example_allows_unimplemented :
public std::exception {
48 example_allows_unimplemented(
const char *message) noexcept
50 virtual const char *what() const noexcept
override {
return message; }
58 inline int handle_example_errors(
59 std::initializer_list<dnnl::engine::kind> engine_kinds,
60 std::function<
void()> example) {
65 }
catch (example_allows_unimplemented &e) {
66 std::cout << e.message << std::endl;
69 std::cout <<
"DNNL error caught: " << std::endl
70 <<
"\tStatus: " << dnnl_status2str(e.status) << std::endl
71 <<
"\tMessage: " << e.
what() << std::endl;
73 }
catch (std::exception &e) {
74 std::cout <<
"Error in the example: " << e.what() <<
"." << std::endl;
78 std::string engine_kind_str;
79 for (
auto it = engine_kinds.begin(); it != engine_kinds.end(); ++it) {
80 if (it != engine_kinds.begin()) engine_kind_str +=
"/";
81 engine_kind_str += engine_kind2str_upper(*it);
84 std::cout <<
"Example " << (exit_code ?
"failed" :
"passed") <<
" on "
85 << engine_kind_str <<
"." << std::endl;
91 inline int handle_example_errors(
94 return handle_example_errors(
95 {engine_kind}, [&]() { example(engine_kind, argc, argv); });
99 inline int handle_example_errors(
102 return handle_example_errors(
103 {engine_kind}, [&]() { example(engine_kind); });
107 int argc,
char **argv,
int extra_args = 0) {
111 }
else if (argc <= extra_args + 2) {
112 std::string engine_kind_str = argv[1];
114 if (engine_kind_str ==
"cpu") {
116 }
else if (engine_kind_str ==
"gpu") {
122 std::cout <<
"Inappropriate engine kind." << std::endl
123 <<
"Please run the example like this: " << argv[0] <<
" [cpu|gpu]"
124 << (extra_args ?
" [extra arguments]" :
"") <<
"." << std::endl;
131 assert(!
"not expected");
132 return "<Unknown engine>";
136 inline void read_from_dnnl_memory(
void *handle,
dnnl::memory &mem) {
141 bool is_cpu_sycl = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL
143 bool is_gpu_sycl = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL
145 if (is_cpu_sycl || is_gpu_sycl) {
146 #ifdef DNNL_USE_SYCL_BUFFERS
148 auto src = buffer.get_access<cl::sycl::access::mode::read>();
149 uint8_t *src_ptr = src.get_pointer();
150 #elif defined(DNNL_USE_DPCPP_USM)
153 #error "Not expected"
155 for (
size_t i = 0; i < size; ++i)
156 ((uint8_t *)handle)[i] = src_ptr[i];
160 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
163 cl_command_queue q = s.get_ocl_command_queue();
166 cl_int ret = clEnqueueReadBuffer(
167 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
168 if (ret != CL_SUCCESS)
169 throw std::runtime_error(
"clEnqueueReadBuffer failed.");
176 for (
size_t i = 0; i < size; ++i)
177 ((uint8_t *)handle)[i] = src[i];
181 assert(!
"not expected");
185 inline void write_to_dnnl_memory(
void *handle,
dnnl::memory &mem) {
190 bool is_cpu_sycl = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL
192 bool is_gpu_sycl = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL
194 if (is_cpu_sycl || is_gpu_sycl) {
195 #ifdef DNNL_USE_SYCL_BUFFERS
197 auto dst = buffer.get_access<cl::sycl::access::mode::write>();
198 uint8_t *dst_ptr = dst.get_pointer();
199 #elif defined(DNNL_USE_DPCPP_USM)
202 #error "Not expected"
204 for (
size_t i = 0; i < size; ++i)
205 dst_ptr[i] = ((uint8_t *)handle)[i];
209 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
212 cl_command_queue q = s.get_ocl_command_queue();
215 cl_int ret = clEnqueueWriteBuffer(
216 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
217 if (ret != CL_SUCCESS)
218 throw std::runtime_error(
"clEnqueueWriteBuffer failed.");
225 for (
size_t i = 0; i < size; ++i)
226 dst[i] = ((uint8_t *)handle)[i];
230 assert(!
"not expected");