Logo AND Algorithmique Numérique Distribuée

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