Discussion:
What is the use of MPI_Init?
(too old to reply)
Klaas Vantournhout
2007-11-01 16:14:41 UTC
Permalink
Hi all,

I am currently reading several tutorials of MPI and testing a bit with
some simple examples. But after all this reading and looking at
examples something is puzzling me which I can not figure out.

A simple program looks like this

----
#include <mpi.h>

int main(void) {

// PART 1
// some declarations here

MPI_Init(argc, argv);
// PART 2
// your parallel stuff here
MPI_Finalize();

// PART 3
// some stuff

return 0;
}
---

What I am wondering about is the following.

What is the use of PART 1 and PART 3 ?

When you write the following simple program, compile and execute it

--- test1.cpp ---
#include <iostream>
#include <mpi.h>
int main(void) {
std::cout << "foo" << std::endl;
return 0;
}
---
$ mpic++ -o test1.out test1.cpp
$ mpirun -np 4 test1.out
foo
foo
foo
foo

Why is this executed on all 4 processors? I would expect this to be
executed on only 1 processor since we did not initialize MPI yet.

So if mpirun anyway starts a program immediately on all processors doing
everything in PART 1, what is then the use of PART 1?

And as I see it, can't we just move everything from PART1 under PART2
then? And if so, what is then actually the use of MPI_Init(argc, argv)?

The first thing I have seen in the example programs is an immediate call
to MPI_Init, why do you need it then? Can't mpirun or mpiexec not just
do this Initialisation?

The same reasoning for PART3 and MPI_Finalize();

Or am I just missing something crucial here, which I guess is the case.

Thanks for the help.

Klaas
Mark Westwood
2007-11-02 11:45:33 UTC
Permalink
Hi Klaas

I think (and I'm sure others on the group will correct me) that you
have to call MPI_Init because you have to call MPI_Init, it's a
standard to which implementations ought to conform. And the same goes
for MPI_Finalize too. They are there to ensure that the run-time
system starts and finishes properly. So, if your program breaks these
rules its behaviour is undefined. You might get lucky and your
program might run OK despite breaking the rules. Move your code to
another platform, or use another MPI library, and it might just
break.

Since those of us who work in parallel computing spend a lot of time
moving codes from one machine to another and from one library to
another, we learn to stick to the standards. That way, if the code
doesn't work on a new platform we don't have to worry about non-
compliance with standards as the source of the issue -- well, not
unless we get terribly stuck.

As to your test program's behaviour, well it doesn't look like an MPI
program to me -- there are no calls to any of the MPI functions. Try
inserting a valid MPI statement into the program, then see if it (a)
compiles and (b) runs.

Regards

Mark Westwood
Post by Klaas Vantournhout
Hi all,
I am currently reading several tutorials of MPI and testing a bit with
some simple examples. But after all this reading and looking at
examples something is puzzling me which I can not figure out.
A simple program looks like this
----
#include <mpi.h>
int main(void) {
// PART 1
// some declarations here
MPI_Init(argc, argv);
// PART 2
// your parallel stuff here
MPI_Finalize();
// PART 3
// some stuff
return 0;}
---
What I am wondering about is the following.
What is the use of PART 1 and PART 3 ?
When you write the following simple program, compile and execute it
--- test1.cpp ---
#include <iostream>
#include <mpi.h>
int main(void) {
std::cout << "foo" << std::endl;
return 0;}
---
$ mpic++ -o test1.out test1.cpp
$ mpirun -np 4 test1.out
foo
foo
foo
foo
Why is this executed on all 4 processors? I would expect this to be
executed on only 1 processor since we did not initialize MPI yet.
So if mpirun anyway starts a program immediately on all processors doing
everything in PART 1, what is then the use of PART 1?
And as I see it, can't we just move everything from PART1 under PART2
then? And if so, what is then actually the use of MPI_Init(argc, argv)?
The first thing I have seen in the example programs is an immediate call
to MPI_Init, why do you need it then? Can't mpirun or mpiexec not just
do this Initialisation?
The same reasoning for PART3 and MPI_Finalize();
Or am I just missing something crucial here, which I guess is the case.
Thanks for the help.
Klaas
Michael Hofmann
2007-11-02 18:53:16 UTC
Permalink
You are right. There should be no problem in moving everything from PART1 and PART3 to PART2. But as Mark already said: the MPI standard says you have to call MPI_Init at the beginning and MPI_Finalize at the end. That's it, there is nothing to puzzle about.
Post by Klaas Vantournhout
The first thing I have seen in the example programs is an immediate call
to MPI_Init, why do you need it then? Can't mpirun or mpiexec not just
do this Initialisation?
Since MPI is just an additional bunch of library functions, it is more natural to do the initialization inside the program using some of the functions. Maybe mpirun is able to do this (on some platforms) as well. But, I guess this would involve ugly OS-depending process creation and management stuff (you have to initialize a library that resides inside the address space of the process from outside).

Additionally, (AFAIK) MPI (1.1) doesn't know anything about mpirun. And because the MPI people wanted to take care of the initialization stuff, they had to create MPI_Init and co.


Michael

Loading...