c - 搜索/等待 MS-MPI 中的任何传输

标签 c mpi

这是一个主从关系。我怎样才能让主进程以非阻塞的方式搜索传输给他的消息。如果在搜索时没有消息传输给 master,它将继续迭代。但是,如果有消息传送给他,它将处理该消息并继续迭代。看里面的注释/* */

int main(int argc, char *argv[])
{
    int numprocs,
        rank;

    MPI_Request request;
    MPI_Status status;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    if(rank == 0) // the searching process
    {
        for (int i=0; i < 4000000; i++)
        {   
            // do some stuff here; does not matter what

            /* see if any message has been transmitted to me at this point
             without blocking the process; if at this time it happens to be
             transmitted, do something and than continue with for iternations;
             or just continue with for iterations and maybe next time will
             have a message which sends me to do something */
        }
    }
    else
    {
        int flag = 1;
        while(flag) 
        {
            // something done that at some point changes flag
        }

        // send a message to process with rank 0 and don't get stuck here
        MPI_Isend(12, 1, MPI_INT, 0, 100, MPI_COMM_WORLD, &request);

        // some other stuff done

        // wait for message to be transmitted
        MPI_Wait(&request, &status);
    }

    MPI_Finalize();
    return 0;
}

最佳答案

一种解决方案是使用 MPI_IProbe() 来测试消息是否正在等待。

  • 在这一行中,使用指针代替“12”

    MPI_Isend(12, 1, MPI_INT, 0, 100, MPI_COMM_WORLD, &request);
    
  • 在此处添加 flag=0 :

    while(flag!=0) 
     {
        // something done that at some point changes flag
     }
    

代码如下:

#include "mpi.h"
#include "stdio.h"

int main(int argc, char *argv[])
{
int numprocs,
    rank;

MPI_Request request;
MPI_Status status;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
if(rank == 0) // the searching process
{
int i;
    for (i=0; i < 4000000; i++)
    {   
        // do some stuff here; does not matter what
    //printf("I am still running!\n");
    int flag;
    MPI_Iprobe(MPI_ANY_SOURCE,100,MPI_COMM_WORLD,&flag,&status);
    if(flag!=0){
        int value;
        MPI_Recv(&value, 1, MPI_INT, status.MPI_SOURCE, status.MPI_TAG, MPI_COMM_WORLD, &status);
        printf("I (0) received %d \n",value);
    }
        /* see if any message has been transmitted to me at this point
         without blocking the process; if at this time it happens to be
         transmitted, do something and than continue with for iternations;
         or just continue with for iterations and maybe next time will
         have a message which sends me to do something */

    }
}
else
{
int i;
for(i=0;i<42;i++){
    int flag = 1;
    while(flag!=0) 
    {
        // something done that at some point changes flag
    flag=0;
    }

int bla=1000*rank+i;
    // send a message to process with rank 0 and don't get stuck here
    MPI_Isend(&bla, 1, MPI_INT, 0, 100, MPI_COMM_WORLD, &request);

    // some other stuff done
printf("I (%d) do something\n",rank);
    // wait for message to be transmitted
    MPI_Wait(&request, &status);
}
}

MPI_Finalize();
return 0;
}

再见,

弗朗西斯

关于c - 搜索/等待 MS-MPI 中的任何传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20017814/

相关文章:

c - 文件指针是否导致代码中的段错误

对 Makefile (C/unix) 感到困惑

c++ - MPI_Comm_split 和 openmpi 1.4.3 的问题

c++ - MPI_Ssend 和 MPI_Recv 的意外行为

c++ - 如何使用 MPI 库为进程子集调用 (c++) 函数?

c++ - 为什么 mpi_bcast 比 mpi_reduce 慢这么多?

用 C 语言为 IRC 机器人创建一个基本 shell,并在后台运行另一个线程

c - C 中使用递归函数的指数函数

C 函数没有返回正确的东西?

c - 在 C 中使用 MPI_Type_Vector 和 MPI_Gather