Cleaned up src/MPI2_message_passing/.
[parallel_computing.git] / src / MPI2_message_passing / simple_message.c
index 2865852e45764edc75ef246f09d789efb7e2ba10..00d93b4e916a815b88e8e376b49846161e9c7a0b 100644 (file)
@@ -1,44 +1,42 @@
+/* Simplest code to illustrate Message Passing */
+/* Nodes 1 to size-1 send messages to node 0. */
 
-                /*  Simplest code to illustrate  */
-                /*       Message Passing         */
+/* Michel Vallieres */
 
-                /* nodes 1 to size-1 send message to node 0 */
-
-                                                   /* Michel Vallieres */
-#include       <stdio.h>
-#include       <mpi.h>
+#include <stdio.h>
+#include <mpi.h>
 
 int main (int argc, char *argv[])
 {
-int my_rank, size;
-int err, process, send_mesg, recv_mesg;
-
-MPI_Status  recv_status;
+  int rank, size;
+  int err, process, source, count, tag, send_mesg, recv_mesg;
+  MPI_Status recv_status;
 
-    MPI_Init(&argc, &argv);
+  MPI_Init(&argc, &argv);
+  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+  MPI_Comm_size(MPI_COMM_WORLD, &size);
 
-    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
-    MPI_Comm_size(MPI_COMM_WORLD, &size);
+  count = 1;  /* only sending a single MPI_INT */
+  tag = 123;  /* arbitrary integer tagging the message */
 
-    if ( my_rank == 0 )
-     {
-       for ( process=1; process<size; process++)
+  if (rank == 0)
+    {
+      for (process=1; process < size; process++)
         {
-           err = MPI_Recv(&recv_mesg, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG,
-                 MPI_COMM_WORLD, &recv_status);
-           printf("Received message %d from node %d  \n",
-                 recv_mesg, recv_status.MPI_SOURCE ); 
-           printf("err %d,  ( byte count, tag, and error code were: %d %d %d ) \n",
-                 err, recv_status.count,recv_status.MPI_TAG,
-                 recv_status.MPI_ERROR );
+         err = MPI_Recv(&recv_mesg, count, MPI_INT, MPI_ANY_SOURCE,
+                        MPI_ANY_TAG, MPI_COMM_WORLD, &recv_status);
+         printf("Received message %d from node %d\n",
+                 recv_mesg, recv_status.MPI_SOURCE);
+         printf(" (byte count %d, tag %d, error code %d)\n",
+                recv_status.count, recv_status.MPI_TAG, err);
         }
-     }
-    else
-     {
-        send_mesg=2*my_rank;
-        MPI_Ssend(&send_mesg, 1, MPI_INT, 0, 121, MPI_COMM_WORLD);
-     }
-    
-    
+    }
+  else
+    {
+      process = 0;         /* send to node 0 */
+      send_mesg = 2*rank;
+      MPI_Ssend(&send_mesg, count, MPI_INT, process, tag, MPI_COMM_WORLD);
+    }
+
     MPI_Finalize();
 }