1/*******************************************************************************
2* Copyright 2020-2022 Intel Corporation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*******************************************************************************/
16
17/// @example sum.cpp
18/// > Annotated version: @ref sum_example_cpp
19///
20/// @page sum_example_cpp_short
21///
22/// This C++ API example demonstrates how to create and execute a
23/// [Sum](@ref dev_guide_sum) primitive.
24///
25/// Key optimizations included in this example:
26/// - Identical memory formats for source (src) and destination (dst) tensors.
27///
28/// @page sum_example_cpp Sum Primitive Example
29/// @copydetails sum_example_cpp_short
30///
31/// @include sum.cpp
32
33#include <algorithm>
34#include <cmath>
35#include <iostream>
36#include <string>
37#include <vector>
38
39#include "example_utils.hpp"
40#include "oneapi/dnnl/dnnl.hpp"
41
42using namespace dnnl;
43
44using tag = memory::format_tag;
45using dt = memory::data_type;
46
47void sum_example(dnnl::engine::kind engine_kind) {
48
49 // Create execution dnnl::engine.
50 dnnl::engine engine(engine_kind, 0);
51
52 // Create dnnl::stream.
53 dnnl::stream engine_stream(engine);
54
55 // Tensor dimensions.
56 const memory::dim N = 3, // batch size
57 IC = 3, // channels
58 IH = 227, // tensor height
59 IW = 227; // tensor width
60
61 // Source (src) and destination (dst) tensors dimensions.
62 memory::dims src_dims = {N, IC, IH, IW};
63
64 // Allocate buffers.
65 std::vector<float> src_data(product(src_dims));
66 std::vector<float> dst_data(product(src_dims));
67
68 // Initialize src.
69 std::generate(src_data.begin(), src_data.end(), []() {
70 static int i = 0;
71 return std::cos(i++ / 10.f);
72 });
73
74 // Number of src tensors.
75 const int num_src = 10;
76
77 // Scaling factors.
78 std::vector<float> scales(num_src);
79 std::generate(scales.begin(), scales.end(),
80 [](int n = 0) { return sin(float(n)); });
81
82 // Create an array of memory descriptors and memory objects for src tensors.
83 std::vector<memory::desc> src_md;
84 std::vector<memory> src_mem;
85
86 for (int n = 0; n < num_src; ++n) {
87 auto md = memory::desc(src_dims, dt::f32, tag::nchw);
88 auto mem = memory(md, engine);
89
90 // Write data to memory object's handle.
91 write_to_dnnl_memory(src_data.data(), mem);
92
93 src_md.push_back(md);
94 src_mem.push_back(mem);
95 }
96
97 // Create primitive descriptor.
98 auto sum_pd = sum::primitive_desc(engine, scales, src_md);
99
100 // Create the primitive.
101 auto sum_prim = sum(sum_pd);
102
103 // Create memory object for dst.
104 auto dst_mem = memory(sum_pd.dst_desc(), engine);
105
106 // Primitive arguments.
107 std::unordered_map<int, memory> sum_args;
108 sum_args.insert({DNNL_ARG_DST, dst_mem});
109 for (int n = 0; n < num_src; ++n) {
110 sum_args.insert({DNNL_ARG_MULTIPLE_SRC + n, src_mem[n]});
111 }
112
113 // Primitive execution: sum.
114 sum_prim.execute(engine_stream, sum_args);
115
116 // Wait for the computation to finalize.
117 engine_stream.wait();
118
119 // Read data from memory object's handle.
120 read_from_dnnl_memory(dst_data.data(), dst_mem);
121}
122
123int main(int argc, char **argv) {
124 return handle_example_errors(sum_example, parse_engine_kind(argc, argv));
125}
126