Logo AND Algorithmique Numérique Distribuée

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