Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / tests / benchdnn / rnn / perf_report.cpp
1 /*******************************************************************************
2  * Copyright 2018 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 <float.h>
18 #include <math.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "mkldnn.h"
24 #include "mkldnn_memory.hpp"
25
26 #include "rnn/rnn.hpp"
27
28 namespace rnn {
29
30 void perf_report(const rnn_prb_t *p, const res_t *r, const char *pstr) {
31     const auto &t = r->timer;
32     const int max_len = 400;
33     int rem_len = max_len - 1;
34     char buffer[max_len], *buf = buffer;
35
36 #   define DPRINT(...) do { \
37         int l = snprintf(buf, rem_len, __VA_ARGS__); \
38         buf += l; rem_len -= l; \
39     } while(0)
40
41     auto modifier2mode = [](char c) {
42         if (c == '-') return benchdnn_timer_t::min;
43         if (c == '0') return benchdnn_timer_t::avg;
44         if (c == '+') return benchdnn_timer_t::max;
45         return benchdnn_timer_t::min;
46     };
47
48     auto modifier2unit = [](char c) {
49         if (c == 'K') return 1e3;
50         if (c == 'M') return 1e6;
51         if (c == 'G') return 1e9;
52         return 1e0;
53     };
54
55     const char *pt = perf_template;
56     char c;
57
58     while ((c = *pt++) != '\0') {
59         if (c != '%') { *buf++ = c; rem_len--; continue; }
60
61         c = *pt++;
62
63         benchdnn_timer_t::mode_t mode = benchdnn_timer_t::min;
64         double unit = 1e0;
65
66         if (c == '-' || c == '0' || c == '+') {
67             mode = modifier2mode(c);
68             c = *pt++;
69         }
70
71         if (c == 'K' || c == 'M' || c == 'G') {
72             unit = modifier2unit(c);
73             c = *pt++;
74         }
75         // cellkind:activation:direction:l d mb
76         if (c == 'd') DPRINT("%s_%s_%s_l%dd%dt%dmb%d_slc%dsic%ddic%d",
77                              alg2str(p->alg), activation2str(p->activation), direction2str(p->direction),
78                              p->n_layer, p->n_directions(), p->n_iter, p->mb, p->slc, p->sic, p->dic);
79         else if (c == 'D')
80             DPRINT("%s", pstr);
81         else if (c == 'n')
82             DPRINT("%s", p->name);
83         else if (c == 'z')
84             DPRINT("%s", prop2str(p->prop));
85         else if (c == 'F')
86             DPRINT("%g", t.ticks(mode) / t.ms(mode) / unit * 1e3);
87         else if (c == 't')
88             DPRINT("%g", t.ms(mode) / unit);
89         else if (c == 'c')
90             DPRINT("%g", t.ticks(mode) / unit);
91         else
92             []() { SAFE(FAIL, CRIT); return 0; }();
93     }
94
95     *buf = '\0';
96     assert(rem_len >= 0);
97
98 #   undef DPRINT
99     print(0, "%s\n", buffer);
100 }
101
102 } // namespace rnn