实验一1.Hello.c#includempi.h#includestdio.hintmain(intargc,char**argv){MPI_Init(&argc,&argv);printf(helloparallelworld!\n);MPI_Finalize();}2.isend.c#includestdio.h#includestdlib.h#includempi.hintmain(intargc,char**argv){inti,numprocs,namelen,myid;charprocessor_name[MPI_MAX_PROCESSOR_NAME];intbuf[5];intflag=0;MPI_Statusstatus;MPI_Requestr;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;i5;i++){buf[i]=i;}MPI_Isend(&buf,5,MPI_INT,1,99,MPI_COMM_WORLD,&r);}else{MPI_Irecv(&buf,5,MPI_INT,0,99,MPI_COMM_WORLD,&r);MPI_Test(&r,&flag,&status);printf(BeforeMPI_Waitflag=%d.Thebufiscan'tbeused!\n,flag);for(i=0;i5;i++){printf(buf[%d]=\t%d\n,i,buf[i]);}MPI_Wait(&r,&status);MPI_Test(&r,&flag,&status);printf(AfterMPI_Waitflag=%d.Thebufiscanbeused!\n,flag);for(i=0;i5;i++){printf(buf[%d]=\t%d\n,i,buf[i]);}}MPI_Finalize();return0;}3.message.c#includestdio.h#includempi.hintmain(intargc,char**argv){intmyid,numprocs,source;MPI_Statusstatus;charmessage[100];MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&myid);MPI_Comm_size(MPI_COMM_WORLD,&numprocs);if(myid!=0){strcpy(message,HelloWorld!);MPI_Send(message,strlen(message)+1,MPI_CHAR,0,99,MPI_COMM_WORLD);}else{for(source=1;sourcenumprocs;source++){MPI_Recv(message,100,MPI_CHAR,source,99,MPI_COMM_WORLD,&status);printf(Iamprocess%d.Irecvstring'%s'fromprocess%d.\n,myid,message,source);}}MPI_Finalize();}4.mtpi.c#includempi.h#includestdio.h#includestdlib.h#includetime.hmain(intargc,char**argv){intmyid,numprocs;intnamelen,source;longcount=1000000;doubley;doublex;longm=0,m1=0,i=0,p=0;doublepi=0.0,n=0.0;charprocessor_name[MPI_MAX_PROCESSOR_NAME];MPI_Statusstatus;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);srand((int)time(0));for(i=0;icount;i++){x=(double)rand()/(double)RAND_MAX;y=(double)rand()/(double)RAND_MAX;if((x-0.5)*(x-0.5)+(y-0.5)*(y-0.5)0.25)m++;}n=4.0*m/count;printf(Process%dof%don%spi=%f\n,myid,numprocs,processor_name,n);if(myid!=0){MPI_Send(&m,1,MPI_DOUBLE,0,1,MPI_COMM_WORLD);}else{p=m;for(source=1;sourcenumprocs;source++){MPI_Recv(&m1,1,MPI_DOUBLE,source,1,MPI_COMM_WORLD,&status);p=p+m1;}printf(pi=%f\n,4.0*p/(count*numprocs));}MPI_Finalize();}5.who.c#includempi.h#includestdio.hintmain(intargc,char**argv){intmyid,numprocs;intnamelen;charprocessor_name[MPI_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(HelloWorld!Process%dof%don%s\n,myid,numprocs,processor_name);MPI_Finalize();}实验二1.共享存储模型#includestdio.h#includestdlib.h#includeomp.h#defineN10000intmain(intargc,char*argv[]){inti;intarray[N];intcount,nthreads,tid,chunk;unsignednum;chunk=100;count=0;doublestart,end;printf(pleasechoosenumberofthreads:1,2or4.\n)scanf(%d,&num);//提示输入计算线程数omp_set_num_threads(num);#pragmaompparallelshared(nthreads){tid=omp_get_thread_num();if(tid==0)nthreads=omp_get_num_threads();printf(\nStartingcountwith%dthreads\n,nthreads);}}start=omp_get_wtime();#pragmaompparallelforreduction(+:count)schedule(static,chunk)for(i=0;iN;i++){array[i]=rand()%10;if(array[i]==3)count++;}end=omp_get_wtime();printf(Thecountcost%fsec.time.\n,end-start);printf(Thenumberof3appearedinthearrayare%d,count);}2.消息传递模型#includetime.h#defineMPICH_SKIP_MPICXX#includestdio.h#includempi.h#includestdlib.h#defineRrand()#defineN10000intmain(intargc,char**argv){intmyid,numprocs;intarray[N];intcountall=0;MPI_Statusstatus;srand((int)time(0));for(inti=0;iN;i++){array[i]=rand()%10;}MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&myid);MPI_Comm_size(MPI_COMM_WORLD,&numprocs);intcount=0,counti=0;for(intj=N/numprocs*myid;jN/numprocs*myid+N/numprocs;j++){if(array[j]==3)count++;}if(myid!=0){MPI_Send(&count,10,MPI_CHAR,0,99,MPI_COMM_WORLD);}else{countall=count;printf(Iamprocess%d,inmyprocessthenumberis%d.\n,myid,count);for(intsource=1;sourcenumprocs;source++){MPI_Recv(&counti,10,MPI_CHAR,source,99,MPI_COMM_WORLD,&status);countall=countall+counti;printf(Iamprocess%d,Ireceivethenumber%dfrom%d.\n,myid,counti,source);}printf(Iamprocess%d,Thetotalof3fortheallareais%d.\n,myid,countall);}MPI_Finalize();}实验三1.矩阵相乘简单划分并行算法#includestdio.h#includestdlib.h#includempi.h#defineintsizesizeof(int)#definefloatsizesizeof(float)#definecharsizesizeof(char)#defineA(x,y)A[x*K+y]#defineB(x,y)B[x*N+y]#defineC(x,y)C[x*N+y]#definea(x,y)a[x*K+y]#defineb(x,y)b[x*n+y]#definebuffer(x,y)buffer[x*n+y]/*此宏用来简化对标号为奇数的处理器内的缓冲空间的访问*/#definec(l,x,y)c[x*N+y+l*n]float*a,*b,*c,*buffer;ints;float*A,*B,*C;/*A[M,K],B[P,N].正确的情况下K应该等于P,否则无法进行矩阵相乘*/intM,N,K,P;intm,n;intmyid;intp;/*保存工作站集群中处理器数目,也即通信子大小*/FILE*dataFile;/*用于读取输入文件内容和将计算结果输出到结果文件的临时文件指针*/MPI_Statusstatus;doubletime1;doublestarttime,endtime;/**函数名:readData*功能:此函数被rankID为0的进程调用,负责从dataIn.txt文件中读入*A[M,K],B[P,N]两个相乘矩阵的数据,并为结果矩阵C[M,N]分配空间。*其中C[N,N]=A[M,K]*B[P,N]*