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))
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) {
172 if (!handle)
throw std::runtime_error(
"handle is nullptr.");
174 #ifdef DNNL_WITH_SYCL
179 if (is_cpu_sycl || is_gpu_sycl) {
182 auto buffer = dnnl::sycl_interop::get_buffer<uint8_t>(mem);
183 auto src =
buffer.get_access<cl::sycl::access::mode::read>();
184 uint8_t *src_ptr = src.get_pointer();
186 throw std::runtime_error(
"get_pointer returned nullptr.");
187 for (
size_t i = 0; i < size; ++i)
188 ((uint8_t *)handle)[i] = src_ptr[i];
193 throw std::runtime_error(
"get_data_handle returned nullptr.");
195 for (
size_t i = 0; i < size; ++i)
196 ((uint8_t *)handle)[i] = src_ptr[i];
200 sycl_queue.memcpy(src_ptr, handle, size).wait();
206 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
212 cl_int ret = clEnqueueReadBuffer(
213 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
214 if (ret != CL_SUCCESS)
215 throw std::runtime_error(
"clEnqueueReadBuffer failed.");
222 if (!src)
throw std::runtime_error(
"get_data_handle returned nullptr.");
223 for (
size_t i = 0; i < size; ++i)
224 ((uint8_t *)handle)[i] = src[i];
228 assert(!
"not expected");
232 inline void write_to_dnnl_memory(
void *handle,
dnnl::memory &mem) {
236 if (!handle)
throw std::runtime_error(
"handle is nullptr.");
238 #ifdef DNNL_WITH_SYCL
243 if (is_cpu_sycl || is_gpu_sycl) {
246 auto buffer = dnnl::sycl_interop::get_buffer<uint8_t>(mem);
247 auto dst =
buffer.get_access<cl::sycl::access::mode::write>();
248 uint8_t *dst_ptr = dst.get_pointer();
250 throw std::runtime_error(
"get_pointer returned nullptr.");
251 for (
size_t i = 0; i < size; ++i)
252 dst_ptr[i] = ((uint8_t *)handle)[i];
257 throw std::runtime_error(
"get_data_handle returned nullptr.");
259 for (
size_t i = 0; i < size; ++i)
260 dst_ptr[i] = ((uint8_t *)handle)[i];
264 sycl_queue.memcpy(dst_ptr, handle, size).wait();
270 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
276 cl_int ret = clEnqueueWriteBuffer(
277 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
278 if (ret != CL_SUCCESS)
279 throw std::runtime_error(
"clEnqueueWriteBuffer failed.");
286 if (!dst)
throw std::runtime_error(
"get_data_handle returned nullptr.");
287 for (
size_t i = 0; i < size; ++i)
288 dst[i] = ((uint8_t *)handle)[i];
292 assert(!
"not expected");
#define DNNL_RUNTIME_SYCL
SYCL runtime.
Definition: dnnl_types.h:2627
cl_command_queue get_command_queue(const stream &astream)
Returns OpenCL queue object associated with the execution stream.
Definition: dnnl_ocl.hpp:108
cl_mem get_mem_object(const memory &amemory)
Returns the OpenCL memory object associated with the memory object.
Definition: dnnl_ocl.hpp:120
cl::sycl::queue get_queue(const stream &astream)
Returns the SYCL queue associated with an execution stream.
Definition: dnnl_sycl.hpp:132
memory_kind get_memory_kind(const memory &amemory)
Returns the memory allocation kind associated with a memory object.
Definition: dnnl_sycl.hpp:204
@ buffer
Buffer memory allocation kind.
@ usm
USM (device, shared, host, or unknown) memory allocation kind.
An execution engine.
Definition: dnnl.hpp:869
kind
Kinds of engines.
Definition: dnnl.hpp:874
static size_t get_count(kind akind)
Returns the number of engines of a certain kind.
Definition: dnnl.hpp:893
oneDNN exception class.
Definition: dnnl.hpp:84
const char * what() const noexcept override
Returns the explanatory string.
Definition: dnnl.hpp:96
size_t get_size() const
Returns size of the memory descriptor in bytes.
Definition: dnnl.hpp:2200
Memory object.
Definition: dnnl.hpp:1108
dnnl_dim_t dim
Integer type for representing dimension sizes and indices.
Definition: dnnl.hpp:1112
void * get_data_handle() const
Returns the underlying memory buffer.
Definition: dnnl.hpp:2289
engine get_engine() const
Returns the associated engine.
Definition: dnnl.hpp:2278
desc get_desc() const
Returns the associated memory descriptor.
Definition: dnnl.hpp:2270
std::vector< dim > dims
Vector of dimensions.
Definition: dnnl.hpp:1115
An execution stream.
Definition: dnnl.hpp:985