This C API example demonstrates programming flow when reordering memory between CPU and GPU engines.
#include <stdio.h>
#include <stdlib.h>
#include "example_utils.h"
size_t product(
int n_dims,
const dnnl_dim_t dims[]) {
size_t n_elems = 1;
for (int d = 0; d < n_dims; ++d) {
n_elems *= (size_t)dims[d];
}
return n_elems;
}
const size_t n_elems = product(n_dims, dims);
float *array = (float *)malloc(n_elems * sizeof(float));
if (!array) COMPLAIN_EXAMPLE_ERROR_AND_EXIT("%s", "malloc returned NULL");
for (size_t e = 0; e < n_elems; ++e) {
array[e] = e % 7 ? 1.0f : -1.0f;
}
write_to_dnnl_memory(array, mem);
free(array);
}
const size_t n_elems = product(n_dims, dims);
float *array = (float *)malloc(n_elems * sizeof(float));
if (!array) COMPLAIN_EXAMPLE_ERROR_AND_EXIT("%s", "malloc returned NULL");
read_from_dnnl_memory(array, mem);
int negs = 0;
for (size_t e = 0; e < n_elems; ++e) {
negs += array[e] < 0.0f;
}
free(array);
return negs;
}
void cross_engine_reorder() {
fill(m_cpu, 4, tz);
if (find_negative(m_cpu, 4, tz) == 0)
COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
"%s", "incorrect data fill, no negative values found");
&r1_pd, &m_cpu_md, engine_cpu, &m_gpu_md, engine_gpu, NULL));
&relu_pd, &relu_d, NULL, engine_gpu, NULL));
&r2_pd, &m_gpu_md, engine_gpu, &m_cpu_md, engine_cpu, NULL));
if (find_negative(m_cpu, 4, tz) != 0)
COMPLAIN_EXAMPLE_ERROR_AND_EXIT(
"%s", "found negative values after ReLU applied");
}
int main() {
cross_engine_reorder();
printf("Example passed on CPU/GPU.\n");
return 0;
}
dnnl_status_t DNNL_API dnnl_eltwise_forward_desc_init(dnnl_eltwise_desc_t *eltwise_desc, dnnl_prop_kind_t prop_kind, dnnl_alg_kind_t alg_kind, const dnnl_memory_desc_t *data_desc, float alpha, float beta)
Initializes a descriptor for eltwise forward propagation primitive.
dnnl_status_t DNNL_API dnnl_engine_destroy(dnnl_engine_t engine)
Destroys an engine.
dnnl_status_t DNNL_API dnnl_engine_create(dnnl_engine_t *engine, dnnl_engine_kind_t kind, size_t index)
Creates an engine.
@ dnnl_gpu
GPU engine.
Definition: dnnl_types.h:2268
@ dnnl_cpu
CPU engine.
Definition: dnnl_types.h:2266
dnnl_status_t DNNL_API dnnl_memory_create(dnnl_memory_t *memory, const dnnl_memory_desc_t *memory_desc, dnnl_engine_t engine, void *handle)
Creates a memory object.
int64_t dnnl_dim_t
A type to describe tensor dimension.
Definition: dnnl_types.h:1444
dnnl_status_t DNNL_API dnnl_memory_destroy(dnnl_memory_t memory)
Destroys a memory object.
#define DNNL_MEMORY_ALLOCATE
Special pointer value that indicates that the library needs to allocate an underlying buffer for a me...
Definition: dnnl_types.h:1622
dnnl_status_t DNNL_API dnnl_memory_desc_init_by_tag(dnnl_memory_desc_t *memory_desc, int ndims, const dnnl_dims_t dims, dnnl_data_type_t data_type, dnnl_format_tag_t tag)
Initializes a memory descriptor using dimensions and memory format tag.
@ dnnl_f32
32-bit/single-precision floating point.
Definition: dnnl_types.h:70
@ dnnl_nchw
4D CNN activations tensor, an alias to dnnl_abcd
Definition: dnnl_types.h:609
dnnl_status_t DNNL_API dnnl_primitive_desc_create(dnnl_primitive_desc_t *primitive_desc, const_dnnl_op_desc_t op_desc, const_dnnl_primitive_attr_t attr, dnnl_engine_t engine, const_dnnl_primitive_desc_t hint_forward_primitive_desc)
Creates a primitive descriptor.
#define DNNL_ARG_DST
A special mnemonic for destination argument for primitives that have a single destination.
Definition: dnnl_types.h:2422
dnnl_status_t DNNL_API dnnl_primitive_execute(const_dnnl_primitive_t primitive, dnnl_stream_t stream, int nargs, const dnnl_exec_arg_t *args)
Executes a primitive.
dnnl_status_t DNNL_API dnnl_primitive_desc_destroy(dnnl_primitive_desc_t primitive_desc)
Destroys a primitive descriptor.
#define DNNL_ARG_FROM
A special mnemonic for reorder source argument.
Definition: dnnl_types.h:2404
dnnl_status_t DNNL_API dnnl_primitive_destroy(dnnl_primitive_t primitive)
Destroys a primitive.
#define DNNL_ARG_SRC
A special mnemonic for source argument for primitives that have a single source.
Definition: dnnl_types.h:2398
dnnl_status_t DNNL_API dnnl_primitive_create(dnnl_primitive_t *primitive, const_dnnl_primitive_desc_t primitive_desc)
Creates a primitive.
#define DNNL_ARG_TO
A special mnemonic for reorder destination argument.
Definition: dnnl_types.h:2425
@ dnnl_eltwise_relu
Eltwise: ReLU.
Definition: dnnl_types.h:1198
@ dnnl_forward
Forward data propagation (alias for dnnl_forward_training).
Definition: dnnl_types.h:1118
dnnl_status_t DNNL_API dnnl_reorder_primitive_desc_create(dnnl_primitive_desc_t *reorder_primitive_desc, const dnnl_memory_desc_t *src_desc, dnnl_engine_t src_engine, const dnnl_memory_desc_t *dst_desc, dnnl_engine_t dst_engine, const_dnnl_primitive_attr_t attr)
Creates a primitive descriptor for a reorder primitive.
dnnl_status_t DNNL_API dnnl_stream_wait(dnnl_stream_t stream)
Waits for all primitives in the execution stream to finish computations.
dnnl_status_t DNNL_API dnnl_stream_destroy(dnnl_stream_t stream)
Destroys an execution stream.
dnnl_status_t DNNL_API dnnl_stream_create(dnnl_stream_t *stream, dnnl_engine_t engine, unsigned flags)
Creates an execution stream.
@ dnnl_stream_default_flags
Default stream configuration.
Definition: dnnl_types.h:2717
A descriptor of a element-wise operation.
Definition: dnnl_types.h:1721
An opaque structure to describe an engine.
A structure that contains an index and a memory object, and is used to pass arguments to dnnl_primiti...
Definition: dnnl_types.h:2600
Memory descriptor.
Definition: dnnl_types.h:1557
An opaque structure to describe a memory.
An opaque structure to describe a primitive descriptor.
An opaque structure to describe a primitive.
An opaque structure to describe an execution stream.