I need the reducing node to get a copy of a list of elements (stored in a vector) from the other nodes. I defined my own reducing function but it is not working. The program terminates/crashes.
This is the code:
#include <iostream>
#include "mpi.h"
#include <vector>
using namespace std;
void pushTheElem(vector<int>* in, vector<int>* inout, int *len, MPI_Datatype *datatype)
{
vector<int>::iterator it;
for (it = in->begin(); it < in->end(); it++)
{
inout->push_back(*it);
}
}
int main(int argc, char **argv)
{
int numOfProc, procID;
vector<int> vect, finalVect;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numOfProc);
MPI_Comm_rank(MPI_COMM_WORLD, &procID);
MPI_Op myOp;
MPI_Op_create((MPI_User_function*)pushTheElem, true, &myOp);
for (int i = 0; i < 5; i++)
{
vect.push_back(procID);
}
MPI_Reduce(&vect, &finalVect, 5, MPI_INT, myOp, 0, MPI_COMM_WORLD);
if (procID == 0)
{
vector<int>::iterator it;
cout << "Final vector elements: " << endl;
for (it = finalVect.begin(); it < finalVect.end(); it++)
cout << *it << endl;
}
MPI_Finalize();
return 0;
}