History

dnn_backend_native_layer_conv2d.c: fix memory allocation bug in multithread function.

Before patch, memory was allocated in each thread functions, which may cause more than one time of memory allocation and cause crash. After patch, memory is allocated in the main thread once, an index was parsed into thread functions. Bug fixed.

Xu Jun committed 6 years ago

Commit 8e67ae21 parent a265e661 file · +26 −31Browse files

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

Unified · Split

libavfilter/dnn/dnn_backend_native_layer_conv2d.c+26−31
@@ -32,6 +32,7 @@ typedef struct thread_common_param{ · 31 unmodified lines
3232 int32_t output_operand_index;
3333 const void *parameters;
3434 NativeContext *ctx;
35+ float *output_data;
3536 int thread_num;
3637 } thread_common_param;
3738
@@ -111,9 +112,7 @@ static void * dnn_execute_layer_conv2d_thread(void *threadarg) · 73 unmodified lines
111112 thread_param *thread_param = (struct thread_param *)threadarg;
112113 thread_common_param *thread_common_param = thread_param->thread_common_param;
113114 DnnOperand *operands = thread_common_param->operands;
114− float *output;
115115 int32_t input_operand_index = thread_common_param->input_operand_indexes[0];
116− int number = operands[input_operand_index].dims[0];
117116 int height = operands[input_operand_index].dims[1];
118117 int width = operands[input_operand_index].dims[2];
119118 int channel = operands[input_operand_index].dims[3];
@@ -130,24 +129,7 @@ static void * dnn_execute_layer_conv2d_thread(void *threadarg) · 10 unmodified lines
130129 int thread_start = thread_stride * thread_param->thread_index + pad_size;
131130 int thread_end = (thread_param->thread_index == thread_common_param->thread_num - 1) ? (height - pad_size) : (thread_start + thread_stride);
132131
133− DnnOperand *output_operand = &operands[thread_common_param->output_operand_index];
134− output_operand->dims[0] = number;
135− output_operand->dims[1] = height - pad_size * 2;
136− output_operand->dims[2] = width - pad_size * 2;
137− output_operand->dims[3] = conv_params->output_num;
138− output_operand->data_type = operands[input_operand_index].data_type;
139− output_operand->length = calculate_operand_data_length(output_operand);
140− if (output_operand->length <= 0) {
141− av_log(thread_common_param->ctx, AV_LOG_ERROR, "The output data length overflow\n");
142− return (void *)DNN_ERROR;
143− }
144− output_operand->data = av_realloc(output_operand->data, output_operand->length);
145− if (!output_operand->data) {
146− av_log(thread_common_param->ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
147− return (void *)DNN_ERROR;
148− }
149−
150− output = output_operand->data;
132+ float *output = thread_common_param->output_data;
151133 output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_start - pad_size);
152134
153135 av_assert0(channel == conv_params->input_num);
@@ -213,16 +195,33 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 59 unmodified lines
213195 pthread_t *thread_id = av_malloc(thread_num * sizeof(pthread_t));
214196 #endif
215197 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
220198 thread_common_param thread_common_param;
199+ const ConvolutionalParams *conv_params = (const ConvolutionalParams *)(parameters);
200+ 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];
202+
203+ 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;
206+ output_operand->dims[3] = conv_params->output_num;
207+ output_operand->data_type = operands[input_operand_indexes[0]].data_type;
208+ output_operand->length = calculate_operand_data_length(output_operand);
209+ if (output_operand->length <= 0) {
210+ av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
211+ return DNN_ERROR;
212+ }
213+ output_operand->data = av_realloc(output_operand->data, output_operand->length);
214+ if (!output_operand->data) {
215+ av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
216+ return DNN_ERROR;
217+ }
218+ thread_common_param.output_data = output_operand->data;
221219 thread_common_param.operands = operands;
222220 thread_common_param.input_operand_indexes = input_operand_indexes;
223221 thread_common_param.output_operand_index = output_operand_index;
224222 thread_common_param.parameters = parameters;
225223 thread_common_param.ctx = ctx;
224+
226225 #if HAVE_PTHREAD_CANCEL
227226 thread_common_param.thread_num = thread_num;
228227
@@ -236,9 +235,7 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 7 unmodified lines
236235
237236 //join threads, res gets function return
238237 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;
238+ pthread_join(thread_id[i], NULL);
242239 }
243240
244241 //release memory
@@ -252,12 +249,10 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 7 unmodified lines
252249 thread_param[0] = av_malloc(sizeof(thread_param));
253250 thread_param[0]->thread_common_param = &thread_common_param;
254251 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;
252+ dnn_execute_layer_conv2d_thread((void *)thread_param[0]);
258253 av_free(thread_param[0]);
259254 #endif
260255
261256 av_free(thread_param);
262− return error_flag;
257+ return DNN_SUCCESS;
263258 }

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