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 DNNL 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"); \
49 printf(
"Application couldn't find GPU, please run with CPU "
59 dnnl_status_t s_ = f; \
60 if (s_ != dnnl_success) COMPLAIN_DNNL_ERROR_AND_EXIT(#f, s_); \
66 return validate_engine_kind(
dnnl_cpu);
67 }
else if (argc == 2) {
69 char *engine_kind_str = argv[1];
70 if (!strcmp(engine_kind_str,
"cpu")) {
71 return validate_engine_kind(
dnnl_cpu);
72 }
else if (!strcmp(engine_kind_str,
"gpu")) {
73 return validate_engine_kind(
dnnl_gpu);
78 COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
79 "inappropriate engine kind.\n"
80 "Please run the example like this: %s [cpu|gpu].",
87 return "<Unknown engine>";
91 static inline void read_from_dnnl_memory(
void *handle,
dnnl_memory_t mem) {
103 = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL && eng_kind ==
dnnl_cpu);
105 = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL && eng_kind ==
dnnl_gpu);
106 if (is_cpu_sycl || is_gpu_sycl) {
107 void *mapped_ptr = NULL;
110 for (
size_t i = 0; i < bytes; ++i) {
111 ((
char *)handle)[i] = ((
char *)mapped_ptr)[i];
119 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
125 CHECK(dnnl_memory_get_ocl_mem_object(mem, &m));
127 CHECK(dnnl_stream_get_ocl_command_queue(s, &q));
129 cl_int ret = clEnqueueReadBuffer(
130 q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
131 if (ret != CL_SUCCESS)
132 COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
133 "clEnqueueReadBuffer failed (status code: %d)", ret);
143 for (
size_t i = 0; i < bytes; ++i) {
144 ((
char *)handle)[i] = ((
char *)ptr)[i];
150 assert(!
"not expected");
154 static inline void write_to_dnnl_memory(
void *handle,
dnnl_memory_t mem) {
166 = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL && eng_kind ==
dnnl_cpu);
168 = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL && eng_kind ==
dnnl_gpu);
169 if (is_cpu_sycl || is_gpu_sycl) {
170 void *mapped_ptr = NULL;
173 for (
size_t i = 0; i < bytes; ++i) {
174 ((
char *)mapped_ptr)[i] = ((
char *)handle)[i];
182 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
188 CHECK(dnnl_memory_get_ocl_mem_object(mem, &m));
190 CHECK(dnnl_stream_get_ocl_command_queue(s, &q));
192 cl_int ret = clEnqueueWriteBuffer(
193 q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
194 if (ret != CL_SUCCESS)
195 COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
196 "clEnqueueWriteBuffer failed (status code: %d)", ret);
207 for (
size_t i = 0; i < bytes; ++i) {
208 ((
char *)ptr)[i] = ((
char *)handle)[i];
214 assert(!
"not expected");