I have this code:
#pragma omp declare reduction(* : scalar : omp_out *= omp_in)
scalar like=1;
vector<scalar>& bigsum;
#pragma omp parallel for // reduction(* : like)
for (int m = 0; m < M-1; m++)
like *= bigsum[m];
I am trying to get a consistent result but it doesn't (race condition problem), but how should I fix it? as it is visible in the code I have my own reduction function
but it doesn't work either. Is there any trick for scalar and std::vector that I should be aware of?
Scalar variable in here is just overridden floating point by apply log() on each double that I has been created since there are so many double to double multiplications and the result after couple of them becomes close to zero. For example by doing the log() then multiplication becomes adding and etcetera.
One answer for consistent answer would be this:
#pragma omp parallel
{
scalar loc = 1;
#pragma omp for
for (std::size_t m = 1; m < M;m++)
{
_flm[m-1] = Qratio[m-1] * k1 + k2;
bigsum[m-1] = kappa0omegaproduct + kappa[1] * _flm[m-1];
#pragma omp critical (reduce_product)
{
like *= bigsum[m-1];
}
}
}
This answer is correct but so slow it is almost 8 times slower on my 8 core machine!