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";
}
}
}