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

Unified · Split

libavfilter/dnn/dnn_backend_native_layer_conv2d.c+26−31
@@ -32,6 +32,7 @@ typedef struct thread_common_param{ · 31 unmodified lines
32 int32_t output_operand_index;32 int32_t output_operand_index;
33 const void *parameters;33 const void *parameters;
34 NativeContext *ctx;34 NativeContext *ctx;
35 float *output_data;
35 int thread_num;36 int thread_num;
36} thread_common_param;37} thread_common_param;
37 38
@@ -111,9 +112,7 @@ static void * dnn_execute_layer_conv2d_thread(void *threadarg) · 73 unmodified lines
111 thread_param *thread_param = (struct thread_param *)threadarg;112 thread_param *thread_param = (struct thread_param *)threadarg;
112 thread_common_param *thread_common_param = thread_param->thread_common_param;113 thread_common_param *thread_common_param = thread_param->thread_common_param;
113 DnnOperand *operands = thread_common_param->operands;114 DnnOperand *operands = thread_common_param->operands;
114 float *output;
115 int32_t input_operand_index = thread_common_param->input_operand_indexes[0];115 int32_t input_operand_index = thread_common_param->input_operand_indexes[0];
116 int number = operands[input_operand_index].dims[0];
117 int height = operands[input_operand_index].dims[1];116 int height = operands[input_operand_index].dims[1];
118 int width = operands[input_operand_index].dims[2];117 int width = operands[input_operand_index].dims[2];
119 int channel = operands[input_operand_index].dims[3];118 int channel = operands[input_operand_index].dims[3];
@@ -130,24 +129,7 @@ static void * dnn_execute_layer_conv2d_thread(void *threadarg) · 10 unmodified lines
130 int thread_start = thread_stride * thread_param->thread_index + pad_size;129 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);130 int thread_end = (thread_param->thread_index == thread_common_param->thread_num - 1) ? (height - pad_size) : (thread_start + thread_stride);
132 131
133 DnnOperand *output_operand = &operands[thread_common_param->output_operand_index];132 float *output = thread_common_param->output_data;
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;
151 output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_start - pad_size);133 output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_start - pad_size);
152 134
153 av_assert0(channel == conv_params->input_num);135 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
213 pthread_t *thread_id = av_malloc(thread_num * sizeof(pthread_t));195 pthread_t *thread_id = av_malloc(thread_num * sizeof(pthread_t));
214#endif196#endif
215 thread_param **thread_param = av_malloc(thread_num * sizeof(*thread_param));197 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;198 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;
221 thread_common_param.operands = operands;219 thread_common_param.operands = operands;
222 thread_common_param.input_operand_indexes = input_operand_indexes;220 thread_common_param.input_operand_indexes = input_operand_indexes;
223 thread_common_param.output_operand_index = output_operand_index;221 thread_common_param.output_operand_index = output_operand_index;
224 thread_common_param.parameters = parameters;222 thread_common_param.parameters = parameters;
225 thread_common_param.ctx = ctx;223 thread_common_param.ctx = ctx;
224
226#if HAVE_PTHREAD_CANCEL225#if HAVE_PTHREAD_CANCEL
227 thread_common_param.thread_num = thread_num;226 thread_common_param.thread_num = thread_num;
228 227
@@ -236,9 +235,7 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 7 unmodified lines
236 235
237 //join threads, res gets function return236 //join threads, res gets function return
238 for (int i = 0; i < thread_num; i++){237 for (int i = 0; i < thread_num; i++){
239 pthread_join(thread_id[i], &res);238 pthread_join(thread_id[i], NULL);
240 if ((int)res != DNN_SUCCESS)
241 error_flag = (int)res;
242 }239 }
243 240
244 //release memory241 //release memory
@@ -252,12 +249,10 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 7 unmodified lines
252 thread_param[0] = av_malloc(sizeof(thread_param));249 thread_param[0] = av_malloc(sizeof(thread_param));
253 thread_param[0]->thread_common_param = &thread_common_param;250 thread_param[0]->thread_common_param = &thread_common_param;
254 thread_param[0]->thread_index = 0;251 thread_param[0]->thread_index = 0;
255 res = dnn_execute_layer_conv2d_thread((void *)thread_param[0]);252 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]);253 av_free(thread_param[0]);
259#endif254#endif
260 255
261 av_free(thread_param);256 av_free(thread_param);
262 return error_flag;257 return DNN_SUCCESS;
263}258}

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