Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use swags to handle task states and change structure s_SD_task_t.
[simgrid.git] / src / simdag / sd_task.c
index f4b08df..e159888 100644 (file)
@@ -3,86 +3,89 @@
 #include "xbt/sysdep.h"
 #include "xbt/dynar.h"
 
+static void __SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state);
+
 /* Creates a task.
  */
 SD_task_t SD_task_create(const char *name, void *data, double amount) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(amount > 0, "amount must be positive");
 
-  SD_task_data_t sd_data = xbt_new0(s_SD_task_data_t, 1); /* task private data */
+  SD_task_t task = xbt_new0(s_SD_task_t, 1);
 
   /* general information */
+  task->data = data; /* user data */
   if (name != NULL)
-    sd_data->name = xbt_strdup(name);
+    task->name = xbt_strdup(name);
   else
-    sd_data->name = NULL;
+    task->name = NULL;
 
-  sd_data->state = SD_NOT_SCHEDULED;
-  sd_data->amount = amount;
-  sd_data->surf_action = NULL;
-  sd_data->watch_points = 0;
+  task->state_set = sd_global->not_scheduled_task_set;
+  task->amount = amount;
+  task->surf_action = NULL;
+  task->watch_points = 0;
 
   /* dependencies */
-  sd_data->tasks_before = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
-  sd_data->tasks_after = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
+  task->tasks_before = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
+  task->tasks_after = xbt_dynar_new(sizeof(SD_dependency_t), NULL);
 
   /* scheduling parameters */
-  sd_data->workstation_nb = 0;
-  sd_data->workstation_list = NULL;
-  sd_data->computation_amount = NULL;
-  sd_data->communication_amount = NULL;
-  sd_data->rate = 0;
+  task->workstation_nb = 0;
+  task->workstation_list = NULL;
+  task->computation_amount = NULL;
+  task->communication_amount = NULL;
+  task->rate = 0;
 
-  SD_task_t task = xbt_new0(s_SD_task_t, 1);
-  task->sd_data = sd_data; /* private data */
-  task->data = data; /* user data */
+  xbt_dynar_push(sd_global->tasks, &task);
 
   return task;
 }
 
-/* Schedules a task.
- * task: the task to schedule
- * workstation_nb: number of workstations where the task will be executed
- * workstation_list: workstations where the task will be executed
- * computation_amount: computation amount for each workstation
- * communication_amount: communication amount between each pair of workstations
- * rate: task execution speed rate
+/* Returns the data of a task.
  */
