1

Why the Bcast function works with my array incorrectly? I did according to the following example (only there is not an array, but a number).

#include <iostream>
#include <mpi.h>
#include <vector>

void test(int rnk) {
    int n = 5;

    vector<int> array;
    array.reserve(n);

    if (rnk == 0) {
        for (int i = 0; i < n; i++) {
            array[i] = i;
        }
    }

    cout << "Process " << rnk << " before Bcast:" << endl;
    for (auto x: array) {
        cout << x << " ";
    }
    cout << endl;

    MPI_Bcast(&array[0], n, MPI_INT, 0, MPI_COMM_WORLD);

    cout << "Process " << rnk << " after Bcast:" << endl;
    for (auto x: array) {
        cout << x << " ";
    }
    cout << endl;
}


int main(int argc, char **argv) {
    int num, rank;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &num);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    test(rank);

    MPI_Finalize();
    return 0;
}

Result - empty rows without data:

Process 2 before Bcast:

Process 0 before Bcast:

Process 0 after Bcast:

Process 2 after Bcast:

Process 1 before Bcast:

Process 1 after Bcast:

Process 3 before Bcast:

Process 3 after Bcast:

And what if not one array, but for example 3? Will there be as much MPI_Bcast? Need MPI_Barrier then?

Samuel
  • 19
  • 5

0 Answers0