Question

I heard that all code in an MPI program should be decared between MPI_Init and MPI_Finalize. So what is the impact of the following difference in the beow MPI programs.

int main(int argc, char** argv)
{
    MPI_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    int** array = createArray(START_VAL, ARRAY_SIZE);

    printArray(array, ARRAY_SIZE);

    MPI_Finalize();

    return 0;
}

And...

int main(int argc, char** argv)
{
    int** array = createArray(START_VAL, ARRAY_SIZE);

    MPI_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    printArray(array, ARRAY_SIZE);

    MPI_Finalize();

    return 0;
}
Was it helpful?

Solution

Either one is fine. The only thing mandated by the MPI Standard is that you can't use any other MPI functions before you call MPI_INIT or after you call MPI_FINALIZE. You can do other things before and after those calls, but you can't use MPI.

There's an entire section about this in the MPI Standard (Section 8.7, older HTML version available here).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top