@@ -439,7 +439,8 @@ namespace
439
439
class ConvolutionImpl : public Convolution
440
440
{
441
441
public:
442
- explicit ConvolutionImpl (Size user_block_size_) : user_block_size(user_block_size_) {}
442
+ explicit ConvolutionImpl (Size user_block_size_) : user_block_size(user_block_size_), planR2C(0 ), planC2R(0 ) {}
443
+ ~ConvolutionImpl ();
443
444
444
445
void convolve (InputArray image, InputArray templ, OutputArray result, bool ccorr = false , Stream& stream = Stream::Null());
445
446
@@ -452,6 +453,9 @@ namespace
452
453
Size user_block_size;
453
454
Size dft_size;
454
455
456
+ cufftHandle planR2C, planC2R;
457
+ Size plan_size;
458
+
455
459
GpuMat image_spect, templ_spect, result_spect;
456
460
GpuMat image_block, templ_block, result_data;
457
461
};
@@ -491,6 +495,27 @@ namespace
491
495
// Use maximum result matrix block size for the estimated DFT block size
492
496
block_size.width = std::min (dft_size.width - templ_size.width + 1 , result_size.width );
493
497
block_size.height = std::min (dft_size.height - templ_size.height + 1 , result_size.height );
498
+
499
+ if (dft_size != plan_size)
500
+ {
501
+ if (planR2C != 0 )
502
+ cufftSafeCall ( cufftDestroy (planR2C) );
503
+ if (planC2R != 0 )
504
+ cufftSafeCall ( cufftDestroy (planC2R) );
505
+
506
+ cufftSafeCall ( cufftPlan2d (&planC2R, dft_size.height , dft_size.width , CUFFT_C2R) );
507
+ cufftSafeCall ( cufftPlan2d (&planR2C, dft_size.height , dft_size.width , CUFFT_R2C) );
508
+
509
+ plan_size = dft_size;
510
+ }
511
+ }
512
+
513
+ ConvolutionImpl::~ConvolutionImpl ()
514
+ {
515
+ if (planR2C != 0 )
516
+ cufftSafeCall ( cufftDestroy (planR2C) );
517
+ if (planC2R != 0 )
518
+ cufftSafeCall ( cufftDestroy (planC2R) );
494
519
}
495
520
496
521
Size ConvolutionImpl::estimateBlockSize (Size result_size)
@@ -516,10 +541,6 @@ namespace
516
541
517
542
cudaStream_t stream = StreamAccessor::getStream (_stream);
518
543
519
- cufftHandle planR2C, planC2R;
520
- cufftSafeCall ( cufftPlan2d (&planC2R, dft_size.height , dft_size.width , CUFFT_C2R) );
521
- cufftSafeCall ( cufftPlan2d (&planR2C, dft_size.height , dft_size.width , CUFFT_R2C) );
522
-
523
544
cufftSafeCall ( cufftSetStream (planR2C, stream) );
524
545
cufftSafeCall ( cufftSetStream (planC2R, stream) );
525
546
@@ -559,9 +580,6 @@ namespace
559
580
}
560
581
}
561
582
562
- cufftSafeCall ( cufftDestroy (planR2C) );
563
- cufftSafeCall ( cufftDestroy (planC2R) );
564
-
565
583
syncOutput (result, _result, _stream);
566
584
}
567
585
}
0 commit comments