The task sounds simple: “In each process (except 0) you have a set of 1 to 5 numbers. Send each set to main processor with MPI_Bsend, and print those sets in main process in ascending order of the ranks of the sending processes. To get the size of sent sets use MPI_Get_count”.
However, I’m doing something wrong. The program shows an error like this for each process from 1 to 7:
[1] fatal error
Fatal error in MPI_Bsend: Invalid buffer pointer, error stack:
MPI_Bsend(buf=0x000002AE44C39290, count=2, MPI_INT, dest=0, tag=0, MPI_COMM_WORLD) failed
Insufficient space in Bsend buffer; requested 8; total buffer size is 0
Program code is right here:
#include <mpi.h>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
int rank, size;
const int TAG = 0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
srand(time(0) + rank);
if (rank == 0)
{
vector<vector<int>> data(size - 1);
for (int i = 1; i < size; i++)
{
MPI_Status status;
int count = 0;
MPI_Probe(i, TAG, MPI_COMM_WORLD, &status);
MPI_Get_count(&status, MPI_INT, &count);
data[i - 1].resize(count);
MPI_Recv(data[i - 1].data(), count, MPI_INT, i, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
for (int i = 0; i < data.size(); i++)
{
cout << "Process " << i + 1 << " sent: ";
for (int num : data[i])
cout << num << " ";
cout << endl;
}
}
else
{
int N;
vector<int> numbers;
N = rand() % 5 + 1;
numbers.resize(N);
for (int i = 0; i < N; i++)
numbers.push_back(rand() % 100);
cout << "Process " << rank << " initialized with values: ";
for (int i = 0; i < N; i++)
cout << numbers[i] << " ";
cout << endl;
MPI_Bsend(numbers.data(), N, MPI_INT, 0, TAG, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
I guess the main problem lies in the moment when I try to recv vectors with sizes of the messages that I got from the processes that do the Bsend, but have no actual clue how to fix this. Any ideas?
Vitaly Zveryaev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.