gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:53:13 24 4
gpt4 key购买 nike

这是一个主从关系。我怎样才能让主进程以非阻塞的方式搜索传输给他的消息。如果在搜索时没有消息传输给 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com