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);
93 int smpi_process_argc(void) {
94 smpi_process_data_t data = smpi_process_data();
96 return data->argc ? *(data->argc) - 1 : 0;
99 int smpi_process_getarg(integer* index, char* dst, ftnlen len) {
100 smpi_process_data_t data = smpi_process_data();
104 if(!data->argc || !data->argv
105 || *index < 1 || *index >= *(data->argc)) {
108 arg = (*data->argv)[*index];
109 for(i = 0; i < len && arg[i] != '\0'; i++) {
112 for(; i < len; i++) {
118 int smpi_global_size(void) {
119 char* value = getenv("SMPI_GLOBAL_SIZE");
122 fprintf(stderr, "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
129 smpi_process_data_t smpi_process_data(void)
131 return SIMIX_process_self_get_data(SIMIX_process_self());
134 smpi_process_data_t smpi_process_remote_data(int index)
136 return process_data[index];
139 void smpi_process_set_user_data(void *data)
141 smpi_process_data_t process_data = smpi_process_data();
142 process_data->data = data;
145 void* smpi_process_get_user_data(){
146 smpi_process_data_t process_data = smpi_process_data();
147 return process_data->data;
150 int smpi_process_count(void)
152 return process_count;
155 int smpi_process_index(void)
157 smpi_process_data_t data = smpi_process_data();
162 smx_rdv_t smpi_process_mailbox(void) {
163 smpi_process_data_t data = smpi_process_data();
165 return data->mailbox;
168 smx_rdv_t smpi_process_mailbox_small(void) {
169 smpi_process_data_t data = smpi_process_data();
171 return data->mailbox_small;
174 smx_rdv_t smpi_process_remote_mailbox(int index) {
175 smpi_process_data_t data = smpi_process_remote_data(index);
177 return data->mailbox;
181 smx_rdv_t smpi_process_remote_mailbox_small(int index) {
182 smpi_process_data_t data = smpi_process_remote_data(index);
184 return data->mailbox_small;
187 xbt_os_timer_t smpi_process_timer(void)
189 smpi_process_data_t data = smpi_process_data();
194 void smpi_process_simulated_start(void) {
195 smpi_process_data_t data = smpi_process_data();
197 data->simulated = SIMIX_get_clock();
200 double smpi_process_simulated_elapsed(void) {
201 smpi_process_data_t data = smpi_process_data();
203 return SIMIX_get_clock() - data->simulated;
206 MPI_Comm smpi_process_comm_self(void) {
207 smpi_process_data_t data = smpi_process_data();
209 return data->comm_self;
212 void print_request(const char *message, MPI_Request request) {
213 XBT_DEBUG("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
214 message, request, request->buf, request->size,
215 request->src, request->dst, request->tag, request->flags);
218 static void SMPI_comm_copy_buffer_callback(smx_action_t comm, void* buff, size_t buff_size)
220 XBT_DEBUG("Copy the data over");
221 memcpy(comm->comm.dst_buff, buff, buff_size);
222 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
224 //It seems that the request is used after the call there this should
225 //be free somewhereelse but where???
226 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
227 //inside the user data and should be free
228 comm->comm.src_buff = NULL;
232 void smpi_global_init(void)
236 char name[MAILBOX_NAME_MAXLEN];
238 SIMIX_comm_set_copy_data_callback(&SMPI_comm_copy_buffer_callback);
239 process_count = SIMIX_process_count();
240 process_data = xbt_new(smpi_process_data_t, process_count);
241 for (i = 0; i < process_count; i++) {
242 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
243 process_data[i]->index = i;
244 process_data[i]->argc = NULL;
245 process_data[i]->argv = NULL;
246 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
247 process_data[i]->mailbox_small = simcall_rdv_create(get_mailbox_name_small(name, i));
248 process_data[i]->timer = xbt_os_timer_new();
249 group = smpi_group_new(1);
250 process_data[i]->comm_self = smpi_comm_new(group);
251 smpi_group_set_mapping(group, i, 0);
253 group = smpi_group_new(process_count);
254 MPI_COMM_WORLD = smpi_comm_new(group);
255 for (i = 0; i < process_count; i++) {
256 smpi_group_set_mapping(group, i, i);
260 void smpi_global_destroy(void)
262 int count = smpi_process_count();
265 smpi_bench_destroy();
266 smpi_group_destroy(smpi_comm_group(MPI_COMM_WORLD));
267 smpi_comm_destroy(MPI_COMM_WORLD);
268 MPI_COMM_WORLD = MPI_COMM_NULL;
269 for (i = 0; i < count; i++) {
270 smpi_comm_destroy(process_data[i]->comm_self);
271 xbt_os_timer_free(process_data[i]->timer);
272 simcall_rdv_destroy(process_data[i]->mailbox);
273 simcall_rdv_destroy(process_data[i]->mailbox_small);
274 xbt_free(process_data[i]);
276 xbt_free(process_data);
282 /* Fortran specific stuff */
283 /* With smpicc, the following weak symbols are used */
284 /* With smpiff, the following weak symbols are replaced by those in libf2c */
285 int __attribute__((weak)) smpi_simulated_main(int argc, char** argv) {
286 xbt_die("Should not be in this smpi_simulated_main");
290 int __attribute__((weak)) main(int argc, char** argv) {
291 return MAIN__(smpi_simulated_main,argc,argv);
294 int MAIN__(int (*realmain) (int argc, char *argv[]),int argc, char *argv[])
296 srand(SMPI_RAND_SEED);
298 if(getenv("SMPI_PRETEND_CC") != NULL) {
299 /* Hack to ensure that smpicc can pretend to be a simple compiler. Particularly handy to pass it to the configuration tools */
303 /* Connect log categories. See xbt/log.c */
304 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this function: xbt_log_appender_file.c depends on it
305 DO NOT connect this in XBT or so, or it will be useless to xbt_log_appender_file.c */
306 XBT_LOG_CONNECT(smpi_base);
307 XBT_LOG_CONNECT(smpi_bench);
308 XBT_LOG_CONNECT(smpi_coll);
309 XBT_LOG_CONNECT(smpi_comm);
310 XBT_LOG_CONNECT(smpi_group);
311 XBT_LOG_CONNECT(smpi_kernel);
312 XBT_LOG_CONNECT(smpi_mpi);
313 XBT_LOG_CONNECT(smpi_mpi_dt);
314 XBT_LOG_CONNECT(smpi_pmpi);
315 XBT_LOG_CONNECT(smpi_replay);
318 TRACE_global_init(&argc, argv);
321 SIMIX_global_init(&argc, argv);
327 // parse the platform file: get the host list
328 SIMIX_create_environment(argv[1]);
330 SIMIX_function_register_default(realmain);
331 SIMIX_launch_application(argv[2]);
335 /* Clean IO before the run */
344 if (surf_cfg_get_int("smpi/display_timing"))
345 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
347 smpi_global_destroy();