mpi学习随记
根据mpi教程: 学习并总结出的mpi学习随记,希望对你有所帮助。学习原文见:A Comprehensive MPI Tutorial Resource · MPI Tutorial
目录
二、blocking point-to-point communication
三、Basic collective communication
四、Advanced collective communication
五、Introduction to Groups and Communicators
一、MPI简介与安装
1.Introduction
mpi是一个跨语言的通讯协议,用于编写并行计算机。
2.mpich的安装参见:MPICH下载安装_CSDN博客(非常有用!)
3.在这一步时,由于我没有一张信用卡or借记卡无法完成对Amazon EC2的完整注册工作hhhhh。。。所以这一步未完成。【但是不影响下面所有章节的阅读学习】
4.在“运行 MPI hello world 程序”这一环节中,我们已经在安装时添加了环境变量,所以不用做。
可以参见以下博客对mpi_hello_world.c代码做编译:mpi编译参见_CSDN博客(第三步中的测试可以基本了解mpi的使用运行方法)
mpicxx -g -Wall -o mpi_hello_world.o mpi_hello_world.cpp
mpirun -n 4 ./mpi_hello_world.o {适用于下面所有希望使用mpi运行的代码的编译}
这里全部显示ubuntu的原因是无法启动Amazon EC2,使用的全部是本机。
知识点:
-
MPI_Init
的过程中,所有 MPI 的全局变量或者内部变量都会被创建; -
MPI_COMM_WORLD
(是一个 communicator 且是 MPI 帮我们生成的)这个变量包含了当前 MPI 任务中所有的进程; -
MPI_Comm_size
会返回 communicator 的大小,也就是 communicator 中可用的进程数量; -
MPI_Comm_rank
这个函数会返回 communicator 中当前进程的 rank。
二、blocking point-to-point communication
1.MPI send and receive
The structure of Mpi sending and receiving function:
MPI_Send(void* data,
int count,
MPI_Datatype datatype,
int destination,
int tag,
MPI_Comm communicator)
MPI_Recv(void* data,
int count,
MPI_Datatype datatype,
int source,int tag,
MPI_Comm communicator,
MPI_Status* status)
这里面的ping_pong.c运行结果如下图更容易理解一些:
2.Dynamic Receiving with MPI Probe
MPI_Status/MPI_Probe —— Aim: solve the problem of uncertainty about the length of the message
MPI_Status是一个结构体,而MPI_Probe是一个函数。而MPI_Probe的优点在于分配空间更加合理。
MPI_Probe(int source,
int tag,
MPI_Comm comm,
MPI_Status* status)
3.Point-to-Point Communication Application - Random Walk
推荐看一下原始代码,可能会有更多收获与感想:
int maximum_sends_recvs = max_walk_size / (domain_size / world_size) + 1;
//question:【137行】关于这里百思不得其解?
三、Basic collective communication
1.MPI Broadcast and Collective Communication
Be attention! : 假如MPI_Bcast中的root为process 0,则0进程广播是将data的值发送给其余所有进程,而其他进程广播data的值会被赋成从0进程接收到的消息。
hhhhh诡异的事情发生了,经测试:当使用MPI_Bcast发送更少的消息时,性能更加(example:100000->1000)
2.MPI Scatter, Gather, and Allgather
3.Performing Parallel Rank with MPI
这篇中的代码很有意思,仔细阅读下会有很多收获!!!
四、Advanced collective communication
MPI Reduce and Allreduce
MPI_Reduce与MPI_Allreduce的关系和MPI_Gather与MPI_Allgather的关系类似。
五、Introduction to Groups and Communicators
最好先看 Overview of groups再看Other communicators creation functions。
注意:MPI_Comm_create
与 MPI_Comm_create_group
的主要区别在于创建新的communicator时作用的对象不一样。