1 #include <stdio.h> 2 #include "mpi.h" 3 4 int main( int argc, char **argv ) 5 { 6 int rank, value; 7 MPI_Init( &argc, &argv ); 8 9 MPI_Comm_rank( MPI_COMM_WORLD, &rank ); 10 do { 11 if (rank == 0) 12 scanf( "%d", &value ); 13 14 MPI_Bcast( &value, 1, MPI_INT, 0, MPI_COMM_WORLD ); 15 16 printf( "Process %d got %d\n", rank, value ); 17 fflush(stdout); 18 19 } while (value >= 0); 20 21 MPI_Finalize( ); 22 return 0; 23 } |
1 #include <stdio.h> 2 #include <string.h> 3 #include <mpi.h> 4 5 #define TRUE 1 6 #define FALSE 0 7 #define MASTER_RANK 0 8 9 int main( int argc, char *argv[] ) 10 { 11 int count, pool_size, my_rank, my_name_length, i_am_the_master = FALSE; 12 char my_name[BUFSIZ/2], master_name[BUFSIZ/2], send_buffer[2*BUFSIZ], 13 recv_buffer[2*BUFSIZ]; 14 MPI_Status status; 15 16 MPI_Init(&argc, &argv); 17 MPI_Comm_size(MPI_COMM_WORLD, &pool_size); 18 MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); 19 MPI_Get_processor_name(my_name, &my_name_length); 20 21 if (my_rank == MASTER_RANK) { 22 i_am_the_master = TRUE; 23 strcpy (master_name, my_name); 24 } 25 26 MPI_Bcast(master_name, BUFSIZ, MPI_CHAR, MASTER_RANK, MPI_COMM_WORLD); 27 28 sprintf(send_buffer, "hello %s, greetings from %s, rank = %d", 29 master_name, my_name, my_rank); 30 MPI_Send (send_buffer, strlen(send_buffer) + 1, MPI_CHAR, 31 MASTER_RANK, 0, MPI_COMM_WORLD); 32 33 if (i_am_the_master) { 34 for (count = 1; count <= pool_size; count++) { 35 MPI_Recv (recv_buffer, BUFSIZ, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, 36 MPI_COMM_WORLD, &status); 37 printf ("%s\n", recv_buffer); 38 } 39 } 40 41 MPI_Finalize(); 42 } |
1 #include <stdio.h> 2 #include "mpi.h" 3 #include <math.h> 4 5 int main( int argc, char *argv[] ) 6 { 7 int done = 0, n, myid, numprocs, i, rc; 8 double PI25DT = 3.141592653589793238462643; 9 double mypi, pi, h, sum, x, a; 10 11 MPI_Init(&argc,&argv); 12 MPI_Comm_size(MPI_COMM_WORLD,&numprocs); 13 MPI_Comm_rank(MPI_COMM_WORLD,&myid); 14 while (!done) 15 { 16 if (myid == 0) { 17 printf("Enter the number of intervals: (0 quits) "); 18 scanf("%d",&n); 19 } 20 MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); 21 if (n == 0) break; 22 23 h = 1.0 / (double) n; 24 sum = 0.0; 25 for (i = myid + 1; i <= n; i += numprocs) { 26 x = h * ((double)i - 0.5); 27 sum += 4.0 / (1.0 + x*x); 28 } 29 mypi = h * sum; 30 31 MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); 32 33 if (myid == 0) 34 printf("pi is approximately %.16f, Error is %.16f\n", 35 pi, fabs(pi - PI25DT)); 36 } 37 MPI_Finalize(); 38 } |