17 #ifndef EXAMPLE_UTILS_H
18 #define EXAMPLE_UTILS_H
29 #define COMPLAIN_DNNL_ERROR_AND_EXIT(what, status) \
31 printf("[%s:%d] `%s` returns oneDNN error: %s.\n", __FILE__, __LINE__, \
32 what, dnnl_status2str(status)); \
33 printf("Example failed.\n"); \
37 #define COMPLAIN_EXAMPLE_ERROR_AND_EXIT(complain_fmt, ...) \
39 printf("[%s:%d] Error in the example: " complain_fmt ".\n", __FILE__, \
40 __LINE__, __VA_ARGS__); \
41 printf("Example failed.\n"); \
47 dnnl_status_t s_ = f; \
48 if (s_ != dnnl_success) COMPLAIN_DNNL_ERROR_AND_EXIT(#f, s_); \
55 }
else if (argc == 2) {
57 char *engine_kind_str = argv[1];
58 if (!strcmp(engine_kind_str,
"cpu")) {
60 }
else if (!strcmp(engine_kind_str,
"gpu")) {
63 COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
"%s",
64 "could not find compatible GPU\nPlease run the example "
71 COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
72 "inappropriate engine kind.\n"
73 "Please run the example like this: %s [cpu|gpu].",
80 return "<Unknown engine>";
84 static inline void read_from_dnnl_memory(
void *handle,
dnnl_memory_t mem) {
98 for (
size_t i = 0; i < bytes; ++i) {
99 ((
char *)handle)[i] = ((
char *)ptr)[i];
105 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
111 CHECK(dnnl_memory_get_ocl_mem_object(mem, &m));
113 CHECK(dnnl_stream_get_ocl_command_queue(s, &q));
115 cl_int ret = clEnqueueReadBuffer(
116 q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
117 if (ret != CL_SUCCESS)
118 COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
119 "clEnqueueReadBuffer failed (status code: %d)", ret);
127 static inline void write_to_dnnl_memory(
void *handle,
dnnl_memory_t mem) {
141 for (
size_t i = 0; i < bytes; ++i) {
142 ((
char *)ptr)[i] = ((
char *)handle)[i];
148 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
154 CHECK(dnnl_memory_get_ocl_mem_object(mem, &m));
156 CHECK(dnnl_stream_get_ocl_command_queue(s, &q));
158 cl_int ret = clEnqueueWriteBuffer(
159 q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
160 if (ret != CL_SUCCESS)
161 COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
162 "clEnqueueWriteBuffer failed (status code: %d)", ret);