-void SD_task_schedule(SD_task_t task, int workstation_nb,
-                    const SD_workstation_t *workstation_list, double *computation_amount,
-                    double *communication_amount, double rate) {
+void* SD_task_get_data(SD_task_t task) {
   SD_CHECK_INIT_DONE();
-  xbt_assert0(task, "Invalid parameter");
-  xbt_assert1(SD_task_get_state(task) == SD_NOT_SCHEDULED, "Task '%s' has already been scheduled.", SD_task_get_name(task));
-  xbt_assert0(workstation_nb > 0, "workstation_nb must be positive");
-
-  SD_task_data_t sd_data = task->sd_data;
-  sd_data->workstation_nb = workstation_nb;
-  sd_data->rate = rate;
-
-  sd_data->computation_amount = xbt_new0(double, workstation_nb);
-  memcpy(sd_data->computation_amount, computation_amount, sizeof(double) * workstation_nb);
-
-  int communication_nb = workstation_nb * workstation_nb;
-  sd_data->communication_amount = xbt_new0(double, communication_nb);
-  memcpy(sd_data->communication_amount, communication_amount, sizeof(double) * communication_nb);
-
-  /* we have to create a Surf workstation array instead of the SimDag workstation array */
-  sd_data->workstation_list = xbt_new0(void*, workstation_nb);
-  int i;
-  for (i = 0; i < workstation_nb; i++) {
-    sd_data->workstation_list[i] = workstation_list[i]->sd_data->surf_workstation;
-  }
-
-  sd_data->state = SD_SCHEDULED;
+  xbt_assert0(task != NULL, "Invalid parameter");
+  return task->data;
 }
 
-/* Returns the data of a task.
+/* Returns the state of a task: SD_NOT_SCHEDULED, SD_SCHEDULED, SD_RUNNING, SD_DONE or SD_FAILED.
  */
-void* SD_task_get_data(SD_task_t task) {
+e_SD_task_state_t SD_task_get_state(SD_task_t task) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(task != NULL, "Invalid parameter");
-  return task->data;
+
+  if (task->state_set == sd_global->not_scheduled_task_set)
+    return SD_NOT_SCHEDULED;
+  if (task->state_set == sd_global->scheduled_task_set)
+    return SD_SCHEDULED;
+  if (task->state_set == sd_global->running_task_set)
+    return SD_RUNNING;
+  if (task->state_set == sd_global->done_task_set)
+    return SD_DONE;
+  return SD_FAILED;
+}
+
+/* Changes the state of a task and update the swags.
+ */
+static void __SD_task_set_state(SD_task_t task, e_SD_task_state_t new_state) {
+  xbt_swag_remove(task, task->state_set);
+  switch (new_state) {
+  case SD_NOT_SCHEDULED:
+    task->state_set = sd_global->not_scheduled_task_set;
+    break;
+  case SD_SCHEDULED:
+    task->state_set = sd_global->scheduled_task_set;
+    break;
+  case SD_RUNNING:
+    task->state_set = sd_global->running_task_set;
+    break;
+  case SD_DONE:
+    task->state_set = sd_global->done_task_set;
+    break;
+  default: /* SD_FAILED */
+    task->state_set = sd_global->failed_task_set;
+  }
 }
 
 /* Sets the data of a task.
@@ -98,7 +101,7 @@ void SD_task_set_data(SD_task_t task, void *data) {
 const char* SD_task_get_name(SD_task_t task) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(task != NULL, "Invalid parameter");
-  return task->sd_data->name;
+  return task->name;
 }
 
 /* Returns the computing amount of a task.
@@ -106,7 +109,7 @@ const char* SD_task_get_name(SD_task_t task) {
 double SD_task_get_amount(SD_task_t task) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(task != NULL, "Invalid parameter");
-  return task->sd_data->amount;
+  return task->amount;
 }
 
 /* Returns the remaining computing amount of a task.
@@ -114,17 +117,17 @@ double SD_task_get_amount(SD_task_t task) {
 double SD_task_get_remaining_amount(SD_task_t task) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(task != NULL, "Invalid parameter");
-  SD_task_data_t sd_data = task->sd_data;
-  if (sd_data->surf_action)
-    return sd_data->amount;
+  
+  if (task->surf_action)
+    return task->amount;
   else
-    return sd_data->surf_action->remains;
+    return task->surf_action->remains;
 }
 
-/* temporary function for debbuging */
+/* temporary function for debbuging
 void __SD_print_dependencies(SD_task_t task) {
   printf("The following tasks must be executed before %s:", SD_task_get_name(task));
-  xbt_dynar_t dynar = task->sd_data->tasks_before;
+  xbt_dynar_t dynar = task->tasks_before;
   int length = xbt_dynar_length(dynar);
   int i;
   SD_dependency_t dependency;
@@ -135,13 +138,21 @@ void __SD_print_dependencies(SD_task_t task) {
 
   printf("\nThe following tasks must be executed after %s:", SD_task_get_name(task));
 
-  dynar = task->sd_data->tasks_after;
+  dynar = task->tasks_after;
   length = xbt_dynar_length(dynar);
   for (i = 0; i < length; i++) {
     xbt_dynar_get_cpy(dynar, i, &dependency);
     printf(" %s", SD_task_get_name(dependency->dst));
   }
   printf("\n----------------------------\n");
+}*/
+
+/* Destroys a dependency between two tasks.
+ */
+void __SD_task_destroy_dependency(void *dependency) {
+  if (((SD_dependency_t) dependency)->name != NULL)
+    xbt_free(((SD_dependency_t) dependency)->name);
+  xbt_free(dependency);
 }
 
 /* Adds a dependency between two tasks.
@@ -151,7 +162,7 @@ void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task
   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
   xbt_assert1(src != dst, "Cannot add a dependency between task '%s' and itself", SD_task_get_name(src));
 
-  xbt_dynar_t dynar = src->sd_data->tasks_after;
+  xbt_dynar_t dynar = src->tasks_after;
   int length = xbt_dynar_length(dynar);
   int found = 0;
   int i;
@@ -160,7 +171,7 @@ void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task
     xbt_dynar_get_cpy(dynar, i, &dependency);
     found = (dependency->dst == dst);
   }
-  xbt_assert2(!found, "A dependency already exists between task '%s' and task '%s'", src->sd_data->name, dst->sd_data->name);
+  xbt_assert2(!found, "A dependency already exists between task '%s' and task '%s'", SD_task_get_name(src), SD_task_get_name(dst));
 
   dependency = xbt_new0(s_SD_dependency_t, 1);
 
@@ -171,8 +182,8 @@ void SD_task_dependency_add(const char *name, void *data, SD_task_t src, SD_task
   dependency->dst = dst;
 
   /* src must be executed before dst */
-  xbt_dynar_push(src->sd_data->tasks_after, &dependency);
-  xbt_dynar_push(dst->sd_data->tasks_before, &dependency);
+  xbt_dynar_push(src->tasks_after, &dependency);
+  xbt_dynar_push(dst->tasks_before, &dependency);
 
   /*  __SD_print_dependencies(src);
       __SD_print_dependencies(dst); */
@@ -185,7 +196,7 @@ void SD_task_dependency_remove(SD_task_t src, SD_task_t dst) {
   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
   xbt_assert1(src != dst, "Cannot remove a dependency between task '%s' and itself", SD_task_get_name(src));
 
-  xbt_dynar_t dynar = src->sd_data->tasks_after;
+  xbt_dynar_t dynar = src->tasks_after;
   int length = xbt_dynar_length(dynar);
   int found = 0;
   int i;
@@ -198,9 +209,9 @@ void SD_task_dependency_remove(SD_task_t src, SD_task_t dst) {
     }
   }
   xbt_assert4(found, "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
-             src->sd_data->name, dst->sd_data->name, dst->sd_data->name, src->sd_data->name);
+             SD_task_get_name(src), SD_task_get_name(dst), SD_task_get_name(dst), SD_task_get_name(src));
 
