Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ansi C declaration of the variables (at the beginning of the blocks)
[simgrid.git] / src / smpi / smpi_global.c
1 #include <stdio.h>
2
3 #include "private.h"
4
5 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
6
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi, "Logging specific to SMPI (kernel)");
8
9 smpi_global_t     smpi_global     = NULL;
10
11 void *smpi_request_new(void);
12
13 void *smpi_request_new()
14 {
15         smpi_mpi_request_t request = xbt_new(s_smpi_mpi_request_t, 1);
16
17         request->buf       = NULL;
18         request->completed = 0;
19         request->mutex     = SIMIX_mutex_init();
20         request->cond      = SIMIX_cond_init();
21         request->data      = NULL;
22         request->forward   = 0;
23
24         return request;
25 }
26
27 void smpi_request_free(void *pointer);
28
29 void smpi_request_free(void *pointer)
30 {
31
32         smpi_mpi_request_t request = pointer;
33
34         SIMIX_cond_destroy(request->cond);
35         SIMIX_mutex_destroy(request->mutex);
36         xbt_free(request);
37
38         return;
39 }
40
41 void smpi_request_reset(void *pointer);
42
43 void smpi_request_reset(void *pointer)
44 {
45         smpi_mpi_request_t request = pointer;
46
47         request->buf       = NULL;
48         request->completed = 0;
49         request->data      = NULL;
50         request->forward   = 0;
51
52         return;
53 }
54
55
56 void *smpi_message_new(void);
57
58 void *smpi_message_new()
59 {
60         smpi_received_message_t message = xbt_new(s_smpi_received_message_t, 1);
61         message->buf = NULL;
62         return message;
63 }
64
65 void smpi_message_free(void *pointer);
66
67 void smpi_message_free(void *pointer)
68 {
69         xbt_free(pointer);
70         return;
71 }
72
73 void smpi_message_reset(void *pointer);
74
75 void smpi_message_reset(void *pointer)
76 {
77         smpi_received_message_t message = pointer;
78         message->buf = NULL;
79         return;
80 }
81
82 int smpi_create_request(void *buf, int count, smpi_mpi_datatype_t datatype,
83         int src, int dst, int tag, smpi_mpi_communicator_t comm, smpi_mpi_request_t *requestptr)
84 {
85         int retval = MPI_SUCCESS;
86
87         smpi_mpi_request_t request = NULL;
88
89         // parameter checking prob belongs in smpi_mpi, but this is less repeat code
90         if (NULL == buf) {
91                 retval = MPI_ERR_INTERN;
92         } else if (0 > count) {
93                 retval = MPI_ERR_COUNT;
94         } else if (NULL == datatype) {
95                 retval = MPI_ERR_TYPE;
96         } else if (MPI_ANY_SOURCE != src && (0 > src || comm->size <= src)) {
97                 retval = MPI_ERR_RANK;
98         } else if (0 > dst || comm->size <= dst) {
99                 retval = MPI_ERR_RANK;
100         } else if (MPI_ANY_TAG != tag && 0 > tag) {
101                 retval = MPI_ERR_TAG;
102         } else if (NULL == comm) {
103                 retval = MPI_ERR_COMM;
104         } else if (NULL == requestptr) {
105                 retval = MPI_ERR_ARG;
106         } else {
107                 request           = xbt_mallocator_get(smpi_global->request_mallocator);
108                 request->comm     = comm;
109                 request->src      = src;
110                 request->dst      = dst;
111                 request->tag      = tag;
112                 request->buf      = buf;
113                 request->datatype = datatype;
114                 request->count    = count;
115
116                 *requestptr       = request;
117         }
118         return retval;
119 }
120
121 void smpi_global_init()
122 {
123         int i;
124
125         int size = SIMIX_host_get_number();
126
127         smpi_global                                      = xbt_new(s_smpi_global_t, 1);
128
129         // config variable
130         smpi_global->reference_speed                     = SMPI_DEFAULT_SPEED;
131
132         smpi_global->root_ready                          = 0;
133         smpi_global->ready_process_count                 = 0;
134
135         // start/stop
136         smpi_global->start_stop_mutex                    = SIMIX_mutex_init();
137         smpi_global->start_stop_cond                     = SIMIX_cond_init();
138
139         // host info blank until sim starts
140         // FIXME: is this okay?
141         smpi_global->hosts                               = NULL;
142         smpi_global->host_count                          = 0;
143
144         // running hosts
145         smpi_global->running_hosts_count_mutex           = SIMIX_mutex_init();
146         smpi_global->running_hosts_count                 = 0;
147
148         // mallocators
149         smpi_global->request_mallocator                  = xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE,
150                                                              smpi_request_new, smpi_request_free, smpi_request_reset);
151         smpi_global->message_mallocator                  = xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE,
152                                                              smpi_message_new, smpi_message_free, smpi_message_reset);
153
154         // queues
155         smpi_global->pending_send_request_queues         = xbt_new(xbt_fifo_t,  size);
156         smpi_global->pending_send_request_queues_mutexes = xbt_new(smx_mutex_t, size);
157         smpi_global->pending_recv_request_queues         = xbt_new(xbt_fifo_t,  size);
158         smpi_global->pending_recv_request_queues_mutexes = xbt_new(smx_mutex_t, size);
159         smpi_global->received_message_queues             = xbt_new(xbt_fifo_t,  size);
160         smpi_global->received_message_queues_mutexes     = xbt_new(smx_mutex_t, size);
161
162         // sender/receiver processes
163         smpi_global->sender_processes                    = xbt_new(smx_process_t, size);
164         smpi_global->receiver_processes                  = xbt_new(smx_process_t, size);
165
166         // timers
167         smpi_global->timer                               = xbt_os_timer_new();
168         smpi_global->timer_mutex                         = SIMIX_mutex_init();
169         smpi_global->timer_cond                          = SIMIX_cond_init();
170
171         smpi_global->do_once_duration_nodes              = NULL;
172         smpi_global->do_once_duration                    = NULL;
173         smpi_global->do_once_mutex                       = SIMIX_mutex_init();
174
175         for (i = 0; i < size; i++) {
176                 smpi_global->pending_send_request_queues[i]         = xbt_fifo_new();
177                 smpi_global->pending_send_request_queues_mutexes[i] = SIMIX_mutex_init();
178                 smpi_global->pending_recv_request_queues[i]         = xbt_fifo_new();
179                 smpi_global->pending_recv_request_queues_mutexes[i] = SIMIX_mutex_init();
180                 smpi_global->received_message_queues[i]             = xbt_fifo_new();
181                 smpi_global->received_message_queues_mutexes[i]     = SIMIX_mutex_init();
182         }
183
184 }
185
186 void smpi_global_destroy()
187 {
188         int i;
189
190         int size = SIMIX_host_get_number();
191
192         smpi_do_once_duration_node_t curr, next;
193
194         // start/stop
195         SIMIX_mutex_destroy(smpi_global->start_stop_mutex);
196         SIMIX_cond_destroy(smpi_global->start_stop_cond);
197
198         // processes
199         xbt_free(smpi_global->sender_processes);
200         xbt_free(smpi_global->receiver_processes);
201
202         // running hosts
203         SIMIX_mutex_destroy(smpi_global->running_hosts_count_mutex);
204
205         // mallocators
206         xbt_mallocator_free(smpi_global->request_mallocator);
207         xbt_mallocator_free(smpi_global->message_mallocator);
208
209         xbt_os_timer_free(smpi_global->timer);
210         SIMIX_mutex_destroy(smpi_global->timer_mutex);
211         SIMIX_cond_destroy(smpi_global->timer_cond);
212
213         for(curr = smpi_global->do_once_duration_nodes; NULL != curr; curr = next) {
214                 next = curr->next;
215                 xbt_free(curr->file);
216                 xbt_free(curr);
217         }
218
219         SIMIX_mutex_destroy(smpi_global->do_once_mutex);
220
221         for(i = 0; i < size; i++) {
222                 xbt_fifo_free(smpi_global->pending_send_request_queues[i]);
223                 SIMIX_mutex_destroy(smpi_global->pending_send_request_queues_mutexes[i]);
224                 xbt_fifo_free(smpi_global->pending_recv_request_queues[i]);
225                 SIMIX_mutex_destroy(smpi_global->pending_recv_request_queues_mutexes[i]);
226                 xbt_fifo_free(smpi_global->received_message_queues[i]);
227                 SIMIX_mutex_destroy(smpi_global->received_message_queues_mutexes[i]);
228         }
229
230         xbt_free(smpi_global->pending_send_request_queues);
231         xbt_free(smpi_global->pending_send_request_queues_mutexes);
232         xbt_free(smpi_global->pending_recv_request_queues);
233         xbt_free(smpi_global->pending_recv_request_queues_mutexes);
234         xbt_free(smpi_global->received_message_queues);
235         xbt_free(smpi_global->received_message_queues_mutexes);
236
237         xbt_free(smpi_global);
238
239         smpi_global = NULL;
240 }
241
242 int smpi_host_index()
243 {
244         smx_host_t host = SIMIX_host_self();
245         smpi_host_data_t hdata = (smpi_host_data_t)SIMIX_host_get_data(host);
246         return hdata->index;
247 }
248
249 smx_mutex_t smpi_host_mutex()
250 {
251         smx_host_t host = SIMIX_host_self();
252         smpi_host_data_t hdata = (smpi_host_data_t)SIMIX_host_get_data(host);
253         return hdata->mutex;
254 }
255
256 smx_cond_t smpi_host_cond()
257 {
258         smx_host_t host = SIMIX_host_self();
259         smpi_host_data_t hdata = (smpi_host_data_t)SIMIX_host_get_data(host);
260         return hdata->cond;
261 }
262
263 int smpi_run_simulation(int *argc, char **argv)
264 {
265         smx_cond_t   cond           = NULL;
266         smx_action_t action         = NULL;
267
268         xbt_fifo_t   actions_failed = xbt_fifo_new();
269         xbt_fifo_t   actions_done   = xbt_fifo_new();
270
271         srand(SMPI_RAND_SEED);
272
273         SIMIX_global_init(argc, argv);
274
275         SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
276         SIMIX_function_register("smpi_sender",         smpi_sender);
277         SIMIX_function_register("smpi_receiver",       smpi_receiver);
278
279         // FIXME: ought to verify these files...
280         SIMIX_create_environment(argv[1]);
281
282         // must initialize globals between creating environment and launching app....
283         smpi_global_init();
284
285         SIMIX_launch_application(argv[2]);
286
287         /* Prepare to display some more info when dying on Ctrl-C pressing */
288         // FIXME: doesn't work
289         //signal(SIGINT, inthandler);
290
291         /* Clean IO before the run */
292         fflush(stdout);
293         fflush(stderr);
294
295         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
296                 while ((action = xbt_fifo_pop(actions_failed))) {
297                         DEBUG1("** %s failed **", action->name);
298                         while((cond = xbt_fifo_pop(action->cond_list))) {
299                                 SIMIX_cond_broadcast(cond);
300                         }
301                 }
302                 while((action = xbt_fifo_pop(actions_done))) {
303                         DEBUG1("** %s done **",action->name);
304                         while((cond = xbt_fifo_pop(action->cond_list))) {
305                                 SIMIX_cond_broadcast(cond);
306                         }
307                 }
308         }
309
310         // FIXME: cleanup incomplete
311         xbt_fifo_free(actions_failed);
312         xbt_fifo_free(actions_done);
313
314         INFO1("simulation time %g", SIMIX_get_clock());
315
316         smpi_global_destroy();
317
318         SIMIX_clean();
319
320         return 0;
321 }