I need to divide a nested loop evenly. Now I know how to do this if i and j roughly even
mystart = (n / numproc) * myid;
if (n % numproc > myid){
mystart += myid;
myend = mystart + (n / numproc) + 1;
}else{
mystart += n % numproc;
myend = mystart + (n / numproc);
for(i=mystart; i<myend; ++i)
But I need to calculate distances between each particle so the loops look like this:
* * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
But bigger. Each * is an operation. So if I were to use that previous algorithm then last processes would finish their work while the first processes wouldn't even be halfway there, which isn't efficient.
Is there some kind of algorithm that I'd be able to use with MPI?
I thought that there may be a way to assign each iteration to the next process, like this: (i - iteration, p - process (out of 3))
i1 -> p1, i2 -> p2, i3 -> p3, i4 -> p1, i5 -> p2 (and so on)
But I'm not sure if it'd work and how to implement this. If you guys have some ideas then I'd love to see them with an example of usage in MPI.
EDIT: @Zulan It'd be easier to explain with code. My code generates random particles (x,y,z) inside a box. All the information abut those particles are known only to process rank==0. I have to implement methods (links to code below) using MPI to make those computations faster.
I have to find a pair of particles with the smallest distance betweeen them (so n*(n-1)) ops ) the remove that pair, and I have to do this NumberofParticlestoRemove times. This method is my main concern though, since I'm not sure how to divide the loop.
Here's the source code: http://pastebin.com/XxBGd50j And here's my attempt (it's unfinished) : http://pastebin.com/Yd8DwwfX
After computing the distance I figured I'd use MPI_Reduce to get the min distance, but I don't know how to tie that pair positions to the minimal distance value (there are three vectors x,y,z, so if the closest distance would be between particle ex. 5 and 10 it'd be x[5],y[5],z[5]. x[10],y[10],z[10], So even though it'd be able to extract the min distance I don't know how to save the numbers 5 and 10, to later eliminate those particles. I say eliminate but I have to create a new particle out of that pair that is positioned exactly at the middle of their disntace (function in Helper).