Reputation: 37
I'm trying to parallelize my code. The code consists on calculating the accumulative probability distribution function of mvir (counting how many object have a mass, mvir, larger than a given one). However, if I run my code with and without the parallelization I don't obtain the same results.
Here's the code:
vector<double> mvir, contador;
double mlimite;
for (i = 0; i != mvir.size() - 1; i++)
{
mlimite = mvir[i];
cuenta = 0;
#pragma omp parallel for
for (j = 0; j != mvir.size() - 1; j++)
{
if (mvir[j] >= mlimite)
{
cuenta++;
}
}
contador.push_back(cuenta);
}
Upvotes: 0
Views: 52
Reputation: 50698
This is totally normal to get wrong results with a parallel for
since the loop increment sequentially cuenta
causing a race condition. You can fix the problem using a parallel reduction:
vector<double> mvir, contador;
double mlimite;
for (i = 0; i != mvir.size() - 1; i++)
{
mlimite = mvir[i];
cuenta = 0;
#pragma omp parallel for reduction(+:cuenta)
for (j = 0; j != mvir.size() - 1; j++)
if (mvir[j] >= mlimite)
cuenta++;
contador.push_back(cuenta);
}
Note that using a parallel
section in a hot loop will probably not be efficient. It is likely much better to compute the 2D problem in parallel at once. Moreover, not that the algorithm is not very efficient too. Indeed, you can sort the values to solve this problem in O(n log(n))
time rather than O(n^2)
time (where n = mvir.size()
). Not to mention you can directly create contador
with the size mvir.size() - 1
to avoid unnecessary allocations/resizes.
Upvotes: 3