并行程序设计---MPI

来源:互联网 发布:gta5低配优化补丁3dm 编辑:程序博客网 时间:2024/05/29 17:44
什么是MPI ?官方文档:https://computing.llnl.gov/tutorials/mpi/#Abstracthttp://mpi-forum.org/docs/M P I = Message Passing Interface. 消息传递接口MPI is a specification for the developers and users of message passing libraries. By itself, it is NOT a library - but rather the specification of what such a library should be.MPI 是一种特殊的提供给开发者和用户的库,就他自己而言,它本身不是一个库,而是规定如何调用的规则。程序模型:Originally, MPI was designed for distributed memory architectures, which were becoming increasingly popular at that time (1980s - early 1990s).最早,MPI 当时是为非常受欢迎的分布式内存架构设计的。distributed_memAs architecture trends changed, shared memory SMPs were combined over networks creating hybrid distributed memory / shared memory systems.随着架构的变化,共享内存模式SMP 包括由网络连接的混合分布式内存结构,和共享内存结构。MPI implementors adapted their libraries to handle both types of underlying memory architectures seamlessly. They also adapted/developed ways of handling different interconnects and protocols.MPI 也是应多种内存结构的类型,并且同样适用不同的协议。hybrid_memToday, MPI runs on virtually any hardware platform:
  • Distributed Memory
  • Shared Memory
  • Hybrid
今天MPI 工作在多种硬件平台上,包括分布式平台,共享内存平台,混合架构平台。Reasons for Using MPI:Standardization - MPI is the only message passing library that can be considered a standard. It is supported on virtually all HPC platforms. Practically, it has replaced all previous message passing libraries.Portability - There is little or no need to modify your source code when you port your application to a different platform that supports (and is compliant with) the MPI standard.Performance Opportunities - Vendor implementations should be able to exploit native hardware features to optimize performance. Any implementation is free to develop optimized algorithms.Functionality - There are over 430 routines defined in MPI-3, which includes the majority of those in MPI-2 and MPI-1.Most MPI programs can be written using a dozen or less routines使用MPI 的原因:标准化     可移植性      多样化       多功能历史版本:
  • 1995   MPI 1.1
  • 1997   MPI 1.2
  • 2008   MPI 1.3
  • 2008   MPI-2.1
  • 2009   MPI-2.2
  • 2015   MPI-3.1
平台标准:
  • MVAPICH - Linux clusters
  • Open MPI - Linux clusters
  • IBM MPI - BG/Q clusters
环境安装我使用的是fedora 22 使用了阿里的软件源。直接使用yum install mpich.noarch  然后就接着使用yum  安装就行了。这里有一段测试代码。
#include<stdio.h>#include<stdlib.h>#include"mpi.h"#define MASTER     0      /*0号进程*/int main(int argc ,char **argv){    int numtasks,taskid,len;    /*进程数,进程ID,消息长度*/    char hostname[MPI_MAX_PROCESSOR_NAME];    MPI_Init(&argc,&argv);      /*初始化MPI 系统*/    MPI_Comm_size(MPI_COMM_WORLD,&numtasks);   /*设置工作进程的个数*/    MPI_Comm_rank(MPI_COMM_WORLD,&taskid);     /*设置集进程的ID*/    MPI_Get_processor_name(hostname,&len);     /*获取本机名称*/    printf("Hello from task %d on %s \n",taskid,hostname);    if(taskid == MASTER){        printf("MASTER number of MPI task is %d\n",numtasks);    }    MPI_Finalize();            /*释放MPI系统资源*/}
这是基于C 的代码,所以使用mpicc 编译本质上使用的是GCC,用法也一样,执行使用mpicrun -np [number] 来执行。假设我们创建10个进程来执行这段代码。这段代码现在的意义是创建10个进程内部使用0开始作为进程的编号,然后打印本机名和进程编号,0号进程单独打印。执行代码:  mpirun -np 10 ./a.out2017-01-20 15-51-23 的屏幕截图结果如上。这还没有使用MPI 核心的消息传递功能,接着我们使用MPI的消息传递功能。这个程序使用一个进程接受消息,其余进程发送消息的方式工作。
#include<stdio.h>#include<stdlib.h>#include<string.h>#include"mpi.h"const int MAX_STRING = 100;int main(int argc,char **argv){    char greeting[MAX_STRING];    int  comm_sz;    int  my_rank;        MPI_Init(NULL,NULL);    MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);    MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);    if(my_rank != 0){   /*如果不是0号进程就执行发送函数*/        sprintf(greeting,"greenting form process %d of %d\n",my_rank,comm_sz);        MPI_Send(greeting,strlen(greeting)+1,MPI_CHAR,0,0,MPI_COMM_WORLD);    }else{        printf("Greetings from process %d of %d \n",my_rank,comm_sz);        for(int q = 1;q < comm_sz;q++){    /*0号进程调用接受函数打印各个进程发送的消息*/            MPI_Recv(greeting,MAX_STRING,MPI_CHAR,q,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);            printf("%s\n",greeting);        }    }    MPI_Finalize();    return 0;}
2017-01-20 16-21-56 的屏幕截图我们需要介绍几个概念与函数接口:通信子:一组可以互相发消息的集合。MPI_init 的其中的一个目的,就是在用户启动的时候,定义由用户启动的所有进程所组成的通信子。名字就是MPI__COMM_WORLD 。其中我们可以使用MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);这两个函数来获取其中的信息。SPMD程序,我们现在的模式叫做单指令多数据并行。接着我们来看看MPI 的两个核心函数,SEND 和 RECV。我们从men 手册中获取文档2017-01-20 16-35-45 的屏幕截图第一个参数是包含消息内容的指针,第二个表示发送的数据量,第三个是数据类型。第四个指定了要接受消息的进程的进程号,第五个参数是用来区分看上去一样的消息,一般是一个非负数。最后一个参数是一个通信子,所有涉及通信的MPI的函数都需要一个通信子最重要的目的之一是指定通信的范围。2017-01-20 16-44-14 的屏幕截图这几个参数类似,前三个用于指定接受消息的内存,后面的三个参数用来识别消息。最后一个参数大部分情况下不使用。消息匹配规则
  1. recv_type = send_type. 同时recv_buf >= send_buf_sz, 这是基本规则。
  2. 发送的消息和接受的要在编程时就配对设计。
  3. 消息发送后依据数据量的大小决定返回时机。
  4. 消息不可超越,原因是MPI不能对网络性能有强制的要求。
  5. 合理的设计避免“悬挂陷阱”。
  6. 无接受的消息最终将会被丢弃。

1 0
原创粉丝点击