Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move parts of the kernel to the right subdir
[simgrid.git] / src / instr / jedule / jedule_platform.cpp
index 312b06f..dfa9129 100644 (file)
@@ -4,50 +4,29 @@
 /* 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 <stdlib.h>
-#include <string.h>
+#include "simgrid/jedule/jedule_platform.h"
+
 #include "xbt/asserts.h"
 #include "xbt/dynar.h"
+#include "xbt/str.h"
 
-#include "simgrid/jedule/jedule_platform.h"
-
-#ifdef HAVE_JEDULE
+#include <stdlib.h>
+#include <string.h>
 
-/********************************************************************/
+#if HAVE_JEDULE
 
 static xbt_dict_t host2_simgrid_parent_container;
 static xbt_dict_t container_name2container;
 
-/********************************************************************/
-
-static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
-    jed_simgrid_container_t parent);
-
-static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
-    jed_simgrid_container_t parent);
-
-static void jed_free_container(jed_simgrid_container_t container);
-
-/********************************************************************/
-
-static int compare_hostnames(const void *host1, const void *host2) {
-  const char *hp1 = *((const char**) host1);
-  const char *hp2 = *((const char**) host2);
-  return strcmp (hp1, hp2);
-}
-
 static int compare_ids(const void *num1, const void *num2) {
-  int *i1 = (int*) num1;
-  int *i2 = (int*) num2;
-  return *i1 - *i2;
+  return *((int*) num1) - *((int*) num2);
 }
 
 static void jed_free_container(jed_simgrid_container_t container) {
-
   xbt_dict_free(&container->name2id);
   xbt_dynar_free(&container->resource_list);
 
-  if( container->container_children != NULL ) {
+  if( container->container_children != nullptr ) {
     unsigned int iter;
     jed_simgrid_container_t child_container;
     xbt_dynar_foreach(container->container_children, iter, child_container) {
@@ -55,69 +34,58 @@ static void jed_free_container(jed_simgrid_container_t container) {
     }
     xbt_dynar_free(&container->container_children);
   }
-
   xbt_free(container->name);
   xbt_free(container);
 }
 
-void jed_simgrid_create_container(jed_simgrid_container_t *container,
-                                  const char *name)
+void jed_simgrid_create_container(jed_simgrid_container_t *container, const char *name)
 {
-  xbt_assert( name != NULL );
+  xbt_assert( name != nullptr );
 
   *container = xbt_new0(s_jed_simgrid_container_t,1);
   (*container)->name = xbt_strdup(name);
   (*container)->is_lowest = 0;
-  (*container)->container_children = xbt_dynar_new(sizeof(jed_simgrid_container_t), NULL);
-  (*container)->parent = NULL;
+  (*container)->container_children = xbt_dynar_new(sizeof(jed_simgrid_container_t), nullptr);
+  (*container)->parent = nullptr;
 
-  xbt_dict_set(container_name2container, (*container)->name, *container, NULL);
+  xbt_dict_set(container_name2container, (*container)->name, *container, nullptr);
 }
 
-
-void jed_simgrid_add_container(jed_simgrid_container_t parent,
-    jed_simgrid_container_t child) {
-  xbt_assert(parent != NULL);
-  xbt_assert(child != NULL);
+void jed_simgrid_add_container(jed_simgrid_container_t parent, jed_simgrid_container_t child) {
+  xbt_assert(parent != nullptr);
+  xbt_assert(child != nullptr);
   xbt_dynar_push(parent->container_children, &child);
   child->parent = parent;
 }
 
-void jed_simgrid_add_resources(jed_simgrid_container_t parent,
-    xbt_dynar_t host_names) {
-
+void jed_simgrid_add_resources(jed_simgrid_container_t parent, xbt_dynar_t host_names) {
   unsigned int iter;
   char *host_name;
   char *buf;
 
   parent->is_lowest = 1;
   xbt_dynar_free(&parent->container_children);
-  parent->container_children = NULL;
+  parent->container_children = nullptr;
   parent->name2id = xbt_dict_new_homogeneous(xbt_free_f);
   parent->last_id = 0;
-  parent->resource_list = xbt_dynar_new(sizeof(char *), NULL);
+  parent->resource_list = xbt_dynar_new(sizeof(char *), nullptr);
 
-  xbt_dynar_sort (host_names,  &compare_hostnames);
+  xbt_dynar_sort_strings(host_names);
 
   xbt_dynar_foreach(host_names, iter, host_name) {
     buf = bprintf("%d", parent->last_id);
     (parent->last_id)++;
-    xbt_dict_set(parent->name2id, host_name, buf, NULL);
-    xbt_dict_set(host2_simgrid_parent_container, host_name, parent, NULL);
+    xbt_dict_set(parent->name2id, host_name, buf, nullptr);
+    xbt_dict_set(host2_simgrid_parent_container, host_name, parent, nullptr);
     xbt_dynar_push(parent->resource_list, &host_name);
   }
-
 }
 
-static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
-    jed_simgrid_container_t parent) {
-
+static void add_subset_to(xbt_dynar_t subset_list, int start, int end, jed_simgrid_container_t parent) {
   jed_res_subset_t subset;
 
-  xbt_assert( subset_list != NULL );
-  xbt_assert( parent != NULL );
-
-  // printf(">>> start=%d end=%d\n", start, end);
+  xbt_assert( subset_list != nullptr );
+  xbt_assert( parent != nullptr );
 
   subset = xbt_new0(s_jed_res_subset_t,1);
   subset->start_idx = start;
@@ -125,12 +93,9 @@ static void add_subset_to(xbt_dynar_t subset_list, int start, int end,
   subset->parent    = parent;
 
   xbt_dynar_push(subset_list, &subset);
-
 }
 
-static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
-    jed_simgrid_container_t parent) {
-
+static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup, jed_simgrid_container_t parent) {
   unsigned int iter;
   char *host_name;
   xbt_dynar_t id_list;
@@ -143,16 +108,16 @@ static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
   // compact ids
   // create subset for each id group
 
-  xbt_assert( host2_simgrid_parent_container != NULL );
-  xbt_assert( subset_list != NULL );
-  xbt_assert( hostgroup != NULL );
-  xbt_assert( parent != NULL );
+  xbt_assert( host2_simgrid_parent_container != nullptr );
+  xbt_assert( subset_list != nullptr );
+  xbt_assert( hostgroup != nullptr );
+  xbt_assert( parent != nullptr );
 
-  id_list = xbt_dynar_new(sizeof(char *), NULL);
+  id_list = xbt_dynar_new(sizeof(char *), nullptr);
 
   xbt_dynar_foreach(hostgroup, iter, host_name) {
     jed_simgrid_container_t parent;
-    xbt_assert( host_name != NULL );
+    xbt_assert( host_name != nullptr );
     parent = (jed_simgrid_container_t)xbt_dict_get(host2_simgrid_parent_container, host_name);
     id_str = (char*)xbt_dict_get(parent->name2id, host_name);
     xbt_dynar_push(id_list, &id_str);
@@ -161,7 +126,7 @@ static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
   nb_ids = xbt_dynar_length(id_list);
   id_ar = xbt_new0(int,nb_ids);
   xbt_dynar_foreach(id_list, iter, id_str) {
-    id_ar[iter] = atoi(id_str);
+    id_ar[iter] = xbt_str_parse_int(id_str, "Parse error: not a number: %s");
   }
 
   qsort (id_ar, nb_ids, sizeof(int), &compare_ids);
@@ -173,16 +138,13 @@ static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
 
     pos = start;
     for(i=0; i<nb_ids; i++) {
-
       if( id_ar[i] - id_ar[pos] > 1 ) {
-
         add_subset_to( subset_list, id_ar[start], id_ar[pos], parent );
         start = i;
 
         if( i == nb_ids-1 ) {
           add_subset_to( subset_list, id_ar[i], id_ar[i], parent );
         }
-
       } else {
         if( i == nb_ids-1 ) {
           add_subset_to( subset_list, id_ar[start], id_ar[i], parent );
@@ -195,79 +157,66 @@ static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup,
 
   free(id_ar);
   xbt_dynar_free(&id_list);
-
-
 }
 
-void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list,
-    xbt_dynar_t host_names) {
-
+void jed_simgrid_get_resource_selection_by_hosts(xbt_dynar_t subset_list, xbt_dynar_t host_names) {
   char *host_name;
   unsigned int iter;
   xbt_dict_t parent2hostgroup;  // group hosts by parent
 
-  parent2hostgroup = xbt_dict_new_homogeneous(NULL);
+  parent2hostgroup = xbt_dict_new_homogeneous(nullptr);
 
-  xbt_assert( host_names != NULL );
+  xbt_assert( host_names != nullptr );
 
   // for each host name
   //  find parent container
   //  group by parent container
 
   xbt_dynar_foreach(host_names, iter, host_name) {
-    //printf("checking %s \n", host_name);
-
     jed_simgrid_container_t parent = (jed_simgrid_container_t)xbt_dict_get(host2_simgrid_parent_container, host_name);
-    xbt_assert( parent != NULL );
+    xbt_assert( parent != nullptr );
 
     xbt_dynar_t hostgroup = (xbt_dynar_t)xbt_dict_get_or_null (parent2hostgroup, parent->name);
-    if( hostgroup == NULL ) {
-      hostgroup = xbt_dynar_new(sizeof(char*), NULL);
-      xbt_dict_set(parent2hostgroup, parent->name, hostgroup, NULL);
+    if( hostgroup == nullptr ) {
+      hostgroup = xbt_dynar_new(sizeof(char*), nullptr);
+      xbt_dict_set(parent2hostgroup, parent->name, hostgroup, nullptr);
     }
 
     xbt_dynar_push(hostgroup, &host_name);
   }
 
-  {
-    xbt_dict_cursor_t cursor=NULL;
-    char *parent_name;
-    xbt_dynar_t hostgroup;
-    jed_simgrid_container_t parent;
+  xbt_dict_cursor_t cursor=nullptr;
+  char *parent_name;
+  xbt_dynar_t hostgroup;
+  jed_simgrid_container_t parent;
 
-    xbt_dict_foreach(parent2hostgroup,cursor,parent_name,hostgroup) {
-      parent = (jed_simgrid_container_t)xbt_dict_get(container_name2container, parent_name);
-      // printf("subset parent >>> %s\n", parent->name);
-      add_subsets_to(subset_list, hostgroup, parent);
-    }
-    xbt_dynar_free(&hostgroup);
+  xbt_dict_foreach(parent2hostgroup,cursor,parent_name,hostgroup) {
+    parent = (jed_simgrid_container_t)xbt_dict_get(container_name2container, parent_name);
+    add_subsets_to(subset_list, hostgroup, parent);
   }
+  xbt_dynar_free(&hostgroup);
 
   xbt_dict_free(&parent2hostgroup);
-
 }
 
-
 void jedule_add_meta_info(jedule_t jedule, char *key, char *value) {
-
   char *val_cp;
 
-  xbt_assert(key != NULL);
-  xbt_assert(value != NULL);
+  xbt_assert(key != nullptr);
+  xbt_assert(value != nullptr);
 
   val_cp = xbt_strdup(value);
-  xbt_dict_set(jedule->jedule_meta_info, key, val_cp, NULL);
+  xbt_dict_set(jedule->jedule_meta_info, key, val_cp, nullptr);
 }
 
 void jed_create_jedule(jedule_t *jedule) {
   *jedule = xbt_new0(s_jedule_t,1);
-  host2_simgrid_parent_container = xbt_dict_new_homogeneous(NULL);
-  container_name2container       = xbt_dict_new_homogeneous(NULL);
-  (*jedule)->jedule_meta_info    = xbt_dict_new_homogeneous(NULL);
+  host2_simgrid_parent_container = xbt_dict_new_homogeneous(nullptr);
+  container_name2container       = xbt_dict_new_homogeneous(nullptr);
+  (*jedule)->jedule_meta_info    = xbt_dict_new_homogeneous(nullptr);
 }
 
 void jed_free_jedule(jedule_t jedule) {
-
   jed_free_container(jedule->root_container);
 
   xbt_dict_free(&jedule->jedule_meta_info);
@@ -276,5 +225,4 @@ void jed_free_jedule(jedule_t jedule) {
   xbt_dict_free(&host2_simgrid_parent_container);
   xbt_dict_free(&container_name2container);
 }
-
 #endif