简体   繁体   中英

openMP histogram comparison

I am working on the code that compares image histograms, buy calculating correlation, intersection, ChiSquare and few other methods. General look of these functions are very similar to each other.

Usually I working with pthreads, but this time I decided to build small prototype with openMP (due to it simplicity) and see what kind of results I will get.

This is example of comparing by correlation, code is identical to serial implementation except single line of openMP loop.

double comp(CHistogram* h1, CHistogram* h2){

    double Sa = 0;
    double Sb = 0;
    double Saa = 0;
    double Sbb = 0;
    double Sab = 0;

    double a, b;
    int N = h1->length;

    #pragma omp parallel for reduction(+:Sa,Sb,Saa,Sbb,Sab) private(a ,b)
    for (int i = 0; i<N;i++){
        a =h1->data[i];
        b =h2->data[i];

        Sa+=a;
        Sb+=b;
        Saa+=a*a;
        Sbb+=b*b;
        Sab+=a*b;

    }

    double sUp = Sab - Sa*Sb / N;
    double sDown = (Saa-Sa*Sa / N)*(Sbb-Sb*Sb / N);

    return sUp / sqrt(sDown);
}

Are there more ways to speed up this function with openMP ?

Thanks!

PS: I know that fastest way would be just to compare different pairs of histograms across multiple threads, but this is not applicable to my situation since only 2 histograms are available at a time.

Tested on quad core machine 结果长度为255的直方图的垂直时间水平迭代

I have a little bit of uncertainty, on a longer run openmp seems to perform better than a serial. But if I compare it just for a single histogram and measure time in useconds, then serial is faster in about 20 times.

I guess openmp puts some optimization once it see outside for loop. But in a real solution I will have some code in between histogram comparisons, and I not sure if it will behave the same way.

OpenMp takes some time to set up the parallel region. This overhead means you need to be careful that the overhead isn't greater than the performance that is gained by setting up a parallel region. In your case this means that only when N reaches a certain number will openMP speed up the calculation.

You should think about ways to reduce the total number of openMP calls, for instance is it possible to set up a parallel region outside this function so that you compare different histograms in parallel?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM