并行程序设计

阅读: 评论:0

并行程序设计

并行程序设计

什么是MPI ?官方文档:://mpi-forum/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 当时是为非常受欢迎的分布式内存架构设计的。正在上传…
取消As 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 也是应多种内存结构的类型,并且同样适用不同的协议。正在上传…
取消Today, 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 arch  然后就接着使用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 %dn",numtasks);}MPI_Finalize();            /*释放MPI系统资源*/}

这是基于C 的代码,所以使用mpicc 编译本质上使用的是GCC,用法也一样,执行使用mpicrun -np [number] 来执行。假设我们创建10个进程来执行这段代码。这段代码现在的意义是创建10个进程内部使用0开始作为进程的编号,然后打印本机名和进程编号,0号进程单独打印。执行代码:  mpirun -np 10 ./a.out结果如上。这还没有使用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 %dn",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("%sn",greeting);}}MPI_Finalize();return 0;
}

我们需要介绍几个概念与函数接口:通信子:一组可以互相发消息的集合。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 手册中获取文档第一个参数是包含消息内容的指针,第二个表示发送的数据量,第三个是数据类型。第四个指定了要接受消息的进程的进程号,第五个参数是用来区分看上去一样的消息,一般是一个非负数。最后一个参数是一个通信子,所有涉及通信的MPI的函数都需要一个通信子最重要的目的之一是指定通信的范围。这几个参数类似,前三个用于指定接受消息的内存,后面的三个参数用来识别消息。最后一个参数大部分情况下不使用。消息匹配规则

  1. recv_type = send_type. 同时recv_buf >= send_buf_sz, 这是基本规则。
  2. 发送的消息和接受的要在编程时就配对设计。
  3. 消息发送后依据数据量的大小决定返回时机。
  4. 消息不可超越,原因是MPI不能对网络性能有强制的要求。
  5. 合理的设计避免“悬挂陷阱”。
  6. 无接受的消息最终将会被丢弃。

 

本文发布于:2024-01-29 20:19:32,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170653077418026.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:程序设计
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23