Logo AND Algorithmique Numérique Distribuée

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