Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
woops revalidate some tesh files
[simgrid.git] / examples / simdag / scheduling / minmin_test.c
index 3a0788d..2d60522 100644 (file)
@@ -1,6 +1,6 @@
 /* simple test to schedule a DAX file with the Min-Min algorithm.           */
 
-/* Copyright (c) 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2009-2015. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -8,7 +8,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
-#include "simdag/simdag.h"
+#include "simgrid/simdag.h"
 #include "xbt/log.h"
 #include "xbt/ex.h"
 #include <string.h>
@@ -18,8 +18,9 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(test,
 
 typedef struct _WorkstationAttribute *WorkstationAttribute;
 struct _WorkstationAttribute {
-  /* Earliest time at wich a workstation is ready to execute a task */
+  /* Earliest time at which a workstation is ready to execute a task */
   double available_at;
+  SD_task_t last_scheduled_task;
 };
 
 static void SD_workstation_allocate_attribute(SD_workstation_t workstation)
@@ -51,6 +52,19 @@ static void SD_workstation_set_available_at(SD_workstation_t workstation,
   SD_workstation_set_data(workstation, attr);
 }
 
+static SD_task_t SD_workstation_get_last_scheduled_task( SD_workstation_t workstation){
+  WorkstationAttribute attr =
+      (WorkstationAttribute) SD_workstation_get_data(workstation);
+  return attr->last_scheduled_task;
+}
+
+static void SD_workstation_set_last_scheduled_task(SD_workstation_t workstation,
+    SD_task_t task){
+  WorkstationAttribute attr =
+      (WorkstationAttribute) SD_workstation_get_data(workstation);
+  attr->last_scheduled_task=task;
+  SD_workstation_set_data(workstation, attr);
+}
 
 static xbt_dynar_t get_ready_tasks(xbt_dynar_t dax)
 {
@@ -143,7 +157,7 @@ static SD_workstation_t SD_task_get_best_workstation(SD_task_t task)
   int i;
   double EFT, min_EFT = -1.0;
   const SD_workstation_t *workstations = SD_workstation_get_list();
-  int nworkstations = SD_workstation_get_number();
+  int nworkstations = SD_workstation_get_count();
   SD_workstation_t best_workstation;
 
   best_workstation = workstations[0];
@@ -168,7 +182,7 @@ static void output_xml(FILE * out, xbt_dynar_t dax)
 {
   unsigned int i, j, k;
   int current_nworkstations;
-  const int nworkstations = SD_workstation_get_number();
+  const int nworkstations = SD_workstation_get_count();
   const SD_workstation_t *workstations = SD_workstation_get_list();
   SD_task_t task;
   SD_workstation_t *list;
@@ -235,12 +249,12 @@ int main(int argc, char **argv)
 {
   unsigned int cursor;
   double finish_time, min_finish_time = -1.0;
-  SD_task_t task, selected_task = NULL;
+  SD_task_t task, selected_task = NULL, last_scheduled_task;
   xbt_dynar_t ready_tasks;
   SD_workstation_t workstation, selected_workstation = NULL;
   int total_nworkstations = 0;
   const SD_workstation_t *workstations = NULL;
-  xbt_dynar_t dax, changed;
+  xbt_dynar_t dax;
   FILE *out = NULL;
 
   /* initialization of SD */
@@ -270,7 +284,7 @@ int main(int argc, char **argv)
   SD_create_environment(argv[1]);
 
   /*  Allocating the workstation attribute */
-  total_nworkstations = SD_workstation_get_number();
+  total_nworkstations = SD_workstation_get_count();
   workstations = SD_workstation_get_list();
 
   for (cursor = 0; cursor < total_nworkstations; cursor++)
@@ -289,12 +303,11 @@ int main(int argc, char **argv)
   workstation = SD_task_get_best_workstation(task);
   SD_task_schedulel(task, 1, workstation);
 
-  while (!xbt_dynar_is_empty((changed = SD_simulate(-1.0)))) {
+  while (!xbt_dynar_is_empty(SD_simulate(-1.0))) {
     /* Get the set of ready tasks */
     ready_tasks = get_ready_tasks(dax);
     if (xbt_dynar_is_empty(ready_tasks)) {
       xbt_dynar_free_container(&ready_tasks);
-      xbt_dynar_free_container(&changed);
       /* there is no ready task, let advance the simulation */
       continue;
     }
@@ -318,12 +331,36 @@ int main(int argc, char **argv)
           SD_workstation_get_name(selected_workstation));
 
     SD_task_schedulel(selected_task, 1, selected_workstation);
+
+    /*
+     * SimDag allows tasks to be executed concurrently when they can by default.
+     * Yet schedulers take decisions assuming that tasks wait for resource
+     * availability to start.
+     * The solution (well crude hack is to keep track of the last task scheduled
+     * on a workstation and add a special type of dependency if needed to
+     * force the sequential execution meant by the scheduler.
+     * If the last scheduled task is already done, has failed or is a 
+     * predecessor of the current task, no need for a new dependency
+    */
+
+    last_scheduled_task = 
+      SD_workstation_get_last_scheduled_task(selected_workstation);
+    if (last_scheduled_task && 
+  (SD_task_get_state(last_scheduled_task) != SD_DONE) &&
+  (SD_task_get_state(last_scheduled_task) != SD_FAILED) &&
+  !SD_task_dependency_exists(
+     SD_workstation_get_last_scheduled_task(selected_workstation),
+     selected_task))
+      SD_task_dependency_add("resource", NULL,
+           last_scheduled_task, selected_task);
+    
+    SD_workstation_set_last_scheduled_task(selected_workstation, selected_task);
+    
     SD_workstation_set_available_at(selected_workstation, min_finish_time);
 
     xbt_dynar_free_container(&ready_tasks);
     /* reset the min_finish_time for the next set of ready tasks */
     min_finish_time = -1.;
-    xbt_dynar_free_container(&changed);
   }
 
   XBT_INFO("Simulation Time: %f", SD_get_clock());
@@ -344,7 +381,6 @@ int main(int argc, char **argv)
 
 
   xbt_dynar_free_container(&ready_tasks);
-  xbt_dynar_free_container(&changed);
 
   xbt_dynar_foreach(dax, cursor, task) {
     SD_task_destroy(task);