Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix context registration which was disabled by a bug
[simgrid.git] / examples / smpi / mvmul.c
index 8706b84..c7e8353 100644 (file)
@@ -1,5 +1,12 @@
+/* Copyright (c) 2009-2010, 2013-2014. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* 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 <stdlib.h>
+#include <ctype.h>
 #include <mpi.h>
 
 #define ITERATIONS         10
@@ -13,11 +20,11 @@ int main(int argc, char *argv[])
 
   int size, rank;
   int N, n, i, j, k, current_iteration, successful_iterations = 0;
-  double *matrix, *vector, *vcalc, *vcheck;
+  double *matrix = NULL, *vector = NULL, *vcalc, *vcheck;
   MPI_Status status;
-  struct timeval *start_time, *stop_time;
+  struct timeval *start_time = NULL, *stop_time = NULL;
   long parallel_usecs, parallel_usecs_total =
-    0, sequential_usecs, sequential_usecs_total = 0;
+      0, sequential_usecs, sequential_usecs_total = 0;
 
   MPI_Init(&argc, &argv);
 
@@ -33,7 +40,7 @@ int main(int argc, char *argv[])
       exit(USAGE_ERROR);
     }
 
-    N = atoi(argv[1]);
+    N = xbt_str_parse_int(argv[1], "Invalid size: %s");
 
     start_time = (struct timeval *) malloc(sizeof(struct timeval));
     stop_time = (struct timeval *) malloc(sizeof(struct timeval));
@@ -84,15 +91,16 @@ int main(int argc, char *argv[])
 
         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);
         }
@@ -124,14 +132,15 @@ int main(int argc, char *argv[])
 
         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);
         }
@@ -144,8 +153,8 @@ int main(int argc, char *argv[])
         }
 
         parallel_usecs =
-          (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
-          (start_time->tv_sec * 1000000 + start_time->tv_usec);
+            (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");
@@ -168,8 +177,8 @@ int main(int argc, char *argv[])
         }
 
         sequential_usecs =
-          (stop_time->tv_sec * 1000000 + stop_time->tv_usec) -
-          (start_time->tv_sec * 1000000 + start_time->tv_usec);
+            (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++);
@@ -178,11 +187,11 @@ int main(int argc, char *argv[])
 
         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));
+              ("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;
@@ -206,14 +215,15 @@ int main(int argc, char *argv[])
     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));
+          ("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");
     }