Currently I'm working on a project and I want to improve the execution speed. I'm completely new to parallel coding. My program initially had six loops and I managed to optimize it to three loops and the execution time reduced 300%. From what I studied I believe that the problem is in the indexing. I used already parfor in a previous loop but there the index was i = 1:vs_max-1.
In the following code:
for i = 1:vs_max-1
for j = i+1:vs_max-1
d = max(abs(X(i,:)-X(j,:)));
DD = exp(-(d/r)^n);
D(i,j) = DD;
D(j,i) = DD;
while (i~=vs_max)
d2 = max(abs(X2(i,:)-X2(j,:)));
DD2 = exp(-(d2/r)^n);
D2(i,j) = DD2;
D2(j,i) = DD2;
break;
end
end
end
I believe the problem is in second loop indexing where j requires also the i value and when we have more than one thread we have multiple access to these values.
Can anyone help me in re-indexing this loop in order the parfor not to prompt or provide another type of code optimization in order the above code to become more faster?
Thank you in advance for your kindness reading and answering my post.