我正在使用 openMPI,而且我有一个奇怪的错误。
看来,即使在 MPI_Finalize() 之后,每个线程仍在运行。
我遵循了一个简单的 Hello World 程序的指南,它看起来像这样:
#include <mpi.h>;
int main(int argc, char** argv) {
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Print off a hello world message
printf("Hello world from processor %s, rank %d"
" out of %d processors\n",
processor_name, world_rank, world_size);
// Finalize the MPI environment.
MPI_Finalize();
printf("This is after finalize");
}
注意最后一个 printf()... 这应该只打印一次,因为并行部分已经完成,对吧?!
但是,例如,如果我使用 6 个处理器运行该程序,则该程序的输出是:
mpirun -np 6 ./hello_world
Hello world from processor ubuntu, rank 2 out of 6 processors
Hello world from processor ubuntu, rank 1 out of 6 processors
Hello world from processor ubuntu, rank 3 out of 6 processors
Hello world from processor ubuntu, rank 0 out of 6 processors
Hello world from processor ubuntu, rank 4 out of 6 processors
Hello world from processor ubuntu, rank 5 out of 6 processors
This is after finalize...
This is after finalize...
This is after finalize...
This is after finalize...
This is after finalize...
This is after finalize...
我误解了 MPI 的工作原理吗?每个线程/进程不应该被 finalize 停止吗?
最佳答案
这只是未定义的行为。
The number of processes running after this routine is called is undefined; it is best not to perform much more than a return rc after calling MPI_Finalize.
http://www.mpich.org/static/docs/v3.1/www3/MPI_Finalize.html
关于c - MPI_Finalize() 不结束任何进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30439856/