标签:type recv amp argv class malloc 例子 ISE style
1.集合通信
(1)广播:是一对多通信的典型例子,调用格式如下:
MPI_Bcast(Address,Count,Datatype,Root,Comm)
1 #include <stdio.h> 2 #include <mpi.h> 3 4 int main(int argc,char **argv) 5 { 6 int rank; 7 int ibuf; 8 9 MPI_Init(&argc,&argv); 10 MPI_Comm_rank(MPI_COMM_WORLD,&rank); 11 12 if(rank == 0) 13 ibuf = 12345; 14 else 15 ibuf = 0; 16 17 MPI_Bcast(&ibuf,1,MPI_INT,0,MPI_COMM_WORLD); 18 19 if(rank != 0) 20 { 21 printf("my rank = %d ibuf = %d\n",rank,ibuf); 22 } 23 MPI_Finalize(); 24 25 return 0; 26 }
运行结果:
(2)广播
实例:
1 #include <stdio.h> 2 #include <mpi.h> 3 int main(int argc,char **argv) 4 { 5 int i; 6 int rank,nproc; 7 int isend,irecv[32]; 8 MPI_Comm comm = MPI_COMM_WORLD; 9 MPI_Init(&argc,&argv); 10 MPI_Comm_size(comm,&nproc); 11 MPI_Comm_rank(comm,&rank); 12 13 isend = rank + 1; 14 MPI_Gather(&isend,1,MPI_INT,irecv,1,MPI_INT,0,comm); 15 16 if(rank == 0) 17 { 18 for( i=0; i<nproc; i++) 19 { 20 printf("irecv = %d \n",irecv[i]); 21 } 22 } 23 24 MPI_Finalize(); 25 26 27 return 0; 28 }
运行结果:
(3)散播
实例:
1 #include "stdio.h" 2 #include "mpi.h" 3 #include "stdlib.h" 4 #define N 10 5 int main(int argc, char **argv) 6 { 7 int size, rank; 8 int* send; 9 int *recv; 10 int i = 0; 11 12 MPI_Init(&argc, &argv); 13 MPI_Comm_size(MPI_COMM_WORLD, &size); 14 MPI_Comm_rank(MPI_COMM_WORLD, &rank); 15 16 17 send = (int *)malloc(size*N*sizeof(int)); 18 recv = (int *)malloc(N*sizeof(int)); 19 20 if (rank == 0) 21 { 22 for (; i < size*N; i++) { 23 send[i] = i; 24 } 25 } 26 27 MPI_Scatter(send, N, MPI_INT,recv, N, MPI_INT, 0, MPI_COMM_WORLD); 28 29 30 printf("------------------------------------------------\nrank=%d\n", rank); 31 for (i = 0; i < N; i++) 32 { 33 printf("recv_buffer[%d] = %d\n", i, recv[i]); 34 } 35 printf("------------------------------------------------\n"); 36 37 MPI_Finalize(); 38 return 0; 39 }
运行结果:
实例:
标签:type recv amp argv class malloc 例子 ISE style
原文地址:https://www.cnblogs.com/lin1216/p/12899006.html