MPI 的 manpages 需要在线查看,或者在 Linux 系统中用 man 查看,不方便。这里我做了一些对常用函数的分类总结。
基本结构:启动和终止
#include <mpi.h> // ******************1
#include <stdio.h>
int main(int argc, char** argv) {
MPI_Init(NULL, NULL); // ******************2
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
MPI_Finalize(); // ******************3
}
MPI_Init
MPI_Init(
int* argc,
char*** argv)
所有 MPI 的全局变量或者内部变量都会被创建。举例来说,一个通讯器 communicator 会根据所有可用的进程被创建出来(进程是我们通过 mpi 运行时的参数指定的),然后每个进程会被分配独一无二的秩 rank
MPI_Finalize
MPI_Finalize()
用来清理 MPI 环境的。这个调用之后就没有 MPI 函数可以被调用了。
MPI_Comm_size
MPI_Comm_size(
MPI_Comm communicator,
int* size)
返回 communicator 的大小,也就是 communicator 中可用的进程数量。
MPI_Comm_rank
MPI_Comm_rank(
MPI_Comm communicator,
int* rank)
返回 communicator 中当前进程的 rank。 communicator 中每个进程会以此得到一个从 0 开始递增的数字作为 rank 值。rank 值主要是用来指定发送或者接受信息时对应的进程。
MPI_Get_processor_name
MPI_Get_processor_name(
char* name,
int* name_length)
得到当前进程实际跑的时候所在的处理器名字。
点对点的通信:发送和接收
MPI_Send
MPI_Send(
void* data,
int count,
MPI_Datatyp