#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) {
const size_t N = std::accumulate(tz_dims.begin(), tz_dims.end(), (size_t)1,
std::multiplies<size_t>());
memory::desc mem_d(
auto sycl_buf = sycl_interop::get_buffer<float>(mem);
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(
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;
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;
}
@ eltwise_relu
Elementwise: rectified linear unit (ReLU)
@ forward
Forward data propagation, alias for dnnl::prop_kind::forward_training.
#define DNNL_ARG_DST
A special mnemonic for destination argument for primitives that have a single destination.
Definition: dnnl_types.h:2376
#define DNNL_ARG_SRC
A special mnemonic for source argument for primitives that have a single source.
Definition: dnnl_types.h:2352
cl::sycl::queue get_queue(const stream &astream)
Returns the SYCL queue associated with an execution stream.
Definition: dnnl_sycl.hpp:132
@ buffer
Buffer memory allocation kind.
memory make_memory(const memory::desc &memory_desc, const engine &aengine, memory_kind kind, void *handle=DNNL_MEMORY_ALLOCATE)
Creates a memory object.
Definition: dnnl_sycl.hpp:240
oneDNN namespace
Definition: dnnl.hpp:74
kind
Kinds of engines.
Definition: dnnl.hpp:890
oneDNN exception class.
Definition: dnnl.hpp:84
const char * what() const noexcept override
Returns the explanatory string.
Definition: dnnl.hpp:96
@ nchw
4D CNN activations tensor; an alias for dnnl::memory::format_tag::abcd
@ f32
32-bit/single-precision floating point.
std::vector< dim > dims
Vector of dimensions.
Definition: dnnl.hpp:1131
An execution stream.
Definition: dnnl.hpp:1001