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#include "reduction.hpp"
18
19namespace reduction {
20
21alg_t str2alg(const char *str) {
22#define CASE(_alg) \
23 if (!strcasecmp(STRINGIFY(_alg), str)) return _alg
24 CASE(max);
25 CASE(reduction_max);
26 CASE(min);
27 CASE(reduction_min);
28 CASE(sum);
29 CASE(reduction_sum);
30 CASE(mul);
31 CASE(reduction_mul);
32 CASE(mean);
33 CASE(reduction_mean);
34 CASE(norm_lp_max);
35 CASE(reduction_norm_lp_max);
36 CASE(norm_lp_sum);
37 CASE(reduction_norm_lp_sum);
38 CASE(norm_lp_power_p_max);
39 CASE(reduction_norm_lp_power_p_max);
40 CASE(norm_lp_power_p_sum);
41 CASE(reduction_norm_lp_power_p_sum);
42
43#undef CASE
44 assert(!"unknown algorithm");
45 return undef;
46}
47
48const char *alg2str(alg_t alg) {
49 if (alg == max) return "max";
50 if (alg == min) return "min";
51 if (alg == sum) return "sum";
52 if (alg == mul) return "mul";
53 if (alg == mean) return "mean";
54 if (alg == norm_lp_max) return "norm_lp_max";
55 if (alg == norm_lp_sum) return "norm_lp_sum";
56 if (alg == norm_lp_power_p_max) return "norm_lp_power_p_max";
57 if (alg == norm_lp_power_p_sum) return "norm_lp_power_p_sum";
58 assert(!"unknown algorithm");
59 return "undef";
60}
61
62dnnl_alg_kind_t alg2alg_kind(alg_t alg) {
63 if (alg == max) return dnnl_reduction_max;
64 if (alg == min) return dnnl_reduction_min;
65 if (alg == sum) return dnnl_reduction_sum;
66 if (alg == mul) return dnnl_reduction_mul;
67 if (alg == mean) return dnnl_reduction_mean;
68 if (alg == norm_lp_max) return dnnl_reduction_norm_lp_max;
69 if (alg == norm_lp_sum) return dnnl_reduction_norm_lp_sum;
70 if (alg == norm_lp_power_p_max) return dnnl_reduction_norm_lp_power_p_max;
71 if (alg == norm_lp_power_p_sum) return dnnl_reduction_norm_lp_power_p_sum;
72 assert(!"unknown algorithm");
73 return dnnl_alg_kind_undef;
74}
75
76std::ostream &operator<<(std::ostream &s, const prb_t &prb) {
77 dump_global_params(s);
78 settings_t def;
79
80 if (canonical || prb.sdt != def.sdt[0]) s << "--sdt=" << prb.sdt << " ";
81 if (canonical || prb.ddt != def.ddt[0]) s << "--ddt=" << prb.ddt << " ";
82 if (canonical || prb.stag != def.stag[0]) s << "--stag=" << prb.stag << " ";
83 if (canonical || prb.dtag != def.dtag[0]) s << "--dtag=" << prb.dtag << " ";
84 if (canonical || prb.alg != def.alg[0])
85 s << "--alg=" << alg2str(prb.alg) << " ";
86 if (canonical || prb.p != def.p[0]) s << "--p=" << prb.p << " ";
87 if (canonical || prb.eps != def.eps[0]) s << "--eps=" << prb.eps << " ";
88
89 s << prb.attr;
90 if (canonical || prb.ctx_init != def.ctx_init[0])
91 s << "--ctx-init=" << prb.ctx_init << " ";
92 if (canonical || prb.ctx_exe != def.ctx_exe[0])
93 s << "--ctx-exe=" << prb.ctx_exe << " ";
94
95 s << static_cast<prb_vdims_t>(prb);
96
97 return s;
98}
99
100} // namespace reduction
101