Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/cmd/pool/ccv_nnc_avg_pool_cpu_ref.c
Line
Count
Source (jump to first uncovered line)
1
#include <ccv.h>
2
#include <ccv_internal.h>
3
#include <nnc/ccv_nnc.h>
4
#include <nnc/ccv_nnc_easy.h>
5
#include <nnc/ccv_nnc_internal.h>
6
#ifdef USE_OPENMP
7
#include <omp.h>
8
#endif
9
#ifdef USE_DISPATCH
10
#include <dispatch/dispatch.h>
11
#endif
12
13
static int _ccv_nnc_avg_pool_forw(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, ccv_nnc_tensor_t* const* const inputs, const int input_size, ccv_nnc_tensor_t* const* const outputs, const int output_size, ccv_nnc_stream_context_t* const stream_context)
14
629
{
15
629
  assert(input_size == 1);
16
629
  const ccv_nnc_tensor_view_t* a = (ccv_nnc_tensor_view_t*)inputs[0];
17
629
  assert(output_size == 1);
18
629
  ccv_nnc_tensor_view_t* b = (ccv_nnc_tensor_view_t*)outputs[0];
19
629
  const int *dim = cmd.info.size.dim;
20
629
  int i[CCV_NNC_MAX_DIM];
21
629
  int n[CCV_NNC_MAX_DIM];
22
629
  int m[CCV_NNC_MAX_DIM];
23
629
  int j[CCV_NNC_MAX_DIM];
24
629
  int c;
25
629
  const int a_nd = ccv_nnc_tensor_nd(a->info.dim);
26
629
  assert(a_nd == CCV_NNC_MAX_DIM + 1 || a_nd == CCV_NNC_MAX_DIM + 2);
27
629
  const int* adim = (a_nd == CCV_NNC_MAX_DIM + 1) ? 
a->info.dim15
:
a->info.dim + 1614
;
28
629
  const int b_nd = ccv_nnc_tensor_nd(b->info.dim);
29
629
  assert(b_nd == CCV_NNC_MAX_DIM + 1 || b_nd == CCV_NNC_MAX_DIM + 2);
30
629
  const int* bdim = (b_nd == CCV_NNC_MAX_DIM + 1) ? 
b->info.dim15
:
b->info.dim + 1614
;
31
629
  float* ap = a->data.f32;
32
629
  const int* ainc = CCV_IS_TENSOR_VIEW(a) ? 
((a_nd == 0
CCV_NNC_MAX_DIM0
+ 1) ?
a->inc0
:
a->inc + 10
) : adim;
33
629
  float* bp = b->data.f32;
34
629
  const int* binc = CCV_IS_TENSOR_VIEW(b) ? 
((b_nd == 0
CCV_NNC_MAX_DIM0
+ 1) ?
b->inc0
:
b->inc + 10
) : bdim;
35
3.82k
  for (i[0] = 0; i[0] < bdim[0]; 
i[0]++3.19k
)
36
3.19k
  {
37
3.19k
    SET_BORDER_OFFSET_SIZE_FOR(0, i, hint, dim, adim, n, m);
38
23.0k
    for (i[1] = 0; i[1] < bdim[1]; 
i[1]++19.8k
)
39
19.8k
    {
40
19.8k
      SET_BORDER_OFFSET_SIZE_FOR(1, i, hint, dim, adim, n, m);
41
681k
      for (c = 0; c < bdim[CCV_NNC_MAX_DIM]; 
c++661k
)
42
661k
      {
43
661k
        float* apz = ap + ccv_max(i[1] * hint.stride.dim[1] - hint.border.begin[1], 0) * ainc[CCV_NNC_MAX_DIM] + c;
44
661k
        float v = 0;
45
2.64M
        for (j[0] = 0; j[0] < m[0]; 
j[0]++1.98M
)
46
1.98M
        {
47
7.92M
          for (j[1] = 0; j[1] < m[1]; 
j[1]++5.94M
)
48
5.94M
            v += apz[j[1] * ainc[CCV_NNC_MAX_DIM]];
49
1.98M
          apz += ainc[CCV_NNC_MAX_DIM - 1] * ainc[CCV_NNC_MAX_DIM];
50
1.98M
        }
51
661k
        bp[i[1] * binc[CCV_NNC_MAX_DIM] + c] = v / (m[0] * m[1]);
52
661k
      }
53
19.8k
    }
54
3.19k
    bp += binc[CCV_NNC_MAX_DIM - 1] * binc[CCV_NNC_MAX_DIM];
55
3.19k
    ap += ainc[CCV_NNC_MAX_DIM - 1] * ainc[CCV_NNC_MAX_DIM] * (ccv_max((i[0] + 1) * hint.stride.dim[0] - hint.border.begin[0], 0) - ccv_max(i[0] * hint.stride.dim[0] - hint.border.begin[0], 0));
56
3.19k
  }
57
629
  return CCV_NNC_EXEC_SUCCESS;
58
629
}
59
60
static int _ccv_nnc_avg_pool_back(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, ccv_nnc_tensor_t* const* const inputs, const int input_size, ccv_nnc_tensor_t* const* const outputs, const int output_size, ccv_nnc_stream_context_t* const stream_context)
61
736
{
62
736
  assert(input_size >= 1);
63
736
  const ccv_nnc_tensor_view_t* g = (ccv_nnc_tensor_view_t*)inputs[0];
64
736
  assert(output_size == 1);
65
736
  ccv_nnc_tensor_view_t* h = (ccv_nnc_tensor_view_t*)outputs[0];
66
736
  const int *dim = cmd.info.size.dim;
67
736
  int i[CCV_NNC_MAX_DIM];
68
736
  int n[CCV_NNC_MAX_DIM];
69
736
  int m[CCV_NNC_MAX_DIM];
70
736
  int j[CCV_NNC_MAX_DIM];
71
736
  int c;
72
736
  const int g_nd = ccv_nnc_tensor_nd(g->info.dim);
73
736
  assert(g_nd == CCV_NNC_MAX_DIM + 1 || g_nd == CCV_NNC_MAX_DIM + 2);
74
736
  const int* gdim = (g_nd == CCV_NNC_MAX_DIM + 1) ? 
g->info.dim2
:
g->info.dim + 1734
;
75
736
  const int h_nd = ccv_nnc_tensor_nd(h->info.dim);
76
736
  assert(h_nd == CCV_NNC_MAX_DIM + 1 || h_nd == CCV_NNC_MAX_DIM + 2);
77
736
  const int* hdim = (h_nd == CCV_NNC_MAX_DIM + 1) ? 
h->info.dim2
:
h->info.dim + 1734
;
78
736
  float* gp = g->data.f32;
79
736
  const int* ginc = CCV_IS_TENSOR_VIEW(g) ? 
((g_nd == 0
CCV_NNC_MAX_DIM0
+ 1) ?
g->inc0
:
g->inc + 10
) : gdim;
80
736
  float* hp = h->data.f32;
81
736
  const int* hinc = CCV_IS_TENSOR_VIEW(h) ? 
((h_nd == 0
CCV_NNC_MAX_DIM0
+ 1) ?
h->inc0
:
h->inc + 10
) : hdim;
82
736
  ccv_nnc_tensor_zero(h);
83
4.41k
  for (i[0] = 0; i[0] < gdim[0]; 
i[0]++3.67k
)
84
3.67k
  {
85
3.67k
    SET_BORDER_OFFSET_SIZE_FOR(0, i, hint, dim, hdim, n, m);
86
24.9k
    for (i[1] = 0; i[1] < gdim[1]; 
i[1]++21.3k
)
87
21.3k
    {
88
21.3k
      SET_BORDER_OFFSET_SIZE_FOR(1, i, hint, dim, hdim, n, m);
89
808k
      for (c = 0; c < gdim[CCV_NNC_MAX_DIM]; 
c++787k
)
90
787k
      {
91
787k
        float* hpz = hp + ccv_max(i[1] * hint.stride.dim[1] - hint.border.begin[1], 0) * hinc[CCV_NNC_MAX_DIM] + c;
92
787k
        float u = gp[i[1] * ginc[CCV_NNC_MAX_DIM] + c] / (m[0] * m[1]);
93
3.14M
        for (j[0] = 0; j[0] < m[0]; 
j[0]++2.36M
)
94
2.36M
        {
95
9.44M
          for (j[1] = 0; j[1] < m[1]; 
j[1]++7.08M
)
96
7.08M
            hpz[j[1] * hinc[CCV_NNC_MAX_DIM]] += u;
97
2.36M
          hpz += hinc[CCV_NNC_MAX_DIM - 1] * hinc[CCV_NNC_MAX_DIM];
98
2.36M
        }
99
787k
      }
100
21.3k
    }
101
3.67k
    gp += ginc[CCV_NNC_MAX_DIM - 1] * ginc[CCV_NNC_MAX_DIM];
102
3.67k
    hp += hinc[CCV_NNC_MAX_DIM - 1] * hinc[CCV_NNC_MAX_DIM] * (ccv_max((i[0] + 1) * hint.stride.dim[0] - hint.border.begin[0], 0) - ccv_max(i[0] * hint.stride.dim[0] - hint.border.begin[0], 0));
103
3.67k
  }
104
736
  return CCV_NNC_EXEC_SUCCESS;
105
736
}
106
107
REGISTER_COMMAND_BACKEND(CCV_NNC_AVERAGE_POOL_FORWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
108
1
{
109
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC;
110
1
  registry->tensor_datatypes = CCV_32F;
111
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
112
1
  registry->algorithms = 1;
113
1
  registry->exec = _ccv_nnc_avg_pool_forw;
114
1
}
115
116
REGISTER_COMMAND_BACKEND(CCV_NNC_AVERAGE_POOL_BACKWARD, CCV_NNC_BACKEND_CPU_REF)(ccv_nnc_cmd_backend_registry_t* const registry)
117
1
{
118
1
  registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC;
119
1
  registry->tensor_datatypes = CCV_32F;
120
1
  registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
121
1
  registry->algorithms = 1;
122
1
  registry->exec = _ccv_nnc_avg_pool_back;
123
1
}