1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
12 #include "smpi_mpi_dt_private.h"
14 #include "surf/surf.h"
15 #include "simix/smx_private.h"
16 #include "simgrid/sg_config.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
20 "Logging specific to SMPI (kernel)");
22 typedef struct s_smpi_process_data {
27 smx_rdv_t mailbox_small;
31 void *data; /* user data */
32 } s_smpi_process_data_t;
34 static smpi_process_data_t *process_data = NULL;
35 static int process_count = 0;
37 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
39 MPI_Errhandler* MPI_ERRORS_RETURN = NULL;
40 MPI_Errhandler* MPI_ERRORS_ARE_FATAL = NULL;
41 MPI_Errhandler* MPI_ERRHANDLER_NULL = NULL;
43 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
45 static char* get_mailbox_name(char* str, int index) {
46 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int)(sizeof(int) * 2), index);
50 static char* get_mailbox_name_small(char* str, int index) {
51 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int)(sizeof(int) * 2), index);
55 void smpi_process_init(int *argc, char ***argv)
58 smpi_process_data_t data;
62 proc = SIMIX_process_self();
63 index = atoi((*argv)[1]);
64 data = smpi_process_remote_data(index);
65 simcall_process_set_data(proc, data);
68 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
69 (*argv)[(*argc) - 1] = NULL;
74 simcall_rdv_set_receiver(data->mailbox_small, proc);// set the process attached to the mailbox
75 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
79 void smpi_process_destroy(void)
81 int index = smpi_process_index();
83 XBT_DEBUG("<%d> Process left the game", index);
87 * @brief Prepares the current process for termination.
89 void smpi_process_finalize(void)
91 // wait for all pending asynchronous comms to finish
92 while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
93 simcall_process_sleep(0.01);
98 int smpi_process_argc(void) {
99 smpi_process_data_t data = smpi_process_data();
101 return data->argc ? *(data->argc) - 1 : 0;
104 int smpi_process_getarg(integer* index, char* dst, ftnlen len) {
105 smpi_process_data_t data = smpi_process_data();
109 if(!data->argc || !data->argv
110 || *index < 1 || *index >= *(data->argc)) {
113 arg = (*data->argv)[*index];
114 for(i = 0; i < len && arg[i] != '\0'; i++) {
117 for(; i < len; i++) {
123 int smpi_global_size(void) {
124 char* value = getenv("SMPI_GLOBAL_SIZE");
127 fprintf(stderr, "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
134 smpi_process_data_t smpi_process_data(void)
136 return SIMIX_process_self_get_data(SIMIX_process_self());
139 smpi_process_data_t smpi_process_remote_data(int index)
141 return process_data[index];
144 void smpi_process_set_user_data(void *data)
146 smpi_process_data_t process_data = smpi_process_data();
147 process_data->data = data;
150 void* smpi_process_get_user_data(){
151 smpi_process_data_t process_data = smpi_process_data();
152 return process_data->data;
155 int smpi_process_count(void)
157 return process_count;
160 int smpi_process_index(void)
162 smpi_process_data_t data = smpi_process_data();
167 smx_rdv_t smpi_process_mailbox(void) {
168 smpi_process_data_t data = smpi_process_data();
170 return data->mailbox;
173 smx_rdv_t smpi_process_mailbox_small(void) {
174 smpi_process_data_t data = smpi_process_data();
176 return data->mailbox_small;
179 smx_rdv_t smpi_process_remote_mailbox(int index) {
180 smpi_process_data_t data = smpi_process_remote_data(index);
182 return data->mailbox;
186 smx_rdv_t smpi_process_remote_mailbox_small(int index) {
187 smpi_process_data_t data = smpi_process_remote_data(index);
189 return data->mailbox_small;
192 xbt_os_timer_t smpi_process_timer(void)
194 smpi_process_data_t data = smpi_process_data();
199 void smpi_process_simulated_start(void) {
200 smpi_process_data_t data = smpi_process_data();
202 data->simulated = SIMIX_get_clock();
205 double smpi_process_simulated_elapsed(void) {
206 smpi_process_data_t data = smpi_process_data();
208 return SIMIX_get_clock() - data->simulated;
211 MPI_Comm smpi_process_comm_self(void) {
212 smpi_process_data_t data = smpi_process_data();
214 return data->comm_self;
217 void print_request(const char *message, MPI_Request request) {
218 XBT_DEBUG("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
219 message, request, request->buf, request->size,
220 request->src, request->dst, request->tag, request->flags);
223 static void SMPI_comm_copy_buffer_callback(smx_action_t comm, void* buff, size_t buff_size)
225 XBT_DEBUG("Copy the data over");
226 memcpy(comm->comm.dst_buff, buff, buff_size);
227 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
229 //It seems that the request is used after the call there this should
230 //be free somewhereelse but where???
231 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
232 //inside the user data and should be free
233 comm->comm.src_buff = NULL;
237 void smpi_global_init(void)
241 char name[MAILBOX_NAME_MAXLEN];
243 SIMIX_comm_set_copy_data_callback(&SMPI_comm_copy_buffer_callback);
244 process_count = SIMIX_process_count();
245 process_data = xbt_new(smpi_process_data_t, process_count);
246 for (i = 0; i < process_count; i++) {
247 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
248 process_data[i]->index = i;
249 process_data[i]->argc = NULL;
250 process_data[i]->argv = NULL;
251 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
252 process_data[i]->mailbox_small = simcall_rdv_create(get_mailbox_name_small(name, i));
253 process_data[i]->timer = xbt_os_timer_new();
254 group = smpi_group_new(1);
255 process_data[i]->comm_self = smpi_comm_new(group);
256 smpi_group_set_mapping(group, i, 0);
258 group = smpi_group_new(process_count);
259 MPI_COMM_WORLD = smpi_comm_new(group);
260 for (i = 0; i < process_count; i++) {
261 smpi_group_set_mapping(group, i, i);
265 void smpi_global_destroy(void)
267 int count = smpi_process_count();
270 smpi_bench_destroy();
271 smpi_group_destroy(smpi_comm_group(MPI_COMM_WORLD));
272 smpi_comm_destroy(MPI_COMM_WORLD);
273 MPI_COMM_WORLD = MPI_COMM_NULL;
274 for (i = 0; i < count; i++) {
275 smpi_group_destroy(smpi_comm_group(process_data[i]->comm_self));
276 smpi_comm_destroy(process_data[i]->comm_self);
277 xbt_os_timer_free(process_data[i]->timer);
278 simcall_rdv_destroy(process_data[i]->mailbox);
279 simcall_rdv_destroy(process_data[i]->mailbox_small);
280 xbt_free(process_data[i]);
282 xbt_free(process_data);
288 /* Fortran specific stuff */
289 /* With smpicc, the following weak symbols are used */
290 /* With smpiff, the following weak symbols are replaced by those in libf2c */
291 int __attribute__((weak)) xargc;
292 char** __attribute__((weak)) xargv;
295 void __attribute__((weak)) user_main_(){
296 xbt_die("Should not be in this smpi_simulated_main");
299 int __attribute__((weak)) smpi_simulated_main_(int argc, char** argv) {
300 smpi_process_init(&argc, &argv);
302 //xbt_die("Should not be in this smpi_simulated_main");
306 int __attribute__((weak)) main(int argc, char** argv) {
307 return smpi_main(smpi_simulated_main_,argc,argv);
310 int __attribute__((weak)) MAIN__(){
311 return smpi_main(smpi_simulated_main_,xargc, xargv);
315 int smpi_main(int (*realmain) (int argc, char *argv[]),int argc, char *argv[])
317 srand(SMPI_RAND_SEED);
319 if(getenv("SMPI_PRETEND_CC") != NULL) {
320 /* Hack to ensure that smpicc can pretend to be a simple compiler. Particularly handy to pass it to the configuration tools */
324 /* Connect log categories. See xbt/log.c */
325 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
326 function: xbt_log_appender_file.c depends on it
327 DO NOT connect this in XBT or so, or it will be
328 useless to xbt_log_appender_file.c */
330 XBT_LOG_CONNECT(instr_smpi);
332 XBT_LOG_CONNECT(smpi_base);
333 XBT_LOG_CONNECT(smpi_bench);
334 XBT_LOG_CONNECT(smpi_coll);
335 XBT_LOG_CONNECT(smpi_comm);
336 XBT_LOG_CONNECT(smpi_group);
337 XBT_LOG_CONNECT(smpi_kernel);
338 XBT_LOG_CONNECT(smpi_mpi);
339 XBT_LOG_CONNECT(smpi_mpi_dt);
340 XBT_LOG_CONNECT(smpi_pmpi);
341 XBT_LOG_CONNECT(smpi_replay);
342 XBT_LOG_CONNECT(smpi_colls);
345 TRACE_global_init(&argc, argv);
347 TRACE_add_start_function(TRACE_smpi_alloc);
348 TRACE_add_end_function(TRACE_smpi_release);
351 SIMIX_global_init(&argc, argv);
357 // parse the platform file: get the host list
358 SIMIX_create_environment(argv[1]);
360 SIMIX_function_register_default(realmain);
361 SIMIX_launch_application(argv[2]);
363 int gather_id = find_coll_description(mpi_coll_gather_description,
364 sg_cfg_get_string("smpi/gather"));
365 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
366 void*, int, MPI_Datatype, int, MPI_Comm))
367 mpi_coll_gather_description[gather_id].coll;
369 int allgather_id = find_coll_description(mpi_coll_allgather_description,
370 sg_cfg_get_string("smpi/allgather"));
371 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
372 void*, int, MPI_Datatype, MPI_Comm))
373 mpi_coll_allgather_description[allgather_id].coll;
375 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
376 sg_cfg_get_string("smpi/allgatherv"));
377 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype,
378 void*, int*, int*, MPI_Datatype, MPI_Comm))
379 mpi_coll_allgatherv_description[allgatherv_id].coll;
381 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
382 sg_cfg_get_string("smpi/allreduce"));
383 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount, \
384 MPI_Datatype dtype, MPI_Op op, MPI_Comm comm))
385 mpi_coll_allreduce_description[allreduce_id].coll;
387 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
388 sg_cfg_get_string("smpi/alltoall"));
389 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
390 void*, int, MPI_Datatype, MPI_Comm))
391 mpi_coll_alltoall_description[alltoall_id].coll;
393 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
394 sg_cfg_get_string("smpi/alltoallv"));
395 mpi_coll_alltoallv_fun = (int (*)(void *, int*, int*, MPI_Datatype,
396 void*, int*, int*, MPI_Datatype, MPI_Comm))
397 mpi_coll_alltoallv_description[alltoallv_id].coll;
399 int bcast_id = find_coll_description(mpi_coll_bcast_description,
400 sg_cfg_get_string("smpi/bcast"));
401 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype, \
402 int root, MPI_Comm com))
403 mpi_coll_bcast_description[bcast_id].coll;
405 int reduce_id = find_coll_description(mpi_coll_reduce_description,
406 sg_cfg_get_string("smpi/reduce"));
407 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count, MPI_Datatype datatype, \
408 MPI_Op op, int root, MPI_Comm comm))
409 mpi_coll_reduce_description[reduce_id].coll;
411 int reduce_scatter_id = find_coll_description(mpi_coll_reduce_scatter_description,
412 sg_cfg_get_string("smpi/reduce_scatter"));
413 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,\
414 MPI_Datatype dtype,MPI_Op op,MPI_Comm comm))
415 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
418 /* Clean IO before the run */
423 MC_modelcheck_safety();
427 if (sg_cfg_get_boolean("smpi/display_timing"))
428 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
430 smpi_global_destroy();