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

时间:2018-03-03 18:52:01

标签: c++ mpi

我正在尝试将矢量发送到使用 MPI_Comm_spawn 创建的一组流程。我已经尝试了互联网上的所有内容但我无法解决还没完成。 到目前为止,我设法使这些单独的组进行通信,并且parend向工作人员发送一个int。我需要使用std :: vector

对于父母

#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;
}

在此之后,我有一个简单的工人:

#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一切都很好,但是如果我尝试发送一个矢量,那么事情就会发挥作用。

我想做什么?

我有一个带路径的向量(来自磁盘的文件),路径数将被分成进程数。每个进程都会收到一个文件块,每个文件的这个进程将启动一个工作进程,该进程将对文件进行一些验证。

到目前为止我尝试了什么:

  • 我已经阅读并且我播种了MPI不具备类似MPI_STRING的内容,但是如果我将字符串转换为char *事情并不好。
  • 我从这个prezentation std::vector/std::String in MPI尝试了这个想法,但我仍有一些问题。

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

谢谢。

//修改

我尝试发送这样的数据: 在部分:

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);

在工人方面:

/* 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);

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

1 个答案:

答案 0 :(得分:1)

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

<强> master.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]);
    ...
}