Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use standard function fmax() for doubles.
[simgrid.git] / examples / simdag / scheduling / sd_scheduling.c
index 7269bc6..b1b06aa 100644 (file)
@@ -1,14 +1,15 @@
-/* Copyright (c) 2009-2016. The SimGrid Team.
+/* Copyright (c) 2009-2018. 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. */
 
 /* simple test to schedule a DAX file with the Min-Min algorithm.           */
-#include <string.h>
 #include "simgrid/simdag.h"
+#include <math.h>
+#include <string.h>
 
-#if HAVE_JEDULE
+#if SIMGRID_HAVE_JEDULE
 #include "simgrid/jedule/jedule_sd_binding.h"
 #endif
 
@@ -21,19 +22,6 @@ struct _HostAttribute {
   SD_task_t last_scheduled_task;
 };
 
-static void sg_host_allocate_attribute(sg_host_t host)
-{
-  void *data;
-  data = calloc(1, sizeof(struct _HostAttribute));
-  sg_host_user_set(host, data);
-}
-
-static void sg_host_free_attribute(sg_host_t host)
-{
-  free(sg_host_user(host));
-  sg_host_user_set(host, NULL);
-}
-
 static double sg_host_get_available_at(sg_host_t host)
 {
   HostAttribute attr = (HostAttribute) sg_host_user(host);
@@ -78,38 +66,29 @@ static xbt_dynar_t get_ready_tasks(xbt_dynar_t dax)
 static double finish_on_at(SD_task_t task, sg_host_t host)
 {
   double result;
-  unsigned int i;
-  double data_available = 0.;
-  double redist_time = 0;
-  double last_data_available;
 
   xbt_dynar_t parents = SD_task_get_parents(task);
 
   if (!xbt_dynar_is_empty(parents)) {
+    unsigned int i;
+    double data_available = 0.;
+    double redist_time    = 0;
+    double last_data_available;
     /* compute last_data_available */
     SD_task_t parent;
     last_data_available = -1.0;
     xbt_dynar_foreach(parents, i, parent) {
       /* normal case */
       if (SD_task_get_kind(parent) == SD_TASK_COMM_E2E) {
-        xbt_dynar_t grand_parents = SD_task_get_parents(parent);
-        SD_task_t grand_parent;
-
-        xbt_assert(xbt_dynar_length(grand_parents) <2, "Error: transfer %s has 2 parents", SD_task_get_name(parent));
-
-        xbt_dynar_get_cpy(grand_parents, 0, &grand_parent);
-
-        sg_host_t * grand_parent_host_list = SD_task_get_workstation_list(grand_parent);
+        sg_host_t * parent_host= SD_task_get_workstation_list(parent);
         /* Estimate the redistribution time from this parent */
         if (SD_task_get_amount(parent) <= 1e-6){
           redist_time= 0;
         } else {
-          redist_time = SD_route_get_latency(grand_parent_host_list[0], host) +
-                        SD_task_get_amount(parent) / SD_route_get_bandwidth(grand_parent_host_list[0], host);
+          redist_time = sg_host_route_latency(parent_host[0], host) +
+                        SD_task_get_amount(parent) / sg_host_route_bandwidth(parent_host[0], host);
         }
-        data_available = SD_task_get_finish_time(grand_parent) + redist_time;
-
-        xbt_dynar_free_container(&grand_parents);
+        data_available = SD_task_get_start_time(parent) + redist_time;
       }
 
       /* no transfer, control dependency */
@@ -123,7 +102,7 @@ static double finish_on_at(SD_task_t task, sg_host_t host)
 
     xbt_dynar_free_container(&parents);
 
-    result = MAX(sg_host_get_available_at(host), last_data_available) + SD_task_get_amount(task)/sg_host_speed(host);
+    result = fmax(sg_host_get_available_at(host), last_data_available) + SD_task_get_amount(task) / sg_host_speed(host);
   } else {
     xbt_dynar_free_container(&parents);
 
@@ -134,7 +113,7 @@ static double finish_on_at(SD_task_t task, sg_host_t host)
 
 static sg_host_t SD_task_get_best_host(SD_task_t task)
 {
-  const sg_host_t *hosts = sg_host_list();
+  sg_host_t *hosts = sg_host_list();
   int nhosts = sg_host_count();
   sg_host_t best_host = hosts[0];
   double min_EFT = finish_on_at(task, hosts[0]);
@@ -148,6 +127,7 @@ static sg_host_t SD_task_get_best_host(SD_task_t task)
       best_host = hosts[i];
     }
   }
+  xbt_free(hosts);
   return best_host;
 }
 
@@ -155,7 +135,8 @@ int main(int argc, char **argv)
 {
   unsigned int cursor;
   double min_finish_time = -1.0;
-  SD_task_t task, selected_task = NULL;
+  SD_task_t task;
+  SD_task_t selected_task = NULL;
   xbt_dynar_t ready_tasks;
   sg_host_t selected_host = NULL;
   char * tracefilename = NULL;
@@ -175,10 +156,10 @@ int main(int argc, char **argv)
 
   /*  Allocating the host attribute */
   int total_nhosts = sg_host_count();
-  const sg_host_t *hosts = sg_host_list();
+  sg_host_t *hosts = sg_host_list();
 
   for (cursor = 0; cursor < total_nhosts; cursor++)
-    sg_host_allocate_attribute(hosts[cursor]);
+    sg_host_user_set(hosts[cursor], xbt_new0(struct _HostAttribute, 1));
 
   /* load the DAX file */
   xbt_dynar_t dax = SD_daxload(argv[2]);
@@ -191,13 +172,18 @@ int main(int argc, char **argv)
   xbt_dynar_get_cpy(dax, 0, &task);
   sg_host_t host = SD_task_get_best_host(task);
   SD_task_schedulel(task, 1, host);
+  xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
+  SD_simulate_with_update(-1.0, changed_tasks);
 
-  while (!xbt_dynar_is_empty(SD_simulate(-1.0))) {
+  while (!xbt_dynar_is_empty(changed_tasks)) {
     /* Get the set of ready tasks */
     ready_tasks = get_ready_tasks(dax);
+    xbt_dynar_reset(changed_tasks);
+
     if (xbt_dynar_is_empty(ready_tasks)) {
       xbt_dynar_free_container(&ready_tasks);
       /* there is no ready task, let advance the simulation */
+      SD_simulate_with_update(-1.0, changed_tasks);
       continue;
     }
     /* For each ready task:
@@ -231,7 +217,7 @@ int main(int argc, char **argv)
     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(sg_host_get_last_scheduled_task(selected_host), selected_task))
-      SD_task_dependency_add("resource", NULL, last_scheduled_task, selected_task);
+      SD_task_dependency_add(last_scheduled_task, selected_task);
 
     sg_host_set_last_scheduled_task(selected_host, selected_task);
     sg_host_set_available_at(selected_host, min_finish_time);
@@ -239,27 +225,31 @@ int main(int argc, char **argv)
     xbt_dynar_free_container(&ready_tasks);
     /* reset the min_finish_time for the next set of ready tasks */
     min_finish_time = -1.;
+    xbt_dynar_reset(changed_tasks);
+    SD_simulate_with_update(-1.0, changed_tasks);
   }
 
   XBT_INFO("Simulation Time: %f", SD_get_clock());
   XBT_INFO("------------------- Produce the trace file---------------------------");
   XBT_INFO("Producing a jedule output (if active) of the run into %s", tracefilename?tracefilename:"minmin_test.jed");
-#if HAVE_JEDULE
+#if SIMGRID_HAVE_JEDULE
   jedule_sd_dump(tracefilename);
 #endif
   free(tracefilename);
 
   xbt_dynar_free_container(&ready_tasks);
+  xbt_dynar_free(&changed_tasks);
 
   xbt_dynar_foreach(dax, cursor, task) {
     SD_task_destroy(task);
   }
   xbt_dynar_free_container(&dax);
 
-  for (cursor = 0; cursor < total_nhosts; cursor++)
-    sg_host_free_attribute(hosts[cursor]);
+  for (cursor = 0; cursor < total_nhosts; cursor++) {
+    free(sg_host_user(hosts[cursor]));
+    sg_host_user_set(hosts[cursor], NULL);
+  }
 
-  /* exit */
-  SD_exit();
+  xbt_free(hosts);
   return 0;
 }