c++ - 混合 openMP/MPI 代码中多线程发送/接收错误

标签 c++ mpi openmp ms-mpi

我在以下代码中使用了 OpenMP 和 MPI。我正在一台多核 Windows 机器上进行测试。

我得到了这两个错误:

[0] fatal error MPI_Send 中的 fatal error :其他 MPI 错误,错误堆栈: MPI_Send(buf=0x00007FF67497F33C,count=1,MPI_INT,dest=2,tag=1,MPI_COMM_WORLD)失败 名片中缺少主机名或主机/端口描述无效

[1] fatal error MPI_Recv 中的 fatal error :其他 MPI 错误,错误堆栈: MPI_Recv(buf=0x00007FF67497F33C, count=1, MPI_INT, src=0, tag=1, MPI_COMM_WORLD, status=0x00007FF67497F348) 失败 共享内存队列名称没有空间

然后在另一次运行中我得到了这个错误:

[0] fatal error MPI_Send 中的 fatal error :其他 MPI 错误,错误堆栈: MPI_Send(buf=0x000000172E31FCC4, count=1, MPI_INT, dest=2, tag=1, MPI_COMM_WORLD) 失败 之前尝试与 2 通信失败

#include<iostream>
#include "mpi.h"
#include <omp.h>

using namespace std;

int numOfProc, id, array_size, portion;
int *arr = NULL;
MPI_Status status;
const static int tag = 1;


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

    cout << "Hello from Process # " << id << '\n';

    int data; 

    omp_set_num_threads(2);
#pragma omp parallel for
    for (int i = 1; i < 20; i++)
    {
        if (id == 0)//master
        {
            for (int p = 1; p < numOfProc; p++)
            {
                data = i*p;
                MPI_Send(&data, 1, MPI_INT, p, tag, MPI_COMM_WORLD);
            }
        }
        else // slaves 
        {
            MPI_Recv(&data, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
            cout << "Process " << id << " recieved " << data << " by thread " << omp_get_thread_num() << endl; 
        }
    }

    MPI_Finalize();
}

最佳答案

首先,您需要使用 MPI_THREAD_MULTIPLE MPI_Init_thread() 并确保您的库提供此功能。

关于c++ - 混合 openMP/MPI 代码中多线程发送/接收错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45035248/

相关文章:

c++ - 高效更新多条记录的 SQLite 表

c++ - openMPI 会导致双 Hexacore 机器出现性能问题吗?

c - OpenMP 给出(核心转储)

r - OpenMP、R 和 MacOS

c++ - Visual Studio 2012 静态库与 Visual Studio 2017

c++ - 使用 "X x(42)"和 "X x = 42"的对象构造有何不同?

c++ - MPI - 当数组初始化值必须为常量时,如何为工作人员创建部分数组?

C++ OpenMP 写入共享数组/vector 的特定元素

c++ - 处理和触发从 QML 到 C++ 的事件,反之亦然

c - 在MPI中,如何编写下面的程序 等待所有计算完成