c - MPI_Finalize() 不结束任何进程

标签 c parallel-processing mpi openmpi

我正在使用 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/

相关文章:

c - dlmalloc 中的内存对齐掩码

c - 文件指针指向的地址是什么?

c - 如何编写用于分析的 MPI 库

python - 如何使用 MPI 在 Python 中的进程之间共享数据?

c - Windows目录是存放临时文件的地方 - GetTempPath函数

c - 使用信号量编译但未运行的生产者-消费者代码

c++ - OpenCL - GPU 总和和 CPU 总和不一样

node.js - 将函数作为单独的 node.js 进程运行?

java - 如何并行调用独立函数并在使用 CompletableFuture 失败时抛出异常

我可以使用 MPI 中的 API 读取远程文件吗?