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) 56 struct example_allows_unimplemented :
public std::exception {
57 example_allows_unimplemented(
const char *message) noexcept
59 const char *what() const noexcept
override {
return message; }
67 inline int handle_example_errors(
68 std::initializer_list<dnnl::engine::kind> engine_kinds,
69 std::function<
void()> example) {
74 }
catch (example_allows_unimplemented &e) {
75 std::cout << e.message << std::endl;
78 std::cout <<
"oneDNN error caught: " << std::endl
79 <<
"\tStatus: " << dnnl_status2str(e.status) << std::endl
80 <<
"\tMessage: " << e.
what() << std::endl;
82 }
catch (std::exception &e) {
83 std::cout <<
"Error in the example: " << e.what() <<
"." << std::endl;
87 std::string engine_kind_str;
88 for (
auto it = engine_kinds.begin(); it != engine_kinds.end(); ++it) {
89 if (it != engine_kinds.begin()) engine_kind_str +=
"/";
90 engine_kind_str += engine_kind2str_upper(*it);
93 std::cout <<
"Example " << (exit_code ?
"failed" :
"passed") <<
" on " 94 << engine_kind_str <<
"." << std::endl;
100 inline int handle_example_errors(
103 return handle_example_errors(
104 {engine_kind}, [&]() { example(engine_kind, argc, argv); });
108 inline int handle_example_errors(
111 return handle_example_errors(
112 {engine_kind}, [&]() { example(engine_kind); });
116 int argc,
char **argv,
int extra_args = 0) {
120 }
else if (argc <= extra_args + 2) {
121 std::string engine_kind_str = argv[1];
123 if (engine_kind_str ==
"cpu") {
125 }
else if (engine_kind_str ==
"gpu") {
128 std::cout <<
"Could not find compatible GPU" << std::endl
129 <<
"Please run the example with CPU instead" 138 std::cout <<
"Inappropriate engine kind." << std::endl
139 <<
"Please run the example like this: " << argv[0] <<
" [cpu|gpu]" 140 << (extra_args ?
" [extra arguments]" :
"") <<
"." << std::endl;
147 assert(!
"not expected");
148 return "<Unknown engine>";
153 std::multiplies<dnnl::memory::dim>());
157 inline void read_from_dnnl_memory(
void *handle,
dnnl::memory &mem) {
163 for (
size_t i = 0; i < bytes; ++i)
164 ((uint8_t *)handle)[i] = src[i];
166 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL 169 cl_command_queue q = s.get_ocl_command_queue();
172 cl_int ret = clEnqueueReadBuffer(
173 q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
174 if (ret != CL_SUCCESS)
175 throw std::runtime_error(
"clEnqueueReadBuffer failed.");
181 inline void write_to_dnnl_memory(
void *handle,
dnnl::memory &mem) {
187 for (
size_t i = 0; i < bytes; ++i)
188 dst[i] = ((uint8_t *)handle)[i];
190 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL 193 cl_command_queue q = s.get_ocl_command_queue();
197 cl_int ret = clEnqueueWriteBuffer(
198 q, m, CL_TRUE, 0, bytes, handle, 0, NULL, NULL);
199 if (ret != CL_SUCCESS)
200 throw std::runtime_error(
"clEnqueueWriteBuffer failed.");
void * get_data_handle() const
Returns the underlying memory buffer.
Definition: dnnl.hpp:2070
static size_t get_count(kind akind)
Returns the number of engines of a certain kind.
Definition: dnnl.hpp:864
An execution engine.
Definition: dnnl.hpp:840
oneDNN exception class.
Definition: dnnl.hpp:91
const char * what() const noexcept override
Returns the explanatory string.
Definition: dnnl.hpp:103
kind
Kinds of engines.
Definition: dnnl.hpp:845
dnnl_dim_t dim
Integer type for representing dimension sizes and indices.
Definition: dnnl.hpp:1186
engine get_engine() const
Returns the associated engine.
Definition: dnnl.hpp:2060
Memory object.
Definition: dnnl.hpp:1184
desc get_desc() const
Returns the associated memory descriptor.
Definition: dnnl.hpp:2052
cl_mem get_ocl_mem_object() const
Returns the OpenCL memory object associated with the memory.
Definition: dnnl.hpp:2167
std::vector< dim > dims
Vector of dimensions.
Definition: dnnl.hpp:1189
size_t get_size() const
Returns size of the memory descriptor in bytes.
Definition: dnnl.hpp:1981
An execution stream.
Definition: dnnl.hpp:1043