话说 直插排序串行只需要十几行 并行就大不一样。。。
基本思路是
1/主线程控制数据读取 分发和汇总
2/从线程中数据按线程号非降有序 每次有新数据 主线程广播 从线程按照数据大小判断是否在自己处理范围内 如果在的话 在自己保存的数组中进行直接插入
3/最后 主线程按序收集显示所有数据
#include "mpi.h"
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define END 0
#define INIT 1
#define DATA 2
#define ROOT 0
int readValue(char* file,int* values)
{
int fd,size,count=0;
char buffer[80],num[11];
fd=open(file,O_RDONLY);
do
{
size=read(fd,buffer,sizeof(buffer));
int j=0;
for(int i=0;i<size;++i)
{
if(buffer[i]<'9'&&buffer[i]>'0')
{
num[j++]=buffer[i];
}else
{
num[j]='\0';
values[count++]=atoi(num);
j=0;
}
}
}while(size!=0);
close(fd);
return count;
}
void insert(int* arr,int pos,int temp)
{
while(pos>=0&&temp<arr[pos])
{
arr[pos+1]=arr[pos];
--pos;
}
arr[pos+1]=temp;
}
void serial(int* arr,int size)
{
for(int i=1;i<size;++i)
{
insert(arr,i-1,arr[i]);
}
}
int main(int argc,char *argv[])
{
int* values;
int self,size,length,temp;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&self);
MPI_Status status;
if(self==0)
{
//read value from file
values=(int*)malloc(100*sizeof(int));
length=readValue("/home/gt/parellel/sort/data.in",values);
//initial root value of each process
serial(values,size);
//broadcast the max length of each process
MPI_Bcast(&length,1,MPI_INT,ROOT,MPI_COMM_WORLD);
//the boundary
for(int i=1;i<size;++i)
{
MPI_Ssend(&values[i-1],1,MPI_INT,i,INIT,MPI_COMM_WORLD);
}
MPI_Recv(&temp,1,MPI_INT,self+1,DATA,MPI_COMM_WORLD,&status);
//broadcast each value and do insert in each process
for(int i=size-1;i<length;++i)
{
temp=values[i];
MPI_Bcast(&temp,1,MPI_INT,ROOT,MPI_COMM_WORLD);
printf("broadcast %d \n",values[i]);
//MPI_Barrier(MPI_COMM_WORLD);
}
//ends
printf("ends \n");
temp=END;
MPI_Bcast(&temp,1,MPI_INT,ROOT,MPI_COMM_WORLD);
//recieve from each process in order
int pos=0;
int len=0;
for(int i=1;i<size;++i)
{
MPI_Recv(&len,1,MPI_INT,i,DATA,MPI_COMM_WORLD,&status);
for(int j=0;j<len;++j)
{
MPI_Recv(&temp,1,MPI_INT,i,DATA,MPI_COMM_WORLD,&status);
values[pos++]=temp;
}
}
for(int i=0;i<length;++i)
{
printf("%d \n",values[i]);
}
free(values);
}
else
{
MPI_Request request;
//recieve max length
MPI_Bcast(&length,1,MPI_INT,ROOT,MPI_COMM_WORLD);
//position
int pos=0;
//post irecv
MPI_Irecv(&temp,1,MPI_INT,ROOT,INIT,MPI_COMM_WORLD,&request);
//data on this process
int* local=(int*) malloc(length*sizeof(int)+1);
//wait for the recieve
MPI_Wait(&request,&status);
//exchange upper limit
int upper=999999;//max of int
if(self!=size-1)MPI_Irecv(&upper,1,MPI_INT,self+1,DATA,MPI_COMM_WORLD,&request);
MPI_Ssend(&temp,1,MPI_INT,self-1,DATA,MPI_COMM_WORLD);
if(self!=size-1)MPI_Wait(&request,&status);
local[pos++]=temp;
MPI_Bcast(&temp,1,MPI_INT,ROOT,MPI_COMM_WORLD);
//MPI_Barrier(MPI_COMM_WORLD);
while(temp!=END)
{
printf("%d recieve %d \n",self,temp);
if(temp<=upper&&(self==1||local[0]<temp))
{
printf("%d insert %d \n",self,temp);
local[pos++]=temp;
insert(local,pos-2,temp);
}
MPI_Bcast(&temp,1,MPI_INT,ROOT,MPI_COMM_WORLD);
//MPI_Barrier(MPI_COMM_WORLD);
}
//ends
//sends the data in order to root process
MPI_Ssend(&pos,1,MPI_INT,ROOT,DATA,MPI_COMM_WORLD);
for(int i=0;i<pos;++i)
{
MPI_Ssend(&local[i],1,MPI_INT,ROOT,DATA,MPI_COMM_WORLD);
printf("% d send back %d \n",self,local[i]);
}
free(local);
}
MPI_Finalize();
return 0;
}
分享到:
相关推荐
利用MPI实现快速排序的并行算法,算法使用C语言实现
矩阵乘法mpi实现 并行运算 在linux下成功运行 使用mpicc -o 编译 使用mpirun命令运行
使用MPI计算的完整的PSRS(并行排序(parallel sorting by regular sampling))代码。并行计算课实验所用代码。
运用mpi实现奇偶排序,在不同的处理器之间通过消息传递完成奇偶index的数的交换,实现最终的数列排序
Linux 环境下 MPI 并行编程与算法实现研究 ...本文简要介绍了 MPI 并行编程环境、MPI 并行编程模式、MPI 并行算法实现等知识点,并对实验结果进行了分析,证明了使用 MPI 实现并行算法可以取得较好的性能。
对该算法的并行化是很简单的,假设对一个长为n的输入序列使用n个处理器进行排序,只需使每个处理器负责完成对其中一个元素的定位,然后将所有的定位信息集中到主进程钟,由主进程负责完成所有元素的最终排位。
用MPI实现的快速排序,提高快速排序的效率
墨尔本大学研究生并行计算课程的一个作业,包含一个Sequential代码和一个blocking communication和一个nonblocking communication的并行代码,均是用C语言完成,并附有完整的report。可在集群计算机上并行运行
基于Linux平台下的MPI并行PC集群搭建是指使用MPI实现并行计算的PC集群,在Linux平台下搭建的并行PC集群能够满足高性能计算的需求。 四、集群系统的特点 集群系统是充分利用计算资源的一个重要概念。集群系统的主要...
基于MPI的并行计算实现Jacobi迭代
该程序基于 Boost 和 MPI 实现了Cannon 算法实现对矩阵得并行求积。
MPI并行设计 MPI MPI并行设计 MPI并行设计 一个小程序
MPI 和 OpenMP 并行计算 - 冒泡排序 本实验报告的目的是比较 MPI 和 OpenMP 两种并行计算方式在冒泡排序算法中的应用和性能差异。实验中,我们将串行冒泡排序程序改为 MPI 并行程序和 OpenMP 并行程序,并比较它们...
代码稍微有点儿缺陷,默认是10个数据,用5个进程进行排序。所以其中的比较函数写死了,大家可以自己重新改一下这里。
fft并行算法实现,C语言开发,用mpi编程完成 陈国良并行算法的设计与分析中算法的源码全部
mpi并行程序设计
高斯消元法的MPI并行化,使用C++编写,通过MPI平台调试结果正确无误
为了实现并行计算,是否使用MPI技术即可实现? 3.2. MPI技术原理是什么,即基础设施提供什么样的支持能力? 3.3. 为了实现并行计算,应用软件需要什么样的特殊设计 3.4. 什么样的软件需要并行计算 4. 部分参考资料
生命游戏在C语言基础上的MPI并行实现,运行环境VS+MPICH2
使用 MPI(通过 mpi4py python 包)使用主从实现模型在多个节点上并行化粒子群优化算法的代码。 被粒子群优化算法最小化的目标函数是 Alpine 1 函数,它通过引入 0.1 秒的暂停而人为地变得昂贵。 设置 并行化的...