Skip to content

keep cufftPlan2d across ConvolveImpl::convolve calls #3386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions modules/cudaarithm/src/arithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ namespace
class ConvolutionImpl : public Convolution
{
public:
explicit ConvolutionImpl(Size user_block_size_) : user_block_size(user_block_size_) {}
explicit ConvolutionImpl(Size user_block_size_) : user_block_size(user_block_size_), planR2C(0), planC2R(0) {}
~ConvolutionImpl();

void convolve(InputArray image, InputArray templ, OutputArray result, bool ccorr = false, Stream& stream = Stream::Null());

Expand All @@ -452,6 +453,9 @@ namespace
Size user_block_size;
Size dft_size;

cufftHandle planR2C, planC2R;
Size plan_size;

GpuMat image_spect, templ_spect, result_spect;
GpuMat image_block, templ_block, result_data;
};
Expand Down Expand Up @@ -491,6 +495,27 @@ namespace
// Use maximum result matrix block size for the estimated DFT block size
block_size.width = std::min(dft_size.width - templ_size.width + 1, result_size.width);
block_size.height = std::min(dft_size.height - templ_size.height + 1, result_size.height);

if (dft_size != plan_size)
{
if (planR2C != 0)
cufftSafeCall( cufftDestroy(planR2C) );
if (planC2R != 0)
cufftSafeCall( cufftDestroy(planC2R) );

cufftSafeCall( cufftPlan2d(&planC2R, dft_size.height, dft_size.width, CUFFT_C2R) );
cufftSafeCall( cufftPlan2d(&planR2C, dft_size.height, dft_size.width, CUFFT_R2C) );

plan_size = dft_size;
}
}

ConvolutionImpl::~ConvolutionImpl()
{
if (planR2C != 0)
cufftSafeCall( cufftDestroy(planR2C) );
if (planC2R != 0)
cufftSafeCall( cufftDestroy(planC2R) );
}

Size ConvolutionImpl::estimateBlockSize(Size result_size)
Expand All @@ -516,10 +541,6 @@ namespace

cudaStream_t stream = StreamAccessor::getStream(_stream);

cufftHandle planR2C, planC2R;
cufftSafeCall( cufftPlan2d(&planC2R, dft_size.height, dft_size.width, CUFFT_C2R) );
cufftSafeCall( cufftPlan2d(&planR2C, dft_size.height, dft_size.width, CUFFT_R2C) );

cufftSafeCall( cufftSetStream(planR2C, stream) );
cufftSafeCall( cufftSetStream(planC2R, stream) );

Expand Down Expand Up @@ -559,9 +580,6 @@ namespace
}
}

cufftSafeCall( cufftDestroy(planR2C) );
cufftSafeCall( cufftDestroy(planC2R) );

syncOutput(result, _result, _stream);
}
}
Expand Down