slowslowslowly 2017-03-17 07:51 采纳率: 0%
浏览 1295

mpi独立进程间的通信无法实现

客户端检测不到服务端发布的服务名
源代码:
客户端程序:
#include
#include
#include
#include

int main(int argc, wchar_t **argv)
{
int errs = 0;
wchar_t port_name[MPI_MAX_PORT_NAME], port_name_out[MPI_MAX_PORT_NAME];
wchar_t serv_name[256];
int merr, mclass;
wchar_t errmsg[MPI_MAX_ERROR_STRING];
char outstr[1024];
char sb[1024];
char rb[1024];
int msglen;
MPI_Status stat;
int rank, newrank, flag;
MPI_Comm newcomm;

MPI_Init(&argc, &argv);

flag = 123; 
wcscpy_s(serv_name, L"MyTest");

MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Barrier(MPI_COMM_WORLD);
merr = MPI_Lookup_name(serv_name, MPI_INFO_NULL, port_name_out);
if (merr)
{
    errs++;
    MPI_Error_string(merr, errmsg, &msglen);
    printf("Error in Lookup name:\"%ls\"\n", errmsg);
    fflush(stdout);
}
else
{
    sprintf_s(outstr, "rank:%d,looked service for:%ls", rank, port_name_out);
    fprintf(stdout, "l%s\n", outstr);
}
sprintf_s(outstr, "Grank:%d,Trying connecting to server", rank);
fprintf(stdout, "%ls\n", outstr);
MPI_Comm_connect(port_name_out, MPI_INFO_NULL, 0, MPI_COMM_SELF, &newcomm);

MPI_Comm_rank(newcomm, &newrank);
sprintf_s(outstr, "Grank:%d,Lrank%d,connected to server", rank, newrank);
fprintf(stdout, "%ls\n", outstr);

sprintf_s(outstr, "Grank:%d,Lrank%d,requesting service...", rank, newrank);
fprintf(stdout, "%ls\n", outstr);
sprintf_s(sb, "Grank:%d,Lrank%d,require", rank, newrank);
msglen = strlen(sb);
MPI_Send(sb, msglen, MPI_CHAR, 0, flag, newcomm);

MPI_Barrier(MPI_COMM_WORLD);
MPI_Comm_disconnect(&newcomm);
MPI_Finalize();
return 0;

}
服务端程序:
#include
#include
#include
#include

int main(int argc, wchar_t *argv[])
{
int errs = 0;
wchar_t port_name[MPI_MAX_PORT_NAME], port_name_out[MPI_MAX_PORT_NAME];
wchar_t serv_name[256];
int merr, mclass;
wchar_t errmsg[MPI_MAX_ERROR_STRING];
char outstr[1024];
char sb[1024];
char rb[1024];
int msglen;
MPI_Status stat;
int rank, newrank, flag;
MPI_Comm newcomm;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
wcscpy_s(serv_name, L"MyTest");
MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
flag = 123;
newrank = 0;
sprintf_s(sb, "Grank:%d,Lrank:%d,require", rank, newrank);
msglen = strlen(sb);
fprintf(stdout, "process:%d,opening port....\n", rank);
merr = MPI_Open_port(MPI_INFO_NULL, port_name_out);
fprintf(stdout, "process:%d,port opened,with<%ls>\n", rank, port_name_out);
merr = MPI_Publish_name(serv_name, MPI_INFO_NULL, port_name_out);
if (merr)
    {
        errs++;
        MPI_Error_string(merr, errmsg, &msglen);
        printf("Error in publish_name:\"%ls\"\n", errmsg);
        fflush(stdout);
    }
MPI_Barrier(MPI_COMM_WORLD);


fprintf(stdout, "process:%d,accepting connection on :%ls\n", rank, port_name_out);
merr = MPI_Comm_accept(port_name_out, MPI_INFO_NULL, 0, MPI_COMM_SELF, &newcomm);
fprintf(stdout, "process:%d,accepted a connection on %ls\n", rank, port_name_out);

MPI_Comm_rank(newcomm, &newrank);
sprintf_s(outstr, "Grank:%d,Lrank%d,receiving", rank, newrank);
fprintf(stdout, "%s\n", outstr);
MPI_Recv(rb, msglen, MPI_CHAR, 0, flag, newcomm, &stat);
sprintf_s(outstr, "Grank:%d,Lrank%d,received<--%ls", rank, newrank, rb);
fprintf(stdout, "%s\n", outstr);

MPI_Barrier(MPI_COMM_WORLD);
fprintf(stdout, "server barrier after receiving passed\n");
merr = MPI_Unpublish_name(serv_name, MPI_INFO_NULL, port_name_out);
fprintf(stdout, "server after unpublish name\n");
if(merr)
    {
        errs++;
        MPI_Error_string(merr, errmsg, &msglen);
        printf("Error in Unpublish name:\"%ls\"\n", errmsg);
        fflush(stdout);
    }
    MPI_Comm_disconnect(&newcomm);
    fprintf(stdout, "server after disconnect\n");
MPI_Finalize();
return 0;

}

  • 写回答

1条回答 默认 最新

  • qq_32641137 2018-09-15 15:08
    关注

    我试验了一下你的程序,也连不上,我也是初学者.

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