This C++ API example demonstrates the basics of the oneDNN programming model.
#include <cmath>
#include <numeric>
#include <stdexcept>
#include <vector>
#include "example_utils.hpp"
stream engine_stream(eng);
const int N = 1, H = 13, W = 13, C = 3;
const int stride_N = H * W * C;
const int stride_H = W * C;
const int stride_W = C;
const int stride_C = 1;
auto offset = [=](int n, int h, int w, int c) {
return n * stride_N + h * stride_H + w * stride_W + c * stride_C;
};
const int image_size = N * H * W * C;
std::vector<float> image(image_size);
for (int n = 0; n < N; ++n)
for (int h = 0; h < H; ++h)
for (int w = 0; w < W; ++w)
for (int c = 0; c < C; ++c) {
int off = offset(
n, h, w, c);
image[off] = -std::cos(off / 10.f);
}
{N, C, H, W},
);
auto alt_src_md = memory::desc(
{N, C, H, W},
{stride_N, stride_C, stride_H, stride_W}
);
throw std::logic_error("Memory descriptor initialization mismatch.");
auto src_mem = memory(
src_md, eng);
write_to_dnnl_memory(image.data(), src_mem);
auto dst_mem = memory(
src_md, eng);
auto relu_d = eltwise_forward::desc(
0.f,
0.f
);
auto relu_pd
= eltwise_forward::primitive_desc(relu_d,
eng
);
auto relu = eltwise_forward(relu_pd);
relu.execute(engine_stream,
{
});
engine_stream.wait();
std::vector<float> relu_image(image_size);
read_from_dnnl_memory(relu_image.data(), dst_mem);
}
int main(int argc, char **argv) {
int exit_code = 0;
try {
getting_started_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_inference
Forward data propagation (inference mode).
#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
@ src_md
source memory desc
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
@ nhwc
4D CNN activations tensor; an alias for dnnl::memory::format_tag::acdb
@ f32
32-bit/single-precision floating point.