C++ C++;打开MPI:自定义结构类型无法通过Ssend发送

C++ C++;打开MPI:自定义结构类型无法通过Ssend发送,c++,mpi,distributed-computing,C++,Mpi,Distributed Computing,因此,我正在使用MPI进行一个项目,并尝试创建自己的结构。我的问题是,它似乎没有正常工作,因为我从来没有收到任何内部的工人,所以我超时。我尝试了一个标准的MPI_INT,效果非常好。然而,当我尝试实现我自己的类型时,它不喜欢它。。。这是我的密码: #include <iostream> #include <mpi.h> #include "main.hpp" int main(int argc, char *argv[]){ //initialize MPI

因此,我正在使用MPI进行一个项目,并尝试创建自己的结构。我的问题是,它似乎没有正常工作,因为我从来没有收到任何内部的工人,所以我超时。我尝试了一个标准的MPI_INT,效果非常好。然而,当我尝试实现我自己的类型时,它不喜欢它。。。这是我的密码:

#include <iostream>
#include <mpi.h>
#include "main.hpp"

int main(int argc, char *argv[]){
    //initialize MPI
    MPI_Init(&argc, &argv);

    message_to_worker message;
    //declare my new type
    MPI_Datatype MessageType;

    //declare the types the the structure will have
    MPI_Datatype type[1] = { MPI_INT };
    //the number of results for each type(int[50] will be 50 etc..)
    int blocklen[1] = {1};

    //this will store the displacement for each var in the structure
    MPI_Aint disp[1] = {0};
    MPI_Aint var1, var2;

    //number of vars
    int count = 1;

    //define the type
    MPI_Type_struct(count, blocklen, disp, type, &MessageType);
    MPI_Type_commit(&MessageType); 

    std::cout << "The displacement is " << disp[0] << std::endl;

    int rank;
    int world_size;

    //give me my current rank(node 1 reiceves 1 , node 2 etc... )
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    int counter = 1;
    message.N = 5;

    //IF MASTER
    if (rank == 0) {
        int result;

        //send messages to the workers
        while(counter<world_size){
            //send 1 message of type MessageType using my own structure(first parameter)
            result = MPI_Ssend(&message, 1, MessageType, counter, 123, MPI_COMM_WORLD);

            //make sure the mssage was sent correctly
            if (result == MPI_SUCCESS){
                std::cout << "I am master and the world size is : " << world_size << std::endl;
                std::cout << " I send a message to mky worker thread " << std::endl;
            }else{
                std::cout << "There was a problem with the sending for worker " << counter << std::endl;
            }
            counter++;
        }
    }
    //IF WORKER
    else if (rank > 0) {
        //the worker will wait until it receives a message
        int result = MPI_Recv(&message, 1, MessageType, 0, 0, MPI_COMM_WORLD,
                  MPI_STATUS_IGNORE);

        //make sure the message was RECEIVED correctly
        if (result == MPI_SUCCESS){
            std::cout << "Worker - Rank: " << rank <<" OK! and the value received through the message is " << message.N << std::endl;
        }
    }

    //shutdown MPI
    MPI_Finalize();
    return 0;

}
#包括
#包括
#包括“main.hpp”
int main(int argc,char*argv[]){
//初始化MPI
MPI_Init(&argc,&argv);
消息\u至\u工作者消息;
//声明我的新类型
MPI_数据类型MessageType;
//声明结构将具有的类型
MPI_数据类型[1]={MPI_INT};
//每种类型的结果数量(int[50]将为50等)
int blocklen[1]={1};
//这将存储结构中每个var的位移
MPI_Aint disp[1]={0};
MPI_Aint var1、var2;
//变量数
整数计数=1;
//定义类型
MPI类型结构(计数、块、显示、类型和消息类型);
MPI\u类型\u提交(&MessageType);
std::coutMy tag param(从上次的3个参数)对于发送方和接收方是不同的。.主控端使用的是标记123,工作端正在查找标记0。它们都需要同步,因为它们需要建立一个通信通道。下面是修复方法(注意第5个参数标记是如何更改的)

之前:

MPI_Ssend(&message, 1, MessageType, counter, 123, MPI_COMM_WORLD);

MPI_Recv(&message, 1, MessageType, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
之后

MPI_Ssend(&message, 1, MessageType, counter, 123, MPI_COMM_WORLD);

MPI_Recv(&message, 1, MessageType, 0, 123, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

请将您的解决方案变成一个答案,等待计时器超时并接受它。一个未经请求的建议:检查MPI通信调用的返回值是毫无意义的,除非您已明确更改了通信器的错误处理程序(您尚未这样做)。默认处理程序会在出现错误时终止程序,这就是为什么除了
MPI_SUCCESS
@hristoliev之外,您永远不会看到任何返回代码,谢谢。我确实不知道这一点,我感谢您的建议!