为什么在 MPI_Init 之前应该有最少的工作?

Why should there be minimal work before MPI_Init?

MPICH 和 OpenMPI 的文档都提到在 MPI_Init 之前或 MPI_Finilize 之后应该完成最少的工作:

The MPI standard does not say what a program can do before an MPI_INIT or after an MPI_FINALIZE. In the MPICH implementation, you should do as little as possible.

这背后的原因是什么?

对我来说,进程在开始相互通信之前进行大量计算似乎是完全合理的。

MPICH的MPI_Init documentation给出了一些提示:

The MPI standard does not say what a program can do before an MPI_INIT or after an MPI_FINALIZE. In the MPICH implementation, you should do as little as possible. In particular, avoid anything that changes the external state of the program, such as opening files, reading standard input or writing to standard output.

顺便说一句,我不希望 MPI_Init 进行通信。这些会在以后发生。

还有 mpich/init.c implementation is free software; you can study its source code and understand that it is initializing 一些计时器、一些线程等...(这确实应该很早就发生)。

To me it seems perfectly reasonable for processes to do a significant amount of calculations before starting the communication with each other.

当然,但这些应该发生在 MPI_Init 之后(但在某些 MPI_Send 等之前)。

在某些 supercomputers, MPI might use dedicated hardware (like InfiniBand, Fibre Channel,等等...) 并且可能有一些硬件或操作系统原因需要很早地初始化它。所以尽早调用 MPI_Init 是有意义的。顺便说一句,它还提供了指向 main 参数的指针,我猜它会在 main 进一步处理之前 修改 它们。然后对 MPI_Init 的调用可能是您 main.

的第一条语句

我相信这样的措辞是为了允许 MPI 实现在 MPI_Init 中产生它的等级。这意味着并非所有等级在技术上都保证在 MPI_Init 之前存在。如果您打开了文件描述符或执行了其他对进程状态有副作用的事情,那将变得一团糟。

据我所知,当前没有主要的 MPI 实现会这样做,但是 MPI 实现可能会使用此要求来实现其他技巧。

编辑:我没有找到这方面的证据,只是从很久以前就记得了,所以我不确定。我似乎找不到您从 MPICH 引用的 MPI 标准中的公式。但是,MPI 标准规定了您可以在 MPI_Init:

之前调用哪些 MPI 函数

The only MPI functions that may be invoked before the MPI initialization routines are called are MPI_GET_VERSION, MPI_GET_LIBRARY_VERSION, MPI_INITIALIZED, MPI_FINALIZED, and any function with the prefix MPI_T_.