17 #ifndef EXAMPLE_UTILS_HPP
18 #define EXAMPLE_UTILS_HPP
28 #include <initializer_list>
31 #include "dnnl_debug.h"
33 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
34 #include "dnnl_ocl.hpp"
35 #elif DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL
36 #include "dnnl_sycl.hpp"
39 #if DNNL_CPU_THREADING_RUNTIME == DNNL_RUNTIME_OMP
42 #define PRAGMA_MACRo(x) __pragma(x)
43 #define PRAGMA_MACRO(x) PRAGMA_MACRo(x)
45 #define PRAGMA_MACRo(x) _Pragma(#x)
46 #define PRAGMA_MACRO(x) PRAGMA_MACRo(x)
50 #if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
54 #define PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(n) PRAGMA_MACRO(omp parallel for collapse(n))
55 #else // DNNL_CPU_THREADING_RUNTIME == DNNL_RUNTIME_OMP
56 #define PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(n)
63 std::cout <<
"Application couldn't find GPU, please run with CPU "
74 struct example_allows_unimplemented :
public std::exception {
75 example_allows_unimplemented(
const char *message) noexcept
77 const char *what() const noexcept
override {
return message; }
85 inline int handle_example_errors(
86 std::initializer_list<dnnl::engine::kind> engine_kinds,
87 std::function<
void()> example) {
92 }
catch (example_allows_unimplemented &e) {
93 std::cout << e.message << std::endl;
96 std::cout <<
"oneDNN error caught: " << std::endl
97 <<
"\tStatus: " << dnnl_status2str(e.status) << std::endl
98 <<
"\tMessage: " << e.
what() << std::endl;
100 }
catch (std::exception &e) {
101 std::cout <<
"Error in the example: " << e.what() <<
"." << std::endl;
105 std::string engine_kind_str;
106 for (
auto it = engine_kinds.begin(); it != engine_kinds.end(); ++it) {
107 if (it != engine_kinds.begin()) engine_kind_str +=
"/";
108 engine_kind_str += engine_kind2str_upper(*it);
111 std::cout <<
"Example " << (exit_code ?
"failed" :
"passed") <<
" on "
112 << engine_kind_str <<
"." << std::endl;
118 inline int handle_example_errors(
121 return handle_example_errors(
122 {engine_kind}, [&]() { example(engine_kind, argc, argv); });
126 inline int handle_example_errors(
129 return handle_example_errors(
130 {engine_kind}, [&]() { example(engine_kind); });
134 int argc,
char **argv,
int extra_args = 0) {
138 }
else if (argc <= extra_args + 2) {
139 std::string engine_kind_str = argv[1];
141 if (engine_kind_str ==
"cpu") {
143 }
else if (engine_kind_str ==
"gpu") {
149 std::cout <<
"Inappropriate engine kind." << std::endl
150 <<
"Please run the example like this: " << argv[0] <<
" [cpu|gpu]"
151 << (extra_args ?
" [extra arguments]" :
"") <<
"." << std::endl;
158 assert(!
"not expected");
159 return "<Unknown engine>";
164 std::multiplies<dnnl::memory::dim>());
168 inline void read_from_dnnl_memory(
void *handle,
dnnl::memory &mem) {
177 if (is_cpu_sycl || is_gpu_sycl) {
180 auto buffer = dnnl::sycl_interop::get_buffer<uint8_t>(mem);
181 auto src =
buffer.get_access<cl::sycl::access::mode::read>();
182 uint8_t *src_ptr = src.get_pointer();
183 for (
size_t i = 0; i < size; ++i)
184 ((uint8_t *)handle)[i] = src_ptr[i];
186 assert(mkind == dnnl::sycl_interop::memory_kind::usm);
189 for (
size_t i = 0; i < size; ++i)
190 ((uint8_t *)handle)[i] = src_ptr[i];
194 sycl_queue.memcpy(src_ptr, handle, size).wait();
200 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
206 cl_int ret = clEnqueueReadBuffer(
207 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
208 if (ret != CL_SUCCESS)
209 throw std::runtime_error(
"clEnqueueReadBuffer failed.");
216 for (
size_t i = 0; i < size; ++i)
217 ((uint8_t *)handle)[i] = src[i];
221 assert(!
"not expected");
225 inline void write_to_dnnl_memory(
void *handle,
dnnl::memory &mem) {
234 if (is_cpu_sycl || is_gpu_sycl) {
237 auto buffer = dnnl::sycl_interop::get_buffer<uint8_t>(mem);
238 auto dst =
buffer.get_access<cl::sycl::access::mode::write>();
239 uint8_t *dst_ptr = dst.get_pointer();
240 for (
size_t i = 0; i < size; ++i)
241 dst_ptr[i] = ((uint8_t *)handle)[i];
243 assert(mkind == dnnl::sycl_interop::memory_kind::usm);
246 for (
size_t i = 0; i < size; ++i)
247 dst_ptr[i] = ((uint8_t *)handle)[i];
251 sycl_queue.memcpy(dst_ptr, handle, size).wait();
257 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
263 cl_int ret = clEnqueueWriteBuffer(
264 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
265 if (ret != CL_SUCCESS)
266 throw std::runtime_error(
"clEnqueueWriteBuffer failed.");
273 for (
size_t i = 0; i < size; ++i)
274 dst[i] = ((uint8_t *)handle)[i];
278 assert(!
"not expected");