2016-07-26 101 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; 
}