c++ - MPI - 将 std::vector<string> 发送到另一组进程

标签 c++ mpi

我正在尝试将 vector 发送到一组使用 MPI_Comm_spawn 创建的进程。我已经尝试了互联网上的所有方法,但我还没有解决它. 直到现在,我设法让这个单独的组进行通信,并且 parend 向工作人员发送了一个 int。我需要让它与 std::vector 一起工作

给 parent

#include <stdio.h>
#include <string.h>
#include "mpi.h"

int main(int argc, char* argv[]) {
    int i;
    int my_rank; /* rank of process */
    int p; /* number of processes */

    /* start up MPI */
    MPI_Init(&argc, &argv);

    /* find out process rank */
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    /* find out number of processes */
    MPI_Comm_size(MPI_COMM_WORLD, &p);

    MPI_Group grp_world;
    MPI_Group grp_new;
    MPI_Comm newComm;

    MPI_Comm_group(MPI_COMM_WORLD, &grp_world); // get the group of processes for MPI_COMM_WORLD communicator

    MPI_Group_incl(grp_world, 1, &my_rank, &grp_new); // create a new group with only one member (my_rank process)

    MPI_Comm_create(MPI_COMM_WORLD, grp_new, &newComm); // create a new communicator for the new group

    int my_rank_grp; /* rank of process in grp_new */
    int p_grp; /* number of processes in grp_new */
    /* find out process rank in the new group */
    MPI_Comm_rank(newComm, &my_rank_grp);
    /* find out number of processes of the new group */
    MPI_Comm_size(newComm, &p_grp);

    printf("Process rank/size in WORLD/OWN GROUP: [%d/%d]/[%d/%d]\n",
            my_rank, p, my_rank_grp, p_grp);

    MPI_Comm workercomm; // intercommunicator
    int b = my_rank; // some data to be sent

    //[!!!] due to some MPI implementation problems, parallel calls the MPI_Comm_spawn trigger an error, so for the moment we just serialize these calls.
    for (i = 0; i < p; ++i) {
        if (i == my_rank) {
            MPI_Comm_spawn("/home/workspace_test/worker/Debug/worker",
                    MPI_ARGV_NULL, 3, MPI_INFO_NULL, 0, newComm, &workercomm,
                    MPI_ERRCODES_IGNORE );
        }
        MPI_Barrier(MPI_COMM_WORLD );
    }

    /* collective communication sample */
    MPI_Bcast(&b, 1, MPI_INT, MPI_ROOT, workercomm);
    MPI_Comm_free(&workercomm);

    /* shut down MPI */
    MPI_Finalize();

    return 0;
}

然后我有一个简单的 worker :

#include <stdio.h>
#include <string.h>
#include "mpi.h"

int main(int argc, char* argv[]) {
    int my_rank; /* rank of process */
    int p; /* number of processes */
    int p_remote; /* number of processes in parent group */

    /* start up MPI */
    MPI_Init(&argc, &argv);

    /* find out process rank */
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    /* find out number of processes */
    MPI_Comm_size(MPI_COMM_WORLD, &p);

    MPI_Comm parentcomm; // intercommunicator
    int b; // some data to be received

    MPI_Comm_get_parent(&parentcomm);

    /* find out number of processes in parent group */
    MPI_Comm_remote_size(parentcomm, &p_remote);


    MPI_Bcast(&b, 1, MPI_INT, 0, parentcomm);

    printf("[%d] Worker process: %d. Parent group size: %d!\n", b, my_rank, p_remote);

    MPI_Comm_free(&parentcomm);

    /* shut down MPI */
    MPI_Finalize();

    return 0;
}

这个简单的 int Bcast 一切都很好,但是如果我尝试发送一个 vector ,事情就不会像我期望的那样工作。

我想做什么?

我有一个带有路径的 vector (来自磁盘的文件),路径的数量将除以进程的数量。每个进程都会收到一个文件 block ,每个文件的这个进程都会启动一个工作进程,对文件进行一些验证。

到目前为止我尝试的是:

  • 我已经阅读并了解到 MPI 没有 MPI_STRING 之类的东西,但如果我将字符串转换为 char* 也不会更好。
  • 我已经尝试过这个演示中的想法 std::vector/std::String in MPI但我仍然有一些问题。

也许对 mpi 有一些经验的人可以给我一些关于如何发送整个 vector 的提示。

谢谢。

//编辑

我尝试像这样发送数据: 在partent上:

std::vector<std::string> m_image_data2;
m_image_data2.push_back("test");
m_image_data2.push_back("test");
m_image_data2.push_back("test");
m_image_data2.push_back("test");
m_image_data2.push_back("test");
char* data = const_cast<char*>(m_image_data2[0].c_str());
MPI_Bcast(data, 5, MPI_CHAR, MPI_ROOT, workercomm);
MPI_Comm_free(&workercomm);

在 worker 方面:

/* find out number of processes in parent group */
MPI_Comm_remote_size(parentcomm, &p_remote);
std::vector<std::string> m_image_data2;
MPI_Bcast(&m_image_data2, 5, MPI_CHAR, 0, parentcomm);

所以当我尝试在派生任务之间发送字符串 vector 时,我现在的问题就出现了。

最佳答案

检查以下代码以查看您的错误。

ma​​ster.cpp

#include <stdio.h>
#include <string>
#include <mpi.h>
#include <vector>
#include <cstring>

// Converts vector<string> to vector<char>.
std::vector<char> string_to_char(const std::vector<std::string>& strings) {
    std::vector<char> cstrings;
    cstrings.reserve(strings.size());
    for(std::string s: strings)
    {
        for(size_t i = 0; i < strlen(s.c_str()); ++i)
        {
            cstrings.push_back(s.c_str()[i]);
        }
    }

    return cstrings;
}

int main(int argc, char* argv[]) {
    ...
    std::vector<char> cstrings = string_to_char(m_image_data2);
    MPI_Bcast(cstrings.data(), cstrings.size(), MPI_CHAR, MPI_ROOT, workercomm);
    ...
}

worker.cpp

#include <stdio.h>
#include <string>
#include <mpi.h>
#include <vector>

int main(int argc, char* argv[]) {
    ...    
    std::vector<char> cstrings(20);
    MPI_Bcast(cstrings.data(), 20, MPI_CHAR, 0, parentcomm);

    // cstrings: testtesttesttesttest
    // print first four chars.
    printf("Worker process: %c%c%c%c\n", cstrings[0], cstrings[1], cstrings[2], cstrings[3]);
    ...
}

关于c++ - MPI - 将 std::vector<string> 发送到另一组进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087936/

相关文章:

c++ - 我不知道这个随机发生器有什么问题

c++ - 我可以将 MPI 与共享内存一起使用吗

c - MPI_Send/MPI_Recv : count of elements or buffer size?

C++ 在非内联函数中使用 typedef

c++ - 自定义迭代器: how to correctly handle distance calculation and equality comparison between a and b if the behavior of those differe

c - MPI 和 C 结构

c - 共享内存的 MPI_Comm_Size 值

c++ - 有没有办法在运行时分析 MPI 应用程序? - 在运行时使用 MPI 调用所花费的时间

c++ - 在不同的编译器上获得不同的输出

c++ - 为什么不在 C++ 图形中打开绘图窗口?