我试图在主从之间来回发送std::list<int>
。但我不确定如何创建MPI数据类型并将列表传递给从机。我想这样做,而不使用另一个库,如boost
。我正在努力解决这个post,但我无法与list
一起工作。以下是我正在使用的代码。使用C++ OpenMPI发送和接收主从进程之间的列表
#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();
}
下面是我用来编译和运行的命令。
mpic++ mpi_prog.cpp -o mpi_prog.o
mpirun -np 5 mpi_prog.o
如果您只是使用'std :: vector'而不是'std :: list ',这就变得微不足道了。发送缓冲区是'&rand_vec [0]',计数是'rand_vec.size()'。是否有特别需要使用列表? –
Novelocrat