如何使用MPI_Send在MPI中发送嵌套结构

时间:2015-09-09 13:37:06

标签: c++ mpi

我需要使用MPI_SEND发送plista数组:

Traza* plista;

    struct Evento{
    char* evento;
    unsigned char cant;
    };

    struct Traza
    {
            char* nombre;
            Evento* eventos;
        unsigned int cantEventos;
            bool revisado;
            unsigned int idTraza;
    };

我读了人们使用MPI_PACKED的巫婆的一些文章,但这是一个复杂的结构。

1 个答案:

答案 0 :(得分:0)

要发送的数据结构很复杂,因为它包含指向子结构的指针。传达指针显然没有意义,相反,你必须以深层复制的方式求助于子结构。此外,一些子结构包含动态长度的数组,事先只有发送者知道,因此接收者无法分配正确的内存量。

您可能想要创建自己的复合MPI数据类型,实际上可以通过使用MPI_BOTTOM并编码数据结构的所有子部分的绝对地址来实现。每次要进行通信时,都需要重新创建此自定义数据类型,因为您可能使用不同的数据,因此绝对地址会发生变化。但是,这仅适用于发送方,因为在接收方上,您仍然不知道为数据结构的每个部分分配了多少内存。

解决此问题的一种方法是进行多次通信,首先发送大小,然后分别发送数据。然而,这具有增加通信数量并且可能引入不必要的延迟的缺点。

输入您已提及的MPI_PACKEDMPI_PACKED允许您将要一次发送一个数据的数据打包到缓冲区中,然后在单个通信中发送整个缓冲区。在接收方,它可以在一次通信中接收,然后逐个打开包装。

以下是使用MPI_PACKED的数据结构的解决方案。

注意:我对拉丁语几乎一无所知,所以我必须猜测一些标识符的含义。即我解释

  • cant作为某种元素计数
  • Evento::evento作为符合char的数字数组,数组的长度为Evento::cant
  • Traza::nombre为数字数组,Traza::eventos为事件数组,Traza::cantEventos
  • 给出两个数组的长度

如果这些解释不正确,应该很容易纠正这些解释(只需从正确的源获取数组的大小,然后按照我在消息中打包Traza的方式单独打包它们)。

#include <iomanip>
#include <iostream>
#include <ostream>
#include <vector>

#include <mpi.h>

struct Evento {
  char* evento;
  unsigned char cant;
};

struct Traza
{
  char* nombre;
  Evento* eventos;
  unsigned int cantEventos;
  bool revisado;
  unsigned int idTraza;
};

void pack_plista(int incount, Traza* data, MPI_Comm comm,
                 std::vector<char> &buf)
{
  int pos = 0;
  buf.clear();
  int size;

  MPI_Pack_size(1, MPI_INT, comm, &size);
  buf.resize(pos+size);
  MPI_Pack(&incount, 1, MPI_INT, buf.data(), buf.size(), &pos, comm);

  for(int t = 0; t < incount; ++t)
  {
    MPI_Pack_size(2, MPI_UNSIGNED, comm, &size);
    buf.resize(pos+size);
    MPI_Pack(&data[t].cantEventos, 1, MPI_UNSIGNED,
             buf.data(), buf.size(), &pos, comm);
    MPI_Pack(&data[t].idTraza, 1, MPI_UNSIGNED,
             buf.data(), buf.size(), &pos, comm);

    MPI_Pack_size(1, MPI_UNSIGNED_CHAR, comm, &size);
    buf.resize(pos+size);
    { // MPI does not know about C++ bool
      unsigned char revisado = data[t].revisado;
      MPI_Pack(&revisado, 1, MPI_UNSIGNED_CHAR,
               buf.data(), buf.size(), &pos, comm);
    }

    // This interprets Traza::nombre as a character code, which is probably incorrect
    // However, that is unlikely to be a problem, unless you are in a
    // heterogeneous ASCII/EBCDIC environment
    MPI_Pack_size(data[t].cantEventos, MPI_CHAR, comm, &size);
    buf.resize(pos+size);
    MPI_Pack(data[t].nombre, data[t].cantEventos, MPI_CHAR,
             buf.data(), buf.size(), &pos, comm);

    for(unsigned int e = 0; e < data[t].cantEventos; ++e)
    {
      // send count (interpret as a number)
      MPI_Pack_size(1, MPI_UNSIGNED_CHAR, comm, &size);
      buf.resize(pos+size);
      MPI_Pack(&data[t].eventos[e].cant, 1, MPI_UNSIGNED_CHAR,
               buf.data(), buf.size(), &pos, comm);

      // send events (interpret as character codes)
      MPI_Pack_size(data[t].eventos[e].cant, MPI_CHAR, comm, &size);
      buf.resize(pos+size);
      MPI_Pack(data[t].eventos[e].evento, data[t].eventos[e].cant, MPI_CHAR,
               buf.data(), buf.size(), &pos, comm);
    }
  }
  buf.resize(pos);
}

