Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
HUSH !
[simgrid.git] / src / surf / surf.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "surf_private.h"
9 #include "xbt/module.h"
10
11 static xbt_heap_float_t NOW=0;
12
13 xbt_dynar_t resource_list = NULL;
14 tmgr_history_t history = NULL;
15 lmm_system_t maxmin_system = NULL;
16
17 e_surf_action_state_t surf_action_get_state(surf_action_t action)
18 {
19   surf_action_state_t action_state = &(action->resource_type->common_public->states); 
20   
21   if(action->state_set == action_state->ready_action_set)
22     return SURF_ACTION_READY;
23   if(action->state_set == action_state->running_action_set)
24     return SURF_ACTION_RUNNING;
25   if(action->state_set == action_state->failed_action_set)
26     return SURF_ACTION_FAILED;
27   if(action->state_set == action_state->done_action_set)
28     return SURF_ACTION_DONE;
29   return SURF_ACTION_NOT_IN_THE_SYSTEM;
30 }
31
32 void surf_action_free(surf_action_t * action)
33 {
34   (*action)->resource_type->common_public->action_cancel(*action);
35   xbt_free(*action);
36   *action=NULL;
37 }
38
39 void surf_action_change_state(surf_action_t action, e_surf_action_state_t state)
40 {
41   surf_action_state_t action_state = &(action->resource_type->common_public->states); 
42
43   xbt_swag_remove(action, action->state_set);
44
45   if(state == SURF_ACTION_READY) 
46     action->state_set = action_state->ready_action_set;
47   else if(state == SURF_ACTION_RUNNING)
48     action->state_set = action_state->running_action_set;
49   else if(state == SURF_ACTION_FAILED)
50     action->state_set = action_state->failed_action_set;
51   else if(state == SURF_ACTION_DONE)
52     action->state_set = action_state->done_action_set;
53   else action->state_set = NULL;
54
55   if(action->state_set) xbt_swag_insert(action, action->state_set);
56 }
57
58 void surf_init(int *argc, char **argv)
59 {
60   xbt_init(argc, argv);
61   if(!resource_list) resource_list = xbt_dynar_new(sizeof(surf_resource_private_t), NULL);
62   if(!history) history = tmgr_history_new();
63   if(!maxmin_system) maxmin_system = lmm_system_new();
64 }
65
66 void surf_finalize(void)
67
68   int i;
69   surf_resource_t resource = NULL;
70
71   xbt_dynar_foreach (resource_list,i,resource) {
72     resource->common_private->finalize();
73   }
74
75   if(maxmin_system) {
76     lmm_system_free(maxmin_system);
77     maxmin_system = NULL;
78   }
79   if(history) {
80     tmgr_history_free(history);
81     history = NULL;
82   }
83   if(resource_list) xbt_dynar_free(&resource_list);
84
85   tmgr_finalize();
86 }
87
88 xbt_heap_float_t surf_solve(void)
89 {
90   static int first_run = 1;
91
92   xbt_heap_float_t min = -1.0;
93   xbt_heap_float_t next_event_date = -1.0;
94   xbt_heap_float_t resource_next_action_end = -1.0;
95   xbt_maxmin_float_t value = -1.0;
96   surf_resource_object_t resource_obj = NULL;
97   surf_resource_t resource = NULL;
98   tmgr_trace_event_t event = NULL;
99   int i;
100
101   if(first_run) {
102     while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
103       if(next_event_date > NOW) break;
104       while ((event = tmgr_history_get_next_event_leq(history, next_event_date,
105                                                       &value, (void **) &resource_obj))) {
106         resource_obj->resource->common_private->update_resource_state(resource_obj,
107                                                                       event, value);
108       }
109     }
110     xbt_dynar_foreach (resource_list,i,resource) {
111       resource->common_private->update_actions_state(NOW, 0.0);
112     }
113     first_run = 0;
114     return 0.0;
115   }
116
117   min = -1.0;
118
119   xbt_dynar_foreach (resource_list,i,resource) {
120     resource_next_action_end = resource->common_private->share_resources(NOW);
121     if(((min<0.0) || (resource_next_action_end<min)) && (resource_next_action_end>=0.0))
122       min = resource_next_action_end;
123   }
124
125   if(min<0.0) return 0.0;
126
127   while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
128     if(next_event_date > NOW+min) break;
129     while ((event=tmgr_history_get_next_event_leq(history, next_event_date,
130                                                   &value, (void **) &resource_obj))) {
131       if(resource_obj->resource->common_private->resource_used(resource_obj)) {
132         min = next_event_date-NOW;
133       }
134       /* update state of resource_obj according to new value. Does not touch lmm.
135          It will be modified if needed when updating actions */
136       resource_obj->resource->common_private->update_resource_state(resource_obj,
137                                                                     event, value);
138     }
139   }
140
141
142   xbt_dynar_foreach (resource_list,i,resource) {
143     resource->common_private->update_actions_state(NOW, min);
144   }
145
146   NOW=NOW+min;
147
148   return min;
149 }
150
151 xbt_heap_float_t surf_get_clock(void)
152 {
153   return NOW;
154 }