Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
some cleanup in smpi examples
[simgrid.git] / examples / smpi / mvmul.c
index acc6af8..fd25813 100644 (file)
@@ -8,25 +8,21 @@
 #include <stdlib.h>
 #include <ctype.h>
 #include <mpi.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <mpi.h>
-
 #include <xbt/str.h>
 
 #define ITERATIONS         10
 #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[])
 {
 #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;
   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);
 
 
   MPI_Init(&argc, &argv);
 
@@ -34,7 +30,6 @@ int main(int argc, char *argv[])
   MPI_Comm_size(MPI_COMM_WORLD, &size);
 
   if (0 == rank) {
   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]);
     // 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));
 
     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) {
     if (0 == rank) {
-
       matrix = (double *) malloc(N * N * sizeof(double));
       vector = (double *) malloc(N * sizeof(double));
 
       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);
       }
 
         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");
       // 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);
       }
       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) {
     } 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) {
       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);
         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) {
           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));
           MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
           exit(SANITY_ERROR);
         }
 #endif
 
         vcalc = (double *) malloc(N * sizeof(double));
-
       } else {
       } 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);
         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++) {
       }
 
       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 {
       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);
         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) {
         }
 
         // 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);
         }
           MPI_Abort(MPI_COMM_WORLD, SANITY_ERROR);
           exit(SANITY_ERROR);
         }
@@ -154,9 +133,8 @@ int main(int argc, char *argv[])
           exit(GETTIMEOFDAY_ERROR);
         }
 
           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");
 
         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);
         }
 
           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++);
 
         // 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("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;
 
           parallel_usecs_total += parallel_usecs;
           sequential_usecs_total += sequential_usecs;
@@ -203,36 +177,28 @@ int main(int argc, char *argv[])
         }
 
         free(vcheck);
         }
 
         free(vcheck);
-
       }
 
       free(matrix);
       free(vector);
       free(vcalc);
     }
       }
 
       free(matrix);
       free(vector);
       free(vcalc);
     }
-
   }
 
   if (0 == rank) {
     printf("prog: blocking, ");
     if (0 < successful_iterations) {
   }
 
   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);
     } else {
       printf("no successful iterations!\n");
     }
 
     free(start_time);
     free(stop_time);
-
   }
 
   MPI_Finalize();
   }
 
   MPI_Finalize();