#include "example_utils.hpp"
#include "oneapi/dnnl/dnnl_sycl.hpp"
#include <CL/sycl.hpp>
#include <cassert>
#include <iostream>
#include <numeric>
using namespace cl::sycl;
class kernel_tag;
void sycl_interop_buffer_tutorial(engine::kind engine_kind) {
memory::dims tz_dims = {2, 3, 4, 5};
const size_t N = std::accumulate(tz_dims.begin(), tz_dims.end(), (size_t)1,
std::multiplies<size_t>());
memory::desc mem_d(
tz_dims, memory::data_type::f32, memory::format_tag::nchw);
memory mem = sycl_interop::make_memory(
mem_d, eng, sycl_interop::memory_kind::buffer);
auto sycl_buf = sycl_interop::get_buffer<float>(mem);
queue q = sycl_interop::get_queue(strm);
q.submit([&](handler &cgh) {
auto a = sycl_buf.get_access<access::mode::write>(cgh);
cgh.parallel_for<kernel_tag>(range<1>(N), [=](id<1> i) {
int idx = (int)i[0];
a[idx] = (idx % 2) ? -idx : idx;
});
});
auto relu_d = eltwise_forward::desc(
prop_kind::forward, algorithm::eltwise_relu, mem_d, 0.0f);
auto relu_pd = eltwise_forward::primitive_desc(relu_d, eng);
auto relu = eltwise_forward(relu_pd);
strm.wait();
auto host_acc = sycl_buf.get_access<access::mode::read>();
for (size_t i = 0; i < N; i++) {
float exp_value = (i % 2) ? 0.0f : i;
if (host_acc[i] != (float)exp_value)
throw std::string(
"Unexpected output, find a negative value after the ReLU "
"execution.");
}
}
int main(int argc, char **argv) {
int exit_code = 0;
engine::kind engine_kind = parse_engine_kind(argc, argv);
try {
sycl_interop_buffer_tutorial(engine_kind);
std::cout << "oneDNN error caught: " << std::endl
<<
"\tStatus: " << dnnl_status2str(e.
status) << std::endl
<<
"\tMessage: " << e.
what() << std::endl;
exit_code = 1;
} catch (std::string &e) {
std::cout << "Error in the example: " << e << "." << std::endl;
exit_code = 2;
}
std::cout << "Example " << (exit_code ? "failed" : "passed") << " on "
<< engine_kind2str_upper(engine_kind) << "." << std::endl;
return exit_code;
}
#define DNNL_ARG_DST
A special mnemonic for destination argument for primitives that have a single destination.
Definition: dnnl_types.h:2307
#define DNNL_ARG_SRC
A special mnemonic for source argument for primitives that have a single source.
Definition: dnnl_types.h:2283
oneDNN namespace
Definition: dnnl.hpp:74
oneDNN exception class.
Definition: dnnl.hpp:84
const char * what() const noexcept override
Returns the explanatory string.
Definition: dnnl.hpp:96
An execution stream.
Definition: dnnl.hpp:985