【问题标题】:How to send the data in an Eigen::MatrixXd with MPI如何使用 MPI 在 Eigen::MatrixXd 中发送数据
【发布时间】:2016-11-30 06:39:23
【问题描述】:

我想使用 MPI 在机器之间发送矩阵。以下是我的测试代码

#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).
--------------------------------------------------------------------------

我应该如何解决它?谢谢!

【问题讨论】:

  • 序列化矩阵并发送?
  • 不是发送eigen 矩阵,而是从int 之类的简单内容开始,然后进行处理。 eigen 矩阵是类似于std::vector 的容器。随之而来的是大量的信息。这是一个例子stackoverflow.com/questions/36021305/…

标签: c++ mpi eigen


【解决方案1】:

正如@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;
}

【讨论】:

    猜你喜欢
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多