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

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

Unified · Split

libavfilter/dnn/dnn_backend_native_layer_conv2d.c+96−11
@@ -19,10 +19,27 @@ · 18 unmodified lines
19 */19 */
20 20
21#include "libavutil/avassert.h"21#include "libavutil/avassert.h"
22#include "libavutil/thread.h"
23#include "libavutil/cpu.h"
22#include "dnn_backend_native_layer_conv2d.h"24#include "dnn_backend_native_layer_conv2d.h"
23 25
24#define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))26#define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
25 27
28//struct to pass parameters
29typedef 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
38typedef struct thread_param{
39 thread_common_param *thread_common_param;
40 int thread_index;
41} thread_param;
42
26int 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)
27{44{
28 ConvolutionalParams *conv_params;45 ConvolutionalParams *conv_params;
@@ -88,17 +105,20 @@ int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int fil · 59 unmodified lines
88 return dnn_size;105 return dnn_size;
89}106}
90 107
91int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes,108static void * dnn_execute_layer_conv2d_thread(void *threadarg)
92 int32_t output_operand_index, const void *parameters, NativeContext *ctx)
93{109{
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;
94 float *output;114 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];
96 int number = operands[input_operand_index].dims[0];116 int number = operands[input_operand_index].dims[0];
97 int height = operands[input_operand_index].dims[1];117 int height = operands[input_operand_index].dims[1];
98 int width = operands[input_operand_index].dims[2];118 int width = operands[input_operand_index].dims[2];
99 int channel = operands[input_operand_index].dims[3];119 int channel = operands[input_operand_index].dims[3];
100 const float *input = operands[input_operand_index].data;120 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);
102 122
103 int radius = conv_params->kernel_size >> 1;123 int radius = conv_params->kernel_size >> 1;
104 int src_linesize = width * conv_params->input_num;124 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
106 int filter_size = conv_params->kernel_size * filter_linesize;126 int filter_size = conv_params->kernel_size * filter_linesize;
107 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;127 int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
108 128
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];
110 output_operand->dims[0] = number;134 output_operand->dims[0] = number;
111 output_operand->dims[1] = height - pad_size * 2;135 output_operand->dims[1] = height - pad_size * 2;
112 output_operand->dims[2] = width - pad_size * 2;136 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
114 output_operand->data_type = operands[input_operand_index].data_type;138 output_operand->data_type = operands[input_operand_index].data_type;
115 output_operand->length = calculate_operand_data_length(output_operand);139 output_operand->length = calculate_operand_data_length(output_operand);
116 if (output_operand->length <= 0) {140 if (output_operand->length <= 0) {
117 av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");141 av_log(thread_common_param->ctx, AV_LOG_ERROR, "The output data length overflow\n");
118 return DNN_ERROR;142 return (void *)DNN_ERROR;
119 }143 }
120 output_operand->data = av_realloc(output_operand->data, output_operand->length);144 output_operand->data = av_realloc(output_operand->data, output_operand->length);
121 if (!output_operand->data) {145 if (!output_operand->data) {
122 av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");146 av_log(thread_common_param->ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
123 return DNN_ERROR;147 return (void *)DNN_ERROR;
124 }148 }
149
125 output = output_operand->data;150 output = output_operand->data;
151 output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_start - pad_size);
126 152
127 av_assert0(channel == conv_params->input_num);153 av_assert0(channel == conv_params->input_num);
128 154
129 for (int y = pad_size; y < height - pad_size; ++y) {155 for (int y = thread_start; y < thread_end; ++y) {
130 for (int x = pad_size; x < width - pad_size; ++x) {156 for (int x = pad_size; x < width - pad_size; ++x) {
131 for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {157 for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
132 if (conv_params->has_bias)158 if (conv_params->has_bias)
@@ -174,5 +200,64 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 41 unmodified lines
174 output += conv_params->output_num;200 output += conv_params->output_num;
175 }201 }
176 }202 }
177 return 0;203 return (void *)DNN_SUCCESS;
204}
205
206
207int 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;
178}263}

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