I'm trying to send a std::list<int>
back and forth between the master and slave. But I'm unsure how to create the MPI datatype and pass the list to the slave. I'd like to do this without using another library like boost
. I'm trying to work off of this post but I haven't been able to get to work with the list
. Below is the code I'm working with.
#include <iostream>
#include <list>
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(int argc, char **argv)
{
int rank, size, tag, rc, i;
MPI_Status status;
char message[20];
char new_message[20];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
tag=7;
if (rank==0) {
strcpy(message, "Hello, world");
for (int i=1; i<size; ++i){
list<int> rand_list;
list<int>::iterator it = rand_list.end();
list<int> *lt_ptr = &rand_list;
for(int j = 0; j < 5; ++j)
{
int r;
r = rand();
rand_list.push_front(r);
it++;
}
//Instead of sending the string I'd like to send the list of random ints
MPI_Send(message, 13, MPI_CHAR, i, tag, MPI_COMM_WORLD);
MPI_Recv(new_message, 13, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
cout << "master: " << new_message << endl;
}
}
else{
// slaves processes
rc = MPI_Recv(message, 13, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
cout << "node " << rank << ": " << message << endl;
strcpy(new_message, "Goodbye, world");
// code to manipulate the lists and send back to the master process
rc = MPI_Send(new_message, 13, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
}
MPI_Finalize();
}
Below are the commands I used to compile and run.
mpic++ mpi_prog.cpp -o mpi_prog.o
mpirun -np 5 mpi_prog.o