1

I creathed in C++ method to find number's dividors. Second step was to use openmp in c++. Unfortunatelly I can't manage why my function doStuff throws memory error. Probably the problem is with threads and I check arrays before all threads stop.. Could someone help me?

There is no need to read all my program, the problem is in doStuff()

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include "omp.h"

using namespace std;

vector<int> dividors;
int NUMBER = 1000;

bool ifContains(vector<int> element, int dividedNumber)
{
    for(int i=0; i<dividors.size(); i++)
    {
        if(dividors[i] == dividedNumber)
            return true;
    }

    return false;
}

void doStuff()
{
    int sqr = (int) sqrt(NUMBER);
    int sqrp1 = sqr + 1;

    #pragma omp parallel
    {
    #pragma omp for nowait
        for (int i = 1; i < sqrp1; i++)
        {
            if (NUMBER % i == 0)
            {
                if (!ifContains(dividors, i))
                    dividors.push_back(i);

                int dividednumber = NUMBER / i;

                if (!ifContains(dividors,dividednumber))
                    dividors.push_back(dividednumber);
            }
        }


        sort(dividors.begin(), dividors.end());

        #pragma omp for nowait
        for (int i = 0; i < dividors.size(); i++)
        {
            cout << dividors[i] << "\r\n";
        }

    }
}

int main()
{
    doStuff();
    return 0;
}

Also I tried this, but It doesn't work

void doStuff()
{
    int sqr = (int) sqrt(NUMBER);
    int sqrp1 = sqr + 1;

    #pragma omp parallel
    {
        #pragma omp for
        for (int i = 1; i < sqrp1; i++)
        {

            if (NUMBER % i == 0)
            {
                if (!ifContains(dividors, i))
                    dividors.push_back(i);

                int dividednumber = NUMBER / i;

                if (!ifContains(dividors,dividednumber))
                    dividors.push_back(dividednumber);
            }
        }



        #pragma omp single
        sort(dividors.begin(), dividors.end());

        #pragma omp single
        for (int i = 0; i < dividors.size(); i++)
        {
            cout << dividors[i] << "\r\n";
        }

    }
}
4

2 回答 2

0

我在 gdb 中运行了您的代码,并且在调用dividors.push_back(...). 这似乎是一种竞争条件,原因是您dividors一次从多个线程更改向量,从这个意义上说,std::vector该类不是线程安全的。请参阅std::vector、线程安全、多线程

您要做的是确保没有线程更改向量,而另一个线程更改或读取它。这适用于在每个线程上对其进行排序。在 a 中进行#pragma omp single,它只需要排序一次,尤其是不需要同时从多个线程中排序。

于 2013-05-23T20:39:08.597 回答
0

有几种方法可以解决这个问题。最简单的就是使用ordered从句。请参阅下面的代码。但是,这消除了一些并行性。更好的方法是在并行块内声明私有除数向量(我称之为dividors_private),以便每个线程获得它自己的私有版本,然后写入dividors关键块中的向量。排序是在私有向量上并行完成的。最后的排序是dividors在一个线程中完成的,但由于大部分已经排序,所以速度很快。请参见下面的第二个代码:

带有命令的代码版本:

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include "omp.h"

using namespace std;

vector<int> dividors;
int NUMBER = 1000;

bool ifContains(vector<int> dividors, int dividedNumber)
{
    for(int i=0; i<dividors.size(); i++)
    {
        if(dividors[i] == dividedNumber)
            return true;
    }

    return false;
}

void doStuff()
{
    int sqr = (int) sqrt(NUMBER);
    int sqrp1 = sqr + 1;

    #pragma omp parallel
    {
        #pragma omp for ordered
        for (int i = 1; i < sqrp1; i++)
        {

            if (NUMBER % i == 0)
            {
                #pragma omp ordered 
                {
                    dividors.push_back(i);
                    if (!ifContains(dividors, i))
                        dividors.push_back(i);

                    int dividednumber = NUMBER / i;

                    if (!ifContains(dividors, dividednumber))
                        dividors.push_back(dividednumber);
                }
            }
        }

    }
    sort(dividors.begin(), dividors.end());  
    for (int i = 0; i < dividors.size(); i++)
    {
        cout << dividors[i] << "\r\n";
    }
}

int main()
{
    doStuff();
    return 0;
}

使用私有除数向量的代码版本

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include "omp.h"

using namespace std;

vector<int> dividors;
int NUMBER = 1000;

bool ifContains(vector<int> dividors, int dividedNumber)
{
    for(int i=0; i<dividors.size(); i++)
    {
        if(dividors[i] == dividedNumber)
            return true;
    }

    return false;
}

void doStuff()
{
    int sqr = (int) sqrt(NUMBER);
    int sqrp1 = sqr + 1;

    #pragma omp parallel
    {
        vector<int> dividors_private;
        #pragma omp for nowait
        for (int i = 1; i < sqrp1; i++)
        {

            if (NUMBER % i == 0)
            {

                dividors_private.push_back(i);
                //printf("i %d\n", i);
                if (!ifContains(dividors_private, i))
                    dividors_private.push_back(i);

                int dividednumber = NUMBER / i;

                if (!ifContains(dividors_private, dividednumber))
                    dividors_private.push_back(dividednumber);
            }
        }
        sort(dividors_private.begin(), dividors_private.end());
        #pragma omp critical
        {
            dividors.insert(dividors.end(), dividors_private.begin(), dividors_private.end());
        }
    }
    sort(dividors.begin(), dividors.end()); 
    for (int i = 0; i < dividors.size(); i++) 
    {
        cout << dividors[i] << "\r\n";
    }
}

int main()
{
    doStuff();
    return 0;
}
于 2013-05-24T10:17:14.347 回答