MPI : Message Passing Interface
多线程编程中进程间消息传递
进程(process) 线程(thread)
threads 之间共享 process 的 address space 和resource
thread safe:线程安全,多个线程访问时不会出现race condition
两种common通信协议
Eager协议:发送进程主动发送,不考虑接收进程是否有能力接收,适合发送小消息
Rendezvous协议:在接收端协调缓存接收信息。适合发送较大消息
rank:每个进程的编号,通过指定rank进行进程间通信
displacement,lb,rb单位均为byte
#include "mpi.h"
MPI_Init(&argc, &argv); //初始化MPI
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // 获取rank
MPI_Get_processor_name(name, &namelen); // 节点hostname
MPI_Comm_size(MPI_COMM_WORLD, &size); // process数量
MPI_Finalize();
MPI_Reduce:将多个进程的结果合并到主进程
MPI_Reduce(
    void* send_data,
    void* recv_data,
    int count,
    MPI_Datatype datatype,
    MPI_Op op,
    int root,
    MPI_Comm communicator)
send_data :需要reduce的data,type为datatype
recv_data:rank root接收的信息,大小sizeof(datatype) * count
op : reduce操作符 e.g. : MPI_MAX, MPI_MIN ....
MPI_Allreduce 将多个进程结果reduce后传给每个进程
MPI_Allreduce(
    void* send_data,
    void* recv_data,
    int count,
    MPI_Datatype datatype,
    MPI_Op op,
    MPI_Comm communicator)
同reduce,不需要root
MPI_Iprobe:非阻塞对信息检验
int MPI_Iprobe(
    int source, 
    int tag, 
    MPI_Comm comm, 
    int *flag, 
    MPI_Status * status)
在接收前检测接收的信息,不需要真正接收
使用MPI_Recv接收信息
MPI_Probe:阻塞的对信息检验
int MPI_Probe(
    int source, 
    int tag, 
    MPI_Comm comm, 
    MPI_Status *status)
同MPI_Iprobe,阻塞检验
MPI_Recv:阻塞接收信息
int MPI_Recv(
    void *buf, 
    int count, 
    MPI_Datatype datatype,
    int source, 
    int tag, 
    MPI_Comm comm, 
    MPI_Status *status)
阻塞接收信息,在写完buf后返回,可以先于对应send返回
MPI_Send(&outMsg, msgLen. MPI_CHAR, receiver, tag, MPI_COMM_WORLD);
MPI_Recv(&inMsg, msgLen, MPI_CHAR, sender, tag, MPI_COMM_WORLD, &stat);
MPI_Bcast 一对多广播
MPI_Bcast(
void* buffer, 
int count, 
MPI_Datatype datatype, 
int root, 
MPI_Comm comm)
count : 信息长度
MPI_Scatter 一对多发送不同消息
MPI_Gather 多对一收集不同消息
MPI_Scatterv 一对多发送不同长度消息
MPI_Gatherv 一对多接收不同长度消息
MPI_Scan 相当于结果为前缀和的all_reduce
MPI_Init(&argc, &argv);
MPI_Finalize();
open mpi
mpirun -n 16 sst emberLoad.py #指定mpi以16个rank执行