From: thiery Date: Mon, 3 Jul 2006 14:23:54 +0000 (+0000) Subject: Add functions SD_link_get_list() and SD_link_get_number() + optimize SD_workstation_g... X-Git-Tag: v3.3~2891 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/149eacb246aabe7926f62376466d558a102e7c24 Add functions SD_link_get_list() and SD_link_get_number() + optimize SD_workstation_get_list() git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2467 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/examples/simdag/sd_test.c b/examples/simdag/sd_test.c index 35d35cd692..b2c435ae9f 100644 --- a/examples/simdag/sd_test.c +++ b/examples/simdag/sd_test.c @@ -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[] = { diff --git a/include/simdag/simdag.h b/include/simdag/simdag.h index e82edbd220..1c2f004151 100644 --- a/include/simdag/simdag.h +++ b/include/simdag/simdag.h @@ -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 ************************************/ diff --git a/src/simdag/private.h b/src/simdag/private.h index 0ef4402ddb..111a0b1765 100644 --- a/src/simdag/private.h +++ b/src/simdag/private.h @@ -12,9 +12,16 @@ /* 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 */ diff --git a/src/simdag/sd_global.c b/src/simdag/sd_global.c index ffd30c7a8a..741805e302 100644 --- a/src/simdag/sd_global.c +++ b/src/simdag/sd_global.c @@ -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); diff --git a/src/simdag/sd_link.c b/src/simdag/sd_link.c index 0edd95e860..3ab7818b95 100644 --- a/src/simdag/sd_link.c +++ b/src/simdag/sd_link.c @@ -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 diff --git a/src/simdag/sd_workstation.c b/src/simdag/sd_workstation.c index 05dab14ecb..f2337be66e 100644 --- a/src/simdag/sd_workstation.c +++ b/src/simdag/sd_workstation.c @@ -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; }