Проблемы с MPI. C++

После использование двух и более подряд функций MPI_Bcast ломается параллельность (далее выполняет код только 0 поток). Пример кода:

#include <iostream>
#include <mpi.h> 
#include <vector>
using namespace std;

int main(int argc, char** argv)
{
    int rank, p;
    int n = 0;
    vector<int> matr;
    vector<int> vec;
    vector<int> res;
    MPI_Comm comm = MPI_COMM_WORLD;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(comm, &rank);
    MPI_Comm_size(comm, &p);
    if (rank == 0)
    {
        cin >> n;
        matr = vector<int>(n*n,2);
        vec = vector<int>(n,4);
        res = vector<int>(n,4);
    }

    MPI_Barrier(comm);

    MPI_Bcast(&n, 1, MPI_INT, 0, comm);
    MPI_Bcast(&matr, (n*n), MPI_INT, 0, comm);
    MPI_Bcast(&vec, n, MPI_INT, 0, comm);
    MPI_Bcast(&res, n, MPI_INT, 0, comm);

    cout << n;
    MPI_Finalize();
}

cout << n выполняет только 0 поток. После чего программа завершается. Если же попробовать вывести элементы какого-нибудь вектора, программа и вовсе крашится с ошибкой (см. скришнот). В чем проблема?

#include <iostream>
#include <mpi.h> 
#include <vector>
using namespace std;

int main(int argc, char** argv)
{
    int rank, p;
    int n = 0;
    vector<int> matr;
    vector<int> vec;
    vector<int> res;
    MPI_Comm comm = MPI_COMM_WORLD;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(comm, &rank);
    MPI_Comm_size(comm, &p);
    if (rank == 0)
    {
        cin >> n;
        matr = vector<int>(n*n,2);
        vec = vector<int>(n,4);
        res = vector<int>(n,4);
    }

    MPI_Barrier(comm);

    MPI_Bcast(&n, 1, MPI_INT, 0, comm);
    MPI_Bcast(&matr, (n*n), MPI_INT, 0, comm);
    MPI_Bcast(&vec, n, MPI_INT, 0, comm);
    MPI_Bcast(&res, n, MPI_INT, 0, comm);


    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << " ";

    MPI_Finalize();
}

введите сюда описание изображения


Ответы (0 шт):