Search code examples
c++mpiopenmpms-mpi

Error in sending/receiving by multi-threads in hybrid openMP/MPI code


I am using OpenMP and MPI in the following code. I am testing on one multi-core Windows machine.

I got these two errors:

[0] fatal error Fatal error in MPI_Send: Other MPI error, error stack: MPI_Send(buf=0x00007FF67497F33C, count=1, MPI_INT, dest=2, tag=1, MPI_COMM_WORLD) failed Missing hostname or invalid host/port description in business card

[1] fatal error Fatal error in MPI_Recv: Other MPI error, error stack: MPI_Recv(buf=0x00007FF67497F33C, count=1, MPI_INT, src=0, tag=1, MPI_COMM_WORLD, status=0x00007FF67497F348) failed no space for the shared memory queue name

Then in another run I got this error:

[0] fatal error Fatal error in MPI_Send: Other MPI error, error stack: MPI_Send(buf=0x000000172E31FCC4, count=1, MPI_INT, dest=2, tag=1, MPI_COMM_WORLD) failed Failed to communicate with 2 on previous attempts

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

Solution

  • At first, you need to MPI_Init_thread() with MPI_THREAD_MULTIPLE and make sure your library does provide this.