Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:41:31+00:00 2026-06-17T23:41:31+00:00

While trying to speed up a simple algorithm using the GPU with OpenCV, I

  • 0

While trying to speed up a simple algorithm using the GPU with OpenCV, I noticed that on my machine (Ubuntu 12.10, NVidia 9800GT, Cuda 4.2.9, g++ 4.7.2) the GPU Version is actually slower than the CPU version. I tested with the following code.

#include <opencv2/opencv.hpp>
#include <opencv2/gpu/gpu.hpp>

#include <chrono>
#include <iostream>

int main()
{
    using namespace cv;
    using namespace std;

    Mat img1(512, 512, CV_32FC3, Scalar(0.1f, 0.2f, 0.3f));
    Mat img2(128, 128, CV_32FC3, Scalar(0.2f, 0.3f, 0.4f));
    Mat img3(128, 128, CV_32FC3, Scalar(0.3f, 0.4f, 0.5f));

    auto startCPU = chrono::high_resolution_clock::now();
    double resultCPU(0.0);
    cout << "CPU ... " << flush;
    for (int y(0); y < img2.rows; ++y)
    {
        for (int x(0); x < img2.cols; ++x)
        {
            Mat roi(img1(Rect(x, y, img2.cols, img2.rows)));
            Mat diff;
            absdiff(roi, img2, diff);
            Mat diffMult(diff.mul(img3));
            Scalar diffSum(sum(diff));
            double diffVal(diffSum[0] + diffSum[1] + diffSum[2]);
            resultCPU += diffVal;
        }
    }
    auto endCPU = chrono::high_resolution_clock::now();
    auto elapsedCPU = endCPU - startCPU;
    cout << "done. " << resultCPU << " - ticks: " << elapsedCPU.count() << endl;

    gpu::GpuMat img1GPU(img1);
    gpu::GpuMat img2GPU(img2);
    gpu::GpuMat img3GPU(img3);
    gpu::GpuMat diffGPU;
    gpu::GpuMat diffMultGPU;
    gpu::GpuMat sumBuf;

    double resultGPU(0.0);
    auto startGPU = chrono::high_resolution_clock::now();
    cout << "GPU ... " << flush;
    for (int y(0); y < img2GPU.rows; ++y)
    {
        for (int x(0); x < img2GPU.cols; ++x)
        {
            gpu::GpuMat roiGPU(img1GPU, Rect(x, y, img2GPU.cols, img2GPU.rows));
            gpu::absdiff(roiGPU, img2GPU, diffGPU);
            gpu::multiply(diffGPU, img3GPU, diffMultGPU);
            Scalar diffSum(gpu::sum(diffMultGPU, sumBuf));
            double diffVal(diffSum[0] + diffSum[1] + diffSum[2]);
            resultGPU += diffVal;
        }
    }
    auto endGPU = chrono::high_resolution_clock::now();
    auto elapsedGPU = endGPU - startGPU;
    cout << "done. " << resultGPU << " - ticks: " << elapsedGPU.count() << endl;
}

My result is as follows:

CPU ... done. 8.05306e+07 - ticks: 4028470
GPU ... done. 3.22122e+07 - ticks: 5459935

If this helps: My profiler (System Profiler 1.1.8) tells me that most of the time is spend in cudaDeviceSynchronize.

Am I doing wrong something fundamental with the way I use the OpenCV GPU functions or is my GPU just slow?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:41:32+00:00Added an answer on June 17, 2026 at 11:41 pm

    Thanks to the comments of hubs and Eric I was able to change my test in a way that the GPU version actually became faster than the CPU version. The mistake leading to the different checksums of both versions is now also eliminated. 😉

    #include <opencv2/opencv.hpp>
    #include <opencv2/gpu/gpu.hpp>
    
    #include <chrono>
    #include <iostream>
    
    int main()
    {
        using namespace cv;
        using namespace std;
    
        Mat img1(512, 512, CV_32FC3, Scalar(1.0f, 2.0f, 3.0f));
        Mat img2(128, 128, CV_32FC3, Scalar(4.0f, 5.0f, 6.0f));
        Mat img3(128, 128, CV_32FC3, Scalar(7.0f, 8.0f, 9.0f));
        Mat resultCPU(img2.rows, img2.cols, CV_32FC3, Scalar(0.0f, 0.0f, 0.0f));
    
        auto startCPU = chrono::high_resolution_clock::now();
        cout << "CPU ... " << flush;
        for (int y(0); y < img1.rows - img2.rows; ++y)
        {
            for (int x(0); x < img1.cols - img2.cols; ++x)
            {
                Mat roi(img1(Rect(x, y, img2.cols, img2.rows)));
                Mat diff;
                absdiff(roi, img2, diff);
                Mat diffMult(diff.mul(img3));
                resultCPU += diffMult;
            }
        }
        auto endCPU = chrono::high_resolution_clock::now();
        auto elapsedCPU = endCPU - startCPU;
        Scalar meanCPU(mean(resultCPU));
        cout << "done. " << meanCPU << " - ticks: " << elapsedCPU.count() << endl;
    
        gpu::GpuMat img1GPU(img1);
        gpu::GpuMat img2GPU(img2);
        gpu::GpuMat img3GPU(img3);
        gpu::GpuMat diffGPU(img2.rows, img2.cols, CV_32FC3);
        gpu::GpuMat diffMultGPU(img2.rows, img2.cols, CV_32FC3);
        gpu::GpuMat resultGPU(img2.rows, img2.cols, CV_32FC3, Scalar(0.0f, 0.0f, 0.0f));
    
        auto startGPU = chrono::high_resolution_clock::now();
        cout << "GPU ... " << flush;
        for (int y(0); y < img1GPU.rows - img2GPU.rows; ++y)
        {
            for (int x(0); x < img1GPU.cols - img2GPU.cols; ++x)
            {
                gpu::GpuMat roiGPU(img1GPU, Rect(x, y, img2GPU.cols, img2GPU.rows));
                gpu::absdiff(roiGPU, img2GPU, diffGPU);
                gpu::multiply(diffGPU, img3GPU, diffMultGPU);
                gpu::add(resultGPU, diffMultGPU, resultGPU);
            }
        }
        auto endGPU = chrono::high_resolution_clock::now();
        auto elapsedGPU = endGPU - startGPU;
        Mat downloadedResultGPU(resultGPU);
        Scalar meanGPU(mean(downloadedResultGPU));
        cout << "done. " << meanGPU << " - ticks: " << elapsedGPU.count() << endl;
    }
    

    Output:

    CPU ... done. [3.09658e+06, 3.53894e+06, 3.98131e+06, 0] - ticks: 34021332
    GPU ... done. [3.09658e+06, 3.53894e+06, 3.98131e+06, 0] - ticks: 20609880
    

    That is not the speedup I expected, but probably my GPU is just not the best for this stuff. Thanks guys.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to troubleshoot a problem I'm having with a project, I noticed that
I am trying to create a simple web crawler using PHP that is capable
While trying to find an answer to Android Jasper Reporting I found out that
While trying to verify to myself, that C# Equals for IEnumerables is a reference
I'm getting a fishy error when using glDrawElements(). I'm trying to render simple primitives
I am trying to implement a simple NSTextField with autocomplete that behaves more like
While trying to tackle a more complex problem, I came to compare access speed
While trying to create a simple game where a square is manipulated via the
While trying to build the mysql2 gem with ruby 1.9.2-p320 on Fedora 16, I
While trying to deploy an ASP.NET MVC3 locally on my IIS7 , I'm facing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.