Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/cmd/util/ccv_nnc_util.c
Line
Count
Source (jump to first uncovered line)
1
#include <ccv.h>
2
#include <nnc/ccv_nnc.h>
3
#include <nnc/ccv_nnc_internal.h>
4
5
static int _ccv_nnc_set_bitmask(const int input_size, const int output_size, const uint64_t* const input_bitmasks, const int input_bitmask_size, const uint64_t* const output_bitmasks, const int output_bitmask_size)
6
1
{
7
1
  int i, j, flag = 0;
8
1
  int output_bitcount = 0;
9
2
  for (i = 0; i < output_bitmask_size; 
i++1
)
10
1
  {
11
2
    for (j = 0; j < 64; 
j++1
)
12
2
      if (output_bitmasks[i] & (uint64_t)1 << j)
13
1
      {
14
1
        if (flag)
15
0
          return 0;
16
1
      } else
17
1
        break;
18
1
    output_bitcount += j;
19
1
    // Trailing zero even if it is not the end of input_bitmask_size, mark flag,
20
1
    // if we encounter additional 1, return invalid.
21
1
    if (j < 64)
22
1
      flag = 1;
23
1
    // Always like 1111100000, no 1110010101
24
64
    for (; j < 64; 
j++63
)
25
63
      if (output_bitmasks[i] & (uint64_t)1 << j)
26
0
        return 0;
27
1
  }
28
1
  return output_size == output_bitcount;
29
1
}
30
31
REGISTER_COMMAND(CCV_NNC_SET_FORWARD)(ccv_nnc_cmd_registry_t* const registry)
32
  FIND_BACKEND(ccv_nnc_util_cpu_ref.c, gpu/ccv_nnc_util_gpu_cudnn.cu)
33
1
{
34
1
  registry->bitmask = _ccv_nnc_set_bitmask;
35
1
  registry->tensor_auto = ccv_nnc_hint_tensor_auto_forward_from_inputs;
36
1
}
37
38
REGISTER_COMMAND(CCV_NNC_SET_BACKWARD)(ccv_nnc_cmd_registry_t* const registry)
39
  FIND_BACKEND(ccv_nnc_util_cpu_ref.c, gpu/ccv_nnc_util_gpu_cudnn.cu)
40
1
{
41
1
  registry->bitmask = _ccv_nnc_set_bitmask;
42
1
  registry->tensor_auto = ccv_nnc_hint_tensor_auto_forward_from_inputs;
43
1
}
44
45
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_SET_FORWARD)
46
#define CMD_SET_FORWARD(_val) ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, CMD_BLAS(_val), 0)
47
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_SET_BACKWARD)
48
#define CMD_SET_BACKWARD(_val) ccv_nnc_cmd(CCV_NNC_SET_BACKWARD, 0, CMD_BLAS(_val), 0)
49
50
static int _ccv_nnc_data_transfer_forw_bitmask(const int input_size, const int output_size, const uint64_t* const input_bitmasks, const int input_bitmask_size, const uint64_t* const output_bitmasks, const int output_bitmask_size)
51
0
{
52
0
  int i, j;
53
0
  int input_flag = 0;
54
0
  int input_bitcount = 0;
55
0
  for (i = 0; i < input_bitmask_size; i++)
56
0
  {
57
0
    for (j = 0; j < 64; j++)
58
0
      if (input_bitmasks[i] & (uint64_t)1 << j)
59
0
      {
60
0
        if (input_flag)
61
0
          return 0;
62
0
      } else
63
0
        break;
64
0
    input_bitcount += j;
65
0
    if (j < 64)
66
0
      input_flag = 1;
67
0
    // Always like 1111100000, no 1110010101
68
0
    for (; j < 64; j++)
69
0
      if (input_bitmasks[i] & (uint64_t)1 << j)
70
0
        return 0;
71
0
  }
72
0
  int output_flag = 0;
73
0
  int output_bitcount = 0;
74
0
  for (i = 0; i < output_bitmask_size; i++)
75
0
  {
76
0
    for (j = 0; j < 64; j++)
77
0
      if (output_bitmasks[i] & (uint64_t)1 << j)
78
0
      {
79
0
        if (output_flag)
80
0
          return 0;
81
0
      } else
82
0
        break;
83
0
    output_bitcount += j;
84
0
    if (j < 64)
85
0
      output_flag = 1;
86
0
    for (; j < 64; j++)
87
0
      if (output_bitmasks[i] & (uint64_t)1 << j)
88
0
        return 0;
89
0
  }
90
0
  return output_bitcount == input_bitcount && input_size == output_size && input_size == input_bitcount;
91
0
}
92
93
static int _ccv_nnc_data_transfer_back_bitmask(const int input_size, const int output_size, const uint64_t* const input_bitmasks, const int input_bitmask_size, const uint64_t* const output_bitmasks, const int output_bitmask_size)
94
77
{
95
77
  int i, j;
96
77
  int input_flag = 0;
97
77
  int input_bitcount = 0;
98
108
  for (i = 0; i < input_bitmask_size; 
i++31
)
99
77
  {
100
212
    for (j = 0; j < 64; 
j++135
)
101
212
      if (input_bitmasks[i] & (uint64_t)1 << j)
102
135
      {
103
135
        if (input_flag)
104
0
          return 0;
105
77
      } else
106
77
        break;
107
77
    input_bitcount += j;
108
77
    if (j < 64)
109
77
      input_flag = 1;
110
77
    // Always like 1111100000, no 1110010101
111
2.03k
    for (; j < 64; 
j++1.95k
)
112
2.00k
      if (input_bitmasks[i] & (uint64_t)1 << j)
113
46
        return 0;
114
77
  }
115
77
  int output_flag = 0;
116
31
  int output_bitcount = 0;
117
62
  for (i = 0; i < output_bitmask_size; 
i++31
)
118
31
  {
119
80
    for (j = 0; j < 64; 
j++49
)
120
80
      if (output_bitmasks[i] & (uint64_t)1 << j)
121
49
      {
122
49
        if (output_flag)
123
0
          return 0;
124
31
      } else
125
31
        break;
126
31
    output_bitcount += j;
127
31
    if (j < 64)
128
31
      output_flag = 1;
129
1.96k
    for (; j < 64; 
j++1.93k
)
130
1.93k
      if (output_bitmasks[i] & (uint64_t)1 << j)
131
0
        return 0;
132
31
  }
133
31
  return output_bitcount <= input_bitcount && 
output_bitcount == output_size23
;
134
31
}
135
136
REGISTER_COMMAND(CCV_NNC_DATA_TRANSFER_FORWARD)(ccv_nnc_cmd_registry_t* const registry)
137
  FIND_BACKEND(ccv_nnc_util_cpu_ref.c, gpu/ccv_nnc_util_gpu_ref.cu)
