Before any MPI commands can be run, the environment needs to be initialized, and finalized in the end:
int main(int argc, char** argv)
{
int res;
res = MPI_Init(&argc,&argv);
if (res != MPI_SUCCESS)
{
fprintf (stderr, "MPI_Init failed\n");
exit (0);
}
...
res = MPI_Finalize();
if (res != MPI_SUCCESS)
{
fprintf (stderr, "MPI_Finalize failed\n");
exit (0);
}
}