采用MPI_Send 和MPI_Recv 编写代码来实现包括MPI_Bcast、MPI_Alltoall、MPI_Gather、MPI_Scatter 等MPI 群集通信函数的功能
2014-05-27 15:36
519 查看
本文引自百度文库
一、实验内容
1、采用MPI_Send 和MPI_Recv 编写代码来实现包括MPI_Bcast、MPI_Alltoall、MPI_Gather、MPI_Scatter 等MPI 群集通信函数的功能。2、给出抽象的并行算法描述、程序源代码及运行时性能加速图表。
二、抽象并行算法描述
ⅰ、MPI_Bcast算法描述:输入:并行处理器个数ProcessNumber
输出:所有进程收到来自Root进程的消息
Procedure MPI_Bcast_a
Begin
对所有处理器同时执行如下的算法:
(1)if 进程为Root进程
(1.1)将message写入消息缓冲
(1.2)for index=0 to size-1 do
向index发送相同的message消息
end for
(2)进程不是Root进程
(2.1)接收消息
End
2、MPI_Alltoall算法描述:
输入:并行处理器个数ProcessNumber
输出:所有进程收到来自其他进程不同的消息
Procedure MPI_Alltoall_a
Begin
对所有处理器同时执行如下的算法:
(1)将message写入消息缓冲
(2)for index=0 to size-1 do
分别向index发送不同的message消息
end for
(3)for source=0 to size-1 do
接收来自source的message消息
end for
End
3、MPI_Gather算法描述:
输入:并行处理器个数ProcessNumber
输出: Root进程从通信域Comm的所有进程接收消息
Procedure MPI_Gather_a
Begin
对所有处理器同时执行如下的算法:
(1)if 进程为Root进程
(1.1)for source=0 to size-1 do
接收来自source的message消息
end for
(2)向Root进程发送自己的消息
End
4、MPI_Scatter算法描述:
输入:并行处理器个数ProcessNumber
输出:向所有进程发送不同的消息
Procedure MPI_Scatter_a
Begin
对所有处理器同时执行如下的算法:
(1)if 进程为Root进程
(1.1)将message写入消息缓冲
(1.2)for index=0 to size-1 do
向index发送不同的message消息
end for
(2)进程不是Root进程
(2.1)接收消息
End
三、程序源代码
#include "mpi.h" /*MPI head file*/#include <stdio.h>
#include <string.h>
#define ProNum 4
void MPI_Bcast_a(int rank,int size,int tag){ //
int index;
char message[100];
MPI_Status status;
if (rank==0){
strcpy(message,"Hello,the message is from process root!\n ");
for(index=0;index<size;index++){
MPI_Send(message,strlen(message), MPI_CHAR, index, tag, MPI_COMM_WORLD); /*sending data to node#1*/
}
printf("There are %d processes in the group.\n",size);
}
else{
MPI_Recv(message,100, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
printf("Process %d received %s\n",rank,message);
}
}
void MPI_Alltoall_a(int rank,int size,int tag){ //
int index,source;
char message[100];
char buffers[ProNum][100];//if buffers *[100],there will be errors.
char buffer[10];
MPI_Status status;
strcpy(message,"Hello,the message is from process ");
//message="Hello,the message is from process ";
snprintf(buffer,10,"%d",rank);
strcat(message,buffer);
for(index=0;index<size;index++){
MPI_Send(message,strlen(message), MPI_CHAR, index, tag, MPI_COMM_WORLD); /*sending data to node#1*/
}
printf("There are %d processes in the group.\n",size);
for(source=0;source<size;source++){
MPI_Recv(buffers[source],100, MPI_CHAR,source, tag, MPI_COMM_WORLD, &status);
printf("Process %d received %s\n",rank,buffers[source]);
}
}
void MPI_Gather_a(int rank,int size,int tag){ //
int index,source;
char message[100];
char buffers[ProNum][100];//
char buffer[10];
MPI_Status status;
strcpy(message,"Hello,the message is from process ");
//message="Hello,the message is from process ";
snprintf(buffer,10,"%d",rank);
strcat(message,buffer);
MPI_Send(message,strlen(message), MPI_CHAR, 0, tag, MPI_COMM_WORLD); /*sending data to node#1*/
printf("There are %d processes in the group.\n",size);
if(rank==0){
for(source=0;source<size;source++){
MPI_Recv(buffers[source],100, MPI_CHAR,source, tag, MPI_COMM_WORLD, &status);
printf("Process %d received %s\n",rank,buffers[source]);
}
}
}
void MPI_Scatter_a(int rank,int size,int tag){ //
int index,source;
char message[100];
//char buffers[ProNum][100];//if buffers *[100],there will be errors.
char buffer[10];
MPI_Status status;
strcpy(message,"Hello,the message is to process ");
//message="Hello,the message is from process ";
if(rank==0){
for(index=0;index<size;index++){
snprintf(buffer,10,"%d",index);
strcat(message,buffer);
MPI_Send(message,strlen(message), MPI_CHAR, index, tag, MPI_COMM_WORLD); /*sending data to node#1*/
}
}
printf("There are %d processes in the group.\n",size);
MPI_Recv(message,100, MPI_CHAR,0, tag, MPI_COMM_WORLD, &status);
printf("Process %d received %s\n",rank,message);
}
int main( int argc, char** argv )
{
int rank, size,index, tag=1;
int senddata,recvdata;
MPI_Status status;
MPI_Init(&argc, &argv); /*initializing */
MPI_Comm_rank(MPI_COMM_WORLD, &rank); /*Process#*/
MPI_Comm_size(MPI_COMM_WORLD, &size); /*Total processes#*/
MPI_Bcast_a(rank,size,tag);
MPI_Alltoall_a(rank,size,tag);
MPI_Gather_a(rank,size,tag);
MPI_Scatter_a(rank,size,tag);
MPI_Finalize(); /*quit from MPI world*/
return (0);
}
相关文章推荐
- 采用 MPI_Send 和 MPI_Recv 编写代码来实现 MPI_Allgather 的功能
- MPI用MPI_Send, MPI_Recv实现MPI_Alltoall的块方法
- 用MPI_Send和MPI_Recv实现简单集群通信函数
- 8、请编写能直接实现 int atoi(const char * pstr)函数功能的代码
- 请编写实现malloc()内存分配函数功能一样的代码。给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。
- 编写实现strstr()函数功能的代码
- MPI之自己实现Alltoall()函数
- 请编写能直接实现 int atoi(const char * pstr)函数功能的代码
- NHibernate+spring.net+jquery打造UI控件库(mvc+webform两种实现) combox,shadow控件的初步实现(包括键盘,过滤功能)(附代码下载)
- 解析PHP留言本模块主要功能的函数说明(代码可实现)
- 解析PHP留言本模块主要功能的函数说明(代码可实现)
- 4、实现malloc内存分配函数功能一样的代码
- SharePoint【ECMAScript对象模型系列】-- 02. 实现编写代码时的智能提示功能
- 【MPI】使用非阻塞Send/Recv实现并行折叠整数和
- [MPI] MPI 组通信 -- 全互换 MPI_Alltoall
- 编写一个程序 提示用户输入三个数集,每个包括5个数 实现以下功能 (图)
- 编写一个函数,实现把C/C++程序代码中的注释去掉,并把结果返回。
- 编写一个函数 实现strncpy一样的功能(图)
- Sharepoint学习笔记—ECMAScript对象模型系列-- 2、实现编写代码时的智能提示功能
- 利用VA实现VS编写WDK代码高亮和自动提示功能