I have written a multithreaded C program using MPI to find palidromes in a 2D char array. I start 4 threads. Now after all threads are finished, I want to kill the threads and continue serial work? How do I achieve this? The code looks something like this:
int main(int argc, char *argv[]) {
// define some varibles
int foo;
// Kick off parallel work
MPI_Init (&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
// some work ...
}
if (rank == 1) {
// some work ...
}
if (rank == 2) {
// some work ...
}
if (rank == 3) {
// some work ...
}
MPI_Finalize();
// Here I want to print some results AFTER all threads are finished. E.g deallocate memory etc
printf("Found: %d", foo);
return 0;
}
CodePudding user response:
You're confused about how MPI works. MPI does not use threads, it uses independent processes. Each process executes the whole of the main program.* So if you want only one process to do work, you have put a condition if (i_am_working_process)
around that code.
*footnote: The standard actually allows different models, but in practice this is true.