Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[smpi,simix-network] remove a useless function
[simgrid.git] / src / smpi / smpi_global.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "private.h"
12 #include "smpi_mpi_dt_private.h"
13 #include "mc/mc.h"
14 #include "surf/surf.h"
15 #include "simix/smx_private.h"
16
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
19                                 "Logging specific to SMPI (kernel)");
20
21 typedef struct s_smpi_process_data {
22   int index;
23   int* argc;
24   char*** argv;
25   smx_rdv_t mailbox;
26   smx_rdv_t mailbox_small;
27   xbt_os_timer_t timer;
28   double simulated;
29   MPI_Comm comm_self;
30 } s_smpi_process_data_t;
31
32 static smpi_process_data_t *process_data = NULL;
33 static int process_count = 0;
34
35 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
36
37 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
38
39 static char* get_mailbox_name(char* str, int index) {
40   snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int)(sizeof(int) * 2), index);
41   return str;
42 }
43
44 static char* get_mailbox_name_small(char* str, int index) {
45   snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int)(sizeof(int) * 2), index);
46   return str;
47 }
48
49 void smpi_process_init(int *argc, char ***argv)
50 {
51   int index;
52   smpi_process_data_t data;
53   smx_process_t proc;
54
55   if(argc && argv) {
56     proc = SIMIX_process_self();
57     index = atoi((*argv)[1]);
58     data = smpi_process_remote_data(index);
59     simcall_process_set_data(proc, data);
60     if (*argc > 2) {
61       free((*argv)[1]);
62       memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
63       (*argv)[(*argc) - 1] = NULL;
64     }
65     (*argc)--;
66     data->argc = argc;
67     data->argv = argv;
68     simcall_rdv_set_receiver(data->mailbox_small, proc);// set the process attached to the mailbox
69     XBT_DEBUG("<%d> New process in the game: %p", index, proc);
70   }
71 }
72
73 void smpi_process_destroy(void)
74 {
75   int index = smpi_process_index();
76
77   XBT_DEBUG("<%d> Process left the game", index);
78 }
79
80 /**
81  * @brief Prepares the current process for termination.
82  */
83 void smpi_process_finalize(void)
84 {
85   // wait for all pending asynchronous comms to finish
86   while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
87     simcall_process_sleep(0.01);
88   }
89 }
90
91 int smpi_process_argc(void) {
92   smpi_process_data_t data = smpi_process_data();
93
94   return data->argc ? *(data->argc) - 1 : 0;
95 }
96
97 int smpi_process_getarg(integer* index, char* dst, ftnlen len) {
98   smpi_process_data_t data = smpi_process_data();
99   char* arg;
100   ftnlen i;
101
102   if(!data->argc || !data->argv
103      || *index < 1 || *index >= *(data->argc)) {
104     return -1;
105   }
106   arg = (*data->argv)[*index];
107   for(i = 0; i < len && arg[i] != '\0'; i++) {
108     dst[i] = arg[i];
109   }
110   for(; i < len; i++) {
111     dst[i] = ' ';
112   }
113   return 0;
114 }
115
116 int smpi_global_size(void) {
117    char* value = getenv("SMPI_GLOBAL_SIZE");
118
119    if(!value) {
120       fprintf(stderr, "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
121       abort();
122    }
123    return atoi(value);
124 }
125
126 smpi_process_data_t smpi_process_data(void)
127 {
128   return SIMIX_process_self_get_data(SIMIX_process_self());
129 }
130
131 smpi_process_data_t smpi_process_remote_data(int index)
132 {
133   return process_data[index];
134 }
135
136 int smpi_process_count(void)
137 {
138   return process_count;
139 }
140
141 int smpi_process_index(void)
142 {
143   smpi_process_data_t data = smpi_process_data();
144
145   return data->index;
146 }
147
148 smx_rdv_t smpi_process_mailbox(void) {
149   smpi_process_data_t data = smpi_process_data();
150
151   return data->mailbox;
152 }
153
154 smx_rdv_t smpi_process_mailbox_small(void) {
155   smpi_process_data_t data = smpi_process_data();
156
157   return data->mailbox_small;
158 }
159
160 smx_rdv_t smpi_process_remote_mailbox(int index) {
161   smpi_process_data_t data = smpi_process_remote_data(index);
162
163   return data->mailbox;
164 }
165
166
167 smx_rdv_t smpi_process_remote_mailbox_small(int index) {
168   smpi_process_data_t data = smpi_process_remote_data(index);
169
170   return data->mailbox_small;
171 }
172
173 xbt_os_timer_t smpi_process_timer(void)
174 {
175   smpi_process_data_t data = smpi_process_data();
176
177   return data->timer;
178 }
179
180 void smpi_process_simulated_start(void) {
181   smpi_process_data_t data = smpi_process_data();
182
183   data->simulated = SIMIX_get_clock();
184 }
185
186 double smpi_process_simulated_elapsed(void) {
187   smpi_process_data_t data = smpi_process_data();
188
189   return SIMIX_get_clock() - data->simulated;
190 }
191
192 MPI_Comm smpi_process_comm_self(void) {
193   smpi_process_data_t data = smpi_process_data();
194
195   return data->comm_self;
196 }
197
198 void print_request(const char *message, MPI_Request request) {
199   XBT_DEBUG("%s  request %p  [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
200          message, request, request->buf, request->size,
201          request->src, request->dst, request->tag, request->flags);
202 }
203
204 void smpi_global_init(void)
205 {
206   int i;
207   MPI_Group group;
208   char name[MAILBOX_NAME_MAXLEN];
209
210   SIMIX_comm_set_copy_data_callback(&SIMIX_comm_copy_buffer_callback);
211   process_count = SIMIX_process_count();
212   process_data = xbt_new(smpi_process_data_t, process_count);
213   for (i = 0; i < process_count; i++) {
214     process_data[i] = xbt_new(s_smpi_process_data_t, 1);
215     process_data[i]->index = i;
216     process_data[i]->argc = NULL;
217     process_data[i]->argv = NULL;
218     process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
219     process_data[i]->mailbox_small = simcall_rdv_create(get_mailbox_name_small(name, i));
220     process_data[i]->timer = xbt_os_timer_new();
221     group = smpi_group_new(1);
222     process_data[i]->comm_self = smpi_comm_new(group);
223     smpi_group_set_mapping(group, i, 0);
224   }
225   group = smpi_group_new(process_count);
226   MPI_COMM_WORLD = smpi_comm_new(group);
227   for (i = 0; i < process_count; i++) {
228     smpi_group_set_mapping(group, i, i);
229   }
230 }
231
232 void smpi_global_destroy(void)
233 {
234   int count = smpi_process_count();
235   int i;
236
237   smpi_bench_destroy();
238   smpi_comm_destroy(MPI_COMM_WORLD);
239   MPI_COMM_WORLD = MPI_COMM_NULL;
240   for (i = 0; i < count; i++) {
241     smpi_comm_destroy(process_data[i]->comm_self);
242     xbt_os_timer_free(process_data[i]->timer);
243     simcall_rdv_destroy(process_data[i]->mailbox);
244     simcall_rdv_destroy(process_data[i]->mailbox_small);
245     xbt_free(process_data[i]);
246   }
247   xbt_free(process_data);
248   process_data = NULL;
249
250   smpi_free_static();
251 }
252
253 /* Fortran specific stuff */
254 /* With smpicc, the following weak symbols are used */
255 /* With smpiff, the following weak symbols are replaced by those in libf2c */
256 int __attribute__((weak)) xargc;
257 char** __attribute__((weak)) xargv;
258
259 int __attribute__((weak)) main(int argc, char** argv) {
260    xargc = argc;
261    xargv = argv;
262    return MAIN__();
263 }
264
265 int MAIN__(void)
266 {
267   srand(SMPI_RAND_SEED);
268
269   if(getenv("SMPI_PRETEND_CC") != NULL) {
270   /* Hack to ensure that smpicc can pretend to be a simple compiler. Particularly handy to pass it to the configuration tools */
271     return 0;
272   }
273
274   /* Connect log categories.  See xbt/log.c */
275   XBT_LOG_CONNECT(smpi);  /* Keep this line as soon as possible in this function: xbt_log_appender_file.c depends on it
276                              DO NOT connect this in XBT or so, or it will be useless to xbt_log_appender_file.c */
277   XBT_LOG_CONNECT(smpi_base);
278   XBT_LOG_CONNECT(smpi_bench);
279   XBT_LOG_CONNECT(smpi_coll);
280   XBT_LOG_CONNECT(smpi_comm);
281   XBT_LOG_CONNECT(smpi_group);
282   XBT_LOG_CONNECT(smpi_kernel);
283   XBT_LOG_CONNECT(smpi_mpi);
284   XBT_LOG_CONNECT(smpi_mpi_dt);
285   XBT_LOG_CONNECT(smpi_pmpi);
286
287 #ifdef HAVE_TRACING
288   TRACE_global_init(&xargc, xargv);
289 #endif
290
291   SIMIX_global_init(&xargc, xargv);
292
293 #ifdef HAVE_TRACING
294   TRACE_start();
295 #endif
296
297   // parse the platform file: get the host list
298   SIMIX_create_environment(xargv[1]);
299
300   SIMIX_function_register_default(smpi_simulated_main);
301   SIMIX_launch_application(xargv[2]);
302
303   smpi_global_init();
304
305   /* Clean IO before the run */
306   fflush(stdout);
307   fflush(stderr);
308
309   if (MC_IS_ENABLED)
310     MC_modelcheck();
311   else
312     SIMIX_run();
313
314   if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
315     XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
316
317   smpi_global_destroy();
318
319 #ifdef HAVE_TRACING
320   TRACE_end();
321 #endif
322
323   SIMIX_clean();
324   return 0;
325 }