This C++ API example demonstrates programming flow when reordering memory between CPU and GPU engines.
This C++ API example demonstrates programming flow when reordering memory between CPU and GPU engines.
#include <iostream>
#include <stdexcept>
#include <vector>
#include "example_utils.hpp"
#include "example_utils.hpp"
using namespace std;
void fill(memory &mem, const memory::dims &adims) {
std::vector<float> array(product(adims));
for (size_t e = 0; e < array.size(); ++e) {
array[e] = e % 7 ? 1.0f : -1.0f;
}
write_to_dnnl_memory(array.data(), mem);
}
int find_negative(memory &mem, const memory::dims &adims) {
int negs = 0;
size_t nelems = product(adims);
std::vector<float> array(nelems);
read_from_dnnl_memory(array.data(), mem);
for (size_t e = 0; e < nelems; ++e)
negs += array[e] < 0.0f;
return negs;
}
void cross_engine_reorder_tutorial() {
auto cpu_engine =
engine(validate_engine_kind(engine::kind::cpu), 0);
auto gpu_engine =
engine(validate_engine_kind(engine::kind::gpu), 0);
auto stream_gpu = stream(gpu_engine, stream::flags::in_order);
const auto tz = memory::dims {2, 16, 1, 1};
auto m_cpu
= memory({{tz}, memory::data_type::f32, memory::format_tag::nchw},
cpu_engine);
auto m_gpu
= memory({{tz}, memory::data_type::f32, memory::format_tag::nchw},
gpu_engine);
fill(m_cpu, tz);
auto r1 = reorder(m_cpu, m_gpu);
auto relu_d = eltwise_forward::desc(prop_kind::forward,
algorithm::eltwise_relu, m_gpu.get_desc(), 0.0f);
auto relu_pd = eltwise_forward::primitive_desc(relu_d, gpu_engine);
auto relu = eltwise_forward(relu_pd);
auto r2 = reorder(m_gpu, m_cpu);
r1.execute(stream_gpu, m_cpu, m_gpu);
r2.execute(stream_gpu, m_gpu, m_cpu);
stream_gpu.wait();
if (find_negative(m_cpu, tz) != 0)
throw std::logic_error(
"Unexpected output, find a negative value after the ReLU "
"execution.");
}
int main(int argc, char **argv) {
return handle_example_errors({engine::kind::cpu, engine::kind::gpu},
cross_engine_reorder_tutorial);
}
#define DNNL_ARG_DST
A special mnemonic for destination argument for primitives that have a single destination.
Definition: dnnl_types.h:2307
#define DNNL_ARG_SRC
A special mnemonic for source argument for primitives that have a single source.
Definition: dnnl_types.h:2283
oneDNN namespace
Definition: dnnl.hpp:74