Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
24d79742025b1114953b533e955d82bd4ce82df4
[simgrid.git] / src / smpi / smpi_global.c
1 #include <stdio.h>
2
3 #include "private.h"
4
5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi, XBT_LOG_ROOT_CAT, "All SMPI categories");
6
7 SMPI_Global_t     smpi_global     = NULL;
8
9 void *smpi_request_new()
10 {
11         smpi_mpi_request_t request = xbt_new(s_smpi_mpi_request_t, 1);
12
13         request->completed = 0;
14         request->mutex     = SIMIX_mutex_init();
15         request->cond      = SIMIX_cond_init();
16
17         return request;
18 }
19
20 void smpi_request_free(void *pointer)
21 {
22
23         smpi_mpi_request_t request = pointer;
24
25         if (NULL != request) {
26                 SIMIX_cond_destroy(request->cond);
27                 SIMIX_mutex_destroy(request->mutex);
28                 xbt_free(request);
29         }
30
31         return;
32 }
33
34 void smpi_request_reset(void *pointer)
35 {
36         smpi_mpi_request_t request = pointer;
37
38         request->completed = 0;
39
40         return;
41 }
42
43
44 void *smpi_message_new()
45 {
46         return xbt_new(s_smpi_received_message_t, 1);
47 }
48
49 void smpi_message_free(void *pointer)
50 {
51         if (NULL != pointer) {
52                 xbt_free(pointer);
53         }
54
55         return;
56 }
57
58 void smpi_message_reset(void *pointer)
59 {
60         return;
61 }
62
63 void smpi_global_init()
64 {
65         int i;
66
67         int size = SIMIX_host_get_number();
68
69         smpi_global                                      = xbt_new(s_SMPI_Global_t, 1);
70
71         // config variable
72         smpi_global->reference_speed                     = SMPI_DEFAULT_SPEED;
73
74         smpi_global->root_ready                          = 0;
75         smpi_global->ready_process_count                 = 0;
76
77         // start/stop
78         smpi_global->start_stop_mutex                    = SIMIX_mutex_init();
79         smpi_global->start_stop_cond                     = SIMIX_cond_init();
80
81         // processes
82         smpi_global->sender_processes                    = xbt_new(smx_process_t, size);
83         smpi_global->receiver_processes                  = xbt_new(smx_process_t, size);
84
85         // running hosts
86         smpi_global->running_hosts_count_mutex           = SIMIX_mutex_init();
87         smpi_global->running_hosts_count                 = 0;
88
89         // mallocators
90         smpi_global->request_mallocator                  = xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE,
91                                                              smpi_request_new, smpi_request_free, smpi_request_reset);
92         smpi_global->message_mallocator                  = xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE,
93                                                              smpi_message_new, smpi_message_free, smpi_message_reset);
94
95         // queues
96         smpi_global->pending_send_request_queues         = xbt_new(xbt_fifo_t,  size);
97         smpi_global->pending_send_request_queues_mutexes = xbt_new(smx_mutex_t, size);
98         smpi_global->pending_recv_request_queues         = xbt_new(xbt_fifo_t,  size);
99         smpi_global->pending_recv_request_queues_mutexes = xbt_new(smx_mutex_t, size);
100         smpi_global->received_message_queues             = xbt_new(xbt_fifo_t,  size);
101         smpi_global->received_message_queues_mutexes     = xbt_new(smx_mutex_t, size);
102
103         // timers
104         smpi_global->timers                              = xbt_new(xbt_os_timer_t, size);
105         smpi_global->timers_mutexes                      = xbt_new(smx_mutex_t, size);
106
107         for(i = 0; i < size; i++) {
108                 smpi_global->pending_send_request_queues[i]         = xbt_fifo_new();
109                 smpi_global->pending_send_request_queues_mutexes[i] = SIMIX_mutex_init();
110                 smpi_global->pending_recv_request_queues[i]         = xbt_fifo_new();
111                 smpi_global->pending_recv_request_queues_mutexes[i] = SIMIX_mutex_init();
112                 smpi_global->received_message_queues[i]             = xbt_fifo_new();
113                 smpi_global->received_message_queues_mutexes[i]     = SIMIX_mutex_init();
114                 smpi_global->timers[i]                              = xbt_os_timer_new();
115                 smpi_global->timers_mutexes[i]                      = SIMIX_mutex_init();
116         }
117
118 }
119
120 void smpi_global_destroy()
121 {
122         int i;
123
124         int size = SIMIX_host_get_number();
125
126         // start/stop
127         SIMIX_mutex_destroy(smpi_global->start_stop_mutex);
128         SIMIX_cond_destroy(smpi_global->start_stop_cond);
129
130         // processes
131         xbt_free(smpi_global->sender_processes);
132         xbt_free(smpi_global->receiver_processes);
133
134         // running hosts
135         SIMIX_mutex_destroy(smpi_global->running_hosts_count_mutex);
136
137         // mallocators
138         xbt_mallocator_free(smpi_global->request_mallocator);
139         xbt_mallocator_free(smpi_global->message_mallocator);
140
141         for(i = 0; i < size; i++) {
142                 xbt_fifo_free(smpi_global->pending_send_request_queues[i]);
143                 SIMIX_mutex_destroy(smpi_global->pending_send_request_queues_mutexes[i]);
144                 xbt_fifo_free(smpi_global->pending_recv_request_queues[i]);
145                 SIMIX_mutex_destroy(smpi_global->pending_recv_request_queues_mutexes[i]);
146                 xbt_fifo_free(smpi_global->received_message_queues[i]);
147                 SIMIX_mutex_destroy(smpi_global->received_message_queues_mutexes[i]);
148                 xbt_os_timer_free(smpi_global->timers[i]);
149                 SIMIX_mutex_destroy(smpi_global->timers_mutexes[i]);
150         }
151
152         xbt_free(smpi_global->pending_send_request_queues);
153         xbt_free(smpi_global->pending_send_request_queues_mutexes);
154         xbt_free(smpi_global->pending_recv_request_queues);
155         xbt_free(smpi_global->pending_recv_request_queues_mutexes);
156         xbt_free(smpi_global->received_message_queues);
157         xbt_free(smpi_global->received_message_queues_mutexes);
158         xbt_free(smpi_global->timers);
159         xbt_free(smpi_global->timers_mutexes);
160
161         xbt_free(smpi_global);
162
163         smpi_global = NULL;
164 }
165
166 int smpi_run_simulation(int argc, char **argv)
167 {
168         xbt_fifo_item_t cond_item   = NULL;
169         smx_cond_t   cond           = NULL;
170         xbt_fifo_item_t action_item = NULL;
171         smx_action_t action         = NULL;
172
173         xbt_fifo_t   actions_failed = xbt_fifo_new();
174         xbt_fifo_t   actions_done   = xbt_fifo_new();
175
176         srand(SMPI_RAND_SEED);
177
178         SIMIX_global_init(&argc, argv);
179
180         SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
181         SIMIX_function_register("smpi_sender",         smpi_sender);
182         SIMIX_function_register("smpi_receiver",       smpi_receiver);
183
184         // FIXME: ought to verify these files...
185         SIMIX_create_environment(argv[1]);
186
187         // must initialize globals between creating environment and launching app....
188         smpi_global_init();
189
190         SIMIX_launch_application(argv[2]);
191
192         /* Prepare to display some more info when dying on Ctrl-C pressing */
193         // FIXME: doesn't work
194         //signal(SIGINT, inthandler);
195
196         /* Clean IO before the run */
197         fflush(stdout);
198         fflush(stderr);
199
200         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
201                 xbt_fifo_foreach(actions_failed, action_item, action, smx_action_t) {
202                         DEBUG1("** %s failed **", action->name);
203                         xbt_fifo_foreach(action->cond_list, cond_item, cond, smx_cond_t) {
204                                 SIMIX_cond_broadcast(cond);
205                         }
206                 }
207                 xbt_fifo_foreach(actions_done, action_item, action, smx_action_t) {
208                         DEBUG1("** %s done **",action->name);
209                         xbt_fifo_foreach(action->cond_list, cond_item, cond, smx_cond_t) {
210                                 SIMIX_cond_broadcast(cond);
211                         }
212                 }
213         }
214
215         // FIXME: cleanup incomplete
216         xbt_fifo_free(actions_failed);
217         xbt_fifo_free(actions_done);
218
219         INFO1("simulation time %g", SIMIX_get_clock());
220
221         smpi_global_destroy();
222
223         SIMIX_clean();
224
225         return 0;
226 }