History

dnn_backend_native_layer_conv2d.c:Add mutithread function

Use pthread to multithread dnn_execute_layer_conv2d. Can be tested with command "./ffmpeg_g -i input.png -vf \ format=yuvj420p,dnn_processing=dnn_backend=native:model= \ espcn.model:input=x:output=y:options=conv2d_threads=23 \ -y sr_native.jpg -benchmark" before patch: utime=11.238s stime=0.005s rtime=11.248s after patch: utime=20.817s stime=0.047s rtime=1.051s on my 3900X 12c24t @4.2GHz About the increase of utime, it's because that CPU HyperThreading technology makes logical cores twice of physical cores while cpu's counting performance improves less than double. And utime sums all cpu's logical cores' runtime. As a result, using threads num near cpu's logical core's number will double utime, while reduce rtime less than half for HyperThreading CPUs.

Xu Jun committed 6 years ago

Commit 3c7cad61 parent 235e01f2 files · +108 −13Browse files

Unified · Split

2 files
  1. libavfilter/dnn/dnn_backend_native_layer_conv2d.c +96 −11
  2. tests/dnn/dnn-layer-conv2d-test.c +12 −2
libavfilter/dnn/dnn_backend_native_layer_conv2d.c+96−11
@@ -19,10 +19,27 @@ · 18 unmodified lines
1919 */
2020
2121 #include "libavutil/avassert.h"
22+#include "libavutil/thread.h"
23+#include "libavutil/cpu.h"
2224 #include "dnn_backend_native_layer_conv2d.h"
2325
2426 #define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
2527
28+//struct to pass parameters
29+typedef struct thread_common_param{
30+ DnnOperand *operands;
31+ const int32_t *input_operand_indexes;
32+ int32_t output_operand_index;
33+ const void *parameters;
34+ NativeContext *ctx;
35+ int thread_num;
36+} thread_common_param;
37+
38+typedef struct thread_param{
39+ thread_common_param *thread_common_param;
40+ int thread_index;
41+} thread_param;
42+
2643 int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
2744 {
2845 ConvolutionalParams *conv_params;
@@ -88,17 +105,20 @@ int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int fil · 59 unmodified lines
88105 return dnn_size;
89106 }
90107
91−int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes,
92− int32_t output_operand_index, const void *parameters, NativeContext *ctx)
108+static void * dnn_execute_layer_conv2d_thread(void *threadarg)
93109 {
110+ //pass parameters
111+ thread_param *thread_param = (struct thread_param *)threadarg;
112+ thread_common_param *thread_common_param = thread_param->thread_common_param;
113+ DnnOperand *operands = thread_common_param->operands;
94114 float *output;
95− int32_t input_operand_index = input_operand_indexes[0];
115+ int32_t input_operand_index = thread_common_param->input_operand_indexes[0];
96116 int number = operands[input_operand_index].dims[0];
97117 int height = operands[input_operand_index].dims[1];
98118 int width = operands[input_operand_index].dims[2];
99119 int channel = operands[input_operand_index].dims[3];
100120 const float *input = operands[input_operand_index].data;
101− const ConvolutionalParams *conv_params = (const ConvolutionalParams *)parameters;
121+ const ConvolutionalParams *conv_params = (const ConvolutionalParams *)(thread_common_param->parameters);
102122
103123 int radius = conv_params->kernel_size >> 1;
104124 int src_linesize = width * conv_params->input_num;
@@ -106,7 +126,11 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 1 unmodified line
106126 int filter_size = conv_params->kernel_size * filter_linesize;
107127 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
108128
109− DnnOperand *output_operand = &operands[output_operand_index];
129+ int thread_stride = (height - pad_size * 2) / thread_common_param->thread_num;
130+ int thread_start = thread_stride * thread_param->thread_index + pad_size;
131+ int thread_end = (thread_param->thread_index == thread_common_param->thread_num - 1) ? (height - pad_size) : (thread_start + thread_stride);
132+
133+ DnnOperand *output_operand = &operands[thread_common_param->output_operand_index];
110134 output_operand->dims[0] = number;
111135 output_operand->dims[1] = height - pad_size * 2;
112136 output_operand->dims[2] = width - pad_size * 2;
@@ -114,19 +138,21 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 1 unmodified line
114138 output_operand->data_type = operands[input_operand_index].data_type;
115139 output_operand->length = calculate_operand_data_length(output_operand);
116140 if (output_operand->length <= 0) {
117− av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
118− return DNN_ERROR;
141+ av_log(thread_common_param->ctx, AV_LOG_ERROR, "The output data length overflow\n");
142+ return (void *)DNN_ERROR;
119143 }
120144 output_operand->data = av_realloc(output_operand->data, output_operand->length);
121145 if (!output_operand->data) {
122− av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
123− return DNN_ERROR;
146+ av_log(thread_common_param->ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
147+ return (void *)DNN_ERROR;
124148 }
149+
125150 output = output_operand->data;
151+ output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_start - pad_size);
126152
127153 av_assert0(channel == conv_params->input_num);
128154
129− for (int y = pad_size; y < height - pad_size; ++y) {
155+ for (int y = thread_start; y < thread_end; ++y) {
130156 for (int x = pad_size; x < width - pad_size; ++x) {
131157 for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
132158 if (conv_params->has_bias)
@@ -174,5 +200,64 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 41 unmodified lines
174200 output += conv_params->output_num;
175201 }
176202 }
177− return 0;
203+ return (void *)DNN_SUCCESS;
204+}
205+
206+
207+int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes,
208+ int32_t output_operand_index, const void *parameters, NativeContext *ctx)
209+{
210+ int thread_num = (ctx->options.conv2d_threads <= 0 || ctx->options.conv2d_threads > av_cpu_count())
211+ ? (av_cpu_count() + 1) : (ctx->options.conv2d_threads);
212+#if HAVE_PTHREAD_CANCEL
213+ pthread_t *thread_id = av_malloc(thread_num * sizeof(pthread_t));
214+#endif
215+ thread_param **thread_param = av_malloc(thread_num * sizeof(*thread_param));
216+ void *res;
217+ int error_flag = DNN_SUCCESS;
218+
219+ //struct used to pass parameters
220+ thread_common_param thread_common_param;
221+ thread_common_param.operands = operands;
222+ thread_common_param.input_operand_indexes = input_operand_indexes;
223+ thread_common_param.output_operand_index = output_operand_index;
224+ thread_common_param.parameters = parameters;
225+ thread_common_param.ctx = ctx;
226+#if HAVE_PTHREAD_CANCEL
227+ thread_common_param.thread_num = thread_num;
228+
229+ //create threads
230+ for (int i = 0; i < thread_num; i++){
231+ thread_param[i] = av_malloc(sizeof(thread_param));
232+ thread_param[i]->thread_common_param = &thread_common_param;
233+ thread_param[i]->thread_index = i;
234+ pthread_create(&thread_id[i], NULL, dnn_execute_layer_conv2d_thread, (void *)thread_param[i]);
235+ }
236+
237+ //join threads, res gets function return
238+ for (int i = 0; i < thread_num; i++){
239+ pthread_join(thread_id[i], &res);
240+ if ((int)res != DNN_SUCCESS)
241+ error_flag = (int)res;
242+ }
243+
244+ //release memory
245+ av_free(thread_id);
246+
247+ for (int i = 0; i < thread_num; i++){
248+ av_free(thread_param[i]);
249+ }
250+#else
251+ thread_common_param.thread_num = 1;
252+ thread_param[0] = av_malloc(sizeof(thread_param));
253+ thread_param[0]->thread_common_param = &thread_common_param;
254+ thread_param[0]->thread_index = 0;
255+ res = dnn_execute_layer_conv2d_thread((void *)thread_param[0]);
256+ if ((int)res != DNN_SUCCESS)
257+ error_flag = (int)res;
258+ av_free(thread_param[0]);
259+#endif
260+
261+ av_free(thread_param);
262+ return error_flag;
178263 }
tests/dnn/dnn-layer-conv2d-test.c+12−2
@@ -25,6 +25,8 @@ · 24 unmodified lines
2525
2626 #define EPSON 0.00001
2727
28+extern const AVClass dnn_native_class;
29+
2830 static int test_with_same_dilate(void)
2931 {
3032 // the input data and expected data are generated with below python code.
@@ -96,6 +98,10 @@ static int test_with_same_dilate(void) · 65 unmodified lines
9698 };
9799 float bias[2] = { -1.6574852, -0.72915393 };
98100
101+ NativeContext ctx;
102+ ctx.class = &dnn_native_class;
103+ ctx.options.conv2d_threads = 1;
104+
99105 params.activation = TANH;
100106 params.has_bias = 1;
101107 params.biases = bias;
@@ -114,7 +120,7 @@ static int test_with_same_dilate(void) · 12 unmodified lines
114120 operands[1].data = NULL;
115121
116122 input_indexes[0] = 0;
117− dnn_execute_layer_conv2d(operands, input_indexes, 1, &params, NULL);
123+ dnn_execute_layer_conv2d(operands, input_indexes, 1, &params, &ctx);
118124
119125 output = operands[1].data;
120126 for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
@@ -196,6 +202,10 @@ static int test_with_valid(void) · 75 unmodified lines
196202 };
197203 float bias[2] = { -0.4773722, -0.19620377 };
198204
205+ NativeContext ctx;
206+ ctx.class = &dnn_native_class;
207+ ctx.options.conv2d_threads = 1;
208+
199209 params.activation = TANH;
200210 params.has_bias = 1;
201211 params.biases = bias;
@@ -214,7 +224,7 @@ static int test_with_valid(void) · 12 unmodified lines
214224 operands[1].data = NULL;
215225
216226 input_indexes[0] = 0;
217− dnn_execute_layer_conv2d(operands, input_indexes, 1, &params, NULL);
227+ dnn_execute_layer_conv2d(operands, input_indexes, 1, &params, &ctx);
218228
219229 output = operands[1].data;
220230 for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {

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