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"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
19 "Logging specific to SMPI (kernel)");
21 typedef struct s_smpi_process_data {
26 smx_rdv_t mailbox_small;
30 void *data; /* user data */
31 } s_smpi_process_data_t;
33 static smpi_process_data_t *process_data = NULL;
34 static int process_count = 0;
36 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
38 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
40 static char* get_mailbox_name(char* str, int index) {
41 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int)(sizeof(int) * 2), index);
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);
50 void smpi_process_init(int *argc, char ***argv)
53 smpi_process_data_t data;
57 proc = SIMIX_process_self();
58 index = atoi((*argv)[1]);
59 data = smpi_process_remote_data(index);
60 simcall_process_set_data(proc, data);
63 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
64 (*argv)[(*argc) - 1] = NULL;
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);
74 void smpi_process_destroy(void)
76 int index = smpi_process_index();
78 XBT_DEBUG("<%d> Process left the game", index);
82 * @brief Prepares the current process for termination.
84 void smpi_process_finalize(void)
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);
92 int smpi_process_argc(void) {
93 smpi_process_data_t data = smpi_process_data();
95 return data->argc ? *(data->argc) - 1 : 0;
98 int smpi_process_getarg(integer* index, char* dst, ftnlen len) {
99 smpi_process_data_t data = smpi_process_data();
103 if(!data->argc || !data->argv
104 || *index < 1 || *index >= *(data->argc)) {
107 arg = (*data->argv)[*index];
108 for(i = 0; i < len && arg[i] != '\0'; i++) {
111 for(; i < len; i++) {
117 int smpi_global_size(void) {
118 char* value = getenv("SMPI_GLOBAL_SIZE");
121 fprintf(stderr, "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
127 smpi_process_data_t smpi_process_data(void)
129 return SIMIX_process_self_get_data(SIMIX_process_self());
132 smpi_process_data_t smpi_process_remote_data(int index)
134 return process_data[index];
137 void smpi_process_set_user_data(void *data)
139 smpi_process_data_t process_data = smpi_process_data();
140 process_data->data = data;
143 void* smpi_process_get_user_data(){
144 smpi_process_data_t process_data = smpi_process_data();
145 return process_data->data;
148 int smpi_process_count(void)
150 return process_count;
153 int smpi_process_index(void)
155 smpi_process_data_t data = smpi_process_data();
160 smx_rdv_t smpi_process_mailbox(void) {
161 smpi_process_data_t data = smpi_process_data();
163 return data->mailbox;
166 smx_rdv_t smpi_process_mailbox_small(void) {
167 smpi_process_data_t data = smpi_process_data();
169 return data->mailbox_small;
172 smx_rdv_t smpi_process_remote_mailbox(int index) {
173 smpi_process_data_t data = smpi_process_remote_data(index);
175 return data->mailbox;
179 smx_rdv_t smpi_process_remote_mailbox_small(int index) {
180 smpi_process_data_t data = smpi_process_remote_data(index);
182 return data->mailbox_small;
185 xbt_os_timer_t smpi_process_timer(void)
187 smpi_process_data_t data = smpi_process_data();
192 void smpi_process_simulated_start(void) {
193 smpi_process_data_t data = smpi_process_data();
195 data->simulated = SIMIX_get_clock();
198 double smpi_process_simulated_elapsed(void) {
199 smpi_process_data_t data = smpi_process_data();
201 return SIMIX_get_clock() - data->simulated;
204 MPI_Comm smpi_process_comm_self(void) {
205 smpi_process_data_t data = smpi_process_data();
207 return data->comm_self;
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);
216 void smpi_global_init(void)
220 char name[MAILBOX_NAME_MAXLEN];
222 SIMIX_comm_set_copy_data_callback(&SIMIX_comm_copy_buffer_callback);
223 process_count = SIMIX_process_count();
224 process_data = xbt_new(smpi_process_data_t, process_count);
225 for (i = 0; i < process_count; i++) {
226 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
227 process_data[i]->index = i;
228 process_data[i]->argc = NULL;
229 process_data[i]->argv = NULL;
230 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
231 process_data[i]->mailbox_small = simcall_rdv_create(get_mailbox_name_small(name, i));
232 process_data[i]->timer = xbt_os_timer_new();
233 group = smpi_group_new(1);
234 process_data[i]->comm_self = smpi_comm_new(group);
235 smpi_group_set_mapping(group, i, 0);
237 group = smpi_group_new(process_count);
238 MPI_COMM_WORLD = smpi_comm_new(group);
239 for (i = 0; i < process_count; i++) {
240 smpi_group_set_mapping(group, i, i);
244 void smpi_global_destroy(void)
246 int count = smpi_process_count();
249 smpi_bench_destroy();
250 smpi_comm_destroy(MPI_COMM_WORLD);
251 MPI_COMM_WORLD = MPI_COMM_NULL;
252 for (i = 0; i < count; i++) {
253 smpi_comm_destroy(process_data[i]->comm_self);
254 xbt_os_timer_free(process_data[i]->timer);
255 simcall_rdv_destroy(process_data[i]->mailbox);
256 simcall_rdv_destroy(process_data[i]->mailbox_small);
257 xbt_free(process_data[i]);
259 xbt_free(process_data);
265 /* Fortran specific stuff */
266 /* With smpicc, the following weak symbols are used */
267 /* With smpiff, the following weak symbols are replaced by those in libf2c */
268 int __attribute__((weak)) xargc;
269 char** __attribute__((weak)) xargv;
271 int __attribute__((weak)) main(int argc, char** argv) {
279 srand(SMPI_RAND_SEED);
281 if(getenv("SMPI_PRETEND_CC") != NULL) {
282 /* Hack to ensure that smpicc can pretend to be a simple compiler. Particularly handy to pass it to the configuration tools */
286 /* Connect log categories. See xbt/log.c */
287 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this function: xbt_log_appender_file.c depends on it
288 DO NOT connect this in XBT or so, or it will be useless to xbt_log_appender_file.c */
289 XBT_LOG_CONNECT(smpi_base);
290 XBT_LOG_CONNECT(smpi_bench);
291 XBT_LOG_CONNECT(smpi_coll);
292 XBT_LOG_CONNECT(smpi_comm);
293 XBT_LOG_CONNECT(smpi_group);
294 XBT_LOG_CONNECT(smpi_kernel);
295 XBT_LOG_CONNECT(smpi_mpi);
296 XBT_LOG_CONNECT(smpi_mpi_dt);
297 XBT_LOG_CONNECT(smpi_pmpi);
300 TRACE_global_init(&xargc, xargv);
303 SIMIX_global_init(&xargc, xargv);
309 // parse the platform file: get the host list
310 SIMIX_create_environment(xargv[1]);
312 SIMIX_function_register_default(smpi_simulated_main);
313 SIMIX_launch_application(xargv[2]);
317 /* Clean IO before the run */
326 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
327 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
329 smpi_global_destroy();