Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fortran-smpi was broken
[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   void *data; /* user data */
31 } s_smpi_process_data_t;
32
33 static smpi_process_data_t *process_data = NULL;
34 static int process_count = 0;
35
36 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
37
38 MPI_Errhandler* MPI_ERRORS_RETURN = NULL;
39 MPI_Errhandler* MPI_ERRORS_ARE_FATAL = NULL;
40 MPI_Errhandler* MPI_ERRHANDLER_NULL = NULL;
41
42 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
43
44 static char* get_mailbox_name(char* str, int index) {
45   snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int)(sizeof(int) * 2), index);
46   return str;
47 }
48
49 static char* get_mailbox_name_small(char* str, int index) {
50   snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int)(sizeof(int) * 2), index);
51   return str;
52 }
53
54 void smpi_process_init(int *argc, char ***argv)
55 {
56   int index;
57   smpi_process_data_t data;
58   smx_process_t proc;
59
60   if(argc && argv) {
61     proc = SIMIX_process_self();
62     index = atoi((*argv)[1]);
63     data = smpi_process_remote_data(index);
64     simcall_process_set_data(proc, data);
65     if (*argc > 2) {
66       free((*argv)[1]);
67       memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
68       (*argv)[(*argc) - 1] = NULL;
69     }
70     (*argc)--;
71     data->argc = argc;
72     data->argv = argv;
73     simcall_rdv_set_receiver(data->mailbox_small, proc);// set the process attached to the mailbox
74     XBT_DEBUG("<%d> New process in the game: %p", index, proc);
75   }
76 }
77
78 void smpi_process_destroy(void)
79 {
80   int index = smpi_process_index();
81
82   XBT_DEBUG("<%d> Process left the game", index);
83 }
84
85 /**
86  * @brief Prepares the current process for termination.
87  */
88 void smpi_process_finalize(void)
89 {
90   // wait for all pending asynchronous comms to finish
91   while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
92     simcall_process_sleep(0.01);
93   }
94 }
95
96 #ifdef SMPI_F2C
97 int smpi_process_argc(void) {
98   smpi_process_data_t data = smpi_process_data();
99
100   return data->argc ? *(data->argc) - 1 : 0;
101 }
102
103 int smpi_process_getarg(integer* index, char* dst, ftnlen len) {
104   smpi_process_data_t data = smpi_process_data();
105   char* arg;
106   ftnlen i;
107
108   if(!data->argc || !data->argv
109      || *index < 1 || *index >= *(data->argc)) {
110     return -1;
111   }
112   arg = (*data->argv)[*index];
113   for(i = 0; i < len && arg[i] != '\0'; i++) {
114     dst[i] = arg[i];
115   }
116   for(; i < len; i++) {
117     dst[i] = ' ';
118   }
119   return 0;
120 }
121
122 int smpi_global_size(void) {
123    char* value = getenv("SMPI_GLOBAL_SIZE");
124
125    if(!value) {
126      fprintf(stderr, "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
127      xbt_abort();
128    }
129    return atoi(value);
130 }
131 #endif
132
133 smpi_process_data_t smpi_process_data(void)
134 {
135   return SIMIX_process_self_get_data(SIMIX_process_self());
136 }
137
138 smpi_process_data_t smpi_process_remote_data(int index)
139 {
140   return process_data[index];
141 }
142
143 void smpi_process_set_user_data(void *data)
144 {
145   smpi_process_data_t process_data = smpi_process_data();
146   process_data->data = data;
147 }
148
149 void* smpi_process_get_user_data(){
150   smpi_process_data_t process_data = smpi_process_data();
151   return process_data->data;
152 }
153
154 int smpi_process_count(void)
155 {
156   return process_count;
157 }
158
159 int smpi_process_index(void)
160 {
161   smpi_process_data_t data = smpi_process_data();
162
163   return data->index;
164 }
165
166 smx_rdv_t smpi_process_mailbox(void) {
167   smpi_process_data_t data = smpi_process_data();
168
169   return data->mailbox;
170 }
171
172 smx_rdv_t smpi_process_mailbox_small(void) {
173   smpi_process_data_t data = smpi_process_data();
174
175   return data->mailbox_small;
176 }
177
178 smx_rdv_t smpi_process_remote_mailbox(int index) {
179   smpi_process_data_t data = smpi_process_remote_data(index);
180
181   return data->mailbox;
182 }
183
184
185 smx_rdv_t smpi_process_remote_mailbox_small(int index) {
186   smpi_process_data_t data = smpi_process_remote_data(index);
187
188   return data->mailbox_small;
189 }
190
191 xbt_os_timer_t smpi_process_timer(void)
192 {
193   smpi_process_data_t data = smpi_process_data();
194
195   return data->timer;
196 }
197
198 void smpi_process_simulated_start(void) {
199   smpi_process_data_t data = smpi_process_data();
200
201   data->simulated = SIMIX_get_clock();
202 }
203
204 double smpi_process_simulated_elapsed(void) {
205   smpi_process_data_t data = smpi_process_data();
206
207   return SIMIX_get_clock() - data->simulated;
208 }
209
210 MPI_Comm smpi_process_comm_self(void) {
211   smpi_process_data_t data = smpi_process_data();
212
213   return data->comm_self;
214 }
215
216 void print_request(const char *message, MPI_Request request) {
217   XBT_DEBUG("%s  request %p  [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
218          message, request, request->buf, request->size,
219          request->src, request->dst, request->tag, request->flags);
220 }
221
222 static void SMPI_comm_copy_buffer_callback(smx_action_t comm, void* buff, size_t buff_size)
223 {
224   XBT_DEBUG("Copy the data over");
225   memcpy(comm->comm.dst_buff, buff, buff_size);
226   if (comm->comm.detached) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the original buffer available to the application ASAP
227     xbt_free(buff);
228     //It seems that the request is used after the call there this should
229     //be free somewhereelse  but where???
230     //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
231     //inside the user data and should be free 
232     comm->comm.src_buff = NULL;
233   }
234 }
235
236 void smpi_global_init(void)
237 {
238   int i;
239   MPI_Group group;
240   char name[MAILBOX_NAME_MAXLEN];
241
242   SIMIX_comm_set_copy_data_callback(&SMPI_comm_copy_buffer_callback);
243   process_count = SIMIX_process_count();
244   process_data = xbt_new(smpi_process_data_t, process_count);
245   for (i = 0; i < process_count; i++) {
246     process_data[i] = xbt_new(s_smpi_process_data_t, 1);
247     process_data[i]->index = i;
248     process_data[i]->argc = NULL;
249     process_data[i]->argv = NULL;
250     process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
251     process_data[i]->mailbox_small = simcall_rdv_create(get_mailbox_name_small(name, i));
252     process_data[i]->timer = xbt_os_timer_new();
253     group = smpi_group_new(1);
254     process_data[i]->comm_self = smpi_comm_new(group);
255     smpi_group_set_mapping(group, i, 0);
256   }
257   group = smpi_group_new(process_count);
258   MPI_COMM_WORLD = smpi_comm_new(group);
259   for (i = 0; i < process_count; i++) {
260     smpi_group_set_mapping(group, i, i);
261   }
262 }
263
264 void smpi_global_destroy(void)
265 {
266   int count = smpi_process_count();
267   int i;
268
269   smpi_bench_destroy();
270   smpi_group_destroy(smpi_comm_group(MPI_COMM_WORLD));
271   smpi_comm_destroy(MPI_COMM_WORLD);
272   MPI_COMM_WORLD = MPI_COMM_NULL;
273   for (i = 0; i < count; i++) {
274     smpi_comm_destroy(process_data[i]->comm_self);
275     xbt_os_timer_free(process_data[i]->timer);
276     simcall_rdv_destroy(process_data[i]->mailbox);
277     simcall_rdv_destroy(process_data[i]->mailbox_small);
278     xbt_free(process_data[i]);
279   }
280   xbt_free(process_data);
281   process_data = NULL;
282
283   smpi_free_static();
284 }
285
286 /* Fortran specific stuff */
287 /* With smpicc, the following weak symbols are used */
288 /* With smpiff, the following weak symbols are replaced by those in libf2c */
289
290 int __attribute__((weak)) xargc;
291 char** __attribute__((weak)) xargv;
292
293 int __attribute__((weak)) smpi_simulated_main(int argc, char** argv) {
294   xbt_die("Should not be in this smpi_simulated_main");
295   return 1;
296 }
297
298 int __attribute__((weak)) main(int argc, char** argv) {
299    return smpi_main(smpi_simulated_main,argc,argv);
300 }
301
302 int __attribute__((weak)) MAIN__(){
303   return smpi_main(smpi_simulated_main,xargc, xargv);
304 };
305
306 int smpi_main(int (*realmain) (int argc, char *argv[]),int argc, char *argv[])
307 {
308   srand(SMPI_RAND_SEED);
309
310   if(getenv("SMPI_PRETEND_CC") != NULL) {
311   /* Hack to ensure that smpicc can pretend to be a simple compiler. Particularly handy to pass it to the configuration tools */
312     return 0;
313   }
314
315   /* Connect log categories.  See xbt/log.c */
316   XBT_LOG_CONNECT(smpi);  /* Keep this line as soon as possible in this function: xbt_log_appender_file.c depends on it
317                              DO NOT connect this in XBT or so, or it will be useless to xbt_log_appender_file.c */
318   XBT_LOG_CONNECT(smpi_base);
319   XBT_LOG_CONNECT(smpi_bench);
320   XBT_LOG_CONNECT(smpi_coll);
321   XBT_LOG_CONNECT(smpi_comm);
322   XBT_LOG_CONNECT(smpi_group);
323   XBT_LOG_CONNECT(smpi_kernel);
324   XBT_LOG_CONNECT(smpi_mpi);
325   XBT_LOG_CONNECT(smpi_mpi_dt);
326   XBT_LOG_CONNECT(smpi_pmpi);
327   XBT_LOG_CONNECT(smpi_replay);
328
329 #ifdef HAVE_TRACING
330   TRACE_global_init(&argc, argv);
331 #endif
332
333   SIMIX_global_init(&argc, argv);
334
335 #ifdef HAVE_TRACING
336   TRACE_start();
337 #endif
338
339   // parse the platform file: get the host list
340   SIMIX_create_environment(argv[1]);
341
342   SIMIX_function_register_default(realmain);
343   SIMIX_launch_application(argv[2]);
344
345   smpi_global_init();
346
347   /* Clean IO before the run */
348   fflush(stdout);
349   fflush(stderr);
350
351   if (MC_is_active())
352     MC_modelcheck();
353   else
354     SIMIX_run();
355
356   if (surf_cfg_get_int("smpi/display_timing"))
357     XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
358
359   smpi_global_destroy();
360
361 #ifdef HAVE_TRACING
362   TRACE_end();
363 #endif
364
365   return 0;
366 }