I have a problem i would like parallelize two for loops with openmp.
how to optimize this loop with openmp :
void test(float** m,tab* t,int n){
int i,j;
float gain;
for (i = 1; i < n; i++)
{
for (j = i + 1;] j < n; j++)
{
if (i != j)
{
gain=m[t[(i+n-1)%n]][t[j]] + m[t[i]][t[(j+1)%n]] - m[t[(i+n -1)%n]][t[i]] - m[t[j]][t[(j+1)%n]]
if (gain< 0)
{
swapTab(t,i,j);
}
}
}
}
}
thx.
As Oli Charlesworth points out the order of traversal of the elements in your matrices matters, so simply slapping a parallel for directive around the outermost loop won’t work.
One option you do have would be to trade some space for time. Make a copy of your array
T(call itT'). In iteration 1 you would replace your line:with something like
(I’m not much of a C++ programmer so ignore defects in the syntax.)
In the second iteration you would want first to copy
T'back toTand then carry on. DON’T do that — set up pointers (Tnew,Told, perhaps) toTandT'and switch the pointers around so thatToldalways points to the array to be read, andTnewalways points to the array to be written.