OpenMP C ++无法通过处理器数量获得线性加速

时间:2013-06-22 15:11:04

标签: c++ multithreading openmp

请参阅以下结果,让我知道在哪里可以进一步优化我的代码以获得更好的加速。

Result

使用的机器:Mac Book Pro处理器:2.5 GHz Intel Core i5(至少4个逻辑核心)
   内存:4GB 1600 MHz    编译器:Mac OSX编译器

Sequential Time:0.016466
Using two threads:0.0120111
Using four threads:0.0109911(Speed Up ~ 1.5)
Using 8 threads: 0.0111289

II机器:    操作系统:Linux    硬件:Intel(R)Core™i5-3550 CPU @ 3.30GHz×4    记忆:7.7 GiB    编译器:G ++版本4.6

Sequential Time:0.0128901
Using two threads:0.00838804
Using four threads:0.00612688(Speed up = 2)
Using 8 threads: 0.0101049

请让我知道我的代码中没有提供线性加速的开销。代码中没有任何内容。我在主函数中调用函数“findParallelUCHWOUP”,如下所示:

#pragma omp parallel for private(th_id)
for (th_id = 0; th_id < nthreads; th_id++)
    findParallelUCHWOUP(points, th_id + 1, nthreads, inp_size, first[th_id], last[th_id]);

代码:

class Point {
    double i, j;
public:
    Point() {
        i = 0;
        j = 0;
    }
    Point(double x, double y) {
        i = x;
        j = y;
    }
    double x() const {
        return i;
    }
    double y() const {
        return j;
    }
    void setValue(double x, double y) {
        i = x;
        j = y;
    }

};
typedef std::vector<Point> Vector;

int second(std::stack<int> &s);
double crossProduct(Point v[], int a, int b, int c);
bool myfunction(Point a, Point b) {
    return ((a.x() < b.x()) || (a.x() == b.x() && a.y() < b.y()));
}

class CTPoint {
    int i, j;
public:
    CTPoint() {
        i = 0;
        j = 0;
    }
    CTPoint(int x, int y) {
        i = x;
        j = y;
    }
    double getI() const {
        return i;
    }
    double getJ() const {
        return j;
    }
};

const int nthreads = 4;
const int inp_size = 1000000;
Point output[inp_size];
int numElems = inp_size / nthreads;
int sizes[nthreads];
CTPoint ct[nthreads][nthreads];


//function that is called from different threads

    int findParallelUCHWOUP(Point* iv, int id, int thread_num, int inp_size, int first, int last) {


        output[first] = iv[first];
        std::stack<int> s;
        s.push(first);
        int i = first + 1;
        while (i < last) {
            if (crossProduct(iv, i, first, last) > 0) {
                s.push(i);
                i++;
                break;
            } else {
                i++;
            }
        }

        if (i == last) {
            s.push(last);
            return 0;
        }

        for (; i <= last; i++) {
            if (crossProduct(iv, i, first, last) >= 0) {
                while (s.size() > 1 && crossProduct(iv, s.top(), second(s), i) <= 0) {
                    s.pop();
                }
                s.push(i);
            }

        }

        int count = s.size();
        sizes[id - 1] = count;
        while (!s.empty()) {
            output[first + count - 1] = iv[s.top()];
            s.pop();
            count--;
        }

        return 0;
    }

    double crossProduct(Point* v, int a, int b, int c) {

        return (v[c].x() - v[b].x()) * (v[a].y() - v[b].y())
                - (v[a].x() - v[b].x()) * (v[c].y() - v[b].y());

    }

    int second(std::stack<int> &s) {

        int temp = s.top();
        s.pop();
        int sec = s.top();
        s.push(temp);
        return sec;
    }

    //reads points from a file and divides the array of points to different threads

    int main(int argc, char *argv[]) {

    // read points from a file and assign them to the input array.
        Point *points = new Point[inp_size];
        unsigned i = 0;
        while (i < Points.size()) {
            points[i] = Points[i];
            i++;
        }



        numElems = inp_size / nthreads;
        int first[nthreads];
        int last[nthreads];
        for(int i=1;i<=nthreads;i++){
            first[i-1] = (i - 1) * numElems;
                if (i == nthreads) {
                    last[i-1] = inp_size - 1;
                } else {
                    last[i-1] = i * numElems - 1;
                }
        }

    /* Parallel Code starts here*/

        int th_id;

        omp_set_num_threads(nthreads);
        double start = omp_get_wtime();
    #pragma omp parallel for private(th_id)
        for (th_id = 0; th_id < nthreads; th_id++)
            findParallelUCHWOUP(points, th_id + 1, nthreads, inp_size, first[th_id], last[th_id]);

    /* Parallel Code ends here*/

        double end = omp_get_wtime();
        double diff = end - start;
        std::cout << "Time Elapsed in seconds:" << diff << '\n';

        return 0;
    }

1 个答案:

答案 0 :(得分:2)

一般情况下线程化,在您的特定情况下,OpenMP确实会引入一定量的开销,这实际上会阻止您获得“真正的”线性加速。你必须考虑到这一点。

其次,测试的运行时间非常短(我假设时间度量是秒?)。在那个级别,你也遇到了函数计时精度的问题,因为开销中的一小部分会对测量结果产生很大的影响。

最后,你还在处理内存访问,如果你正在处理的块和你正在创建的堆栈都不适合处理器缓存,你还必须考虑从中获取数据的开销。记忆。如果您有多个线程读取并可能写入相同的内存区域,后者会变得更糟。这将导致缓存行无效,这意味着您的内核将等待将数据提取到缓存中和/或写入主内存。

我会大规模增加数据的大小,以便您可以在几秒钟内完成运行时,对于初学者,然后再次测量。运行测试代码的时间越长越好,因为如果您进行更多处理,线程的启动和一般开销将扮演较少的角色。

一旦建立了更好的基线,您可能需要一个好的分析器,让您更深入地了解线程,以查看代码中热点的位置。您可能必须为并行化部件滚动自定义数据结构以提高性能,这并不罕见。