Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This one was easy. :)
[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 double 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 double generic_maxmin_share_resources(xbt_swag_t running_actions,
18                                       size_t offset)
19 {
20   surf_action_t action = NULL;
21   double min = -1;
22   double value = -1;
23 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + (offset))))
24
25   lmm_solve(maxmin_system);
26
27   xbt_swag_foreach(action, running_actions) {
28     value = lmm_variable_getvalue(VARIABLE(action));
29     if ((value > 0) || (action->max_duration >= 0))
30       break;
31   }
32
33   if (!action)
34     return -1.0;
35
36   if (value > 0) {
37     min = value = action->remains / value;
38     if ((action->max_duration >= 0) && (action->max_duration < min))
39       min = action->max_duration;
40   } else
41     min = action->max_duration;
42
43
44   for (action = xbt_swag_getNext(action, running_actions->offset);
45        action;
46        action = xbt_swag_getNext(action, running_actions->offset)) {
47     value = lmm_variable_getvalue(VARIABLE(action));
48     if (value > 0) {
49       value = action->remains / value;
50       if (value < min)
51         min = value;
52     }
53     if ((action->max_duration >= 0) && (action->max_duration < min))
54       min = action->max_duration;
55   }
56 #undef VARIABLE
57   return min;
58 }
59
60 e_surf_action_state_t surf_action_get_state(surf_action_t action)
61 {
62   surf_action_state_t action_state =
63       &(action->resource_type->common_public->states);
64
65   if (action->state_set == action_state->ready_action_set)
66     return SURF_ACTION_READY;
67   if (action->state_set == action_state->running_action_set)
68     return SURF_ACTION_RUNNING;
69   if (action->state_set == action_state->failed_action_set)
70     return SURF_ACTION_FAILED;
71   if (action->state_set == action_state->done_action_set)
72     return SURF_ACTION_DONE;
73   return SURF_ACTION_NOT_IN_THE_SYSTEM;
74 }
75
76 void surf_action_free(surf_action_t * action)
77 {
78   (*action)->resource_type->common_public->action_cancel(*action);
79   xbt_free(*action);
80   *action = NULL;
81 }
82
83 void surf_action_change_state(surf_action_t action,
84                               e_surf_action_state_t state)
85 {
86   surf_action_state_t action_state =
87       &(action->resource_type->common_public->states);
88
89   xbt_swag_remove(action, action->state_set);
90
91   if (state == SURF_ACTION_READY)
92     action->state_set = action_state->ready_action_set;
93   else if (state == SURF_ACTION_RUNNING)
94     action->state_set = action_state->running_action_set;
95   else if (state == SURF_ACTION_FAILED)
96     action->state_set = action_state->failed_action_set;
97   else if (state == SURF_ACTION_DONE)
98     action->state_set = action_state->done_action_set;
99   else
100     action->state_set = NULL;
101
102   if (action->state_set)
103     xbt_swag_insert(action, action->state_set);
104 }
105
106 void surf_init(int *argc, char **argv)
107 {
108   xbt_init(argc, argv);
109   if (!resource_list)
110     resource_list = xbt_dynar_new(sizeof(surf_resource_private_t), NULL);
111   if (!history)
112     history = tmgr_history_new();
113   if (!maxmin_system)
114     maxmin_system = lmm_system_new();
115 }
116
117 void surf_finalize(void)
118 {
119   int i;
120   surf_resource_t resource = NULL;
121
122   xbt_dynar_foreach(resource_list, i, resource) {
123     resource->common_private->finalize();
124   }
125
126   if (maxmin_system) {
127     lmm_system_free(maxmin_system);
128     maxmin_system = NULL;
129   }
130   if (history) {
131     tmgr_history_free(history);
132     history = NULL;
133   }
134   if (resource_list)
135     xbt_dynar_free(&resource_list);
136
137   tmgr_finalize();
138 }
139
140 double surf_solve(void)
141 {
142   static int first_run = 1;
143
144   double min = -1.0;
145   double next_event_date = -1.0;
146   double resource_next_action_end = -1.0;
147   double value = -1.0;
148   surf_resource_object_t resource_obj = NULL;
149   surf_resource_t resource = NULL;
150   tmgr_trace_event_t event = NULL;
151   int i;
152
153   if (first_run) {
154     while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
155       if (next_event_date > NOW)
156         break;
157       while ((event =
158               tmgr_history_get_next_event_leq(history, next_event_date,
159                                               &value,
160                                               (void **) &resource_obj))) {
161         resource_obj->resource->common_private->
162             update_resource_state(resource_obj, event, value);
163       }
164     }
165     xbt_dynar_foreach(resource_list, i, resource) {
166       resource->common_private->update_actions_state(NOW, 0.0);
167     }
168     first_run = 0;
169     return 0.0;
170   }
171
172   min = -1.0;
173
174   xbt_dynar_foreach(resource_list, i, resource) {
175     resource_next_action_end =
176         resource->common_private->share_resources(NOW);
177     if (((min < 0.0) || (resource_next_action_end < min))
178         && (resource_next_action_end >= 0.0))
179       min = resource_next_action_end;
180   }
181
182   if (min < 0.0)
183     return 0.0;
184
185   while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
186     if (next_event_date > NOW + min)
187       break;
188     while ((event =
189             tmgr_history_get_next_event_leq(history, next_event_date,
190                                             &value,
191                                             (void **) &resource_obj))) {
192       if (resource_obj->resource->common_private->
193           resource_used(resource_obj)) {
194         min = next_event_date - NOW;
195       }
196       /* update state of resource_obj according to new value. Does not touch lmm.
197          It will be modified if needed when updating actions */
198       resource_obj->resource->common_private->
199           update_resource_state(resource_obj, event, value);
200     }
201   }
202
203
204   xbt_dynar_foreach(resource_list, i, resource) {
205     resource->common_private->update_actions_state(NOW, min);
206   }
207
208   NOW = NOW + min;
209
210   return min;
211 }
212
213 double surf_get_clock(void)
214 {
215   return NOW;
216 }