-  dynar = dst->sd_data->tasks_before;
+  dynar = dst->tasks_before;
   length = xbt_dynar_length(dynar);
   found = 0;
   
@@ -213,10 +224,10 @@ void SD_task_dependency_remove(SD_task_t src, SD_task_t dst) {
     }
   }
   xbt_assert4(found, "SimDag error: task '%s' is a successor of '%s' but task '%s' is not a predecessor of task '%s'",
-             dst->sd_data->name, src->sd_data->name, src->sd_data->name, dst->sd_data->name); /* should never happen... */
+             SD_task_get_name(dst), SD_task_get_name(src), SD_task_get_name(src), SD_task_get_name(dst)); /* should never happen... */
 
-/*   __SD_print_dependencies(src); 
-     __SD_print_dependencies(dst); */
+  /*   __SD_print_dependencies(src); 
+       __SD_print_dependencies(dst); */
 }
 
 /* Returns the data associated to a dependency between two tasks. This data can be NULL.
@@ -226,7 +237,7 @@ void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst) {
   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
   xbt_assert1(src != dst, "Cannot have a dependency between task '%s' and itself", SD_task_get_name(src));
 
-  xbt_dynar_t dynar = src->sd_data->tasks_after;
+  xbt_dynar_t dynar = src->tasks_after;
   int length = xbt_dynar_length(dynar);
   int found = 0;
   int i;
@@ -235,29 +246,20 @@ void *SD_task_dependency_get_data(SD_task_t src, SD_task_t dst) {
     xbt_dynar_get_cpy(dynar, i, &dependency);
     found = (dependency->dst == dst);
   }
-  xbt_assert4(found, "No dependency found between task '%s' and '%s': task '%s' is not a successor of task '%s'",
-             src->sd_data->name, dst->sd_data->name, dst->sd_data->name, src->sd_data->name);
+  xbt_assert2(found, "No dependency found between task '%s' and '%s'", SD_task_get_name(src), SD_task_get_name(dst));
   return dependency->data;
 }
 
-/* Returns the state of a task: SD_NOT_SCHEDULED, SD_SCHEDULED, SD_RUNNING, SD_DONE or SD_FAILED.
- */
-SD_task_state_t SD_task_get_state(SD_task_t task) {
-  SD_CHECK_INIT_DONE();
-  xbt_assert0(task != NULL, "Invalid parameter");
-  return task->sd_data->state;
-}
-
 /* temporary function for debugging */
