3

Recently, I used MPI to parallelize my simulation program to speed up. The way I adopted was to rewrite one function that is very time-consuming but easy to be parallelized.

The simplified model of non-MPI program is as follows,

int main( int argc, char* argv[] ){
    // some declaration here
    Some_OBJ.Serial_Function_1();
    Some_OBJ.Serial_Function_2();
    Some_OBJ.Serial_Function_3(); 
    return 0;
}

While my MPI version is,

#include "mpi.h"
int main( int argc, char* argv[] ){
    // some declaration here
    MPI_Init( NULL, NULL );
    Some_OBJ.Serial_Function_1();
    Some_OBJ.Parallel_Function_2(); // I rewrite this function to replace Some_OBJ.Serial_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 a mpi function, revised the main file to . It works, but very inconveniently. If I update some functions, say OBJ.Serial_Function_1(), I need to copy the code with caution even if I just change a constant. There are still some slight differences between these versions of programs. I felt exhausted to keep them in accordance.

So I wander if there is any way to let MPI program dependent on non MPI version, so that my revisions can be easily applied to both of them safely and conveniently.

Thanks.

Update I finally adopt haraldkl's suggestion. The method is to define a macro to enclose all functions that use MPI interfaces, like this:

#ifdef USE_MPI
void Some_OBJ::Parallel_Function_2(){
  // ...
}
#endif

To initialize MPI automatically, I define a singleton called 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

Including MPI_plugin.h in main.cpp can survive me from adding MPI_Init() and MPI_Finalize() in main.cpp when compiling MPI version. The last step is to add a PHONY target "mpi" in makefile:

CPP := mpic++
OTHER_FLAGS := -DUSE_MPI
.PHONY: mpi
mpi: ${MPI_TARGET}
...

I hope it helpful to anyone who meets the same problem.

6

1 Answer 1

1

One approach to solving your problem would be to install (if it is not already installed) one of the 'dummy MPI' libraries available. So long as your code runs correctly on one MPI process (I'm sure you've written it so that it does) then it should run correctly when linked to a dummy MPI library. If you're not familiar with a dummy MPI library, Google.

3
  • That probably works, since it runs correctly with one node. However, I'm not privileged to install MPI on the machine(also a server) I used to develop my project. Usually, I upload my source code to the clusters, revised and compiled it with mpic++ and submit task by PBS.
    – anecdote
    Commented Apr 30, 2012 at 9:32
  • So you asked the system managers if there is a dummy MPI installation and they said 'no' ? Commented Apr 30, 2012 at 9:36
  • Unfortunately, yes. The manager said since they did not have a cluster to support it, it is unnecessary to install MPI library. I guess the real reason is that he's not willing to get into troubles just for the sake of my programs' compatibility.
    – anecdote
    Commented Apr 30, 2012 at 11:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.