From 251dff0dfe950cc60bfbbd8df3153b0e9b7421c8 Mon Sep 17 00:00:00 2001 From: fuzhiye Date: Wed, 26 Aug 2020 19:26:40 +0800 Subject: [PATCH] malloc using memory pool --- .../kernel/arm/fp32/convolution_3x3.cc | 18 +++++++----------- .../runtime/kernel/arm/fp32/convolution_3x3.h | 8 ++++---- .../kernel/arm/fp32/convolution_winograd.cc | 19 +++++++------------ .../kernel/arm/fp32/convolution_winograd.h | 8 ++++---- 4 files changed, 22 insertions(+), 31 deletions(-) diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_3x3.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_3x3.cc index fbc15eb739..fa750fa856 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_3x3.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_3x3.cc @@ -100,6 +100,13 @@ int Convolution3x3CPUKernel::InitTmpBuffer() { const int k_plane = 16; MS_ASSERT(ctx_->allocator != nullptr); + size_t tile_buffer_size = thread_count_ * C12NUM * C16NUM * ic4 * C4NUM * sizeof(float); + tile_buffer_ = reinterpret_cast(ctx_->allocator->Malloc(tile_buffer_size)); + if (tile_buffer_ == nullptr) { + MS_LOG(ERROR) << "malloc tile buffer failed."; + return RET_ERROR; + } + size_t block_unit_buffer_size = thread_count_ * k_plane * C4NUM * sizeof(float); block_unit_buffer_ = reinterpret_cast(ctx_->allocator->Malloc(block_unit_buffer_size)); if (block_unit_buffer_ == nullptr) { @@ -171,10 +178,6 @@ int Convolution3x3CPUKernel::ReSize() { free(nhwc4_input_); nhwc4_input_ = nullptr; } - if (tile_buffer_ != nullptr) { - free(tile_buffer_); - tile_buffer_ = nullptr; - } ret = ConvolutionBaseCPUKernel::Init(); if (ret != RET_OK) { @@ -192,13 +195,6 @@ int Convolution3x3CPUKernel::ReSize() { } memset(nhwc4_input_, 0, nhwc4_input_size); - size_t tile_buffer_size = thread_count_ * C12NUM * C16NUM * iC4 * C4NUM * sizeof(float); - tile_buffer_ = reinterpret_cast(malloc(tile_buffer_size)); - if (tile_buffer_ == nullptr) { - MS_LOG(ERROR) << "malloc tile buffer failed."; - return RET_ERROR; - } - memset(tile_buffer_, 0, tile_buffer_size); return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_3x3.h b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_3x3.h index 6c4349dda1..53003839ec 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_3x3.h +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_3x3.h @@ -33,10 +33,6 @@ class Convolution3x3CPUKernel : public ConvolutionBaseCPUKernel { if (transformed_filter_addr_ != nullptr) { free(transformed_filter_addr_); } - if (tile_buffer_ != nullptr) { - free(tile_buffer_); - tile_buffer_ = nullptr; - } } int Init() override; int ReSize() override; @@ -49,6 +45,10 @@ class Convolution3x3CPUKernel : public ConvolutionBaseCPUKernel { private: void FreeTmpBuffer() { + if (tile_buffer_ != nullptr) { + ctx_->allocator->Free(tile_buffer_); + tile_buffer_ = nullptr; + } if (block_unit_buffer_ != nullptr) { ctx_->allocator->Free(block_unit_buffer_); block_unit_buffer_ = nullptr; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_winograd.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_winograd.cc index ad5596d052..e08a3bc273 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_winograd.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_winograd.cc @@ -200,6 +200,13 @@ int ConvolutionWinogradCPUKernel::InitTmpBuffer() { int ic4 = UP_DIV(conv_param_->input_channel_, C4NUM); MS_ASSERT(ctx_->allocator != nullptr); + size_t tile_buffer_size = thread_count_ * C12NUM * input_unit_ * input_unit_ * ic4 * C4NUM * sizeof(float); + trans_input_ = reinterpret_cast(ctx_->allocator->Malloc(tile_buffer_size)); + if (trans_input_ == nullptr) { + MS_LOG(ERROR) << "malloc trans_input_ failed."; + return RET_ERROR; + } + gemm_out_ = reinterpret_cast( ctx_->allocator->Malloc(thread_count_ * C12NUM * input_unit_ * input_unit_ * oc8 * C8NUM * sizeof(float))); if (gemm_out_ == nullptr) { @@ -290,10 +297,6 @@ int ConvolutionWinogradCPUKernel::ReSize() { free(nhwc4_input_); nhwc4_input_ = nullptr; } - if (trans_input_ != nullptr) { - free(trans_input_); - trans_input_ = nullptr; - } ret = ConvolutionBaseCPUKernel::Init(); if (ret != RET_OK) { @@ -316,14 +319,6 @@ int ConvolutionWinogradCPUKernel::ReSize() { } memset(nhwc4_input_, 0, nhwc4_input_size); - size_t tile_buffer_size = thread_count_ * C12NUM * input_unit_ * input_unit_ * ic4 * C4NUM * sizeof(float); - trans_input_ = reinterpret_cast(malloc(tile_buffer_size)); - if (trans_input_ == nullptr) { - MS_LOG(ERROR) << "malloc trans_input_ failed."; - return RET_ERROR; - } - memset(trans_input_, 0, tile_buffer_size); - ret = ConfigInputOutput(); if (ret != RET_OK) { MS_LOG(ERROR) << "ConfigInputOutput failed."; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_winograd.h b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_winograd.h index 5181808457..45e6f3f7b1 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_winograd.h +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_winograd.h @@ -38,10 +38,6 @@ class ConvolutionWinogradCPUKernel : public ConvolutionBaseCPUKernel { delete trans_weight_; trans_weight_ = nullptr; } - if (trans_input_ != nullptr) { - free(trans_input_); - trans_input_ = nullptr; - } }; int Init() override; int ReSize() override; @@ -55,6 +51,10 @@ class ConvolutionWinogradCPUKernel : public ConvolutionBaseCPUKernel { private: void FreeTmpBuffer() { + if (trans_input_ != nullptr) { + ctx_->allocator->Free(trans_input_); + trans_input_ = nullptr; + } if (tmp_data_ != nullptr) { ctx_->allocator->Free(tmp_data_); tmp_data_ = nullptr;