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

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/dnn/dnn_backend_native_layer_conv2d.c+14−16
@@ -33,12 +33,11 @@ typedef struct thread_common_param{ · 32 unmodified lines
33 const void *parameters;33 const void *parameters;
34 NativeContext *ctx;34 NativeContext *ctx;
35 float *output_data;35 float *output_data;
36 int thread_num;
37} thread_common_param;36} thread_common_param;
38 37
39typedef struct thread_param{38typedef struct thread_param{
40 thread_common_param *thread_common_param;39 thread_common_param *thread_common_param;
41 int thread_index;40 int thread_start, thread_end;
42} thread_param;41} thread_param;
43 42
44int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)43int 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
125 int filter_size = conv_params->kernel_size * filter_linesize;124 int filter_size = conv_params->kernel_size * filter_linesize;
126 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;125 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
127 126
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
132 float *output = thread_common_param->output_data;127 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);
134 129
135 av_assert0(channel == conv_params->input_num);130 av_assert0(channel == conv_params->input_num);
136 131
137 for (int y = thread_start; y < thread_end; ++y) {132 for (int y = thread_param->thread_start; y < thread_param->thread_end; ++y) {
138 for (int x = pad_size; x < width - pad_size; ++x) {133 for (int x = pad_size; x < width - pad_size; ++x) {
139 for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {134 for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
140 if (conv_params->has_bias)135 if (conv_params->has_bias)
@@ -193,16 +188,19 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 52 unmodified lines
193 ? (av_cpu_count() + 1) : (ctx->options.conv2d_threads);188 ? (av_cpu_count() + 1) : (ctx->options.conv2d_threads);
194#if HAVE_PTHREAD_CANCEL189#if HAVE_PTHREAD_CANCEL
195 pthread_t *thread_id = av_malloc(thread_num * sizeof(pthread_t));190 pthread_t *thread_id = av_malloc(thread_num * sizeof(pthread_t));
191 int thread_stride;
196#endif192#endif
197 thread_param **thread_param = av_malloc(thread_num * sizeof(*thread_param));193 thread_param **thread_param = av_malloc(thread_num * sizeof(*thread_param));
198 thread_common_param thread_common_param;194 thread_common_param thread_common_param;
199 const ConvolutionalParams *conv_params = (const ConvolutionalParams *)(parameters);195 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];
200 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;198 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
201 DnnOperand *output_operand = &operands[output_operand_index];199 DnnOperand *output_operand = &operands[output_operand_index];
202 200
203 output_operand->dims[0] = operands[input_operand_indexes[0]].dims[0];201 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;202 output_operand->dims[1] = height - pad_size * 2;
205 output_operand->dims[2] = operands[input_operand_indexes[0]].dims[2] - pad_size * 2;203 output_operand->dims[2] = width - pad_size * 2;
206 output_operand->dims[3] = conv_params->output_num;204 output_operand->dims[3] = conv_params->output_num;
207 output_operand->data_type = operands[input_operand_indexes[0]].data_type;205 output_operand->data_type = operands[input_operand_indexes[0]].data_type;
208 output_operand->length = calculate_operand_data_length(output_operand);206 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
223 thread_common_param.ctx = ctx;221 thread_common_param.ctx = ctx;
224 222
225#if HAVE_PTHREAD_CANCEL223#if HAVE_PTHREAD_CANCEL
226 thread_common_param.thread_num = thread_num;224 thread_stride = (height - pad_size * 2) / thread_num;
227
228 //create threads225 //create threads
229 for (int i = 0; i < thread_num; i++){226 for (int i = 0; i < thread_num; i++){
230 thread_param[i] = av_malloc(sizeof(**thread_param));227 thread_param[i] = av_malloc(sizeof(**thread_param));
231 thread_param[i]->thread_common_param = &thread_common_param;228 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);
233 pthread_create(&thread_id[i], NULL, dnn_execute_layer_conv2d_thread, (void *)thread_param[i]);231 pthread_create(&thread_id[i], NULL, dnn_execute_layer_conv2d_thread, (void *)thread_param[i]);
234 }232 }
235 233
@@ -245,10 +243,10 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 9 unmodified lines
245 av_free(thread_param[i]);243 av_free(thread_param[i]);
246 }244 }
247#else245#else
248 thread_common_param.thread_num = 1;246 thread_param[0] = av_malloc(sizeof(**thread_param));
249 thread_param[0] = av_malloc(sizeof(thread_param));
250 thread_param[0]->thread_common_param = &thread_common_param;247 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;
252 dnn_execute_layer_conv2d_thread((void *)thread_param[0]);250 dnn_execute_layer_conv2d_thread((void *)thread_param[0]);
253 av_free(thread_param[0]);251 av_free(thread_param[0]);
254#endif252#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