1 #include <stdio.h> 2 #include <mpi.h> 3 4 int main(int argc, char **argv) 5 { 6 int size, my_rank; 7 MPI_Init(&argc,&argv); 8 MPI_Comm_size(MPI_COMM_WORLD,&size); 9 MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); 10 // printf("Executed by all processors: Hello! It is processor %d.\n", my_rank); 11 if (my_rank == 0) 12 { 13 printf("Hello! It is processor 0. There are %d processors in this comm. world.\n", size); 14 printf ("I am process %i out of %i: Hello world!\n",my_rank, size); 15 } 16 else 17 { 18 printf("I am process %i out of %i: Hello world!\n", my_rank, size); 19 } 20 MPI_Finalize(); 21 return 0; 22 } |
1 #include <stdio.h> 2 #include <mpi.h> 3 #include <string.h> 4 5 int main(int argc, char **argv) 6 { 7 int my_rank; /* rank of process */ 8 int size; /* number of processes */ 9 int dest; /* rank of receiver */ 10 int my_age = 4; /* storage for my_age */ 11 char message[100]; /* storage for message */ 12 int recv_my_age = 0; /* storage for received my_age */ 13 MPI_Status status; /* return status for receive */ 14 15 MPI_Init(&argc, &argv); /* Start up MPI */ 16 MPI_Comm_size(MPI_COMM_WORLD,&size); /* Find out number of processes */ 17 MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); /* Find out process rank */ 18 if(my_rank == 0) /* rank of sender */ 19 { 20 sprintf(message,"IKC-MH.57"); /* Create message */ 21 for(dest=1; dest<size; dest++) 22 { 23 printf("Sending to worker num:%d\n", dest); 24 MPI_Send(&my_age, 1, MPI_INT, dest, 1, MPI_COMM_WORLD); 25 MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, 2, MPI_COMM_WORLD); 26 } 27 } 28 else 29 { 30 MPI_Recv(&recv_my_age, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); 31 MPI_Recv(message, sizeof(message), MPI_CHAR, 0, 2, MPI_COMM_WORLD, &status); 32 printf("===========================\n"); 33 printf("I am node: %d\n", my_rank); 34 printf("My age: %d\n", recv_my_age); 35 printf("My name: %s\n", message); 36 printf("===========================\n"); 37 } 38 MPI_Finalize(); /* Shut down MPI */ 39 return 0; 40 } |