Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add functions SD_link_get_list() and SD_link_get_number() + optimize SD_workstation_g...
authorthiery <thiery@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 3 Jul 2006 14:23:54 +0000 (14:23 +0000)
committerthiery <thiery@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 3 Jul 2006 14:23:54 +0000 (14:23 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2467 48e7efb5-ca39-0410-a469-dd3cf9ba447f

examples/simdag/sd_test.c
include/simdag/simdag.h
src/simdag/private.h
src/simdag/sd_global.c
src/simdag/sd_link.c
src/simdag/sd_workstation.c

index 35d35cd..b2c435a 100644 (file)
@@ -39,7 +39,7 @@ int main(int argc, char **argv) {
   /* let's launch the simulation! */
 
   int workstation_number = 2;
-  SD_workstation_t *workstation_list = SD_workstation_get_list();
+  const SD_workstation_t *workstation_list = SD_workstation_get_list();
   double computation_amount[] = {100, 200};
   double communication_amount[] =
     {
index e82edbd..1c2f004 100644 (file)
@@ -20,11 +20,13 @@ SG_BEGIN_DECL()
  *  @see SD_link_t
  *  @{
  */
-void*        SD_link_get_data(SD_link_t link);
-void         SD_link_set_data(SD_link_t link, void *data);
-const char*  SD_link_get_name(SD_link_t link);
-double       SD_link_get_current_bandwidth(SD_link_t link);
-double       SD_link_get_current_latency(SD_link_t link);
+const SD_link_t*   SD_link_get_list(void);
+int                SD_link_get_number(void);
+void*              SD_link_get_data(SD_link_t link);
+void               SD_link_set_data(SD_link_t link, void *data);
+const char*        SD_link_get_name(SD_link_t link);
+double             SD_link_get_current_bandwidth(SD_link_t link);
+double             SD_link_get_current_latency(SD_link_t link);
 /** @} */
 
 /************************** Workstation handling ****************************/
@@ -43,16 +45,16 @@ double       SD_link_get_current_latency(SD_link_t link);
  *  @see SD_workstation_t
  *  @{
  */
-SD_workstation_t   SD_workstation_get_by_name(const char *name);
-SD_workstation_t*  SD_workstation_get_list(void);
-int                SD_workstation_get_number(void);
-void               SD_workstation_set_data(SD_workstation_t workstation, void *data);
-void*              SD_workstation_get_data(SD_workstation_t workstation);
-const char*        SD_workstation_get_name(SD_workstation_t workstation);
-SD_link_t*         SD_workstation_route_get_list(SD_workstation_t src, SD_workstation_t dst);
-int                SD_workstation_route_get_size(SD_workstation_t src, SD_workstation_t dst);
-double             SD_workstation_get_power(SD_workstation_t workstation);
-double             SD_workstation_get_available_power(SD_workstation_t workstation);
+SD_workstation_t        SD_workstation_get_by_name(const char *name);
+const SD_workstation_t* SD_workstation_get_list(void);
+int                     SD_workstation_get_number(void);
+void                    SD_workstation_set_data(SD_workstation_t workstation, void *data);
+void*                   SD_workstation_get_data(SD_workstation_t workstation);
+const char*             SD_workstation_get_name(SD_workstation_t workstation);
+SD_link_t*              SD_workstation_route_get_list(SD_workstation_t src, SD_workstation_t dst);
+int                     SD_workstation_route_get_size(SD_workstation_t src, SD_workstation_t dst);
+double                  SD_workstation_get_power(SD_workstation_t workstation);
+double                  SD_workstation_get_available_power(SD_workstation_t workstation);
 /** @} */
 
 /************************** Task handling ************************************/
index 0ef4402..111a0b1 100644 (file)
 /* Global variables */
 
 typedef struct SD_global {
-  xbt_dict_t workstations; /* workstation list */
+  xbt_dict_t workstations; /* workstation dictionary */
   int workstation_count; /* number of workstations */
-  xbt_dict_t links; /* link list */
+  SD_workstation_t *workstation_list; /* array of workstations, created only if
+                                        necessary in SD_workstation_get_list */
+
+  xbt_dict_t links; /* links */
+  int link_count; /* number of links */
+  SD_link_t *link_list; /* array of links, created only if
+                          necessary in SD_link_get_list */
+
   int watch_point_reached; /* has a task just reached a watch point? */
   
   /* task state sets */
index ffd30c7..741805e 100644 (file)
@@ -25,7 +25,10 @@ void SD_init(int *argc, char **argv) {
   sd_global = xbt_new0(s_SD_global_t, 1);
   sd_global->workstations = xbt_dict_new();
   sd_global->workstation_count = 0;
+  sd_global->workstation_list = NULL;
   sd_global->links = xbt_dict_new();
+  sd_global->link_count = 0;
+  sd_global->link_list = NULL;
   sd_global->watch_point_reached = 0;
 
   s_SD_task_t task;
@@ -227,6 +230,13 @@ void SD_exit(void) {
   if (sd_global != NULL) {
     xbt_dict_free(&sd_global->workstations);
     xbt_dict_free(&sd_global->links);
+
+    if (sd_global->workstation_list != NULL)
+      xbt_free(sd_global->workstation_list);
+
+    if (sd_global->link_list != NULL)
+      xbt_free(sd_global->link_list);
+
     xbt_free(sd_global);
 
     xbt_swag_free(sd_global->not_scheduled_task_set);
index 0edd95e..3ab7818 100644 (file)
@@ -10,16 +10,55 @@ SD_link_t __SD_link_create(void *surf_link, void *data) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(surf_link != NULL, "surf_link is NULL !");
 
-
   SD_link_t link = xbt_new0(s_SD_link_t, 1);
   link->surf_link = surf_link;
   link->data = data; /* user data */
 
   const char *name = SD_link_get_name(link);
   xbt_dict_set(sd_global->links, name, link, __SD_link_destroy); /* add the link to the dictionary */
+  sd_global->link_count++;
 
   return link;
 }
+/**
+ * \brief Returns the link list
+ *
+ * Use SD_link_get_number() to know the array size.
+ *
+ * \return an array of \ref SD_link_t containing all links
+ * \see SD_link_get_number()
+ */
+const SD_link_t*  SD_link_get_list(void) {
+  SD_CHECK_INIT_DONE();
+  xbt_assert0(SD_link_get_number() > 0, "There is no link!");
+
+  SD_link_t *array = sd_global->link_list;
+  xbt_dict_cursor_t cursor;
+  char *key;
+  void *data;
+  int i;
+
+  if (array == NULL) { /* this is the first time the function is called */
+    array = xbt_new0(SD_link_t, sd_global->link_count);
+  
+    i = 0;
+    xbt_dict_foreach(sd_global->links, cursor, key, data) {
+      array[i++] = (SD_link_t) data;
+    }
+  }
+  return array;
+}
+
+/**
+ * \brief Returns the number of links
+ *
+ * \return the number of existing links
+ * \see SD_link_get_list()
+ */
+int SD_link_get_number(void) {
+  SD_CHECK_INIT_DONE();
+  return sd_global->link_count;
+}
 
 /**
  * \brief Returns the user data of a link
index 05dab14..f2337be 100644 (file)
@@ -38,28 +38,31 @@ SD_workstation_t SD_workstation_get_by_name(const char *name) {
 }
 
 /**
- * \brief Returns the workstations list
+ * \brief Returns the workstation list
  *
  * Use SD_workstation_get_number() to know the array size.
  *
  * \return an array of \ref SD_workstation_t containing all workstations
  * \see SD_workstation_get_number()
  */
-SD_workstation_t*  SD_workstation_get_list(void) {
+const SD_workstation_t*  SD_workstation_get_list(void) {
   SD_CHECK_INIT_DONE();
   xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
 
-  SD_workstation_t* array = xbt_new0(SD_workstation_t, sd_global->workstation_count);
-  
+  SD_workstation_t *array = sd_global->workstation_list;
   xbt_dict_cursor_t cursor;
   char *key;
   void *data;
-  int i=0;
+  int i;
 
-  xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
-    array[i++] = (SD_workstation_t) data;
+  if (array == NULL) { /* this is the first time the function is called */
+    array = xbt_new0(SD_workstation_t, sd_global->workstation_count);
+  
+    i = 0;
+    xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
+      array[i++] = (SD_workstation_t) data;
+    }
   }
-
   return array;
 }