title: CUDA memory allocation performance
body: <p>I'm working with image filters on CUDA. Image processing is much faster than it is on CPU. But the problem is that the allocation of the image is really slow.</p>
  
  <p>That is how I allocate memory and set the image.</p>
  
  <pre><code>hr = cudaMalloc(&amp;m_device.originalImage,    size);                                                                          
  hr = cudaMalloc(&amp;m_device.modifiedImage,    size);                                                                          
  hr = cudaMalloc(&amp;m_device.tempImage,    size);                                                                  
  hr = cudaMemset( m_device.modifiedImage, 0, size);                                                                          
  hr = cudaMemcpy( m_device.originalImage, host.originalImage, size, cudaMemcpyHostToDevice); 
  </code></pre>
  
  <p>And here is the result of executing the program.</p>
  
  <pre><code>C:\cpu_gpu_filters(GPU)\x64\Release&gt;cpu_gpu_filters test-case.txt
  C:\Users\Max\Desktop\test_set\cheshire_cat_1280x720.jpg
  Init time: 519 ms
  Time spent: 2.35542 ms
  C:\Users\Max\Desktop\test_set\cheshire_cat_1366x768.jpg
  Init time: 31 ms
  Time spent: 2.68595 ms
  C:\Users\Max\Desktop\test_set\cheshire_cat_1600x900.jpg
  Init time: 44 ms
  Time spent: 3.54835 ms
  C:\Users\Max\Desktop\test_set\cheshire_cat_1920x1080.jpg
  Init time: 61 ms
  Time spent: 4.98131 ms
  C:\Users\Max\Desktop\test_set\cheshire_cat_2560x1440.jpg
  Init time: 107 ms
  Time spent: 9.0727 ms
  C:\Users\Max\Desktop\test_set\cheshire_cat_3840x2160.jpg
  Init time: 355 ms
  Time spent: 20.1453 ms
  C:\Users\Max\Desktop\test_set\cheshire_cat_5120x2880.jpg
  Init time: 449 ms
  Time spent: 35.815 ms
  C:\Users\Max\Desktop\test_set\cheshire_cat_7680x4320.jpg
  Init time: 908 ms
  Time spent: 75.4647 ms
  </code></pre>
  
  <p><strong>UPD</strong> Code with time measuring:</p>
  
  <pre><code>start = high_resolution_clock::now();
  Initialize();
  stop = high_resolution_clock::now();
  long long ms = duration_cast&lt;milliseconds&gt;(stop - start).count();
  long long us = duration_cast&lt;microseconds&gt;(stop - start).count();
  cout &lt;&lt; "Init time: " &lt;&lt; ms &lt;&lt; " ms" &lt;&lt; endl;
  
  
  start = high_resolution_clock::now();
  GpuTimer gpuTimer;
  gpuTimer.Start();
  RunGaussianBlurKernel(
      m_device.modifiedImage,
      m_device.tempImage,
      m_device.originalImage, 
      m_device.filter,
      m_filter.width,
      m_host.originalImage.rows, 
      m_host.originalImage.cols
      );
  gpuTimer.Stop();
  </code></pre>
  
  <p>The first image is the smallest, but initialization takes 519 ms. Maybe, it's because of the necessity to load the drivers or something. Then, when the size of the image increases, initialization time increases as well. Actually, this looks logical, but I'm still not sure that initialization process should be that slow. Am I doing something wrong? </p>