Logo AND Algorithmique Numérique Distribuée

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