17 #ifndef EXAMPLE_UTILS_HPP 18 #define EXAMPLE_UTILS_HPP 32 std::cerr <<
"Application couldn't find GPU, please run with " 33 "CPU instead. Thanks!\n";
41 int argc,
char **argv,
int extra_args = 0) {
45 }
else if (argc <= extra_args + 2) {
46 std::string engine_kind_str = argv[1];
48 if (engine_kind_str ==
"cpu") {
50 }
else if (engine_kind_str ==
"gpu") {
56 std::cerr <<
"Please run example like this" << argv[0] <<
" cpu|gpu";
57 if (extra_args) { std::cerr <<
" [extra arguments]"; }
63 inline void read_from_dnnl_memory(
void *handle,
dnnl::memory &mem) {
68 bool is_cpu_sycl = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL
70 bool is_gpu_sycl = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL
72 if (is_cpu_sycl || is_gpu_sycl) {
73 #ifdef DNNL_USE_SYCL_BUFFERS 75 auto src = buffer.get_access<cl::sycl::access::mode::read>();
76 uint8_t *src_ptr = src.get_pointer();
77 #elif defined(DNNL_USE_DPCPP_USM) 82 if (!handle || !src_ptr) {
83 std::cerr <<
"memory is NULL" 87 for (
size_t i = 0; i < size; ++i)
88 ((uint8_t *)handle)[i] = src_ptr[i];
92 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL 95 cl_command_queue q = s.get_ocl_command_queue();
98 cl_int ret = clEnqueueReadBuffer(
99 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
100 if (ret != CL_SUCCESS)
101 throw std::runtime_error(
"clEnqueueReadBuffer failed. Status Code: " 102 + std::to_string(ret) +
"\n");
109 if (!handle || !src) {
110 std::cerr <<
"memory is NULL" 114 for (
size_t i = 0; i < size; ++i)
115 ((uint8_t *)handle)[i] = src[i];
119 assert(!
"not expected");
123 inline void write_to_dnnl_memory(
void *handle,
dnnl::memory &mem) {
128 bool is_cpu_sycl = (DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL
130 bool is_gpu_sycl = (DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL
132 if (is_cpu_sycl || is_gpu_sycl) {
133 #ifdef DNNL_USE_SYCL_BUFFERS 135 auto dst = buffer.get_access<cl::sycl::access::mode::write>();
136 uint8_t *dst_ptr = dst.get_pointer();
137 #elif defined(DNNL_USE_DPCPP_USM) 140 #error "Not expected" 142 if (!dst_ptr || !handle) {
143 std::cerr <<
"memory is NULL" 147 for (
size_t i = 0; i < size; ++i)
148 dst_ptr[i] = ((uint8_t *)handle)[i];
152 #if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL 155 cl_command_queue q = s.get_ocl_command_queue();
158 cl_int ret = clEnqueueWriteBuffer(
159 q, m, CL_TRUE, 0, size, handle, 0, NULL, NULL);
160 if (ret != CL_SUCCESS)
161 throw std::runtime_error(
162 "clEnqueueWriteBuffer failed. Status Code: " 163 + std::to_string(ret) +
"\n");
170 if (!dst || !handle) {
171 std::cerr <<
"memory is NULL" 175 for (
size_t i = 0; i < size; ++i)
176 dst[i] = ((uint8_t *)handle)[i];
180 assert(!
"not expected");
void * get_data_handle() const
Returns a handle of the data contained in the memory.
Definition: dnnl.hpp:1561
static size_t get_count(kind akind)
Returns the number of engines of a certain kind.
Definition: dnnl.hpp:851
kind get_kind() const
Returns the kind of the engine.
Definition: dnnl.hpp:906
An execution engine.
Definition: dnnl.hpp:832
cl::sycl::buffer< T, ndims > get_sycl_buffer(size_t *offset=nullptr) const
Returns the underlying SYCL buffer object.
Definition: dnnl.hpp:1631
kind
Kinds of engines.
Definition: dnnl.hpp:837
engine get_engine() const
Returns the engine of the memory.
Definition: dnnl.hpp:1551
Memory that describes the data.
Definition: dnnl.hpp:1071
desc get_desc() const
Returns the descriptor of the memory.
Definition: dnnl.hpp:1543
cl_mem get_ocl_mem_object() const
Returns the OpenCL memory object associated with the memory.
Definition: dnnl.hpp:1611
size_t get_size() const
Returns the number of bytes required to allocate the memory described including the padding area...
Definition: dnnl.hpp:1478
An execution stream.
Definition: dnnl.hpp:976