This C++ API example demonstrates basics of DNNL programming model.
#include <cmath>
#include <numeric>
#include <sstream>
#include <vector>
#include "example_utils.hpp"
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},
);
{N, C, H, W},
{stride_N, stride_C, stride_H, stride_W}
);
throw std::string("memory descriptor initialization mismatch");
write_to_dnnl_memory(image.data(), src_mem);
0.f,
0.f
);
auto relu_pd
eng
);
relu.execute(engine_stream,
{
{DNNL_ARG_SRC, src_mem},
{DNNL_ARG_DST, dst_mem},
});
std::vector<float> relu_image(image_size);
read_from_dnnl_memory(relu_image.data(), dst_mem);
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);
float expected = image[off] < 0
? 0.f
: image[off];
if (relu_image[off] != expected) {
std::stringstream ss;
ss << "Unexpected output at index(" << n << ", " << c
<< ", " << h << ", " << w << "): "
<< "Expect " << expected << " "
<< "Got " << relu_image[off];
throw ss.str();
}
}
}
int main(int argc, char **argv) {
try {
getting_started_tutorial(engine_kind);
std::cout << "Example passes" << std::endl;
std::cerr <<
"DNNL error: " << e.
what() << std::endl
<<
"Error status: " << dnnl_status2str(e.
status) << std::endl;
return 1;
} catch (std::string &e) {
std::cerr << "Error in the example: " << e << std::endl;
return 2;
}
return 0;
}