-void __SD_print_watch_points(SD_task_t task) {
+static void __SD_print_watch_points(SD_task_t task) {
   static const int state_masks[] = {SD_SCHEDULED, SD_RUNNING, SD_DONE, SD_FAILED};
   static const char* state_names[] = {"scheduled", "running", "done", "failed"};
 
-  printf("Task '%s' watch points (%x): ", task->sd_data->name, task->sd_data->watch_points);
+  printf("Task '%s' watch points (%x): ", SD_task_get_name(task), task->watch_points);
 
   int i;
   for (i = 0; i < 4; i++) {
-    if (task->sd_data->watch_points & state_masks[i])
+    if (task->watch_points & state_masks[i])
       printf("%s ", state_names[i]);
   }
   printf("\n");
@@ -267,24 +269,68 @@ void __SD_print_watch_points(SD_task_t task) {
    SD_simulate will stop as soon as the state of this task is the one given in argument.
    Watch point is then automatically removed.
  */
-void SD_task_watch(SD_task_t task, SD_task_state_t state) {
+void SD_task_watch(SD_task_t task, e_SD_task_state_t state) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(task != NULL, "Invalid parameter");
 
-  task->sd_data->watch_points = task->sd_data->watch_points | state;
+  task->watch_points = task->watch_points | state;
   __SD_print_watch_points(task);
 }
 
 /* Removes a watch point from a task.
  */
-void SD_task_unwatch(SD_task_t task, SD_task_state_t state) {
+void SD_task_unwatch(SD_task_t task, e_SD_task_state_t state) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(task != NULL, "Invalid parameter");
   
-  task->sd_data->watch_points = task->sd_data->watch_points & ~state;
+  task->watch_points = task->watch_points & ~state;
   __SD_print_watch_points(task);
 }
 
+/* Destroys the data memorised by SD_task_schedule. Task state must be SD_SCHEDULED.
+ */
+static void __SD_task_destroy_scheduling_data(SD_task_t task) {
+  xbt_free(task->workstation_list);
+  xbt_free(task->computation_amount);
+  xbt_free(task->communication_amount);
+}
+
+/* Schedules a task.
+ * task: the task to schedule
+ * workstation_nb: number of workstations where the task will be executed
+ * workstation_list: workstations where the task will be executed
+ * computation_amount: computation amount for each workstation
+ * communication_amount: communication amount between each pair of workstations
+ * rate: task execution speed rate
+ */
+void SD_task_schedule(SD_task_t task, int workstation_nb,
+                    const SD_workstation_t *workstation_list, double *computation_amount,
+                    double *communication_amount, double rate) {
+  SD_CHECK_INIT_DONE();
+  xbt_assert0(task, "Invalid parameter");
+  xbt_assert1(SD_task_get_state(task) == SD_NOT_SCHEDULED, "Task '%s' has already been scheduled.", SD_task_get_name(task));
+  xbt_assert0(workstation_nb > 0, "workstation_nb must be positive");
+
+  task->workstation_nb = workstation_nb;
+  task->rate = rate;
+
+  task->computation_amount = xbt_new0(double, workstation_nb);
+  memcpy(task->computation_amount, computation_amount, sizeof(double) * workstation_nb);
+
+  int communication_nb = workstation_nb * workstation_nb;
+  task->communication_amount = xbt_new0(double, communication_nb);
+  memcpy(task->communication_amount, communication_amount, sizeof(double) * communication_nb);
+
+  /* we have to create a Surf workstation array instead of the SimDag workstation array */
+  task->workstation_list = xbt_new0(void*, workstation_nb);
+  int i;
+  for (i = 0; i < workstation_nb; i++) {
+    task->workstation_list[i] = workstation_list[i]->sd_data->surf_workstation;
+  }
+
+  __SD_task_set_state(task, SD_SCHEDULED);
+}
+
 /* Unschedules a task. The state must be SD_SCHEDULED, SD_RUNNING or SD_FAILED.
  * The task is reinitialised and its state becomes SD_NOT_SCHEDULED.
  * Call SD_task_schedule to schedule it again.
@@ -292,35 +338,37 @@ void SD_task_unwatch(SD_task_t task, SD_task_state_t state) {
 void SD_task_unschedule(SD_task_t task) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(task != NULL, "Invalid parameter");
-  xbt_assert1(task->sd_data->state == SD_SCHEDULED ||
-             task->sd_data->state == SD_RUNNING ||
-             task->sd_data->state == SD_FAILED,
-             "Task %s: the state must be SD_SCHEDULED, SD_RUNNING or SD_FAILED", task->sd_data->name);
+  e_SD_task_state_t state = SD_task_get_state(task);
+  xbt_assert1(state == SD_SCHEDULED ||
+             state == SD_RUNNING ||
+             state == SD_FAILED,
+             "Task %s: the state must be SD_SCHEDULED, SD_RUNNING or SD_FAILED", SD_task_get_name(task));
 
-  if (task->sd_data->state == SD_SCHEDULED)
+  if (state == SD_SCHEDULED)
     __SD_task_destroy_scheduling_data(task);
 
-  task->sd_data->state = SD_NOT_SCHEDULED;
+  __SD_task_set_state(task, SD_NOT_SCHEDULED);
 }
 
 /* Runs a task. This function is called by SD_simulate when a scheduled task can start
  * (ie when its dependencies are satisfied).
  */
-void __SD_task_run(SD_task_t task) {
+surf_action_t __SD_task_run(SD_task_t task) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(task != NULL, "Invalid parameter");
 
-  SD_task_data_t sd_data = task->sd_data;
-  surf_workstation_resource->extension_public->
-    execute_parallel_task(sd_data->workstation_nb,
-                         sd_data->workstation_list,
-                         sd_data->computation_amount,
-                         sd_data->communication_amount,
-                         sd_data->amount,
-                         sd_data->rate);
-  task->sd_data->state = SD_RUNNING;
+
+  surf_action_t surf_action = surf_workstation_resource->extension_public->
+    execute_parallel_task(task->workstation_nb,
+                         task->workstation_list,
+                         task->computation_amount,
+                         task->communication_amount,
+                         task->amount,
+                         task->rate);
+  __SD_task_set_state(task, SD_RUNNING);
 
   __SD_task_destroy_scheduling_data(task); /* now the scheduling data are not useful anymore */
+  return surf_action;
 }
 
 
@@ -330,50 +378,36 @@ void SD_task_destroy(SD_task_t task) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(task != NULL, "Invalid parameter");
 
-  /*  printf("Destroying task %s...\n", SD_task_get_name(task));*/
+  /*printf("Destroying task %s...\n", SD_task_get_name(task));*/
+
+  /* remove the task from SimDag task list */
+  int index = xbt_dynar_search(sd_global->tasks, &task);
+  xbt_dynar_remove_at(sd_global->tasks, index, NULL);
 
   /* we must destroy the dependencies carefuly (with SD_dependency_remove)
      because each one is stored twice */
   SD_dependency_t dependency;
-  while (xbt_dynar_length(task->sd_data->tasks_before) > 0) {
-    xbt_dynar_get_cpy(task->sd_data->tasks_before, 0, &dependency);
+  while (xbt_dynar_length(task->tasks_before) > 0) {
+    xbt_dynar_get_cpy(task->tasks_before, 0, &dependency);
     SD_task_dependency_remove(dependency->src, dependency->dst);
   }
 
-  while (xbt_dynar_length(task->sd_data->tasks_after) > 0) {
-    xbt_dynar_get_cpy(task->sd_data->tasks_after, 0, &dependency);
+  while (xbt_dynar_length(task->tasks_after) > 0) {
+    xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
     SD_task_dependency_remove(dependency->src, dependency->dst);
   }
 
-  if (task->sd_data->state == SD_SCHEDULED)
+  /* if the task was scheduled we have to free the scheduling parameters */
+  if (SD_task_get_state(task) == SD_SCHEDULED)
     __SD_task_destroy_scheduling_data(task);
 
-  if (task->sd_data->name != NULL)
-    xbt_free(task->sd_data->name);
+  if (task->name != NULL)
+    xbt_free(task->name);
 
-  xbt_dynar_free(&task->sd_data->tasks_before);
-  xbt_dynar_free(&task->sd_data->tasks_after);
-  xbt_free(task->sd_data);
+  xbt_dynar_free(&task->tasks_before);
+  xbt_dynar_free(&task->tasks_after);
   xbt_free(task);
 
   /*printf("Task destroyed.\n");*/
 
 }
-
-/* Destroys the data memorised by SD_task_schedule. Task state must be SD_SCHEDULED.
- */
-void __SD_task_destroy_scheduling_data(SD_task_t task) {
-  xbt_free(task->sd_data->workstation_list);
-  xbt_free(task->sd_data->computation_amount);
-  xbt_free(task->sd_data->communication_amount);
-}
-
-/* Destroys a dependency between two tasks.
- */
-void __SD_task_destroy_dependency(void *dependency) {
-  if (((SD_dependency_t) dependency)->name != NULL)
-    xbt_free(((SD_dependency_t) dependency)->name);
-  /*printf("destroying dependency between %s and %s\n", ((SD_dependency_t) dependency)->src->sd_data->name, ((SD_dependency_t) dependency)->dst->sd_data->name);*/
-  xbt_free(dependency);
-  /*printf("destroyed.\n");*/
-}