MPI和C++/Qt混用的收发消息的例子(主从模式)

1、main.cpp

cpp 复制代码
//main.cpp
#include<iostream>
#include<mpi.h>
#include <QtCore>
#include <QThread>

enum MyTag
{
    TAG_TEST = 1,
    TAG_TEST_RSP,
    TAG_FORECAST,
    TAG_COLWARN
};

void doMaster()
{
    MPI_Status status;
    QList<int> dataList;
    for (int i = 0; i < 20000; ++i)
    {
        dataList << i;
    }
    
    char message[100] = {0};
    int nCoreSize;
    MPI_Comm_size(MPI_COMM_WORLD, &nCoreSize);
    for (int i = 1; i < nCoreSize; ++i)
    {
        sprintf(message, "Message-%05d", i);
        MPI_Send(message, strlen(message), MPI_CHAR, i, TAG_TEST, MPI_COMM_WORLD);
    }
    
    std::cout << "Master Sent Over\n";
    
    int nCoreId;
    int nRspCout = 0;
    while (nRspCout < nCoreSize-1)
    {
        //使用MPI_ANY_SOURCE 接收每轮优先到达的消息
        int ret = MPI_Recv((void*)(&nCoreId), 1, MPI_INT, MPI_ANY_SOURCE, TAG_TEST_RSP, MPI_COMM_WORLD, &status);
        if (ret != 0) std::cout << "Master recv ret: " << ret << "," << status.MPI_ERROR  << std::endl;
        
        if (status.MPI_ERROR == 0)
        {
            nRspCout++;
            printf("Master recv response from #%d, %d/%d\n", nCoreId, nRspCout, nCoreSize-1);
        }

    }
    std::cout << "Master END" << std::endl;
}

void doSlave(int nCoreId)
{
    MPI_Status status;
    char message[100] = {0};
    while (true)
    {
        QThread::msleep(100 + qrand() % 100); //使其随机化
        MPI_Recv((void*)&message, 100, MPI_CHAR, 0, TAG_TEST, MPI_COMM_WORLD, &status);
        if (status.MPI_ERROR != 0)
        {
            QThread::msleep(10);
            continue;
        }
        
        std::cout << "Slave revec message (" << message << ") by core " << nCoreId << std::endl;
        MPI_Send((void*)(&nCoreId), 1, MPI_INT, 0, TAG_TEST_RSP, MPI_COMM_WORLD);
        break;
    };
}

int main(int argc,char* argv[])
{
    int nCoreId;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&nCoreId);

    if (nCoreId == 0)
    {
        doMaster();
    }
    else
    {
        doSlave(nCoreId);
    }
    
    MPI_Finalize();
    return 0;
}

2、TestCpp.pro

#pro file

TEMPLATE = app

CONFIG += console c++11

CONFIG -= app_bundle

#CONFIG -= qt

QT -= gui

SOURCES += \

main.cpp

INCLUDEPATH += /usr/mpi/gcc/openmpi-4.0.4rc3/include/

LIBS += -L/usr/mpi/gcc/openmpi-4.0.4rc3/lib64/ -lmpi

3、运行结果示例

$ mpirun -np 8 TestCpp

Master Sent Over

Slave revec message (Message-00006) by core 6

Master recv response from #6, 1/7

Slave revec message (Message-00001) by core 1

Master recv response from #1, 2/7

Slave revec message (Message-00007) by core 7

Master recv response from #7, 3/7

Slave revec message (Message-00002) by core 2

Master recv response from #2, 4/7

Slave revec message (Message-00005) by core 5

Slave revec message (Message-00004) by core 4

Master recv response from #4, 5/7

Master recv response from #5, 6/7

Slave revec message (Message-00003) by core 3

Master recv response from #3, 7/7

Master END

相关推荐
----云烟----5 小时前
QT中QString类的各种使用
开发语言·qt
「QT(C++)开发工程师」10 小时前
【qt版本概述】
开发语言·qt
一路冰雨14 小时前
Qt打开文件对话框选择文件之后弹出两次
开发语言·qt
老赵的博客15 小时前
QT 自定义界面布局要诀
开发语言·qt
码码哈哈0.015 小时前
VSCode 2022 离线安装插件QT VSTOOl报错此扩展不能安装在任何当前安装的产品上。
ide·vscode·qt
feiyangqingyun19 小时前
Qt/C++离线地图的加载和交互/可以离线使用/百度和天地图离线/支持手机上运行
c++·qt·qt天地图·qt离线地图·qt地图导航
gz94561 天前
windows下,用CMake编译qt项目,出现错误By not providing “FindQt5.cmake“...
开发语言·qt
「QT(C++)开发工程师」1 天前
Ubuntu 26.04 LTS 大升级:Qt 6 成为未来新引擎
qt
兆。1 天前
python实战案例----使用 PyQt5 构建简单的 HTTP 接口测试工具
爬虫·python·qt
喝哈喝哈2 天前
pycharm中配置pyqt5
python·qt·pycharm