Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
some cleanup in smpi examples
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 29 Feb 2016 13:43:00 +0000 (14:43 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 29 Feb 2016 13:43:00 +0000 (14:43 +0100)
examples/smpi/bcbench.c
examples/smpi/mvmul.c
examples/smpi/replay_multiple/replay_multiple.c
examples/smpi/smpi_msg_masterslave/masterslave_mailbox_smpi.c

index daea198..fb3c404 100644 (file)
@@ -24,7 +24,6 @@
 
 int main(int argc, char *argv[])
 {
-
   int size, rank;
   int N;
   struct timeval *start_time = NULL, *stop_time = NULL;
@@ -46,7 +45,6 @@ int main(int argc, char *argv[])
   }
 
   for (N = N_START; N <= N_STOP; N = N_NEXT) {
-
     buffer = malloc(sizeof(char) * N);
 
     if (0 == rank) {
@@ -64,8 +62,7 @@ int main(int argc, char *argv[])
       MPI_Bcast(buffer, N, MPI_BYTE, 0, MPI_COMM_WORLD);
       if (0 == rank) {
         for (j = 1; j < size; j++) {
-          MPI_Recv(&check, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD,
-                   MPI_STATUS_IGNORE);
+          MPI_Recv(&check, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
         }
       } else {
         MPI_Send(&rank, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
@@ -78,20 +75,16 @@ int main(int argc, char *argv[])
         MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
         exit(EXIT_FAILURE);
       }
-      seconds =
-          (double) (stop_time->tv_sec - start_time->tv_sec) +
-          (double) (stop_time->tv_usec -
-                    start_time->tv_usec) / ONE_MILLION;
+      seconds = (double) (stop_time->tv_sec - start_time->tv_sec) +
+                (double) (stop_time->tv_usec - start_time->tv_usec) / ONE_MILLION;
     }
 
     free(buffer);
 
     if (0 == rank) {
-      printf("N: %10d, iter: %d, time: %10f s, avg rate: %12f Mbps\n", N,
-             ITER, seconds,
+      printf("N: %10d, iter: %d, time: %10f s, avg rate: %12f Mbps\n", N, ITER, seconds,
              ((double) N * ITER * 8) / (1024.0 * 1024.0 * seconds));
     }
-
   }
 
   if (0 == rank) {
index acc6af8..fd25813 100644 (file)
@@ -8,25 +8,21 @@
 #include <stdlib.h>
 #include <ctype.h>
 #include <mpi.h>
-
 #include <xbt/str.h>
 
 #define ITERATIONS         10
-
 #define USAGE_ERROR        1
 #define SANITY_ERROR       2
 #define GETTIMEOFDAY_ERROR 3
 
 int main(int argc, char *argv[])
 {
-
   int size, rank;
   int N, n, i, j, k, current_iteration, successful_iterations = 0;
   double *matrix = NULL, *vector = NULL, *vcalc, *vcheck;
   MPI_Status status;
   struct timeval *start_time = NULL, *stop_time = NULL;
-  long parallel_usecs, parallel_usecs_total =
-      0, sequential_usecs, sequential_usecs_total = 0;
+  long parallel_usecs, parallel_usecs_total = 0, sequential_usecs, sequential_usecs_total = 0;
 
   MPI_Init(&argc, &argv);
 
@@ -34,7 +30,6 @@ int main(int argc, char *argv[])
   MPI_Comm_size(MPI_COMM_WORLD, &size);
 
   if (0 == rank) {
-
     // root node parses cmdline args
     if (2 > argc || !isdigit(*argv[1])) {
       printf("usage:\n%s <size>\n", argv[0]);
@@ -46,14 +41,10 @@ int main(int argc, char *argv[])
 
     start_time = (struct timeval *) malloc(sizeof(struct timeval));
     stop_time = (struct timeval *) malloc(sizeof(struct timeval));
-
   }
 
-  for (current_iteration = 0; current_iteration < ITERATIONS;
-       current_iteration++) {
-
+  for (current_iteration = 0; current_iteration < ITERATIONS; current_iteration++) {
     if (0 == rank) {
-
       matrix = (double *) malloc(N * N * sizeof(double));
       vector = (double *) malloc(N * sizeof(double));
 
@@ -65,8 +56,7 @@ int main(int argc, char *argv[])
         vector[i] = (double) rand() / ((double) RAND_MAX + 1);
       }
 
-      // for the sake of argument, the parallel algorithm begins
-      // when the root node begins to transmit the matrix to the
+      // for the sake of argument, the parallel algorithm begins when the root node begins to transmit the matrix to the
       // workers.
       if (-1 == gettimeofday(start_time, NULL)) {
         printf("couldn't set start_time on node 0!\n");
@@ -77,48 +67,40 @@ int main(int argc, char *argv[])
       for (i = 1; i < size; i++) {
         MPI_Send(&N, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
       }
-
     } else {
       MPI_Recv(&N, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
     }
 
     // this algorithm uses at most N processors...
     if (rank < N) {
-
       if (size > N)
         size = N;
       n = N / size + ((rank < (N % size)) ? 1 : 0);
 
       if (0 == rank) {
-
         for (i = 1, j = n; i < size && j < N; i++, j += k) {
           k = N / size + ((i < (N % size)) ? 1 : 0);
-          MPI_Send(matrix + N * j, N * k, MPI_DOUBLE, i, 0,
-                   MPI_COMM_WORLD);
+          MPI_Send(matrix + N * j, N * k, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
           MPI_Send(vector, N, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
         }
 
         // sanity check
 #ifdef DEBUG
         if (i != size || j != N) {
-          printf("index calc error: i = %d, size = %d, j = %d, N = %d\n",
-                 i, size, j, N);
+          printf("index calc error: i = %d, size = %d, j = %d, N = %d\n", i, size, j, N);
           MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
           exit(SANITY_ERROR);
         }
 #endif
 
         vcalc = (double *) malloc(N * sizeof(double));
-
       } else {
-
         matrix = (double *) malloc(N * n * sizeof(double));
         vector = (double *) malloc(N * sizeof(double));
         vcalc = (double *) malloc(n * sizeof(double));
 
         MPI_Recv(matrix, N * n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
         MPI_Recv(vector, N, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
-
       }
 
       for (i = 0; i < n; i++) {
@@ -131,18 +113,15 @@ int main(int argc, char *argv[])
       if (0 != rank) {
         MPI_Send(vcalc, n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
       } else {
-
         for (i = 1, j = n; i < size && j < N; i++, j += k) {
           k = N / size + ((i < (N % size)) ? 1 : 0);
-          MPI_Recv(vcalc + j, k, MPI_DOUBLE, i, 0, MPI_COMM_WORLD,
-                   &status);
+          MPI_Recv(vcalc + j, k, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, &status);
         }
 
         // sanity check
 #ifdef DEBUG
         if (i != size || j != N) {
-          printf("index calc error 2: i = %d, size = %d, j = %d, N = %d\n",
-                 i, size, j, N);
+          printf("index calc error 2: i = %d, size = %d, j = %d, N = %d\n", i, size, j, N);
           MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
           exit(SANITY_ERROR);
         }
@@ -154,9 +133,8 @@ int main(int argc, char *argv[])
           exit(GETTIMEOFDAY_ERROR);
         }
 
-        parallel_usecs =
-            (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
-            (start_time->tv_sec * 1000000 + start_time->tv_usec);
+        parallel_usecs = (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
+                         (start_time->tv_sec * 1000000 + start_time->tv_usec);
 
         if (-1 == gettimeofday(start_time, NULL)) {
           printf("couldn't set start_time on node 0!\n");
@@ -178,9 +156,8 @@ int main(int argc, char *argv[])
           exit(GETTIMEOFDAY_ERROR);
         }
 
-        sequential_usecs =
-            (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
-            (start_time->tv_sec * 1000000 + start_time->tv_usec);
+        sequential_usecs = (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
+                           (start_time->tv_sec * 1000000 + start_time->tv_usec);
 
         // verify correctness
         for (i = 0; i < N && vcalc[i] == vcheck[i]; i++);
@@ -188,12 +165,9 @@ int main(int argc, char *argv[])
         printf("prog: blocking, i: %d ", current_iteration);
 
         if (i == N) {
-          printf
-              ("ptime: %ld us, stime: %ld us, speedup: %.3f, nodes: %d, efficiency: %.3f\n",
-               parallel_usecs, sequential_usecs,
-               (double) sequential_usecs / (double) parallel_usecs, size,
-               (double) sequential_usecs / ((double) parallel_usecs *
-                                            (double) size));
+          printf("ptime: %ld us, stime: %ld us, speedup: %.3f, nodes: %d, efficiency: %.3f\n",
+               parallel_usecs, sequential_usecs, (double) sequential_usecs / (double) parallel_usecs, size,
+               (double) sequential_usecs / ((double) parallel_usecs * (double) size));
 
           parallel_usecs_total += parallel_usecs;
           sequential_usecs_total += sequential_usecs;
@@ -203,36 +177,28 @@ int main(int argc, char *argv[])
         }
 
         free(vcheck);
-
       }
 
       free(matrix);
       free(vector);
       free(vcalc);
     }
-
   }
 
   if (0 == rank) {
     printf("prog: blocking, ");
     if (0 < successful_iterations) {
-      printf
-          ("iterations: %d, avg. ptime: %.3f us, avg. stime: %.3f us, avg. speedup: %.3f, nodes: %d, avg. efficiency: %.3f\n",
-           successful_iterations,
-           (double) parallel_usecs_total / (double) successful_iterations,
-           (double) sequential_usecs_total /
-           (double) successful_iterations,
-           (double) sequential_usecs_total / (double) parallel_usecs_total,
-           size,
-           (double) sequential_usecs_total /
-           ((double) parallel_usecs_total * (double) size));
+      printf("iterations: %d, avg. ptime: %.3f us, avg. stime: %.3f us, avg. speedup: %.3f, nodes: %d, avg. efficiency: %.3f\n",
+           successful_iterations, (double) parallel_usecs_total / (double) successful_iterations,
+           (double) sequential_usecs_total / (double) successful_iterations,
+           (double) sequential_usecs_total / (double) parallel_usecs_total, size,
+           (double) sequential_usecs_total / ((double) parallel_usecs_total * (double) size));
     } else {
       printf("no successful iterations!\n");
     }
 
     free(start_time);
     free(stop_time);
-
   }
 
   MPI_Finalize();
index 969fe9a..39697b0 100644 (file)
@@ -4,16 +4,10 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include <stdio.h>
-#include "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
-#include "xbt/sysdep.h"         /* calloc, printf */
+#include "simgrid/msg.h"
 #include "mpi.h"
-/* Create a log channel to have nice outputs. */
-#include "xbt/log.h"
-#include "xbt/asserts.h"
-#include "smpi/smpi.h"
-XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
-    "Messages specific for this msg example");
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
 
 static int smpi_replay(int argc, char *argv[]) {
   smpi_replay_run(&argc, &argv);
@@ -22,29 +16,19 @@ static int smpi_replay(int argc, char *argv[]) {
 
 int main(int argc, char *argv[]){
   msg_error_t res;
-  const char *platform_file;
-  const char *application_file;
-  const char *description_file;
 
   MSG_init(&argc, argv);
 
-  if (argc < 4) {
-    printf("Usage: %s description_file platform_file deployment_file\n", argv[0]);
-    printf("example: %s smpi_multiple_apps msg_platform.xml msg_deployment.xml\n", argv[0]);
-    exit(1);
-  }
-  description_file = argv[1];
-  platform_file = argv[2];
-  application_file = argv[3];
-
+  xbt_assert(argc > 3, "Usage: %s description_file platform_file deployment_file\n"
+             "\tExample: %s smpi_multiple_apps msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
 
   /*  Simulation setting */
-  MSG_create_environment(platform_file);
+  MSG_create_environment(argv[2]);
 
   /*   Application deployment: read the description file in order to identify instances to launch */
-  FILE* fp = fopen(description_file, "r");
+  FILE* fp = fopen(argv[1], "r");
   if (fp == NULL)
-    xbt_die("Cannot open %s", description_file);
+    xbt_die("Cannot open %s", argv[1]);
   ssize_t read;
   char   *line = NULL;
   size_t n   = 0;
@@ -66,7 +50,7 @@ int main(int argc, char *argv[]){
     xbt_free(line_char);
   }
 
-  MSG_launch_application(application_file);
+  MSG_launch_application(argv[3]);
   SMPI_init();
 
   res = MSG_main();
@@ -74,9 +58,5 @@ int main(int argc, char *argv[]){
   XBT_INFO("Simulation time %g", MSG_get_clock());
 
   SMPI_finalize();
-  if (res == MSG_OK)
-    return 0;
-  else
-    return 1;
-
+  return res != MSG_OK;
 }
index 119c30f..9fe316b 100644 (file)
@@ -4,23 +4,11 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include <stdio.h>
-#include "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
-#include "xbt/sysdep.h"         /* calloc, printf */
+#include "simgrid/msg.h"
 #include "mpi.h"
-/* Create a log channel to have nice outputs. */
-#include "xbt/log.h"
-#include "xbt/asserts.h"
-XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
-                             "Messages specific for this msg example");
-
-int master(int argc, char *argv[]);
-int slave(int argc, char *argv[]);
-int master_mpi(int argc, char *argv[]);
-int alltoall_mpi(int argc, char *argv[]);
-
-/** sender function  */
-int master(int argc, char *argv[])
+XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
+
+static int master(int argc, char *argv[])
 {
   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
@@ -38,18 +26,14 @@ int master(int argc, char *argv[])
 
     sprintf(mailbox, "slave-%ld", i % slaves_count);
     sprintf(sprintf_buffer, "Task_%d", i);
-    task =
-        MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
-                        NULL);
+    task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
     if (number_of_tasks < 10000 || i % 10000 == 0)
-      XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
-            number_of_tasks, mailbox);
+      XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
 
     MSG_task_send(task, mailbox);
   }
 
-  XBT_INFO
-      ("All tasks have been dispatched. Let's tell everybody the computation is over.");
+  XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
   for (i = 0; i < slaves_count; i++) {
     char mailbox[80];
 
@@ -58,12 +42,10 @@ int master(int argc, char *argv[])
     MSG_task_send(finalize, mailbox);
   }
 
-//  XBT_INFO("Goodbye now!");
   return 0;
-}                               /* end_of_master */
-
+}
 
-int master_mpi(int argc, char *argv[])
+static int master_mpi(int argc, char *argv[])
 {
   MPI_Init(&argc, &argv);
 
@@ -72,22 +54,18 @@ int master_mpi(int argc, char *argv[])
   XBT_INFO("here for rank %d", rank);
   int test[1000]={rank};
   if(rank==0)
-        MPI_Send(&test, 1000, 
-                 MPI_INT, 1, 1, MPI_COMM_WORLD); 
+    MPI_Send(&test, 1000, MPI_INT, 1, 1, MPI_COMM_WORLD);
   else
-        MPI_Recv(&test, 1000, 
-                 MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUSES_IGNORE); 
+    MPI_Recv(&test, 1000, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
 
   XBT_INFO("After comm %d", rank);
   MPI_Finalize();
 
   XBT_INFO("After finalize %d %d", rank, test[0]);
   return 0;
-}                               /* end_of_master */
-
+}
 
-
-int alltoall_mpi(int argc, char *argv[])
+static int alltoall_mpi(int argc, char *argv[])
 {
   MPI_Init(&argc, &argv);
 
@@ -104,11 +82,9 @@ int alltoall_mpi(int argc, char *argv[])
   free(in);
   MPI_Finalize();
   return 0;
-}                               /* end_of_master */
-
+}
 
-/** Receiver function  */
-int slave(int argc, char *argv[])
+static int slave(int argc, char *argv[])
 {
   msg_task_t task = NULL;
   XBT_ATTRIB_UNUSED int res;
@@ -139,46 +115,32 @@ int slave(int argc, char *argv[])
   XBT_INFO("I'm done. See you!");
 
   return 0;
-}                               /* end_of_slave */
+}
 
-/** Main function */
 int main(int argc, char *argv[])
 {
   msg_error_t res;
-  const char *platform_file;
-  const char *application_file;
 
   MSG_init(&argc, argv);
 
-  if (argc < 3) {
-    printf("Usage: %s platform_file deployment_file\n", argv[0]);
-    printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
-    exit(1);
-  }
-  platform_file = argv[1];
-  application_file = argv[2];
+  xbt_assert(argc > 2,"Usage: %s platform_file deployment_file\n"
+             "\nexample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
+
+  MSG_create_environment(argv[1]);
+
+  MSG_function_register("master", master);
+  MSG_function_register("slave", slave);
+  // launch two MPI applications as well, one using master_mpi function as main on 2 nodes
+  SMPI_app_instance_register("master_mpi", master_mpi,2);
+  // the second performing an alltoall on 4 nodes
+  SMPI_app_instance_register("alltoall_mpi", alltoall_mpi,4);
+  MSG_launch_application(argv[2]);
+  SMPI_init();
 
-  {                             /*  Simulation setting */
-    MSG_create_environment(platform_file);
-  }
-  {                             /*   Application deployment */
-    MSG_function_register("master", master);
-    MSG_function_register("slave", slave);
-    // launch two MPI applications as well, one using master_mpi function as main on 2 nodes
-    SMPI_app_instance_register("master_mpi", master_mpi,2);
-    // the second performing an alltoall on 4 nodes
-    SMPI_app_instance_register("alltoall_mpi", alltoall_mpi,4);
-    MSG_launch_application(application_file);
-    SMPI_init();
-  }
   res = MSG_main();
 
   XBT_INFO("Simulation time %g", MSG_get_clock());
 
-
   SMPI_finalize();
-  if (res == MSG_OK)
-    return 0;
-  else
-    return 1;
-}                               /* end_of_main */
+  return res != MSG_OK;
+}