并行实验源代码

上传人:野鹰 文档编号:2635143 上传时间:2017-07-26 格式:DOCX 页数:20 大小:31.81KB
返回 下载 相关 举报
并行实验源代码_第1页
第1页 / 共20页
并行实验源代码_第2页
第2页 / 共20页
并行实验源代码_第3页
第3页 / 共20页
并行实验源代码_第4页
第4页 / 共20页
并行实验源代码_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《并行实验源代码》由会员分享,可在线阅读,更多相关《并行实验源代码(20页珍藏版)》请在金锄头文库上搜索。

1、实验一1.Hello.c#include mpi.h#include int main(int argc,char *argv)MPI_Init(&argc,&argv);printf(hello parallel world!n);MPI_Finalize();2.isend.c#include #include #include mpi.hint main(int argc,char *argv) int i, numprocs,namelen,myid;char processor_nameMPI_MAX_PROCESSOR_NAME;int buf5;int flag=0;MPI_St

2、atus status;MPI_Request r;MPI_Init( &argc, &argv );MPI_Comm_rank( MPI_COMM_WORLD, &myid );MPI_Comm_size(MPI_COMM_WORLD,&numprocs);MPI_Get_processor_name(processor_name,&namelen);if (myid = 0) for(i=0;i#include mpi.hint main(int argc, char* argv)int myid,numprocs, source;MPI_Status status; char messa

3、ge100;MPI_Init(&argc, &argv);MPI_Comm_rank(MPI_COMM_WORLD, &myid);MPI_Comm_size(MPI_COMM_WORLD,&numprocs);if (myid != 0) strcpy(message, Hello World!);MPI_Send(message,strlen(message)+1, MPI_CHAR, 0,99,MPI_COMM_WORLD); elsefor (source = 1; source #include #includetime.hmain(int argc,char *argv)int m

4、yid, numprocs;int namelen,source;long count=1000000;double y;double x;long m=0,m1=0,i=0,p=0;double pi=0.0,n=0.0;char processor_nameMPI_MAX_PROCESSOR_NAME;MPI_Status status;MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&myid);MPI_Comm_size(MPI_COMM_WORLD,&numprocs);MPI_Get_processor_name(process

5、or_name,&namelen);srand(int)time(0);for(i=0;iint main(int argc,char *argv)int myid, numprocs;int namelen;char processor_nameMPI_MAX_PROCESSOR_NAME;MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&myid);MPI_Comm_size(MPI_COMM_WORLD,&numprocs);MPI_Get_processor_name(processor_name,&namelen);printf(

6、Hello World! Process %d of %d on %sn,myid, numprocs, processor_name);MPI_Finalize();实验二1. 共享存储模型#include#include#include#define N 10000 int main(int argc, char *argv) int i;int arrayN;int count, nthreads, tid, chunk;unsigned num;chunk = 100;count = 0;double start, end;printf(please choose number of

7、threads: 1, 2 or 4.n)scanf(%d, &num); /提示输入计算线程数omp_set_num_threads(num);#pragma omp parallel shared(nthreads) tid=omp_get_thread_num();if(tid=0)nthreads=omp_get_num_threads();printf(nStarting count with %d threadsn,nthreads);start = omp_get_wtime();#pragma omp parallel for reduction(+:count) schedu

8、le(static,chunk)for( i=0; i#define MPICH_SKIP_MPICXX#include #include mpi.h#include stdlib.h#define R rand()#define N 10000int main(int argc, char* argv)int myid,numprocs;int arrayN;int countall=0;MPI_Status status;srand(int)time(0);for(int i=0;i0; i-) if(M%i=0)&(N%i=0)&(i#include #include #include

9、#include #include /* 全局变量声明 */float *A, *B, *C; /* 总矩阵,C = A * B */float *a, *b, *c, *tmp_a, *tmp_b; /* a、b 、c 表分块,tmp_a 、tmp_b 表缓冲区 */int dg, dl, dl2,p, sp; /* dg:总矩阵维数;dl:矩阵块维数;dl2=dl*dl;p: 处理器个数;spsqrt(p) */int my_rank, my_row, my_col; /* my_rank:处理器 ID;(my_row,my_col):处理器逻辑阵列坐标 */MPI_Status stat

10、us;/*函数名: get_index*功能:处理器逻辑阵列坐标至 rank 号的转换*输入:坐标、逻辑阵列维数*输出:rank 号*/int get_index(int row, int col, int sp)return (row+sp)%sp)*sp + (col+sp)%sp;/*函数名:random_A_B*功能:随机生成矩阵 A 和 B*/void random_A_B()int i,j;srand(unsigned int)time(NULL); /*设随机数种子 */*随机生成 A,B,并初始化 C*/for(i=0; idg ; i+)for(j=0; jdg ; j+)A

