I recently used MPI to parallelize my simulation program for acceleration. The way I was adopted was to rewrite one function, which is very time consuming but easy to parallelize.
A simplified model of the program without MPI is as follows:
int main( int argc, char* argv[] ){
Some_OBJ.Serial_Function_1();
Some_OBJ.Serial_Function_2();
Some_OBJ.Serial_Function_3();
return 0;
}
While my version is MPI,
#include "mpi.h"
int main( int argc, char* argv[] ){
MPI_Init( NULL, NULL );
Some_OBJ.Serial_Function_1();
Some_OBJ.Parallel_Function_2();
Some_OBJ.Serial_Function_3();
MPI_Finalize();
return 0;
}
I copied my non-MPI code to a new folder, something like mpi_simulation, and add the mpi function by processing the main file. It works, but very inconvenient. If I update some functions, say, OBJ.Serial_Function_1 (), I need to copy the code with care, even if I just changed the constant. There are still slight differences between these software versions. I felt exhausted to keep them in line.
, , - , MPI MPI, .
.
Update
, , .
, , , MPI, :
#ifdef USE_MPI
void Some_OBJ::Parallel_Function_2(){
}
#endif
MPI , singleton MPI_plugin:
#ifdef USE_MPI
class MPI_plugin{
private:
static MPI_plugin auto_MPI;
MPI_plugin(){
MPI_Init( NULL, NULL );
}
public:
~MPI_plugin(){
MPI_Finalize();
}
};
MPI_plugin::MPI_plugin auto_MPI;
#endif
MPI_plugin.h main.cpp MPI_Init() MPI_Finalize() main.cpp MPI.
- make "mpi" PHONY:
CPP := mpic++
OTHER_FLAGS := -DUSE_MPI
.PHONY: mpi
mpi: ${MPI_TARGET}
...
, , .