4 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 commit: e2d6799c4182f00443b3013aadb1c2412372460f
11 This commit retrieves the old implementation of CPU_TI with multi-levels.
14 #include "surf_private.h"
15 #include "trace_mgr_private.h"
16 #include "cpu_ti_private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_ti, surf,
21 "Logging specific to the SURF CPU TRACE INTEGRATION module");
24 static xbt_swag_t running_action_set_that_does_not_need_being_checked = NULL;
25 static xbt_swag_t modified_cpu = NULL;
26 static xbt_heap_t action_heap;
28 /* prototypes of new trace functions */
29 static double surf_cpu_integrate_trace(surf_cpu_ti_tgmr_t trace, double a,
33 static double surf_cpu_solve_trace(surf_cpu_ti_tgmr_t trace, double a,
35 static double surf_cpu_solve_trace_somewhat_simple(surf_cpu_ti_tgmr_t trace,
36 double a, double amount);
38 static void surf_cpu_free_tmgr(surf_cpu_ti_tgmr_t trace);
40 static double surf_cpu_integrate_trace_simple(surf_cpu_ti_trace_t trace,
42 static double surf_cpu_integrate_trace_simple_point(surf_cpu_ti_trace_t trace,
44 static double surf_cpu_solve_trace_simple(surf_cpu_ti_trace_t trace, double a,
46 static int surf_cpu_binary_search(double *array, double a, int low, int high);
49 static void surf_cpu_free_trace(surf_cpu_ti_trace_t trace)
51 if (trace->time_points)
52 xbt_free(trace->time_points);
54 xbt_free(trace->integral);
58 static void surf_cpu_free_tmgr(surf_cpu_ti_tgmr_t trace)
61 surf_cpu_free_trace(trace->trace);
65 static surf_cpu_ti_trace_t surf_cpu_trace_new(tmgr_trace_t power_trace)
67 surf_cpu_ti_trace_t trace;
73 trace = xbt_new0(s_surf_cpu_ti_trace_t, 1);
75 xbt_malloc0(sizeof(double) *
76 (xbt_dynar_length(power_trace->event_list) + 1));
78 xbt_malloc0(sizeof(double) *
79 (xbt_dynar_length(power_trace->event_list) + 1));
80 trace->nb_points = xbt_dynar_length(power_trace->event_list);
81 xbt_dynar_foreach(power_trace->event_list, cpt, val) {
82 trace->time_points[i] = time;
83 trace->integral[i] = integral;
84 integral += val.delta * val.value;
88 trace->time_points[i] = time;
89 trace->integral[i] = integral;
94 * \brief Creates a new integration trace from a tmgr_trace_t
96 * \param power_trace CPU availability trace
97 * \param value Percentage of CPU power available (useful to fixed tracing)
98 * \param spacing Initial spacing
99 * \return Integration trace structure
101 static surf_cpu_ti_tgmr_t cpu_ti_parse_trace(tmgr_trace_t power_trace,
104 surf_cpu_ti_tgmr_t trace;
105 double total_time = 0.0;
108 trace = xbt_new0(s_surf_cpu_ti_tgmr_t, 1);
110 /* no availability file, fixed trace */
112 trace->type = TRACE_FIXED;
113 trace->value = value;
114 DEBUG1("No availabily trace. Constant value = %lf", value);
118 /* only one point available, fixed trace */
119 if (xbt_dynar_length(power_trace->event_list) == 1) {
120 xbt_dynar_get_cpy(power_trace->event_list, 0, &val);
121 trace->type = TRACE_FIXED;
122 trace->value = val.value;
126 trace->type = TRACE_DYNAMIC;
127 trace->power_trace = power_trace;
129 /* count the total time of trace file */
130 xbt_dynar_foreach(power_trace->event_list, cpt, val) {
131 total_time += val.delta;
133 trace->trace = surf_cpu_trace_new(power_trace);
134 trace->last_time = total_time;
135 trace->total = surf_cpu_integrate_trace_simple(trace->trace, 0, total_time);
137 DEBUG2("Total integral %lf, last_time %lf ",
138 trace->total, trace->last_time);
144 static cpu_ti_t cpu_new(char *name, double power_peak,
146 tmgr_trace_t power_trace,
147 e_surf_resource_state_t state_initial,
148 tmgr_trace_t state_trace, xbt_dict_t cpu_properties)
150 tmgr_trace_t empty_trace;
152 cpu_ti_t cpu = xbt_new0(s_cpu_ti_t, 1);
153 s_surf_action_cpu_ti_t ti_action;
154 xbt_assert1(!surf_model_resource_by_name(surf_cpu_model, name),
155 "Host '%s' declared several times in the platform file", name);
156 cpu->action_set = xbt_swag_new(xbt_swag_offset(ti_action, cpu_list_hookup));
157 cpu->generic_resource.model = surf_cpu_model;
158 cpu->generic_resource.name = name;
159 cpu->generic_resource.properties = cpu_properties;
160 cpu->power_peak = power_peak;
161 xbt_assert0(cpu->power_peak > 0, "Power has to be >0");
162 DEBUG1("power scale %lf", power_scale);
163 cpu->power_scale = power_scale;
164 cpu->avail_trace = cpu_ti_parse_trace(power_trace, power_scale);
165 cpu->state_current = state_initial;
168 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
169 if (power_trace && xbt_dynar_length(power_trace->event_list) > 1) {
170 /* add a fake trace event if periodicity == 0 */
171 xbt_dynar_get_cpy(power_trace->event_list,
172 xbt_dynar_length(power_trace->event_list) - 1, &val);
173 if (val.delta == 0) {
174 empty_trace = tmgr_empty_trace_new();
176 tmgr_history_add_trace(history, empty_trace,
177 cpu->avail_trace->last_time, 0, cpu);
180 xbt_dict_set(surf_model_resource_set(surf_cpu_model), name, cpu,
187 static void parse_cpu_init(void)
189 double power_peak = 0.0;
190 double power_scale = 0.0;
191 tmgr_trace_t power_trace = NULL;
192 e_surf_resource_state_t state_initial = SURF_RESOURCE_OFF;
193 tmgr_trace_t state_trace = NULL;
195 power_peak = get_cpu_power(A_surfxml_host_power);
196 surf_parse_get_double(&power_scale, A_surfxml_host_availability);
197 power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
199 xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
200 (A_surfxml_host_state == A_surfxml_host_state_OFF),
202 if (A_surfxml_host_state == A_surfxml_host_state_ON)
203 state_initial = SURF_RESOURCE_ON;
204 if (A_surfxml_host_state == A_surfxml_host_state_OFF)
205 state_initial = SURF_RESOURCE_OFF;
206 state_trace = tmgr_trace_new(A_surfxml_host_state_file);
208 current_property_set = xbt_dict_new();
209 cpu_new(xbt_strdup(A_surfxml_host_id), power_peak, power_scale,
210 power_trace, state_initial, state_trace, current_property_set);
214 static void add_traces_cpu(void)
216 xbt_dict_cursor_t cursor = NULL;
217 char *trace_name, *elm;
219 static int called = 0;
225 /* connect all traces relative to hosts */
226 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
227 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
228 cpu_ti_t cpu = surf_model_resource_by_name(surf_cpu_model, elm);
230 xbt_assert1(cpu, "Host %s undefined", elm);
231 xbt_assert1(trace, "Trace %s undefined", trace_name);
233 if (cpu->state_event) {
234 DEBUG1("Trace already configured for this CPU(%s), ignoring it", elm);
237 DEBUG2("Add state trace: %s to CPU(%s)", trace_name, elm);
238 cpu->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, cpu);
241 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
242 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
243 cpu_ti_t cpu = surf_model_resource_by_name(surf_cpu_model, elm);
245 xbt_assert1(cpu, "Host %s undefined", elm);
246 xbt_assert1(trace, "Trace %s undefined", trace_name);
248 DEBUG2("Add power trace: %s to CPU(%s)", trace_name, elm);
249 if (cpu->avail_trace)
250 surf_cpu_free_tmgr(cpu->avail_trace);
252 cpu->avail_trace = cpu_ti_parse_trace(trace, cpu->power_scale);
254 /* add a fake trace event if periodicity == 0 */
255 if (trace && xbt_dynar_length(trace->event_list) > 1) {
257 xbt_dynar_get_cpy(trace->event_list,
258 xbt_dynar_length(trace->event_list) - 1, &val);
259 if (val.delta == 0) {
260 tmgr_trace_t empty_trace;
261 empty_trace = tmgr_empty_trace_new();
263 tmgr_history_add_trace(history, empty_trace,
264 cpu->avail_trace->last_time, 0, cpu);
270 static void define_callbacks(const char *file)
272 surf_parse_reset_parser();
273 surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_init);
274 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu);
277 static int resource_used(void *resource_id)
279 cpu_ti_t cpu = resource_id;
280 return xbt_swag_size(cpu->action_set);
283 static int action_unref(surf_action_t action)
286 if (!action->refcount) {
287 xbt_swag_remove(action, action->state_set);
288 /* remove from action_set */
289 xbt_swag_remove(action, ACTION_GET_CPU(action)->action_set);
290 /* remove from heap */
291 xbt_heap_remove(action_heap, ((surf_action_cpu_ti_t) action)->index_heap);
292 xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
299 static void action_cancel(surf_action_t action)
301 surf_action_state_set(action, SURF_ACTION_FAILED);
302 xbt_heap_remove(action_heap, ((surf_action_cpu_ti_t) action)->index_heap);
303 xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
307 static void cpu_action_state_set(surf_action_t action,
308 e_surf_action_state_t state)
310 surf_action_state_set(action, state);
311 xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
316 * \brief Update the remaining amount of actions
318 * \param cpu Cpu on which the actions are running
319 * \param now Current time
321 static void cpu_update_remaining_amount(cpu_ti_t cpu, double now)
323 #define GENERIC_ACTION(action) action->generic_action
325 surf_action_cpu_ti_t action;
327 /* already updated */
328 if (cpu->last_update >= now)
331 /* calcule the surface */
333 surf_cpu_integrate_trace(cpu->avail_trace, cpu->last_update,
334 now) * cpu->power_peak;
335 DEBUG2("Flops total: %lf, Last update %lf", area_total, cpu->last_update);
337 xbt_swag_foreach(action, cpu->action_set) {
338 /* action not running, skip it */
339 if (GENERIC_ACTION(action).state_set !=
340 surf_cpu_model->states.running_action_set)
343 /* bogus priority, skip it */
344 if (GENERIC_ACTION(action).priority <= 0)
347 /* action suspended, skip it */
348 if (action->suspended != 0)
351 /* action don't need update */
352 if (GENERIC_ACTION(action).start >= now)
355 /* skip action that are finishing now */
356 if (GENERIC_ACTION(action).finish >= 0
357 && GENERIC_ACTION(action).finish <= now)
360 /* update remaining */
361 double_update(&(GENERIC_ACTION(action).remains),
362 area_total / (cpu->sum_priority *
363 GENERIC_ACTION(action).priority));
364 DEBUG2("Update remaining action(%p) remaining %lf", action,
365 GENERIC_ACTION(action).remains);
367 cpu->last_update = now;
368 #undef GENERIC_ACTION
372 * \brief Update the finish date of action if necessary
374 * \param cpu Cpu on which the actions are running
375 * \param now Current time
377 static void cpu_update_action_finish_date(cpu_ti_t cpu, double now)
379 #define GENERIC_ACTION(action) action->generic_action
380 surf_action_cpu_ti_t action;
381 double sum_priority = 0.0, total_area, min_finish = -1;
383 /* update remaning amount of actions */
384 cpu_update_remaining_amount(cpu, now);
386 xbt_swag_foreach(action, cpu->action_set) {
387 /* action not running, skip it */
388 if (GENERIC_ACTION(action).state_set !=
389 surf_cpu_model->states.running_action_set)
392 /* bogus priority, skip it */
393 if (GENERIC_ACTION(action).priority <= 0)
396 /* action suspended, skip it */
397 if (action->suspended != 0)
400 sum_priority += 1.0 / GENERIC_ACTION(action).priority;
402 cpu->sum_priority = sum_priority;
404 xbt_swag_foreach(action, cpu->action_set) {
405 /* action not running, skip it */
406 if (GENERIC_ACTION(action).state_set !=
407 surf_cpu_model->states.running_action_set)
410 /* verify if the action is really running on cpu */
411 if (action->suspended == 0 && GENERIC_ACTION(action).priority > 0) {
412 /* total area needed to finish the action. Used in trace integration */
414 (GENERIC_ACTION(action).remains) * sum_priority *
415 GENERIC_ACTION(action).priority;
417 total_area /= cpu->power_peak;
419 GENERIC_ACTION(action).finish =
420 surf_cpu_solve_trace(cpu->avail_trace, now, total_area);
421 /* verify which event will happen before (max_duration or finish time) */
422 if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
423 (GENERIC_ACTION(action).start +
424 GENERIC_ACTION(action).max_duration <
425 GENERIC_ACTION(action).finish))
426 min_finish = GENERIC_ACTION(action).start +
427 GENERIC_ACTION(action).max_duration;
429 min_finish = GENERIC_ACTION(action).finish;
431 /* put the max duration time on heap */
432 if (GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
434 (GENERIC_ACTION(action).start +
435 GENERIC_ACTION(action).max_duration);
437 /* add in action heap */
438 DEBUG2("action(%p) index %d", action, action->index_heap);
439 if (action->index_heap >= 0) {
440 surf_action_cpu_ti_t heap_act =
441 xbt_heap_remove(action_heap, action->index_heap);
442 if (heap_act != action)
445 if (min_finish != NO_MAX_DURATION)
446 xbt_heap_push(action_heap, action, min_finish);
449 ("Update finish time: Cpu(%s) Action: %p, Start Time: %lf Finish Time: %lf Max duration %lf",
450 cpu->generic_resource.name, action, GENERIC_ACTION(action).start,
451 GENERIC_ACTION(action).finish, GENERIC_ACTION(action).max_duration);
453 /* remove from modified cpu */
454 xbt_swag_remove(cpu, modified_cpu);
455 #undef GENERIC_ACTION
458 static double share_resources(double now)
460 cpu_ti_t cpu, cpu_next;
461 double min_action_duration = -1;
463 /* iterates over modified cpus to update share resources */
464 xbt_swag_foreach_safe(cpu, cpu_next, modified_cpu) {
465 cpu_update_action_finish_date(cpu, now);
467 /* get the min next event if heap not empty */
468 if (xbt_heap_size(action_heap) > 0)
469 min_action_duration = xbt_heap_maxkey(action_heap) - now;
471 DEBUG1("Share resources, min next event date: %lf", min_action_duration);
473 return min_action_duration;
476 static void update_actions_state(double now, double delta)
478 #define GENERIC_ACTION(action) action->generic_action
479 surf_action_cpu_ti_t action;
480 while ((xbt_heap_size(action_heap) > 0)
481 && (xbt_heap_maxkey(action_heap) <= now)) {
482 action = xbt_heap_pop(action_heap);
483 DEBUG1("Action %p: finish", action);
484 GENERIC_ACTION(action).finish = surf_get_clock();
485 /* set the remains to 0 due to precision problems when updating the remaining amount */
486 GENERIC_ACTION(action).remains = 0;
487 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
488 /* update remaining amout of all actions */
489 cpu_update_remaining_amount(action->cpu, surf_get_clock());
491 #undef GENERIC_ACTION
494 static void update_resource_state(void *id,
495 tmgr_trace_event_t event_type,
496 double value, double date)
499 surf_action_cpu_ti_t action;
501 if (event_type == cpu->power_event) {
502 tmgr_trace_t power_trace;
503 surf_cpu_ti_tgmr_t trace;
506 DEBUG3("Finish trace date: %lf value %lf date %lf", surf_get_clock(),
508 /* update remaining of actions and put in modified cpu swag */
509 cpu_update_remaining_amount(cpu, date);
510 xbt_swag_insert(cpu, modified_cpu);
512 power_trace = cpu->avail_trace->power_trace;
513 xbt_dynar_get_cpy(power_trace->event_list,
514 xbt_dynar_length(power_trace->event_list) - 1, &val);
516 surf_cpu_free_tmgr(cpu->avail_trace);
517 cpu->power_scale = val.value;
519 trace = xbt_new0(s_surf_cpu_ti_tgmr_t, 1);
520 trace->type = TRACE_FIXED;
521 trace->value = val.value;
522 DEBUG1("value %lf", val.value);
524 cpu->avail_trace = trace;
526 if (tmgr_trace_event_free(event_type))
527 cpu->power_event = NULL;
529 } else if (event_type == cpu->state_event) {
531 cpu->state_current = SURF_RESOURCE_ON;
533 cpu->state_current = SURF_RESOURCE_OFF;
535 /* put all action running on cpu to failed */
536 xbt_swag_foreach(action, cpu->action_set) {
537 if (surf_action_state_get((surf_action_t) action) ==
539 || surf_action_state_get((surf_action_t) action) ==
541 || surf_action_state_get((surf_action_t) action) ==
542 SURF_ACTION_NOT_IN_THE_SYSTEM) {
543 action->generic_action.finish = date;
544 cpu_action_state_set((surf_action_t) action, SURF_ACTION_FAILED);
545 if (action->index_heap >= 0) {
546 surf_action_cpu_ti_t heap_act =
547 xbt_heap_remove(action_heap, action->index_heap);
548 if (heap_act != action)
554 if (tmgr_trace_event_free(event_type))
555 cpu->state_event = NULL;
557 CRITICAL0("Unknown event ! \n");
564 static surf_action_t execute(void *cpu, double size)
566 surf_action_cpu_ti_t action = NULL;
569 XBT_IN2("(%s,%g)", surf_resource_name(CPU), size);
571 surf_action_new(sizeof(s_surf_action_cpu_ti_t), size, surf_cpu_model,
572 CPU->state_current != SURF_RESOURCE_ON);
574 action->index_heap = -1;
576 xbt_swag_insert(CPU, modified_cpu);
578 xbt_swag_insert(action, CPU->action_set);
580 action->suspended = 0; /* Should be useless because of the
581 calloc but it seems to help valgrind... */
584 return (surf_action_t) action;
587 static void action_update_index_heap(void *action, int i)
589 ((surf_action_cpu_ti_t) action)->index_heap = i;
592 static surf_action_t action_sleep(void *cpu, double duration)
594 surf_action_cpu_ti_t action = NULL;
597 duration = MAX(duration, MAXMIN_PRECISION);
599 XBT_IN2("(%s,%g)", surf_resource_name(cpu), duration);
600 action = (surf_action_cpu_ti_t) execute(cpu, 1.0);
601 action->generic_action.max_duration = duration;
602 action->suspended = 2;
603 if (duration == NO_MAX_DURATION) {
604 /* Move to the *end* of the corresponding action set. This convention
605 is used to speed up update_resource_state */
606 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
607 ((surf_action_t) action)->state_set =
608 running_action_set_that_does_not_need_being_checked;
609 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
612 return (surf_action_t) action;
615 static void action_suspend(surf_action_t action)
617 XBT_IN1("(%p)", action);
618 if (((surf_action_cpu_ti_t) action)->suspended != 2) {
619 ((surf_action_cpu_ti_t) action)->suspended = 1;
620 xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
625 static void action_resume(surf_action_t action)
627 XBT_IN1("(%p)", action);
628 if (((surf_action_cpu_ti_t) action)->suspended != 2) {
629 ((surf_action_cpu_ti_t) action)->suspended = 0;
630 xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
635 static int action_is_suspended(surf_action_t action)
637 return (((surf_action_cpu_ti_t) action)->suspended == 1);
640 static void action_set_max_duration(surf_action_t action, double duration)
642 surf_action_cpu_ti_t ACT = (surf_action_cpu_ti_t) action;
645 XBT_IN2("(%p,%g)", action, duration);
647 action->max_duration = duration;
651 (action->start + action->max_duration) <
652 action->finish ? (action->start +
653 action->max_duration) : action->finish;
655 min_finish = action->finish;
657 /* add in action heap */
658 if (ACT->index_heap >= 0) {
659 surf_action_cpu_ti_t heap_act =
660 xbt_heap_remove(action_heap, ACT->index_heap);
664 xbt_heap_push(action_heap, ACT, min_finish);
669 static void action_set_priority(surf_action_t action, double priority)
671 XBT_IN2("(%p,%g)", action, priority);
672 action->priority = priority;
673 xbt_swag_insert(ACTION_GET_CPU(action), modified_cpu);
677 static double action_get_remains(surf_action_t action)
679 XBT_IN1("(%p)", action);
680 cpu_update_remaining_amount((cpu_ti_t) ((surf_action_cpu_ti_t) action)->cpu,
682 return action->remains;
686 static e_surf_resource_state_t get_state(void *cpu)
688 return ((cpu_ti_t) cpu)->state_current;
691 static double get_speed(void *cpu, double load)
693 return load * (((cpu_ti_t) cpu)->power_peak);
697 * \brief Auxiliary function to update the CPU power scale.
699 * This function uses the trace structure to return the power scale at the determined time a.
700 * \param trace Trace structure to search the updated power scale
702 * \return CPU power scale
704 static double surf_cpu_get_power_scale(surf_cpu_ti_tgmr_t trace, double a)
710 reduced_a = a - floor(a / trace->last_time) * trace->last_time;
712 surf_cpu_binary_search(trace->trace->time_points, reduced_a, 0,
713 trace->trace->nb_points - 1);
714 xbt_dynar_get_cpy(trace->power_trace->event_list, 0, &val);
718 static double get_available_speed(void *cpu)
722 surf_cpu_get_power_scale(CPU->avail_trace, surf_get_clock());
723 /* number between 0 and 1 */
724 return CPU->power_scale;
727 static void finalize(void)
730 xbt_dict_cursor_t cursor;
732 xbt_dict_foreach(surf_model_resource_set(surf_cpu_model), cursor, key, cpu) {
734 xbt_swag_free(CPU->action_set);
735 surf_cpu_free_tmgr(CPU->avail_trace);
738 surf_model_exit(surf_cpu_model);
739 surf_cpu_model = NULL;
741 xbt_swag_free(running_action_set_that_does_not_need_being_checked);
742 xbt_swag_free(modified_cpu);
743 running_action_set_that_does_not_need_being_checked = NULL;
744 xbt_heap_free(action_heap);
747 static void surf_cpu_model_init_internal(void)
749 s_surf_action_t action;
752 surf_cpu_model = surf_model_init();
754 running_action_set_that_does_not_need_being_checked =
755 xbt_swag_new(xbt_swag_offset(action, state_hookup));
757 modified_cpu = xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
759 surf_cpu_model->name = "CPU_TI";
761 surf_cpu_model->action_unref = action_unref;
762 surf_cpu_model->action_cancel = action_cancel;
763 surf_cpu_model->action_state_set = cpu_action_state_set;
765 surf_cpu_model->model_private->resource_used = resource_used;
766 surf_cpu_model->model_private->share_resources = share_resources;
767 surf_cpu_model->model_private->update_actions_state = update_actions_state;
768 surf_cpu_model->model_private->update_resource_state =
769 update_resource_state;
770 surf_cpu_model->model_private->finalize = finalize;
772 surf_cpu_model->suspend = action_suspend;
773 surf_cpu_model->resume = action_resume;
774 surf_cpu_model->is_suspended = action_is_suspended;
775 surf_cpu_model->set_max_duration = action_set_max_duration;
776 surf_cpu_model->set_priority = action_set_priority;
777 surf_cpu_model->get_remains = action_get_remains;
779 surf_cpu_model->extension.cpu.execute = execute;
780 surf_cpu_model->extension.cpu.sleep = action_sleep;
782 surf_cpu_model->extension.cpu.get_state = get_state;
783 surf_cpu_model->extension.cpu.get_speed = get_speed;
784 surf_cpu_model->extension.cpu.get_available_speed = get_available_speed;
786 action_heap = xbt_heap_new(8, NULL);
787 xbt_heap_set_update_callback(action_heap, action_update_index_heap);
791 void surf_cpu_model_init_ti(const char *filename)
795 surf_cpu_model_init_internal();
796 define_callbacks(filename);
797 xbt_dynar_push(model_list, &surf_cpu_model);
802 * \brief Integrate trace
804 * Wrapper around surf_cpu_integrate_trace_simple() to get
807 * \param trace Trace structure.
808 * \param a Begin of interval
809 * \param b End of interval
810 * \return the integrate value. -1 if an error occurs.
812 static double surf_cpu_integrate_trace(surf_cpu_ti_tgmr_t trace, double a,
818 int a_index, b_index;
820 if ((a < 0.0) || (a > b)) {
822 ("Error, invalid integration interval [%.2f,%.2f]. You probably have a task executing with negative computation amount. Check your code.",
829 if (trace->type == TRACE_FIXED) {
830 return ((b - a) * trace->value);
833 if (ceil(a / trace->last_time) == a / trace->last_time)
834 a_index = 1 + (int) (ceil(a / trace->last_time));
836 a_index = (int) (ceil(a / trace->last_time));
838 b_index = (int) (floor(b / trace->last_time));
840 if (a_index > b_index) { /* Same chunk */
841 return surf_cpu_integrate_trace_simple(trace->trace,
843 1) * trace->last_time,
844 b - (b_index) * trace->last_time);
847 first_chunk = surf_cpu_integrate_trace_simple(trace->trace,
849 1) * trace->last_time,
851 middle_chunk = (b_index - a_index) * trace->total;
852 last_chunk = surf_cpu_integrate_trace_simple(trace->trace,
855 (b_index) * trace->last_time);
857 DEBUG3("first_chunk=%.2f middle_chunk=%.2f last_chunk=%.2f\n",
858 first_chunk, middle_chunk, last_chunk);
860 return (first_chunk + middle_chunk + last_chunk);
864 * \brief Auxiliary function to calculate the integral between a and b.
865 * It simply calculates the integral at point a and b and returns the difference
867 * \param trace Trace structure
868 * \param a Initial point
869 * \param b Final point
872 static double surf_cpu_integrate_trace_simple(surf_cpu_ti_trace_t trace,
875 return surf_cpu_integrate_trace_simple_point(trace,
877 surf_cpu_integrate_trace_simple_point(trace, a);
881 * \brief Auxiliary function to calculate the integral at point a.
882 * \param trace Trace structure
886 static double surf_cpu_integrate_trace_simple_point(surf_cpu_ti_trace_t trace,
893 surf_cpu_binary_search(trace->time_points, a, 0, trace->nb_points - 1);
894 integral += trace->integral[ind];
895 DEBUG7("a %lf ind %d integral %lf ind + 1 %lf ind %lf time +1 %lf time %lf",
896 a, ind, integral, trace->integral[ind + 1], trace->integral[ind],
897 trace->time_points[ind + 1], trace->time_points[ind]);
898 double_update(&a_aux, trace->time_points[ind]);
901 ((trace->integral[ind + 1] -
902 trace->integral[ind]) / (trace->time_points[ind + 1] -
903 trace->time_points[ind])) * (a -
907 DEBUG2("Integral a %lf = %lf", a, integral);
913 * \brief Calculate the time needed to execute "amount" on cpu.
915 * Here, amount can span multiple trace periods
917 * \param trace CPU trace structure
918 * \param a Initial time
919 * \param amount Amount to be executed
922 static double surf_cpu_solve_trace(surf_cpu_ti_tgmr_t trace, double a,
927 double reduced_amount;
931 /* Fix very small negative numbers */
932 if ((a < 0.0) && (a > -EPSILON)) {
935 if ((amount < 0.0) && (amount > -EPSILON)) {
940 if ((a < 0.0) || (amount < 0.0)) {
942 ("Error, invalid parameters [a = %.2f, amount = %.2f]. You probably have a task executing with negative computation amount. Check your code.",
947 /* At this point, a and amount are positive */
949 if (amount < EPSILON)
952 /* Is the trace fixed ? */
953 if (trace->type == TRACE_FIXED) {
954 return (a + (amount / trace->value));
957 DEBUG2("amount %lf total %lf", amount, trace->total);
958 /* Reduce the problem to one where amount <= trace_total */
959 quotient = (int) (floor(amount / trace->total));
960 reduced_amount = (trace->total) * ((amount / trace->total) -
961 floor(amount / trace->total));
962 reduced_a = a - (trace->last_time) * (int) (floor(a / trace->last_time));
964 DEBUG3("Quotient: %d reduced_amount: %lf reduced_a: %lf", quotient,
965 reduced_amount, reduced_a);
967 /* Now solve for new_amount which is <= trace_total */
969 fprintf(stderr,"reduced_a = %.2f\n",reduced_a);
970 fprintf(stderr,"reduced_amount = %.2f\n",reduced_amount);
973 surf_cpu_solve_trace_somewhat_simple(trace, reduced_a, reduced_amount);
975 /* Re-map to the original b and amount */
976 b = (trace->last_time) * (int) (floor(a / trace->last_time)) +
977 (quotient * trace->last_time) + reduced_b;
982 * \brief Auxiliary function to solve integral
984 * Here, amount is <= trace->total
985 * and a <=trace->last_time
988 static double surf_cpu_solve_trace_somewhat_simple(surf_cpu_ti_tgmr_t trace,
989 double a, double amount)
991 double amount_till_end;
994 DEBUG2("Solve integral: [%.2f, amount=%.2f]", a, amount);
995 amount_till_end = surf_cpu_integrate_trace(trace, a, trace->last_time);
997 fprintf(stderr,"amount_till_end=%.2f\n",amount_till_end);
1000 if (amount_till_end > amount) {
1001 b = surf_cpu_solve_trace_simple(trace->trace, a, amount);
1003 b = trace->last_time +
1004 surf_cpu_solve_trace_simple(trace->trace, 0.0,
1005 amount - amount_till_end);
1011 * \brief Auxiliary function to solve integral.
1012 * It returns the date when the requested amount of flops is available
1013 * \param trace Trace structure
1014 * \param a Initial point
1015 * \param amount Amount of flops
1016 * \return The date when amount is available.
1018 static double surf_cpu_solve_trace_simple(surf_cpu_ti_trace_t trace, double a,
1024 integral_a = surf_cpu_integrate_trace_simple_point(trace, a);
1026 surf_cpu_binary_search(trace->integral, integral_a + amount, 0,
1027 trace->nb_points - 1);
1028 time = trace->time_points[ind];
1030 (integral_a + amount -
1031 trace->integral[ind]) / ((trace->integral[ind + 1] -
1032 trace->integral[ind]) /
1033 (trace->time_points[ind + 1] -
1034 trace->time_points[ind]));
1040 * \brief Binary search in array.
1041 * It returns the first point of the interval in which "a" is.
1042 * \param array Array
1043 * \param a Value to search
1044 * \param low Low bound to search in array
1045 * \param high Upper bound to search in array
1046 * \return Index of point
1048 static int surf_cpu_binary_search(double *array, double a, int low, int high)
1050 int mid = low + (high - low) / 2;
1051 DEBUG5("a %lf low %d high %d mid %d value %lf", a, low, high, mid,
1053 /* a == array[mid] */
1054 if (array[mid] == a)
1056 /* a is between mid and mid+1 */
1057 if (array[mid] < a && array[mid + 1] > a)
1061 return surf_cpu_binary_search(array, a, mid + 1, high);
1063 return surf_cpu_binary_search(array, a, low, mid - 1);