138
1
{
139
1
  registry->bitmask = _ccv_nnc_data_transfer_forw_bitmask;
140
1
  registry->tensor_auto = ccv_nnc_hint_tensor_auto_forward_from_inputs;
141
1
}
142
143
REGISTER_COMMAND(CCV_NNC_DATA_TRANSFER_BACKWARD)(ccv_nnc_cmd_registry_t* const registry)
144
  FIND_BACKEND(ccv_nnc_util_cpu_ref.c, gpu/ccv_nnc_util_gpu_ref.cu)
145
1
{
146
1
  registry->bitmask = _ccv_nnc_data_transfer_back_bitmask;
147
1
  registry->tensor_auto = ccv_nnc_hint_tensor_auto_forward_from_inputs;
148
1
}
149
150
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_DATA_TRANSFER_FORWARD)
151
#define CMD_DATA_TRANSFER_FORWARD() ccv_nnc_cmd(CCV_NNC_DATA_TRANSFER_FORWARD, 0, ccv_nnc_cmd_auto, 0)
152
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_DATA_TRANSFER_BACKWARD)
153
#define CMD_DATA_TRANSFER_BACKWARD() ccv_nnc_cmd(CCV_NNC_DATA_TRANSFER_BACKWARD, 0, ccv_nnc_cmd_auto, 0)
154
155
REGISTER_COMMAND(CCV_NNC_FORMAT_TRANSFORM_FORWARD)(ccv_nnc_cmd_registry_t* const registry)
156
  FIND_BACKEND(ccv_nnc_util_cpu_ref.c, gpu/ccv_nnc_util_gpu_cudnn.cu)
157
1
{
158
1
  registry->bitmask = _ccv_nnc_data_transfer_forw_bitmask;
159
1
  registry->tensor_auto = ccv_nnc_hint_tensor_auto_forward_from_inputs;
160
1
}
161
162
REGISTER_COMMAND(CCV_NNC_FORMAT_TRANSFORM_BACKWARD)(ccv_nnc_cmd_registry_t* const registry)
163
  FIND_BACKEND(ccv_nnc_util_cpu_ref.c, gpu/ccv_nnc_util_gpu_cudnn.cu)
164
1
{
165
1
  registry->bitmask = _ccv_nnc_data_transfer_back_bitmask;
166
1
  registry->tensor_auto = ccv_nnc_hint_tensor_auto_forward_from_inputs;
167
1
}
168
169
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_FORMAT_TRANSFORM_FORWARD)
170
#define CMD_FORMAT_TRANSFORM_FORWARD() ccv_nnc_cmd(CCV_NNC_FORMAT_TRANSFORM_FORWARD, 0, ccv_nnc_cmd_auto, 0)
171
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_FORMAT_TRANSFORM_BACKWARD)
172
#define CMD_FORMAT_TRANSFORM_BACKWARD() ccv_nnc_cmd(CCV_NNC_FORMAT_TRANSFORM_BACKWARD, 0, ccv_nnc_cmd_auto, 0)
173
174
REGISTER_COMMAND(CCV_NNC_DATATYPE_CONVERSION_FORWARD)(ccv_nnc_cmd_registry_t* const registry)
175
  FIND_BACKEND(ccv_nnc_util_cpu_ref.c, gpu/ccv_nnc_util_gpu_ref.cu)
176
1
{
177
1
  registry->bitmask = _ccv_nnc_data_transfer_forw_bitmask;
178
1
  registry->tensor_auto = ccv_nnc_hint_tensor_auto_forward_from_inputs;
179
1
}
180
181
REGISTER_COMMAND(CCV_NNC_DATATYPE_CONVERSION_BACKWARD)(ccv_nnc_cmd_registry_t* const registry)
182
  FIND_BACKEND(ccv_nnc_util_cpu_ref.c, gpu/ccv_nnc_util_gpu_ref.cu)
183
1
{
184
1
  registry->bitmask = _ccv_nnc_data_transfer_back_bitmask;
185
1
  registry->tensor_auto = ccv_nnc_hint_tensor_auto_forward_from_inputs;
186
1
}
187
188
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_DATATYPE_CONVERSION_FORWARD)
189
#define CMD_DATATYPE_CONVERSION_FORWARD() ccv_nnc_cmd(CCV_NNC_DATATYPE_CONVERSION_FORWARD, 0, ccv_nnc_cmd_auto, 0)
190
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_DATATYPE_CONVERSION_BACKWARD)
191
#define CMD_DATATYPE_CONVERSION_BACKWARD() ccv_nnc_cmd(CCV_NNC_DATATYPE_CONVERSION_BACKWARD, 0, ccv_nnc_cmd_auto, 0)