17 #ifndef EXAMPLE_UTILS_HPP
18 #define EXAMPLE_UTILS_HPP
28 #include <initializer_list>
33 #if DNNL_CPU_THREADING_RUNTIME == DNNL_RUNTIME_OMP
36 #define PRAGMA_MACRo(x) __pragma(x)
37 #define PRAGMA_MACRO(x) PRAGMA_MACRo(x)
39 #define PRAGMA_MACRo(x) _Pragma(#x)
40 #define PRAGMA_MACRO(x) PRAGMA_MACRo(x)
44 #if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
48 #define PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(n) PRAGMA_MACRO(omp parallel for collapse(n))
49 #else // DNNL_CPU_THREADING_RUNTIME == DNNL_RUNTIME_OMP
50 #define PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(n)
57 std::cout <<
"Application couldn't find GPU, please run with CPU "
68 struct example_allows_unimplemented :
public std::exception {
69 example_allows_unimplemented(
const char *message) noexcept
71 const char *what() const noexcept
override {
return message; }
79 inline int handle_example_errors(
80 std::initializer_list<dnnl::engine::kind> engine_kinds,
81 std::function<
void()> example) {
86 }
catch (example_allows_unimplemented &e) {
87 std::cout << e.message << std::endl;
90 std::cout <<
"oneDNN error caught: " << std::endl
91 <<
"\tStatus: " << dnnl_status2str(e.status) << std::endl
92 <<
"\tMessage: " << e.
what() << std::endl;
94 }
catch (std::exception &e) {
95 std::cout <<
"Error in the example: " << e.what() <<
"." << std::endl;
99 std::string engine_kind_str;
100 for (
auto it = engine_kinds.begin(); it != engine_kinds.end(); ++it) {
101 if (it != engine_kinds.begin()) engine_kind_str +=
"/";
102 engine_kind_str += engine_kind2str_upper(*it);
105 std::cout <<
"Example " << (exit_code ?
"failed" :
"passed") <<
" on "
106 << engine_kind_str <<
"." << std::endl;
112 inline int handle_example_errors(
115 return handle_example_errors(
116 {engine_kind}, [&]() { example(engine_kind, argc, argv); });
120 inline int handle_example_errors(
123 return handle_example_errors(
124 {engine_kind}, [&]() { example(engine_kind); });
128 int argc,
char **argv,
int extra_args = 0) {
132 }
else if (argc <= extra_args + 2) {
133 std::string engine_kind_str = argv[1];
135 if (engine_kind_str ==
"cpu") {
137 }
else if (engine_kind_str ==
"gpu") {
143 std::cout <<
"Inappropriate engine kind." << std::endl
144 <<
"Please run the example like this: " << argv[0] <<
" [cpu|gpu]"
145 << (extra_args ?
" [extra arguments]" :
"") <<
"." << std::endl;
152 assert(!
"not expected");
153 return "<Unknown engine>";
158 std::multiplies<dnnl::memory::dim>());
162 inline void read_from_dnnl_memory(
void *handle,
dnnl::memory &mem) {
171 if (is_cpu_sycl || is_gpu_sycl) {
172 #ifdef DNNL_USE_SYCL_BUFFERS
174 auto src = buffer.get_access<cl::sycl::access::mode::read>();
175 uint8_t *src_ptr = src.get_pointer();
176 #elif defined(DNNL_USE_DPCPP_USM)
179 #error "Not expected"
181 for (
size_t i = 0; i < size; ++i)
182 ((uint8_t *)handle)[i] = src_ptr[i];
186 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
189 cl_command_queue q = s.get_ocl_command_queue();
192 cl_int ret = clEnqueueReadBuffer(
193 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
194 if (ret != CL_SUCCESS)
195 throw std::runtime_error(
"clEnqueueReadBuffer failed.");
202 for (
size_t i = 0; i < size; ++i)
203 ((uint8_t *)handle)[i] = src[i];
207 assert(!
"not expected");
211 inline void write_to_dnnl_memory(
void *handle,
dnnl::memory &mem) {
220 if (is_cpu_sycl || is_gpu_sycl) {
221 #ifdef DNNL_USE_SYCL_BUFFERS
223 auto dst = buffer.get_access<cl::sycl::access::mode::write>();
224 uint8_t *dst_ptr = dst.get_pointer();
225 #elif defined(DNNL_USE_DPCPP_USM)
228 #error "Not expected"
230 for (
size_t i = 0; i < size; ++i)
231 dst_ptr[i] = ((uint8_t *)handle)[i];
235 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
238 cl_command_queue q = s.get_ocl_command_queue();
241 cl_int ret = clEnqueueWriteBuffer(
242 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
243 if (ret != CL_SUCCESS)
244 throw std::runtime_error(
"clEnqueueWriteBuffer failed.");
251 for (
size_t i = 0; i < size; ++i)
252 dst[i] = ((uint8_t *)handle)[i];
256 assert(!
"not expected");