History

dnn_backend_native_layer_conv2d.c: refine code.

Move thread area allocate out of thread function into main thread.

Xu Jun committed 6 years ago

Commit 7d3cd9f1 parent 8e67ae21 file · +14 −16Browse files

Unified · Split

libavfilter/dnn/dnn_backend_native_layer_conv2d.c+14−16
@@ -33,12 +33,11 @@ typedef struct thread_common_param{ · 32 unmodified lines
3333 const void *parameters;
3434 NativeContext *ctx;
3535 float *output_data;
36− int thread_num;
3736 } thread_common_param;
3837
3938 typedef struct thread_param{
4039 thread_common_param *thread_common_param;
41− int thread_index;
40+ int thread_start, thread_end;
4241 } thread_param;
4342
4443 int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
@@ -125,16 +124,12 @@ static void * dnn_execute_layer_conv2d_thread(void *threadarg) · 80 unmodified lines
125124 int filter_size = conv_params->kernel_size * filter_linesize;
126125 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
127126
128− int thread_stride = (height - pad_size * 2) / thread_common_param->thread_num;
129− int thread_start = thread_stride * thread_param->thread_index + pad_size;
130− int thread_end = (thread_param->thread_index == thread_common_param->thread_num - 1) ? (height - pad_size) : (thread_start + thread_stride);
131−
132127 float *output = thread_common_param->output_data;
133− output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_start - pad_size);
128+ output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_param->thread_start - pad_size);
134129
135130 av_assert0(channel == conv_params->input_num);
136131
137− for (int y = thread_start; y < thread_end; ++y) {
132+ for (int y = thread_param->thread_start; y < thread_param->thread_end; ++y) {
138133 for (int x = pad_size; x < width - pad_size; ++x) {
139134 for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
140135 if (conv_params->has_bias)
@@ -193,16 +188,19 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 52 unmodified lines
193188 ? (av_cpu_count() + 1) : (ctx->options.conv2d_threads);
194189 #if HAVE_PTHREAD_CANCEL
195190 pthread_t *thread_id = av_malloc(thread_num * sizeof(pthread_t));
191+ int thread_stride;
196192 #endif
197193 thread_param **thread_param = av_malloc(thread_num * sizeof(*thread_param));
198194 thread_common_param thread_common_param;
199195 const ConvolutionalParams *conv_params = (const ConvolutionalParams *)(parameters);
196+ int height = operands[input_operand_indexes[0]].dims[1];
197+ int width = operands[input_operand_indexes[0]].dims[2];
200198 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
201199 DnnOperand *output_operand = &operands[output_operand_index];
202200
203201 output_operand->dims[0] = operands[input_operand_indexes[0]].dims[0];
204− output_operand->dims[1] = operands[input_operand_indexes[0]].dims[1] - pad_size * 2;
205− output_operand->dims[2] = operands[input_operand_indexes[0]].dims[2] - pad_size * 2;
202+ output_operand->dims[1] = height - pad_size * 2;
203+ output_operand->dims[2] = width - pad_size * 2;
206204 output_operand->dims[3] = conv_params->output_num;
207205 output_operand->data_type = operands[input_operand_indexes[0]].data_type;
208206 output_operand->length = calculate_operand_data_length(output_operand);
@@ -223,13 +221,13 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 14 unmodified lines
223221 thread_common_param.ctx = ctx;
224222
225223 #if HAVE_PTHREAD_CANCEL
226− thread_common_param.thread_num = thread_num;
227−
224+ thread_stride = (height - pad_size * 2) / thread_num;
228225 //create threads
229226 for (int i = 0; i < thread_num; i++){
230227 thread_param[i] = av_malloc(sizeof(**thread_param));
231228 thread_param[i]->thread_common_param = &thread_common_param;
232− thread_param[i]->thread_index = i;
229+ thread_param[i]->thread_start = thread_stride * i + pad_size;
230+ thread_param[i]->thread_end = (i == thread_num - 1) ? (height - pad_size) : (thread_param[i]->thread_start + thread_stride);
233231 pthread_create(&thread_id[i], NULL, dnn_execute_layer_conv2d_thread, (void *)thread_param[i]);
234232 }
235233
@@ -245,10 +243,10 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 9 unmodified lines
245243 av_free(thread_param[i]);
246244 }
247245 #else
248− thread_common_param.thread_num = 1;
249− thread_param[0] = av_malloc(sizeof(thread_param));
246+ thread_param[0] = av_malloc(sizeof(**thread_param));
250247 thread_param[0]->thread_common_param = &thread_common_param;
251− thread_param[0]->thread_index = 0;
248+ thread_param[0]->thread_start = 0;
249+ thread_param[0]->thread_end = height - pad_size;
252250 dnn_execute_layer_conv2d_thread((void *)thread_param[0]);
253251 av_free(thread_param[0]);
254252 #endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t