11、ij = rand();Bij = rand();Cij = 0.0;/* 函数名:scatter_A_B* 功能: rank 为 0 的处理器向其他处理器发送 A、B 矩阵的相关块*/void scatter_A_B()int i,j,k,l;int p_imin,p_imax,p_jmin,p_jmax;for(k=0; kp; k+)/*计算相应处理器所分得的矩阵块在总矩阵中的坐标范围*/p_jmin = (k % sp ) * dl;p_jmax = (k % sp + 1) * dl-1;p_imin = (k - (k % sp)/sp * dl;p_imax = (k - (k

12、% sp)/sp +1) *dl -1;l = 0;/*rank=0 的处理器将 A,B 中的相应块拷至 tmp_a,tmp_b,准备向其他处理器发送*/for(i=p_imin; i=p_imax; i+)for(j=p_jmin; j=p_jmax; j+)tmp_al = Aij;tmp_bl = Bij;l+;/*rank=0 的处理器直接将自己对应的矩阵块从 tmp_a,tmp_b 拷至 a,b*/if(k=0)memcpy(a, tmp_a, dl2 * sizeof(float); memcpy(b, tmp_b, dl2 * sizeof(float); else /*rank

13、=0 的处理器向其他处理器发送 tmp_a,tmp_b 中相关的矩阵块*/MPI_Send(tmp_a, dl2, MPI_FLOAT, k, 1, MPI_COMM_WORLD);MPI_Send(tmp_b, dl2, MPI_FLOAT, k, 2, MPI_COMM_WORLD);/*函数名:init_alignment*功能:矩阵 A 和 B 初始对准*/void init_alignment()/*将 A 中坐标为(i,j)的分块 A(i,j)向左循环移动 i 步*/MPI_Sendrecv(a, dl2, MPI_FLOAT, get_index(my_row,my_col-my

14、_row,sp), 1,tmp_a, dl2, MPI_FLOAT, get_index(my_row,my_col+my_row,sp), 1, MPI_COMM_WORLD, &status);memcpy(a, tmp_a, dl2 * sizeof(float) );/*将 B 中坐标为(i,j)的分块 B(i,j)向上循环移动 j 步*/MPI_Sendrecv(b, dl2, MPI_FLOAT, get_index(my_row-my_col,my_col,sp), 1,tmp_b, dl2, MPI_FLOAT, get_index(my_row+my_col,my_col,s

15、p), 1, MPI_COMM_WORLD, &status);memcpy(b, tmp_b, dl2 * sizeof(float) );/*函数名:main_shift*功能:分块矩阵左移和上移,并计算分块 c*/void main_shift()int i,j,k,l;for(l=0; lsp; l+)/*矩阵块相乘,c+=a*b */for(i=0; idl; i+)for(j=0; jdl; j+)for(k=0; kdl; k+)ci*dl+j += ai*dl+k*bk*dl+j; /* 将分块 a 左移 1 位 */MPI_Send(a , dl2, MPI_FLOAT, get_index(my_row, my_col-1, sp), 1, MPI_COMM_WORLD);MPI_Recv(a , dl2, MPI_FLOAT, get_index(my_row, my_col+1, sp), 1, MPI_COMM_WORLD, &status);/* 将分块 b 上移 1 位 */MPI_Send(b , dl2, MPI_FLOAT, get_index(my_row-1, my_col, sp), 1, MPI_COMM_WORLD

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号