This C++ API example demonstrates programming for Intel(R) Processor Graphics with DNNL.
Annotated version: Getting started on GPU
#include <iostream>
#include <sstream>
size_t product(const memory::dims adims) {
size_t n_elems = 1;
for (size_t d = 0; d < adims.size(); ++d) {
n_elems *= (size_t)adims[d];
}
return n_elems;
}
void fill(
const memory &mem,
const memory::dims adims) {
for (size_t e = 0; e < adims.size(); ++e) {
array[e] = e % 7 ? 1.0f : -1.0f;
}
}
int find_negative(
const memory &mem,
const memory::dims adims) {
int negs = 0;
for (size_t e = 0; e < adims.size(); ++e) {
negs += array[e] < 0.0f;
}
return negs;
}
void cross_engine_reorder_tutorial() {
auto stream_gpu =
stream(gpu_engine);
const auto tz = memory::dims {2, 16, 1, 1};
auto m_cpu
cpu_engine);
auto m_gpu
gpu_engine);
fill(m_cpu, tz);
r1.execute(stream_gpu, m_cpu, m_gpu);
relu.execute(stream_gpu, {{DNNL_ARG_SRC, m_gpu}, {DNNL_ARG_DST, m_gpu}});
r2.execute(stream_gpu, m_gpu, m_cpu);
stream_gpu.wait();
if (find_negative(m_cpu, tz) != 0) {
std::stringstream ss;
ss << "Unexpected output, find a negative value after the ReLU "
"execution";
throw ss.str();
}
}
int main(int argc, char **argv) {
try {
cross_engine_reorder_tutorial();
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;
}
std::cout << "Example passes" << std::endl;
return 0;
}