OpenMPI的底层实现:
我们知道,OpenMPI应用起来还是比较简单的,但是如果让我自己来实现一个MPI的并行计算,你会怎么设计呢?————这就涉及到比较底层的东西了。
回想起我们最简单的代码,通过comm_rank来决定做不同的事情,那么这个comm_rank是怎么得到的呢?
源代码从哪里看起?在百度,谷歌都没有找到关于源码剖析的一些资料,只能先找找头文件
mpi.h搜索找到了在ompi/include/mpi.h.in中的一个文件,查找一下最简单的函数 MPI_Comm_size 和 MPI_Init 函数,
作为总头文件,所有函数声明似乎都在这里了,那么函数定义怎么找呢....如果有vs那样的F12就爽歪歪了。
面对的应用场景: 我要找OpenMpi文件目录下含有字符串 "MPI_Init"的文本文件,有什么工具吗?
幸好,windows已经提供了这样一个cmd命令findstr————参考 http://www.netingcn.com/window-findstr-command.html
搜出来了,发现有一大堆文件满足上述条件啊,突然间看到C文件里面有很多熟悉的函数名
那么应该是一个函数对应一个文件了,去找找看!
有comm_rank.c 还有 init.c:
进入init.c发现了入门第一个函数: MPI_Init(int *argc, char ***argv)
由于是第一个接触到的函数,所以要认真仔细地一行一行去学习:
#include "ompi_config.h"
#include <stdlib.h>
#include "opal/util/show_help.h"
#include "ompi/mpi/c/bindings.h"
#include "ompi/communicator/communicator.h"
#include "ompi/errhandler/errhandler.h"
#include "ompi/constants.h"
#if OMPI_BUILD_MPI_PROFILING
#if OPAL_HAVE_WEAK_SYMBOLS
#pragma weak MPI_Init = PMPI_Init
#endif
#define MPI_Init PMPI_Init
#endif
static const char FUNC_NAME[] = "MPI_Init";
int MPI_Init(int *argc, char ***argv)
{
int err;
int provided;
char *env;
int required = MPI_THREAD_SINGLE;
/* check for environment overrides for required thread level. If
there is, check to see that it is a valid/supported thread level.
If not, default to MPI_THREAD_MULTIPLE. */
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
required = atoi(env);
if (required < MPI_THREAD_SINGLE || required > MPI_THREAD_MULTIPLE) {
required = MPI_THREAD_MULTIPLE;
}
}
/* Call the back-end initialization function (we need to put as
little in this function as possible so that if it's profiled, we
do