Aiden Strydom
Aiden Strydom

Reputation: 1208

Where to place array creation method in an MPI program

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;
}

Upvotes: 0

Views: 48

Answers (1)

Wesley Bland
Wesley Bland

Reputation: 9062

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).

Upvotes: 2

Related Questions