title: OPENCV : CUDA context initialization for different methods
body: <p>I'm working on a simple c++ program to evaluate the performance of some Opencv GPU methods (cv::cuda). 
  I am using Opencv 3.1 on Ubuntu 15 (with CUDA 7.5) with a GeForce 770. </p>
  
  <p>I previously read that we need to initialize CUDA environment to avoid slow process at first call. So, I initialize my program with a cv::cuda::getDevice() and setDevice(). </p>
  
  <p>Then, I test 2 methods: </p>
  
  <ul>
  <li>cv::cuda::resize() (factor 0.5) </li>
  <li>and cv::cuda::meanStdDev.</li>
  </ul>
  
  <p>Initialization takes 400ms. Then, resizing takes 2 or 3 ms, that's OK. 
  But... meanStdDev takes 476ms! 
  If I run 2 successive meanStdDev, the second one is much faster (3ms).</p>
  
  <p>I really don't understand why the initialization has an effect on resize() but not on meanStdDev().</p>
  
  <p>I compile OPENCV with -DCUDA_ARCH_BIN=3.0. I try with -DCUDA_ARCH_PTX="" but the problem is still the same. </p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;opencv2/cudaimgproc.hpp&gt;
  #include "opencv2/cudawarping.hpp"
  #include "opencv2/cudaarithm.hpp"
  
  using namespace std;
  
  int main(int argc, char *argv[]) 
  {
  
      double t_init_cuda = (double)cv::getTickCount();
      int CudaDevice;
      if(cv::cuda::getCudaEnabledDeviceCount()==0)
      {
          cerr&lt;&lt;endl&lt;&lt;"ERROR: NO CudaEnabledDevice"&lt;&lt;endl;
          exit(2);
      }
      else
      {
          CudaDevice = cv::cuda::getDevice();
          cv::cuda::setDevice(CudaDevice);
      }
      t_init_cuda = ((double)cv::getTickCount() - t_init_cuda)/cv::getTickFrequency() * 1000;
      cout&lt;&lt;endl&lt;&lt;"\t*T_INIT_CUDA="&lt;&lt;t_init_cuda&lt;&lt;"ms\n";;
  
      cv::Mat src = cv::imread(argv[1], 0);
      if (!src.data) exit(1);
      cv::cuda::GpuMat d_src(src);
  
  
      //CV::CUDA::RESIZE
      cv::cuda::GpuMat d_dst;
      double factor = 0.5;
      double t_gpu_resize = cv::getTickCount();
      cv::cuda::resize(d_src, d_dst, cv::Size( (int) ((float) (d_src.cols)*factor) , (int) ((float) (d_src.rows)*factor)), 0, 0, CV_INTER_AREA);
      t_gpu_resize = ((double)cv::getTickCount() - t_gpu_resize)/cv::getTickFrequency() * 1000;
      cout&lt;&lt;endl&lt;&lt;"D_SRC="&lt;&lt;d_src.rows&lt;&lt;"x"&lt;&lt;d_src.cols&lt;&lt;" =&gt; D_DST="&lt;&lt;d_dst.rows&lt;&lt;"x"&lt;&lt;d_dst.cols&lt;&lt;endl;
      cout&lt;&lt;endl&lt;&lt;"\t*T_GPU_RESIZE="&lt;&lt;t_gpu_resize&lt;&lt;"ms\n";;
  
      //CV::CUDA::MEANSTDDEV
      double t_meanstddev = (double)cv::getTickCount();
      cv::Scalar mean, stddev;
      std::vector&lt;cv::cuda::GpuMat&gt; d_src_split;
      cv::cuda::split(d_src, d_src_split);
      cv::cuda::meanStdDev (d_src_split[0], mean, stddev); 
      t_meanstddev = ((double)cv::getTickCount() - t_meanstddev)/cv::getTickFrequency() * 1000.0;
      cout&lt;&lt;endl&lt;&lt;"mean="&lt;&lt;mean.val[0]&lt;&lt;" | stddev="&lt;&lt;stddev.val[0]&lt;&lt;endl;    
      cout&lt;&lt;endl&lt;&lt;"\t*T_GPU_MEANSTDDEV="&lt;&lt;t_meanstddev&lt;&lt;"ms\n";
  
      return 0;
  }
  </code></pre>