I'm trying to sort large arrays by reversal and coding with MPI on C.
Basically, my program splits the array to portions for workers and each worker finds its own increasing and decreasing strips and sends strips back to root. Root makes some reversals by finding and using max and min elements of these strips. The program ends when there is no break point left, which means the array is sorted.
It was a very long code, so I simplified for my problem:
int *ARRAY;
int main(int argc, char *argv[])
{
int p_id, n_procs, flag = 1;
MPI_Init(&argc, &argv);
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &p_id);
MPI_Comm_size(MPI_COMM_WORLD, &n_procs);
if(p_id == 0) {
ARRAY = createRandomArray(N_DATA);
// PRINT UNSORTED ARRAY
while(hasBreakPoints(ARRAY, N_DATA)) {
for(i=1;i<n_procs; i++)
// SEND PORTIONS TO WORKERS
for(i=1;i<n_procs; i++)
// RECEIVE EACH STRIP FROM WORKERS
// FIND MAX AND MIN OF STRIPS
// MAKE REVERSALS ON "ARRAY"
}
flag = 0;
// PRINT SORTED ARRAY
}
else {
while(flag == 1) {
// RECEIVE PORTION FROM ROOT
// FIND MY OWN STRIPS
// SEND MY OWN STRIPS TO ROOT
}
}
MPI_Finalize();
return 0;
}
As you can see, I need to use a while
loop to run program until no break point left. I know that the number of MPI_Send
commands have to be equal to the number of MPI_Receive
commands. So, I simply created a flag to run ROOT
and WORKERS
equal times.
By use of this lazy approach, the program is working without error but never ending and doesn't goes into MPI_Finalize
. Is there any fix on this or more efficient way to use? Thanks for help.