【发布时间】:2016-10-13 02:51:53
【问题描述】:
我正在尝试在主从之间来回发送std::list<int>。但我不确定如何创建 MPI 数据类型并将列表传递给从站。我想在不使用像boost 这样的另一个库的情况下做到这一点。我正在尝试解决这个post,但我无法使用list。下面是我正在使用的代码。
#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<int>而不是std::list<int>,这将变得微不足道。发送缓冲区为&rand_vec[0],计数为rand_vec.size()。是否特别需要使用列表?