MPI_Ssend和MPI_Recv的问题,郁闷了我好几天啦
#include <pthread.h>
#include <iostream>
#undef SEEK_SET
#undef SEEK_END
#undef SEEK_CUR
#define MPICH_SKIP_MPICXX
#include "mpi.h"
#define INFO_MAX_LEN 128
using namespace std;
void* ExecCmd(void *param);
int numprocs,myid,namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
void* Listen(void *param)
{
while (1)
{
MPI_Status status;
int n,m;
pthread_t id;
printf("proc %d call MPI_Recv.\n",myid);
MPI_Recv(&n,1,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
printf("proc %d call MPI_Recv success.\n",myid);
int ret;
m = n;
ret = pthread_create(&id,NULL,ExecCmd,(void*)&m);
}
return NULL;
}
void* ExecCmd(void *param)
{
int *cmd = (int*)param;
int n = *cmd;
int c = 'a';
if (myid == 0)
{
printf("Proc 0 recv cmd from proc %d.\n",*cmd);
MPI_Ssend(&c,1,MPI_INT,n,0,MPI_COMM_WORLD);
fprintf(stderr,"MPI_Ssend %d data,tag=0. in ExecCmd.\n", sizeof(c));
printf("Proc 0 send the cmd to %d.\n",n);
}
else
{
printf("Proc %d Recv cmd from Proc 1.\n",myid);
// MPI_Send(cmd,sizeof(*cmd),MPI_INT,*cmd,0,MPI_COMM_WORLD);
}
return NULL;
}
int main(int argc,char* argv[])
{
/*****MPI环境初始化*****/
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Get_processor_name(processor_name,&namelen);
pthread_t id;
int ret;
ret = pthread_create(&id,NULL,Listen,NULL);
if (myid != 0)
{
MPI_Ssend(&myid,1,MPI_INT,0,0,MPI_COMM_WORLD);
fprintf(stderr,"MPI_Ssend %d data,tag=0. in main func.\n", sizeof(myid));
}
pthread_join(id,NULL);
MPI_Finalize();
/************************************************************************/
return 0;
}
以上是我的代码,装的是MPICH2-1.0.1。
每个进程都创建了一个Listen线程用来接受命令,接受了命令调用ExecCmd执行。
①非0进程向0号进程发送自己的进程编号
②0号进程接受到各进程编号
③0号进程创建执行线程ExecCmd,并把各个进程的编号反馈回给各个进程
问题:
0号进程接受各个进程编号后创建的ExecCmd线程在调用MPI_Ssend时停住,如果改用MPI_Send的话,虽然ExecCmd能够执行完毕,但是其他进程(非0号进程)无法接受到0号进程反馈回来的编号信息。
大家救我啊!