Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Create a proper channel hierarchie for SD
[simgrid.git] / src / simdag / sd_global.c
index 254f7c0..e3bf318 100644 (file)
@@ -1,9 +1,12 @@
-#include "simdag/simdag.h"
 #include "private.h"
 #include "xbt/sysdep.h"
 #include "xbt/dynar.h"
 #include "surf/surf.h"
 
+XBT_LOG_NEW_CATEGORY(sd,"Logging specific to SimDag");
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel,sd,
+                               "Logging specific to SimDag (kernel)");
+
 SD_global_t sd_global = NULL;
 
 /* Initialises SD internal data. This function should be called before any other SD function.
@@ -15,7 +18,6 @@ void SD_init(int *argc, char **argv) {
   sd_global->workstations = xbt_dict_new();
   sd_global->workstation_count = 0;
   sd_global->links = xbt_dict_new();
-  sd_global->tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
   sd_global->watch_point_reached = 0;
 
   s_SD_task_t task;
@@ -69,93 +71,85 @@ SD_task_t* SD_simulate(double how_long)
 
   double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
   double elapsed_time = 0.0;
-  int i;
   SD_task_t task;
-  surf_action_t surf_action;
+  surf_action_t action;
+  SD_task_t *changed_tasks = NULL;
+  int changed_task_number = 0;
+  int changed_task_capacity = 16;
+
+  /* create the array that will be returned */
+  changed_tasks = xbt_new0(SD_task_t, changed_task_capacity); /* changed_task_capacity will be increased if necessary */
+  changed_tasks[0] = NULL;
 
   surf_solve(); /* Takes traces into account. Returns 0.0 */
 
+  sd_global->watch_point_reached = 0;
+
   /* main loop */
   while (elapsed_time >= 0.0 && total_time < how_long && !sd_global->watch_point_reached) {
-    for (i = 0 ; i < xbt_dynar_length(sd_global->tasks); i++) {
-      xbt_dynar_get_cpy(sd_global->tasks, i, &task);
-      printf("Examining task '%s'...\n", SD_task_get_name(task));
-
-      /* if the task is scheduled and the dependencies are satisfied,
-        we can execute the task */
-      if (SD_task_get_state(task) == SD_SCHEDULED) {
-       printf("Task '%s' is scheduled.\n", SD_task_get_name(task));
-
-       if (xbt_dynar_length(task->tasks_before) == 0) {
-         printf("The dependencies are satisfied. Executing task '%s'\n", SD_task_get_name(task));
-         surf_action = __SD_task_run(task);
-       }
-       else {
-         printf("Cannot execute task '%s' because some depencies are not satisfied.\n", SD_task_get_name(task));
+
+    /* explore the scheduled tasks */
+    xbt_swag_foreach(task, sd_global->scheduled_task_set) {
+      INFO1("Examining task '%s'...", SD_task_get_name(task));
+      task->state_changed = 0;
+      if (xbt_dynar_length(task->tasks_before) == 0) {
+       INFO1("The dependencies are satisfied. Executing task '%s'", SD_task_get_name(task));
+       action = __SD_task_run(task);
+       surf_workstation_resource->common_public->action_set_data(action, task);
+       task->state_changed = 1;
+       
+       changed_tasks[changed_task_number++] = task; /* replace NULL by the task */
+       if (changed_task_number == changed_task_capacity) {
+         changed_task_capacity *= 2;
+         changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
        }
+       changed_tasks[changed_task_number] = NULL;
       }
       else {
-       printf("Task '%s' is not scheduled. Nothing to do.\n", SD_task_get_name(task));
+       INFO1("Cannot execute task '%s' now because some depencies are not satisfied.", SD_task_get_name(task));
       }
     }
+
     elapsed_time = surf_solve();
     if (elapsed_time > 0.0)
       total_time += elapsed_time;
-    printf("Total time: %f\n", total_time);
-  }
-
-  return NULL;
-}
-
-void SD_test() {
-  /* temporary test to explore the workstations and the links */
-  xbt_dict_cursor_t cursor = NULL;
-  char *name = NULL;
-  SD_workstation_t workstation = NULL;
-  double power, available_power, bandwidth, latency;
-  SD_link_t link = NULL;
-
-  surf_solve();
+    /*    INFO1("Total time: %f", total_time);*/
+
+    while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.done_action_set))) {
+      task = action->data;
+      INFO1("Task '%s' done", SD_task_get_name(task));
+      __SD_task_set_state(task, SD_DONE);
+      __SD_task_remove_dependencies(task);
+      if (!task->state_changed) {
+       task->state_changed = 1;
+       changed_tasks[changed_task_number++] = task;
+      }
+    }
 
-  xbt_dict_foreach(sd_global->workstations, cursor, name, workstation) {
-    power = SD_workstation_get_power(workstation);
-    available_power = SD_workstation_get_available_power(workstation);
-    printf("Workstation name: %s, power: %f Mflop/s, available power: %f%%\n", name, power, (available_power*100));
+    while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.failed_action_set))) {
+      task = action->data;
+      INFO1("Task '%s' failed", SD_task_get_name(task));
+      __SD_task_set_state(task, SD_FAILED);
+      if (!task->state_changed) {
+       task->state_changed = 1;
+       changed_tasks[changed_task_number++] = task;
+      }
+    }
   }
 
-  xbt_dict_foreach(sd_global->links, cursor, name, link) {
-    bandwidth = SD_link_get_current_bandwidth(link);
-    latency = SD_link_get_current_latency(link);
-    printf("Link name: %s, bandwidth: %f, latency: %f\n", name, bandwidth, latency);
-  }
+  INFO0("Simulation finished");
 
-  /* test the route between two workstations */
-  SD_workstation_t src, dst;
-  xbt_dict_cursor_first(sd_global->workstations, &cursor);
-  xbt_dict_cursor_get_or_free(&cursor, &name, (void**) &src);
-  xbt_dict_cursor_step(cursor);
-  xbt_dict_cursor_get_or_free(&cursor, &name, (void**) &dst);
-  xbt_dict_cursor_free(&cursor);
-
-  SD_link_t *route = SD_workstation_route_get_list(src, dst);
-  int route_size = SD_workstation_route_get_size(src, dst);
-
-  printf("Route between %s and %s (%d links) : ", SD_workstation_get_name(src), SD_workstation_get_name(dst), route_size);
-  int i;
-  for (i = 0; i < route_size; i++) {
-    printf("%s ", SD_link_get_name(route[i]));
-  }
-  printf("\n");
+  INFO1("Number of tasks whose state has changed: %d", changed_task_number);
+  return changed_tasks;
 }
 
 /* Destroys all SD internal data. This function should be called when the simulation is over.
  * The tasks should have been destroyed first.
  */
-void SD_exit() {
+void SD_exit(void) {
   if (sd_global != NULL) {
     xbt_dict_free(&sd_global->workstations);
     xbt_dict_free(&sd_global->links);
-    xbt_dynar_free(&sd_global->tasks);
     xbt_free(sd_global);
 
     xbt_swag_free(sd_global->not_scheduled_task_set);