1/*******************************************************************************
2* Copyright 2019-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#include <stdio.h>
18#include <stdlib.h>
19
20#include <sstream>
21
22#include "dnnl_common.hpp"
23#include "utils/parser.hpp"
24
25#include "sum/sum.hpp"
26
27namespace sum {
28
29void check_correctness(const settings_t &s) {
30 for_(const auto &i_sdt : s.sdt)
31 for_(const auto &i_ddt : s.ddt)
32 for_(const auto &i_stag : s.stag)
33 for_(const auto &i_dtag : s.dtag)
34 for_(const auto &i_input_scales : s.input_scales)
35 for_(const auto &i_scratchpad_mode : s.scratchpad_mode)
36 for_(const auto &i_ctx_init : s.ctx_init)
37 for_(const auto &i_ctx_exe : s.ctx_exe)
38 for (auto i_inplace : s.inplace) {
39 const int n_inputs = static_cast<int>(i_sdt.size());
40 const int n_stags = static_cast<int>(i_stag.size());
41 if (n_stags != n_inputs && n_stags != 1) {
42 BENCHDNN_PRINT(0,
43 "ERROR: Expected number of stag arguments is `1` or `%d`, "
44 "provided `%d`.\n",
45 n_inputs, n_stags);
46 SAFE_V(FAIL);
47 }
48
49 const int n_input_scales = static_cast<int>(i_input_scales.size());
50 if (n_input_scales != n_inputs && n_input_scales != 1) {
51 BENCHDNN_PRINT(0,
52 "ERROR: Expected number of scales arguments is `1` or "
53 "`%d`, provided `%d`.\n",
54 n_inputs, n_input_scales);
55 SAFE_V(FAIL);
56 }
57
58 auto attr = settings_t::get_attr(i_scratchpad_mode);
59
60 const prb_t prb(s.prb_dims, i_sdt, i_ddt, i_stag, i_dtag,
61 i_input_scales, i_inplace, attr, i_ctx_init, i_ctx_exe);
62 std::stringstream ss;
63 ss << prb;
64 const std::string cpp_pstr = ss.str();
65 const char *pstr = cpp_pstr.c_str();
66 BENCHDNN_PRINT(1, "run: %s\n", pstr);
67
68 res_t res {};
69 doit(&prb, &res);
70
71 parse_result(res, pstr);
72
73 if (is_bench_mode(PERF)) {
74 perf_report_t pr(&prb, s.perf_template);
75 pr.report(&res, pstr);
76 }
77 }
78}
79
80static const std::string help_scales
81 = "FLOAT[:FLOAT...] (Default: `1.f`)\n Input scales for source "
82 "values.\n If a single value is specified, will be broadcasted "
83 "for all sources, otherwise number of scales should match number of "
84 "inputs.\n";
85
86int bench(int argc, char **argv) {
87 driver_name = "sum";
88 using namespace parser;
89 static settings_t s;
90 static const settings_t def {};
91 for (; argc > 0; --argc, ++argv) {
92 const bool parsed_options = parse_bench_settings(argv[0])
93 || parse_batch(bench, argv[0])
94 || parse_multi_dt(s.sdt, def.sdt, argv[0])
95 || parse_dt(s.ddt, def.ddt, argv[0], "ddt")
96 || parse_multi_tag(s.stag, def.stag, argv[0])
97 || parse_tag(s.dtag, def.dtag, argv[0], "dtag")
98 || parse_multivector_option(s.input_scales, def.input_scales,
99 atof, argv[0], "scales", help_scales)
100 || parse_inplace(s.inplace, def.inplace, argv[0])
101 || parse_attr_scratchpad_mode(
102 s.scratchpad_mode, def.scratchpad_mode, argv[0])
103 || parse_ctx_init(s.ctx_init, def.ctx_init, argv[0])
104 || parse_ctx_exe(s.ctx_exe, def.ctx_exe, argv[0])
105 || parse_perf_template(s.perf_template, s.perf_template_def,
106 s.perf_template_csv(), argv[0])
107 || parse_reset(s, argv[0]) || parse_help(argv[0]);
108 if (!parsed_options) {
109 catch_unknown_options(argv[0]);
110
111 parse_prb_dims(s.prb_dims, argv[0]);
112 check_correctness(s);
113 }
114 }
115
116 return parse_last_argument();
117}
118
119} // namespace sum
120