Move thread area allocate out of thread function into main thread.
Showing the changes to libavfilter (1 of 1 file). Show all
Unified · Split
| @@ -33,12 +33,11 @@ typedef struct thread_common_param{ · 32 unmodified lines | ||
| 33 | 33 | const void *parameters; |
| 34 | 34 | NativeContext *ctx; |
| 35 | 35 | float *output_data; |
| 36 | − int thread_num; | |
| 37 | 36 | } thread_common_param; |
| 38 | 37 | |
| 39 | 38 | typedef struct thread_param{ |
| 40 | 39 | thread_common_param *thread_common_param; |
| 41 | − int thread_index; | |
| 40 | + int thread_start, thread_end; | |
| 42 | 41 | } thread_param; |
| 43 | 42 | |
| 44 | 43 | int 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 | 124 | int filter_size = conv_params->kernel_size * filter_linesize; |
| 126 | 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 | 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 | 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 | 133 | for (int x = pad_size; x < width - pad_size; ++x) { |
| 139 | 134 | for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) { |
| 140 | 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 | 188 | ? (av_cpu_count() + 1) : (ctx->options.conv2d_threads); |
| 194 | 189 | #if HAVE_PTHREAD_CANCEL |
| 195 | 190 | pthread_t *thread_id = av_malloc(thread_num * sizeof(pthread_t)); |
| 191 | + int thread_stride; | |
| 196 | 192 | #endif |
| 197 | 193 | thread_param **thread_param = av_malloc(thread_num * sizeof(*thread_param)); |
| 198 | 194 | thread_common_param thread_common_param; |
| 199 | 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 | 198 | int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0; |
| 201 | 199 | DnnOperand *output_operand = &operands[output_operand_index]; |
| 202 | 200 | |
| 203 | 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; | |
| 205 | − output_operand->dims[2] = operands[input_operand_indexes[0]].dims[2] - pad_size * 2; | |
| 202 | + output_operand->dims[1] = height - pad_size * 2; | |
| 203 | + output_operand->dims[2] = width - pad_size * 2; | |
| 206 | 204 | output_operand->dims[3] = conv_params->output_num; |
| 207 | 205 | output_operand->data_type = operands[input_operand_indexes[0]].data_type; |
| 208 | 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 | 221 | thread_common_param.ctx = ctx; |
| 224 | 222 | |
| 225 | 223 | #if HAVE_PTHREAD_CANCEL |
| 226 | − thread_common_param.thread_num = thread_num; | |
| 227 | − | |
| 224 | + thread_stride = (height - pad_size * 2) / thread_num; | |
| 228 | 225 | //create threads |
| 229 | 226 | for (int i = 0; i < thread_num; i++){ |
| 230 | 227 | thread_param[i] = av_malloc(sizeof(**thread_param)); |
| 231 | 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 | 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 | 243 | av_free(thread_param[i]); |
| 246 | 244 | } |
| 247 | 245 | #else |
| 248 | − thread_common_param.thread_num = 1; | |
| 249 | − thread_param[0] = av_malloc(sizeof(thread_param)); | |
| 246 | + thread_param[0] = av_malloc(sizeof(**thread_param)); | |
| 250 | 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 | 250 | dnn_execute_layer_conv2d_thread((void *)thread_param[0]); |
| 253 | 251 | av_free(thread_param[0]); |
| 254 | 252 | #endif |