Logo AND Algorithmique Numérique Distribuée

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