2016-07-26 177 views
1

我想使用MPI在机器之间发送矩阵。以下是我的测试代码如何使用MPI将Eigen :: MatrixXd中的数据发送给我

#include <iostream> 
#include <Eigen/Dense> 
#include <mpi.h> 
using std::cin; 
using std::cout; 
using std::endl; 
using namespace Eigen; 

int main(int argc, char ** argv) 
{ 
    MatrixXd a = MatrixXd::Ones(3, 4); 
    int myrank; 
    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank); 
    MPI_Status status; 
    if (0 == myrank) 
    { 
     MPI_Send(&a, 96, MPI_BYTE, 1, 99, MPI_COMM_WORLD); 
    } 
    else if (1 == myrank) 
    { 
     MPI_Recv(&a, 96, MPI_BYTE, 0, 99, MPI_COMM_WORLD, &status); 
     cout << "RANK " << myrank << endl; 
     cout << a << endl; 
    } 
    MPI_Finalize(); 
    return 0; 
} 

它编译成功,没有错误,但是当我启动它时,它返回以下错误。

$ MPI mpiexec -n 2 ./sendMatrixTest 
RANK 1 
[HPNotebook:11633] *** Process received signal *** 
[HPNotebook:11633] Signal: Segmentation fault (11) 
[HPNotebook:11633] Signal code: Address not mapped (1) 
[HPNotebook:11633] Failing at address: 0xf4ba40 
-------------------------------------------------------------------------- 
mpiexec noticed that process rank 1 with PID 11633 on node HPNotebook exited on signal 11 (Segmentation fault). 
-------------------------------------------------------------------------- 

我该如何解决?谢谢!

+0

序列化的矩阵,并将其发送? –

+2

而不是像'int'那样简单地发送一个'eigen'矩阵并开始工作。一个'eigen'矩阵是一个像'std :: vector'一样的容器。随之而来的是大量的信息。这里是一个例子http://stackoverflow.com/questions/36021305/mpi-send-struct-with-a-vector-property-in-c – Matt

回答

3

正如@Matt所指出的,MatrixXd容器中的数据不仅仅是数据。但是,因为在这里你知道矩阵的大小和类型,你可以得到一个指针使用data() method普通的旧数据,使这个工程:

#include <iostream> 
#include <Eigen/Dense> 
#include <mpi.h> 
using std::cin; 
using std::cout; 
using std::endl; 
using namespace Eigen; 

int main(int argc, char ** argv) 
{ 
    MatrixXd a = MatrixXd::Ones(3, 4); 
    int myrank; 
    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank); 
    MPI_Status status; 
    if (0 == myrank) 
    { 
     MPI_Send(a.data(), 12, MPI_DOUBLE, 1, 99, MPI_COMM_WORLD); 
    } 
    else if (1 == myrank) 
    { 
     MPI_Recv(a.data(), 12, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD, &status); 
     cout << "RANK " << myrank << endl; 
     cout << a << endl; 
    } 
    MPI_Finalize(); 
    return 0; 
}