Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
'inline' is C99, remove portability the layer
[simgrid.git] / examples / msg / pastry / pastry.c
index 3c33770..2b6efe9 100644 (file)
@@ -1,17 +1,13 @@
-/* Copyright (c) 2013-2014. The SimGrid Team.
+/* Copyright (c) 2013-2015. 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 <math.h>
 #include "simgrid/msg.h"
-#include "xbt/log.h"
-#include "xbt/asserts.h"
 
- XBT_LOG_NEW_DEFAULT_CATEGORY(msg_pastry,
-                             "Messages specific for this msg example");
+ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_pastry, "Messages specific for this msg example");
 
 /***************************************
  * PASTRY                              *
@@ -40,7 +36,6 @@ static int max_simulation_time = 1000;
 
 extern long int smx_total_comms;
 
-
 typedef struct s_node {
   int id;                                 //128bits generated random(2^128 -1)
   int known_id;
@@ -60,9 +55,7 @@ typedef struct s_state {
   int routing_table[LEVELS_COUNT][LEVEL_SIZE];
 } s_state_t, *state_t;
 
-/**
- * Types of tasks exchanged between nodes.
- */
+/** Types of tasks exchanged between nodes. */
 typedef enum {
   TASK_JOIN,
   TASK_JOIN_REPLY,
@@ -96,7 +89,6 @@ static int routing_next(node_t node, int dest);
 static void create(node_t node);
 static int join(node_t node);
 
-
 /**
  * \brief Gets the mailbox name of a host given its chord id.
  * \param node_id id of a node
@@ -130,7 +122,7 @@ static int shl(int a, int b) {
 }
 
 /*
- * Get the cloest id to the dest in the node namespace_set
+ * Get the closest id to the dest in the node namespace_set
  */
 static int closest_in_namespace_set(node_t node, int dest) {
   int best_dist;
@@ -141,11 +133,11 @@ static int closest_in_namespace_set(node_t node, int dest) {
     int i, dist;
     for (i=0; i<NAMESPACE_SIZE; i++) {
       if (node->namespace_set[i]!=-1) {
-       dist = abs(node->namespace_set[i] - dest);
-       if (dist<best_dist) {
-         best_dist = dist;
-          res = node->namespace_set[i];          
-       }
+  dist = abs(node->namespace_set[i] - dest);
+  if (dist<best_dist) {
+    best_dist = dist;
+          res = node->namespace_set[i];    
+  }
       }
     }
   }
@@ -226,7 +218,7 @@ static void handle_task(node_t node, msg_task_t task) {
       if (next!=node->id) {
         get_mailbox(next, mailbox);
         task_data->sender_id = node->id;
-       task_data->steps++;
+  task_data->steps++;
         task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, task_data);
         MSG_task_send_with_timeout(task_sent, mailbox, timeout);
         type = TASK_JOIN_REPLY;
@@ -259,7 +251,7 @@ static void handle_task(node_t node, msg_task_t task) {
 
       // if first node touched reply, copy its neighborood set
       if (task_data->sender_id == node->known_id) {
-       node->neighborhood_set[0] = task_data->sender_id;
+  node->neighborhood_set[0] = task_data->sender_id;
         for (i=1; i<NEIGHBORHOOD_SIZE; i++)
             node->neighborhood_set[i] = task_data->state->neighborhood_set[i-1]; 
       }
@@ -270,7 +262,7 @@ static void handle_task(node_t node, msg_task_t task) {
       for (i=min;i<max;i++) {
         d = domain(node->id, i); 
         for (j=0; j<LEVEL_SIZE; j++)
-         if (d!=j)
+    if (d!=j)
             node->routing_table[i][j] =  task_data->state->routing_table[i][j];
       }
 
@@ -280,15 +272,15 @@ static void handle_task(node_t node, msg_task_t task) {
         XBT_DEBUG("Node %i is ready!!!", node->id);
 
         while(xbt_fifo_size(node->pending_tasks))
-         handle_task(node, xbt_fifo_pop(node->pending_tasks));
+    handle_task(node, xbt_fifo_pop(node->pending_tasks));
 
-       for (i=0; i<NAMESPACE_SIZE; i++) {
+  for (i=0; i<NAMESPACE_SIZE; i++) {
           j = node->namespace_set[i];
           if (j!=-1) {
             XBT_DEBUG("Send update to %i", j);
             get_mailbox(j, mailbox);
-           
-           req_data = xbt_new0(s_task_data_t,1);
+      
+      req_data = xbt_new0(s_task_data_t,1);
             req_data->answer_id = node->id;
             req_data->steps = 0;
             req_data->type = TASK_UPDATE;
@@ -320,65 +312,65 @@ static void handle_task(node_t node, msg_task_t task) {
       i=0;
       for (; i<NAMESPACE_SIZE/2; i++){
         curr_namespace_set[i] = node->namespace_set[i];
-       task_namespace_set[i] = task_data->state->namespace_set[i];
+  task_namespace_set[i] = task_data->state->namespace_set[i];
       }
       task_namespace_set[i] = task_data->state->id;
       for (; i<NAMESPACE_SIZE; i++){
-        curr_namespace_set[i] = node->namespace_set[i];        
-       task_namespace_set[i+1] = task_data->state->namespace_set[i];   
+        curr_namespace_set[i] = node->namespace_set[i];  
+  task_namespace_set[i+1] = task_data->state->namespace_set[i];  
       }
 
       // get the index of values before and after node->id in task_namespace
       min = -1;
       max = -1;
       for (i=0; i<=NAMESPACE_SIZE; i++) {
-       j = task_namespace_set[i];
+  j = task_namespace_set[i];
         if (i<NAMESPACE_SIZE)
-         printf("%08x %08x | ", j, curr_namespace_set[i]);
-       if (j != -1 && j < node->id) min = i;
-       if (j != -1 && max == -1 && j > node->id) max = i;
+    printf("%08x %08x | ", j, curr_namespace_set[i]);
+  if (j != -1 && j < node->id) min = i;
+  if (j != -1 && max == -1 && j > node->id) max = i;
       }
       printf("\n");
 
       // add lower elements
       j = NAMESPACE_SIZE/2-1;
       for (i=NAMESPACE_SIZE/2-1; i>=0; i--) {
-       printf("i:%i, j:%i, min:%i, currj:%08x, taskmin:%08x\n", i, j, min, curr_namespace_set[j], task_namespace_set[min]);
+  printf("i:%i, j:%i, min:%i, currj:%08x, taskmin:%08x\n", i, j, min, curr_namespace_set[j], task_namespace_set[min]);
         if (min<0) {
-         node->namespace_set[i] = curr_namespace_set[j];
-         j--; 
-       } else if (curr_namespace_set[j] == task_namespace_set[min]) {
-         node->namespace_set[i] = curr_namespace_set[j];
-         j--; min--;
-       } else if (curr_namespace_set[j] > task_namespace_set[min]) {
+    node->namespace_set[i] = curr_namespace_set[j];
+    j--; 
+  } else if (curr_namespace_set[j] == task_namespace_set[min]) {
+    node->namespace_set[i] = curr_namespace_set[j];
+    j--; min--;
+  } else if (curr_namespace_set[j] > task_namespace_set[min]) {
           node->namespace_set[i] = curr_namespace_set[j];
-         j--;
-       } else {
+    j--;
+  } else {
           node->namespace_set[i] = task_namespace_set[min];
-         min--;
-       }
+    min--;
+  }
       }
 
       // add greater elements
       j = NAMESPACE_SIZE/2;
       for (i=NAMESPACE_SIZE/2; i<NAMESPACE_SIZE; i++) {
-       printf("i:%i, j:%i, max:%i, currj:%08x, taskmax:%08x\n", i, j, max, curr_namespace_set[j], task_namespace_set[max]);          
+  printf("i:%i, j:%i, max:%i, currj:%08x, taskmax:%08x\n", i, j, max, curr_namespace_set[j], task_namespace_set[max]);        
         if (min<0 || max>=NAMESPACE_SIZE) {
-         node->namespace_set[i] = curr_namespace_set[j];
-         j++;
-       } else if (curr_namespace_set[j] == -1) {
-         node->namespace_set[i] = task_namespace_set[max];
-         max++;
-       } else if (curr_namespace_set[j] == task_namespace_set[max]) {
-         node->namespace_set[i] = curr_namespace_set[j];
-         j++; max++;
-       } else if (curr_namespace_set[j] < task_namespace_set[max]) {
+    node->namespace_set[i] = curr_namespace_set[j];
+    j++;
+  } else if (curr_namespace_set[j] == -1) {
+    node->namespace_set[i] = task_namespace_set[max];
+    max++;
+  } else if (curr_namespace_set[j] == task_namespace_set[max]) {
+    node->namespace_set[i] = curr_namespace_set[j];
+    j++; max++;
+  } else if (curr_namespace_set[j] < task_namespace_set[max]) {
           node->namespace_set[i] = curr_namespace_set[j];
-         j++;
-       } else {
+    j++;
+  } else {
           node->namespace_set[i] = task_namespace_set[max];
-         max++;
-       }
+    max++;
+  }
       }
       print_node_namespace_set(node);
 
@@ -389,7 +381,7 @@ static void handle_task(node_t node, msg_task_t task) {
             node->routing_table[i][j] = task_data->state->routing_table[i][j];
         }
       }
-  }               
+  }         
 }
 
 /**
@@ -423,7 +415,7 @@ static int join(node_t node){
 }
 
 /*
- * Print the node infomations
+ * Print the node infomation
  */
 static void print_node(node_t node) {
   printf("Node:\n");
@@ -437,7 +429,7 @@ static void print_node(node_t node) {
  * Print the node id
  */
 static void print_node_id(node_t node) {
-  int i;       
+  int i;  
   printf(" id: %i '%08x' ", node->id, node->id);
   for (i=0;i<LEVELS_COUNT;i++)
     printf(" %x", domain(node->id, i));
@@ -448,7 +440,7 @@ static void print_node_id(node_t node) {
  * Print the node neighborood set
  */
 static void print_node_neighborood_set(node_t node) {
-  int i;       
+  int i;  
   printf(" neighborood:\n");
   for (i=0; i<NEIGHBORHOOD_SIZE; i++)
     printf("  %08x\n", node->neighborhood_set[i]);
@@ -458,7 +450,7 @@ static void print_node_neighborood_set(node_t node) {
  * Print the routing table
  */
 static void print_node_routing_table(node_t node) {
-  int i,j;     
+  int i,j;  
   printf(" routing table:\n");
   for (i=0; i<LEVELS_COUNT; i++){
     printf("  ");
@@ -484,7 +476,7 @@ static void print_node_namespace_set(node_t node) {
  * Get the corresponding state of a node
  */
 static state_t node_get_state(node_t node) {
-  int i,j;     
+  int i,j;  
   state_t state = xbt_new0(s_state_t,1);
   state->id = node->id;
   for (i=0; i<NEIGHBORHOOD_SIZE; i++)
@@ -517,7 +509,7 @@ static int node(int argc, char *argv[])
   double deadline;
   xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
   s_node_t node = {0};
-  node.id = atoi(argv[1]);
+  node.id = xbt_str_parse_int(argv[1], "Invalid ID: %s");
   node.known_id = -1;
   node.ready = -1;
   node.pending_tasks = xbt_fifo_new();
@@ -539,14 +531,14 @@ static int node(int argc, char *argv[])
 
   if (argc == 3) { // first ring
     XBT_DEBUG("Hey! Let's create the system.");
-    deadline = atof(argv[2]);
+    deadline = xbt_str_parse_double(argv[2], "Invalid deadline: %s");
     create(&node);
     join_success = 1;
   }
   else {
-    node.known_id = atoi(argv[2]);
-    double sleep_time = atof(argv[3]);
-    deadline = atof(argv[4]);
+    node.known_id = xbt_str_parse_int(argv[2], "Invalid known ID: %s");
+    double sleep_time = xbt_str_parse_double(argv[3], "Invalid sleep time: %s");
+    deadline = xbt_str_parse_double(argv[4], "Invalid deadline: %s");
 
     // sleep before starting
     XBT_DEBUG("Let's sleep during %f", sleep_time);
@@ -613,25 +605,24 @@ static int node(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
   MSG_init(&argc, argv);
-  if (argc < 3) {
-    printf("Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n", argv[0]);
-    printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
-    exit(1);
-  }
+  xbt_assert(argc > 2, 
+       "Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n"
+       "\tExample: %s ../msg_platform.xml pastry10.xml\n", 
+       argv[0], argv[0]);
   
   char **options = &argv[1];
   while (!strncmp(options[0], "-", 1)) {
 
     int length = strlen("-nb_bits=");
     if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
-      nb_bits = atoi(options[0] + length);
+      nb_bits = xbt_str_parse_int(options[0] + length, "Invalid nb_bits parameter: %s");
       XBT_DEBUG("Set nb_bits to %d", nb_bits);
     }
     else {
 
       length = strlen("-timeout=");
       if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
-        timeout = atoi(options[0] + length);
+        timeout = xbt_str_parse_int(options[0] + length, "Invalid timeout parameter: %s");
         XBT_DEBUG("Set timeout to %d", timeout);
       }
       else {
@@ -653,9 +644,5 @@ int main(int argc, char *argv[])
   XBT_CRITICAL("Messages created: %ld", smx_total_comms);
   XBT_INFO("Simulated time: %g", MSG_get_clock());
 
-  if (res == MSG_OK)
-    return 0;
-  else
-    return 1;
-
+  return res != MSG_OK;
 }