From: thiery Date: Fri, 4 Aug 2006 08:55:50 +0000 (+0000) Subject: Optimize function SD_route_get_list() X-Git-Tag: v3.3~2669 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/e727abe62548b7e41e373141953030d1186cb1fb Optimize function SD_route_get_list() git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2690 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/include/simdag/simdag.h b/include/simdag/simdag.h index d4d1414ea4..4eefb48953 100644 --- a/include/simdag/simdag.h +++ b/include/simdag/simdag.h @@ -51,7 +51,7 @@ 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_route_get_list(SD_workstation_t src, SD_workstation_t dst); +const SD_link_t* SD_route_get_list(SD_workstation_t src, SD_workstation_t dst); int SD_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); diff --git a/src/simdag/private.h b/src/simdag/private.h index 35bcbc5848..8d76354346 100644 --- a/src/simdag/private.h +++ b/src/simdag/private.h @@ -23,6 +23,8 @@ typedef struct SD_global { int link_count; /* number of links */ SD_link_t *link_list; /* array of links, created only if necessary in SD_link_get_list */ + SD_link_t *recyclable_route; /* array returned by SD_route_get_list + and mallocated only once */ int watch_point_reached; /* has a task just reached a watch point? */ diff --git a/src/simdag/sd_global.c b/src/simdag/sd_global.c index c6087f498d..aea07a142d 100644 --- a/src/simdag/sd_global.c +++ b/src/simdag/sd_global.c @@ -31,6 +31,7 @@ void SD_init(int *argc, char **argv) { sd_global->links = xbt_dict_new(); sd_global->link_count = 0; sd_global->link_list = NULL; + sd_global->recyclable_route = NULL; sd_global->watch_point_reached = 0; s_SD_task_t task; @@ -162,7 +163,7 @@ SD_task_t* SD_simulate(double how_long) INFO1("Task '%s' done", SD_task_get_name(task)); DEBUG0("Calling __SD_task_just_done"); __SD_task_just_done(task); - INFO1("__SD_task_just_done called on task '%s'", SD_task_get_name(task)); + DEBUG1("__SD_task_just_done called on task '%s'", SD_task_get_name(task)); /* the state has changed */ if (!task->state_changed) { @@ -262,6 +263,9 @@ void SD_exit(void) { if (sd_global->link_list != NULL) xbt_free(sd_global->link_list); + if (sd_global->recyclable_route != NULL) + xbt_free(sd_global->recyclable_route); + DEBUG0("Destroying the swags..."); xbt_swag_free(sd_global->not_scheduled_task_set); xbt_swag_free(sd_global->scheduled_task_set); diff --git a/src/simdag/sd_workstation.c b/src/simdag/sd_workstation.c index a66d29fc5f..2a7ac435c7 100644 --- a/src/simdag/sd_workstation.c +++ b/src/simdag/sd_workstation.c @@ -122,31 +122,37 @@ const char* SD_workstation_get_name(SD_workstation_t workstation) { /** * \brief Returns the route between two workstations * - * Use SD_route_get_size() to know the array size. Don't forget to free the array after use. + * Use SD_route_get_size() to know the array size. * * \param src a workstation * \param dst another workstation * \return a new array of \ref SD_link_t representating the route between these two workstations * \see SD_route_get_size(), SD_link_t */ -SD_link_t* SD_route_get_list(SD_workstation_t src, SD_workstation_t dst) { +const SD_link_t* SD_route_get_list(SD_workstation_t src, SD_workstation_t dst) { SD_CHECK_INIT_DONE(); + static int first_run = 1; + + if (first_run) { + sd_global->recyclable_route = xbt_new0(SD_link_t, SD_link_get_number()); + first_run = 0; + } + void *surf_src = src->surf_workstation; void *surf_dst = dst->surf_workstation; const void **surf_route = surf_workstation_resource->extension_public->get_route(surf_src, surf_dst); int route_size = surf_workstation_resource->extension_public->get_route_size(surf_src, surf_dst); - SD_link_t* route = xbt_new0(SD_link_t, route_size); const char *link_name; int i; for (i = 0; i < route_size; i++) { link_name = surf_workstation_resource->extension_public->get_link_name(surf_route[i]); - route[i] = xbt_dict_get(sd_global->links, link_name); + sd_global->recyclable_route[i] = xbt_dict_get(sd_global->links, link_name); } - return route; + return sd_global->recyclable_route; } /** @@ -215,7 +221,7 @@ double SD_workstation_get_computation_time(SD_workstation_t workstation, double double SD_route_get_current_latency(SD_workstation_t src, SD_workstation_t dst) { SD_CHECK_INIT_DONE(); xbt_assert0(src != NULL && dst != NULL, "Invalid parameter"); - SD_link_t *links = SD_route_get_list(src, dst); + const SD_link_t *links = SD_route_get_list(src, dst); int nb_links = SD_route_get_size(src, dst); double latency = 0.0; int i; @@ -224,7 +230,6 @@ double SD_route_get_current_latency(SD_workstation_t src, SD_workstation_t dst) latency += SD_link_get_current_latency(links[i]); } - free(links); return latency; } @@ -240,7 +245,7 @@ double SD_route_get_current_latency(SD_workstation_t src, SD_workstation_t dst) double SD_route_get_current_bandwidth(SD_workstation_t src, SD_workstation_t dst) { SD_CHECK_INIT_DONE(); xbt_assert0(src != NULL && dst != NULL, "Invalid parameter"); - SD_link_t *links = SD_route_get_list(src, dst); + const SD_link_t *links = SD_route_get_list(src, dst); int nb_links = SD_route_get_size(src, dst); double bandwidth, min_bandwidth = -1.0; int i; @@ -251,7 +256,6 @@ double SD_route_get_current_bandwidth(SD_workstation_t src, SD_workstation_t dst min_bandwidth = bandwidth; } - free(links); return min_bandwidth; } @@ -273,7 +277,7 @@ double SD_route_get_communication_time(SD_workstation_t src, SD_workstation_t ds xbt_assert0(src != NULL && dst != NULL, "Invalid parameter"); xbt_assert0(communication_amount >= 0, "communication_amount must be greater than or equal to zero"); - SD_link_t *links; + const SD_link_t *links; int nb_links; double bandwidth, min_bandwidth; double latency; @@ -293,7 +297,6 @@ double SD_route_get_communication_time(SD_workstation_t src, SD_workstation_t ds if (bandwidth < min_bandwidth || min_bandwidth == -1.0) min_bandwidth = bandwidth; } - xbt_free(links); return latency + (communication_amount / min_bandwidth); }