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. */
11 #include "smpi_mpi_dt_private.h"
13 #include "surf/surf.h"
15 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
18 "Logging specific to SMPI (kernel)");
20 typedef struct s_smpi_process_data {
28 } s_smpi_process_data_t;
30 static smpi_process_data_t *process_data = NULL;
31 static int process_count = 0;
33 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
35 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
37 static char* get_mailbox_name(char* str, int index) {
38 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int)(sizeof(int) * 2), index);
42 void smpi_process_init(int *argc, char ***argv)
45 smpi_process_data_t data;
49 proc = SIMIX_process_self();
50 index = atoi((*argv)[1]);
51 data = smpi_process_remote_data(index);
52 SIMIX_req_process_set_data(proc, data);
55 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
56 (*argv)[(*argc) - 1] = NULL;
61 DEBUG2("<%d> New process in the game: %p", index, proc);
65 void smpi_process_destroy(void)
67 int index = smpi_process_index();
69 DEBUG1("<%d> Process left the game", index);
72 int smpi_process_argc(void) {
73 smpi_process_data_t data = smpi_process_data();
75 return data->argc ? *(data->argc) - 1 : 0;
78 int smpi_process_getarg(int* index, char* dst, size_t len) {
79 smpi_process_data_t data = smpi_process_data();
83 if(!data->argc || !data->argv
84 || *index < 1 || *index >= *(data->argc)) {
87 arg = (*data->argv)[*index];
88 for(i = 0; i < len && arg[i] != '\0'; i++) {
97 int smpi_global_rank(void) {
98 return smpi_process_index();
101 int smpi_global_size(void) {
102 char* value = getenv("SMPI_GLOBAL_SIZE");
110 smpi_process_data_t smpi_process_data(void)
112 return SIMIX_req_process_get_data(SIMIX_process_self());
115 smpi_process_data_t smpi_process_remote_data(int index)
117 return process_data[index];
120 int smpi_process_count(void)
122 return process_count;
125 int smpi_process_index(void)
127 smpi_process_data_t data = smpi_process_data();
132 smx_rdv_t smpi_process_mailbox(void) {
133 smpi_process_data_t data = smpi_process_data();
135 return data->mailbox;
138 smx_rdv_t smpi_process_remote_mailbox(int index) {
139 smpi_process_data_t data = smpi_process_remote_data(index);
141 return data->mailbox;
144 xbt_os_timer_t smpi_process_timer(void)
146 smpi_process_data_t data = smpi_process_data();
151 void smpi_process_simulated_start(void)
153 smpi_process_data_t data = smpi_process_data();
155 data->simulated = SIMIX_get_clock();
158 double smpi_process_simulated_elapsed(void)
160 smpi_process_data_t data = smpi_process_data();
162 return SIMIX_get_clock() - data->simulated;
165 MPI_Comm smpi_process_comm_self(void)
167 smpi_process_data_t data = smpi_process_data();
169 return data->comm_self;
172 void print_request(const char *message, MPI_Request request)
174 DEBUG8("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
175 message, request, request->buf, request->size,
176 request->src, request->dst, request->tag, request->flags);
179 void smpi_global_init(void)
183 char name[MAILBOX_NAME_MAXLEN];
185 SIMIX_comm_set_copy_data_callback
186 (&SIMIX_comm_copy_buffer_callback);
187 process_count = SIMIX_process_count();
188 process_data = xbt_new(smpi_process_data_t, process_count);
189 for (i = 0; i < process_count; i++) {
190 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
191 process_data[i]->index = i;
192 process_data[i]->argc = NULL;
193 process_data[i]->argv = NULL;
194 process_data[i]->mailbox = SIMIX_req_rdv_create(get_mailbox_name(name, i));
195 process_data[i]->timer = xbt_os_timer_new();
196 group = smpi_group_new(1);
197 process_data[i]->comm_self = smpi_comm_new(group);
198 smpi_group_set_mapping(group, i, 0);
200 group = smpi_group_new(process_count);
201 MPI_COMM_WORLD = smpi_comm_new(group);
202 for (i = 0; i < process_count; i++) {
203 smpi_group_set_mapping(group, i, i);
207 void smpi_global_destroy(void)
209 int count = smpi_process_count();
212 smpi_bench_destroy();
213 smpi_comm_destroy(MPI_COMM_WORLD);
214 MPI_COMM_WORLD = MPI_COMM_NULL;
215 for (i = 0; i < count; i++) {
216 smpi_comm_destroy(process_data[i]->comm_self);
217 xbt_os_timer_free(process_data[i]->timer);
218 SIMIX_req_rdv_destroy(process_data[i]->mailbox);
219 xbt_free(process_data[i]);
221 xbt_free(process_data);
225 /* Fortran specific stuff */
226 /* With smpicc, the following weak symbols are used */
227 /* With smpiff, the following weak symbols are replaced by those in libf2c */
228 int __attribute__((weak)) xargc;
229 char** __attribute__((weak)) xargv;
231 int __attribute__((weak)) main(int argc, char** argv) {
239 srand(SMPI_RAND_SEED);
241 double default_reference_speed = 20000.0;
242 xbt_cfg_register(&_surf_cfg_set, "smpi/running_power",
243 "Power of the host running the simulation (in flop/s). Used to bench the operations.",
244 xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL,
247 int default_display_timing = 0;
248 xbt_cfg_register(&_surf_cfg_set, "smpi/display_timing",
249 "Boolean indicating whether we should display the timing after simulation.",
250 xbt_cfgelm_int, &default_display_timing, 1, 1, NULL,
253 double default_threshold = 1e-6;
254 xbt_cfg_register(&_surf_cfg_set, "smpi/cpu_threshold",
255 "Minimal computation time (in seconds) not discarded.",
256 xbt_cfgelm_double, &default_threshold, 1, 1, NULL,
260 TRACE_global_init(&xargc, xargv);
263 SIMIX_global_init(&xargc, xargv);
269 // parse the platform file: get the host list
270 SIMIX_create_environment(xargv[1]);
272 SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
273 SIMIX_launch_application(xargv[2]);
277 /* Clean IO before the run */
286 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
287 INFO1("simulation time %g", SIMIX_get_clock());
289 smpi_global_destroy();
292 TRACE_smpi_release();