This C++ API example demonstrates programming for Intel(R) Processor Graphics with Intel(R) MKL-DNN.
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 gpu_getting_started_tutorial() {
auto stream_gpu =
stream(gpu_engine);
const auto tz = memory::dims{ 2, 16, 1, 1 };
cpu_engine);
gpu_engine);
fill(m_cpu, tz);
r1.execute(stream_gpu, m_cpu, m_gpu);
relu.execute(stream_gpu,
{ { MKLDNN_ARG_SRC, m_gpu }, { MKLDNN_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 {
gpu_getting_started_tutorial();
std::cerr <<
"Intel MKL-DNN error: " << e.
what() << std::endl
<<
"Error status: " << mkldnn_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;
}