Both of reduction and collapse clauses in OMP confuses me, some points raised popped into my head
- Why reduction doesn't work with minus? as in the limitation listed here
- Is there any work around to achieve minus?
- How does a unary operator work, i.e. x++ or x--? is the -- or ++ applied to each partial result? or only once at the creation of the global result? both cases are totally different.
About the collapse.. could we apply collapse on a nested loops but have some lines of code in between for example
for (int i = 0; i < 4; i++)
{
cout << "Hi"; //This is an extra line. which breaks the 2 loops.
for (int j = 0; j < 100; j++)
{
cout << "*";
}
}
The reduction clause requires that the operation is associative and the
x = a[i] - x
operation inis not associative. Try a few iterations.
But
x = x - a[i]
does work e.g.However there is a workaround. The sign alternates every other term. Here is a working solution.
Here is a simpler version which uses the
reduction
clause. The thing to notice is that the odd terms are all one sign and the even terms another. So if we do the reduction two terms at a time the sign does not change and the operation is associative.can be reduced in parallel like this.
1 & 2. For minus, what are you subtracting from? If you have two threads, do you do
result_thread_1 - result_thread_2
, orresult_thread_2 - result_thread_1
? If you have more than 2 threads, then it gets even more confusing: Do I only have one negative term and all others are positive? Is there only one positive term and others are negative? Is it a mix? Which results are which? As such, no, there is no workaround.In the event of
x++
orx--
, assuming that they are within the reduction loop, they should happen to each partial result.Yes, I believe so.