void unpack_plista(int &outcount, Traza* &data, MPI_Comm comm,
                   // buf cannot be reference-to-const since MPI_Unpack takes
                   // pointer-to-nonconst
                   std::vector<char> &buf)
{
  int pos = 0;

  MPI_Unpack(buf.data(), buf.size(), &pos, &outcount, 1, MPI_INT, comm);

  data = new Traza[outcount];
  for(int t = 0; t < outcount; ++t)
  {
    MPI_Unpack(buf.data(), buf.size(), &pos,
               &data[t].cantEventos, 1, MPI_UNSIGNED, comm);
    MPI_Unpack(buf.data(), buf.size(), &pos,
               &data[t].idTraza, 1, MPI_UNSIGNED, comm);

    { // MPI does not know about C++ bool
      unsigned char revisado;
      MPI_Unpack(buf.data(), buf.size(), &pos,
                 &revisado, 1, MPI_UNSIGNED_CHAR, comm);
      data[t].revisado = revisado;
    }

    // This interprets Traza::nombre as a character code, which is probably incorrect
    // However, that is unlikely to be a problem, unless you are in a
    // heterogeneous ASCII/EBCDIC environment
    data[t].nombre = new char[data[t].cantEventos];
    MPI_Unpack(buf.data(), buf.size(), &pos,
               data[t].nombre, data[t].cantEventos, MPI_CHAR, comm);

    data[t].eventos = new Evento[data[t].cantEventos];
    for(unsigned int e = 0; e < data[t].cantEventos; ++e)
    {
      // receive count (interpret as a number)
      MPI_Unpack(buf.data(), buf.size(), &pos,
                 &data[t].eventos[e].cant, 1, MPI_UNSIGNED_CHAR, comm);

      // receive events (interpret as character codes)
      data[t].eventos[e].evento = new char[data[t].eventos[e].cant];
      MPI_Unpack(buf.data(), buf.size(), &pos,
                 data[t].eventos[e].evento, data[t].eventos[e].cant, MPI_CHAR,
                 comm);
    }
  }
}

void send_plista(int incount, Traza* data, int dest, int tag, MPI_Comm comm)
{
  std::vector<char> buf;
  pack_plista(incount, data, comm, buf);
  MPI_Send(buf.data(), buf.size(), MPI_PACKED, dest, tag, comm);
}

void recv_plista(int &outcount, Traza* &data,
                 int src, int tag, MPI_Comm comm)
{
  MPI_Status status;
  MPI_Probe(src, tag, comm, &status);
  int size;
  MPI_Get_count(&status, MPI_PACKED, &size);
  std::vector<char> buf(size);
  MPI_Recv(buf.data(), buf.size(), MPI_PACKED, src, tag, comm, &status);

  unpack_plista(outcount, data, comm, buf);
}

void make_test_data(int &count, Traza *&data) {
  count = 2;
  data = new Traza[2] {
    {
      new char[3] { char(0), char(1), char(3) },
      new Evento[3] {
        {
          new char[4] { 'a', 'b', 'c', 'd' },
          (unsigned char)4,
        },
        {
          new char[3] { 'e', 'f', 'g' },
          (unsigned char)3,
        },
        {
          new char[2] { 'h', 'i' },
          (unsigned char)2,
        },
      },
      3u,
      true,
      0u,
    },
    {
      new char[1] { char(4) },
      new Evento[1] {
        {
          new char[1] { 'j' },
          (unsigned char)1,
        },
      },
      1u,
      false,
      1u,
    },
  };
}

void print_data(std::ostream &out, int count, const Traza *data)
{
  for(int t = 0; t < count; ++t)
  {
    std::cout << "{\n"
              << "  nombre = { ";
    for(unsigned e = 0; e < data[t].cantEventos; ++e)
      std::cout << int(data[t].nombre[e]) << ", ";
    std::cout << "},\n"
              << "  eventos = {\n";
    for(unsigned e = 0; e < data[t].cantEventos; ++e)
    {
      std::cout << "    {\n"
                << "      evento = { ";
      for(int c = 0; c < data[t].eventos[e].cant; ++c)
        std::cout << "'" << data[t].eventos[e].evento[c] << "', ";
      std::cout << "},\n"
                << "      cant = " << int(data[t].eventos[e].cant) << ",\n"
                << "    },\n";
    }
    std::cout << "  },\n"
              << "  cantEventos = " << data[t].cantEventos << ",\n"
              << "  revisado = " << data[t].revisado << ",\n"
              << "  idTraza = " << data[t].idTraza << ",\n"
              << "}," << std::endl;
  }
}

int main(int argc, char **argv)
{
  int rank;
  Traza *plista = nullptr;
  int count = 0;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  if(rank == 1) {
    make_test_data(count, plista);
    send_plista(count, plista, 0, 0, MPI_COMM_WORLD);
  }
  else if(rank == 0) {
    recv_plista(count, plista, 1, 0, MPI_COMM_WORLD);

    std::cout << std::boolalpha;
    print_data(std::cout, count, plista);
  }
  // don't bother freeing allocated memory, the process is ending anyway
  MPI_Finalize();
}

示例运行:

-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Wed Sep  9 20:04:24

set -ex; mpic++ -std=c++11 -Wall -Wno-literal-suffix -o check check.cc; mpirun -n 2 ./check
+ mpic++ -std=c++11 -Wall -Wno-literal-suffix -o check check.cc
+ mpirun -n 2 ./check
{
  nombre = { 0, 1, 3, },
  eventos = {
    {
      evento = { 'a', 'b', 'c', 'd', },
      cant = 4,
    },
    {
      evento = { 'e', 'f', 'g', },
      cant = 3,
    },
    {
      evento = { 'h', 'i', },
      cant = 2,
    },
  },
  cantEventos = 3,
  revisado = true,
  idTraza = 0,
},
{
  nombre = { 4, },
  eventos = {
    {
      evento = { 'j', },
      cant = 1,
    },
  },
  cantEventos = 1,
  revisado = false,
  idTraza = 1,
},

Compilation finished at Wed Sep  9 20:04:26