简体   繁体   English

openMP直方图比较

[英]openMP histogram comparison

I am working on the code that compares image histograms, buy calculating correlation, intersection, ChiSquare and few other methods. 我正在研究比较图像直方图,购买计算相关性,交集,ChiSquare和其他一些方法的代码。 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. 通常我使用pthreads,但这次我决定使用openMP构建小型原型(由于它简单),看看我会得到什么样的结果。

This is example of comparing by correlation, code is identical to serial implementation except single line of openMP loop. 这是通过相关性进行比较的示例,除了单行openMP循环之外,代码与串行实现相同。

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 ? 有没有更多方法可以使用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. PS:我知道最快的方法就是比较多个线程中不同的直方图对,但这不适用于我的情况,因为一次只有2个直方图可用。

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. 我有一点不确定性,在较长时间运行openmp似乎比串行更好。 But if I compare it just for a single histogram and measure time in useconds, then serial is faster in about 20 times. 但是如果我只是将它与单个直方图进行比较并在使用时间内测量时间,那么串行速度大约是20倍。

I guess openmp puts some optimization once it see outside for loop. 我想openmp在看到外部循环时会进行一些优化。 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. OpenMp需要一些时间来设置并行区域。 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. 在你的情况下,这意味着只有当N达到一定数量时才会打开MP加速计算。

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? 您应该考虑减少openMP调用总数的方法,例如是否可以在此函数外部设置并行区域,以便您可以并行比较不同的直方图?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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