简体   繁体   English

MPI_Ssend和MPI_Recv的意外行为

[英]Unexpected behavior with MPI_Ssend and MPI_Recv

I have found some unexpected behavior in MPI (using C++) in this small code example: 我在这个小代码示例中发现了MPI中的一些意外行为(使用C ++):

int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

if(rank == 1) {
    int *sendDone = (int*)malloc(sizeof(int));    
    *sendDone = 1;
    MPI_Ssend(sendDone,1,MPI_INT, 0, 1,MPI_COMM_WORLD);  
    foo();    
} else {
    int *rcvDone = (int*)malloc(sizeof(int));
    bar();
    while(*rcvDone != 1) {
        MPI_Recv(rcvDone,1,MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    }
    cout << *rcvDone << endl;
}
MPI_Finalize();

It is compiled and run with the following commands: 它使用以下命令编译和运行:

mpic++ sim.cc -o sim
mpirun -n 2 ./sim

The Code should be executed in the following order: 该守则应按以下顺序执行:

bar();
foo();

Because the Process #0 is starting to receive after the execution of bar() . 因为在执行bar()之后进程#0开始接收。 But in reality, foo() is sometimes starting before bar() is finished. 但实际上, foo()有时会在bar()完成之前启动。 Can somebody explain that to me and give a solution to the problem? 有人可以向我解释并解决问题吗?

  1. You haven't said how you do the check which function is called first. 您还没有说过如何检查首先调用哪个函数。 Cout'ing something on screen doesn't guarantee proper order of displaying messages (at least while using MPI). 在屏幕上创建一些东西并不能保证显示消息的正确顺序(至少在使用MPI时)。

  2. You don't need to put MPI_Recv in loop since it is a blocking function. 您不需要将MPI_Recv置于循环中,因为它是阻塞函数。 Its not even recommended while you didn't assign starting value to *rcvDone. 在没有为* rcvDone分配起始值时,甚至不建议使用它。

  3. Its not safe to use malloc together with some MPI functions. 将malloc与一些MPI功能一起使用是不安全的。 Read "thread and interrupt safety" section on http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Ssend.html 阅读http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Ssend.html上的“线程和中断安全”部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM