Reputation: 17468
I want to use MPI to send matrces between machines. The following are my test code
#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;
}
It compiled success without error, but when I launched it, it returned the following error.
$ 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).
--------------------------------------------------------------------------
how should I fix it? Thank you!
Upvotes: 2
Views: 2801
Reputation: 50927
As @Matt points out, there is more in a MatrixXd container than just the data. However, since here you know the size and type of the matrix, you can just get a pointer to the plain old data using the data()
method, so that this works:
#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;
}
Upvotes: 9