简体   繁体   English

openmp并行部分基准

[英]openmp parallel sections benchmark

I'm trying to benchmark my implementation of merge sort using openmp. 我正在尝试对我使用openmp进行合并排序的实现进行基准测试。 I have written the following code. 我写了下面的代码。

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <omp.h>
using namespace std;


class Sorter {
private:
    int* data;
    int size;
    bool isSorted;
public:
    Sorter(int* data, int size){
        this->data = data;
        this->size = size;
        this->isSorted = false;
    }

    void sort(){
        vector<int> v(data,data+size);
        vector<int> ans = merge_sort(v);
        copy(ans.begin(),ans.end(),data);
        isSorted = true;
    }
    vector<int> merge_sort(vector<int>& vec){
        if(vec.size() == 1){
            return vec;
        }
        std::vector<int>::iterator middle = vec.begin() + (vec.size() / 2);

        vector<int> left(vec.begin(), middle);
        vector<int> right(middle, vec.end());

        #pragma omp parallel sections
        { 
            #pragma omp section
            {left = merge_sort(left);}
            #pragma omp section
            {right = merge_sort(right);}

        }
        return merge(vec,left, right);
    }

    vector<int> merge(vector<int> &vec,const vector<int>& left, const vector<int>& right){
        vector<int> result;
        unsigned left_it = 0, right_it = 0;

        while(left_it < left.size() && right_it < right.size()) {
            if(left[left_it] < right[right_it]){
                result.push_back(left[left_it]);
                left_it++;
            }else{
                result.push_back(right[right_it]);
                right_it++;
            }
        }

        while(left_it < left.size()){
            result.push_back(left[left_it]);
            left_it++;
        }

        while(right_it < right.size()){
            result.push_back(right[right_it]);
            right_it++;
        }           
        return result;
    }

    int* getSortedData(){
        if(!isSorted){
            sort();
        }
        return data;
    }
};
void printArray(int* array, int size){
    for(int i=0;i<size;i++){
        cout<<array[i]<<", ";
    }
    cout<<endl;
}
bool isSorted(int* array, int size){
    for(int i=0;i<size-1;i++){
        if(array[i] > array[i+1]) {
            cout<<array[i]<<" > "<<array[i+1]<<endl;
            return false;
        }
    }
    return true;
}
int main(int argc, char** argv){
    if(argc<3){
        cout<<"Specify size and threads"<<endl;
        return -1;
    }

    int size = atoi(argv[1]);
    int threads = atoi(argv[2]);
    //omp_set_nested(1);
    omp_set_num_threads(threads);
    cout<<"Merge Sort of "<<size<<" with "<<omp_get_max_threads()<<endl;
    int *array = new int[size];
    srand(time(NULL));
    for(int i=0;i<size;i++){
        array[i] = rand() % 100;
    }
    //printArray(array,size);
    Sorter* s = new Sorter(array, size);
    cout<<"Starting sort"<<endl;
    double start = omp_get_wtime();
    s->sort();
    double stop = omp_get_wtime();
    cout<<"Time: "<<stop-start<<endl;
    int* array2 = s->getSortedData();
    if(size<=10)
        printArray(array2,size);
    cout<<"Array sorted: "<<(isSorted(array2,size)?"yes":"no")<<endl;
    return 0;
}

The program runs correctly, but when i specify the number of threads to be, say 4, the program still creates only 2 threads. 该程序可以正常运行,但是当我指定的线程数为4时,该程序仍然仅创建2个线程。 I tried using omp_set_nested(1) before omp_set_num_threads(threads) but that hands the whole terminal until the program crashes and says "libgomp: Thread creation failed: Resource temporarily unavailable" I think because too many threads are created? 我尝试在omp_set_num_threads(threads)之前使用omp_set_nested(1),但是这样会使整个终端移交给程序崩溃,并说“ libgomp:线程创建失败:资源暂时不可用”我想是因为创建了太多线程? I haven't found a work around it yet. 我还没有找到解决方法。

Edit: After the program crashes, I check the system load and it shows the load to be over 1000! 编辑:程序崩溃后,我检查系统负载,它显示负载超过1000! I have a 4-core AMD A8 CPU and 10GB RAM If I uncomment omp_set_nested(1) and run the program 我有一个4核AMD A8 CPU和10GB RAM如果取消注释omp_set_nested(1)并运行该程序

$ ./mergeSort 10000000 4 
Merge Sort of 10000000 with 4
Starting sort

libgomp: Thread creation failed: Resource temporarily unavailable
libgomp: Thread creation failed: Resource temporarily unavailable
$ uptime
 02:14:12 up 1 day, 11:13,  4 users,  load average: 482.21, 522.87, 338.75

Watching the processes, I can spot 4 threads being launched. 观察进程,我可以发现有4个线程正在启​​动。 If I comment out the omp_set_nested(1) the program runs normally but only uses 2 threads 如果我注释掉omp_set_nested(1),则程序可以正常运行,但仅使用2个线程

Edit: If i use tasks and remove omp_set_nested then it launches the threads correctly, but it doesn't speed up. 编辑:如果我使用任务并删除omp_set_nested,那么它将正确启动线程,但是不会加快速度。 Execution with 1 thread becomes faster than with 4. With sections, it speeds up. 使用1个线程执行的速度比使用4个线程执行的速度快。 but only by a factor less than two (as it launches only 2 threads at a time) 但仅小于2的因数(因为它一次仅启动2个线程)

I tested your code and it did create 4 or more threads, didn't get what you meant exactly. 我测试了您的代码,它确实创建了4个或更多线程,但并没有完全理解您的意思。 Also I suggest you to change omp section to omp task, as by definition in a section only 1 thread handles a given section and in your recursive call you would never utilize your idle threads. 我也建议您将omp部分更改为omp任务,因为根据部分的定义,只有1个线程处理给定的部分,并且在递归调用中,您永远不会利用空闲线程。

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

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