0

I want to use MPI to parallelize a function that is being called multiple times in my code. What i wanted to know was that if I use MPI_Init inside the function, will it be spawn the processes every time the function is called or will the spawning take place only once? Is there some known design pattern to do this in a systematic way?

2
  • Retagged with mpi, since this is really a question about MPI in general and not really specific to MPICH2 Commented Feb 11, 2011 at 13:23
  • 1
    Based on your description, you probably want to consider using OpenMP instead of MPI. Commented Feb 11, 2011 at 14:14

2 Answers 2

2

The MPI_Init() call just initialises the MPI enviroment, it doesn't do any parallelisation itself. The parallelism comes from how you write the program.

A parallel "Hello, World", the printf() does different things depending on what rank (processor) it's running on. The number of processes is determined by how you execute the program (e.g. the number of processes is set via the -n parameter to mpiexec or mpirun)

int main(int argc, char *argv[]) {

        char name[BUFSIZ];
        int length=BUFSIZ;
        int rank;
        int numprocesses;

        MPI_Init(&argc, &argv);    
        MPI_Comm_size(MPI_COMM_WORLD, &numprocesses);
        MPI_Comm_rank( MPI_COMM_WORLD, &rank );
        MPI_Get_processor_name(name, &length);

        printf("%s, Rank %d of %d: hello world\n", name, rank, numprocesses);

        MPI_Finalize();

        return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

2

That's not really the way MPI (or distributed memory programming) works; you can't really just parallelize a function the way you can with something like OpenMP. At MPI, processes aren't spawned at the time of MPI_Init(), but at the time of running the executable (eg, with mpiexec; this is true even with MPI_Comm_spawn(). Part of the reason for that is that in distributed memory computing, launching processes on potentially a large number of shared-nothing nodes is a very expensive task.

You could cobble something together by having the function you're calling be in a separate executable, but I'm not sure that's what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.