1 /* Copyright (c) 2010, 2012-2015. 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 <simgrid/sg_config.h>
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
15 static xbt_dict_t keys;
17 static const char *smpi_colors[] ={
19 "irecv", "1 0.52 0.52",
21 "isend", "0.52 0.52 1",
24 "waitall", "0.78 0.78 0",
25 "waitany", "0.78 0.78 0.58",
26 "test", "0.52 0.52 0",
29 "allgatherv", "1 0.52 0.52",
31 "alltoall", "0.52 0 1",
32 "alltoallv", "0.78 0.52 1",
33 "barrier", "0 0.78 0.78",
34 "bcast", "0 0.78 0.39",
36 "gatherv", "1 1 0.52",
38 "reducescatter", "0.52 1 0.52",
39 "scan", "1 0.58 0.23",
40 "exscan", "1 0.54 0.25",
41 "scatterv", "0.52 0 0.52",
42 "scatter", "1 0.74 0.54",
45 "sleeping", "0 0.5 0.5",
52 "accumulate", "1 0.3 0",
53 "win_fence", "1 0 0.3",
54 "win_post", "1 0 0.8",
55 "win_wait", "1 0.8 0",
56 "win_start", "0.8 0 1",
57 "win_complete", "0.8 1 0",
61 static char *str_tolower (const char *str)
63 char *ret = xbt_strdup (str);
64 int i, n = strlen (ret);
65 for (i = 0; i < n; i++)
66 ret[i] = tolower (str[i]);
70 static const char *instr_find_color (const char *state)
72 char *target = str_tolower (state);
73 const char *ret = NULL;
76 while ((current = smpi_colors[i])){
77 if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
78 if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
85 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
87 snprintf(container, n, "rank-%d", rank);
91 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
93 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
95 //get the dynar for src#dst
96 char aux[INSTR_DEFAULT_STR_SIZE];
97 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
98 xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
100 if(!xbt_dynar_is_empty(d)){
101 //receive was already pushed, perform a get instead
102 TRACE_smpi_get_key(src , dst, key ,n);
107 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
108 xbt_dict_set(keys, aux, d, NULL);
112 static unsigned long long counter = 0;
114 snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
117 char *a = (char*)xbt_strdup(key);
118 xbt_dynar_push_as(d, char *, a);
123 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
125 char aux[INSTR_DEFAULT_STR_SIZE];
126 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
127 xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
129 // xbt_assert(!xbt_dynar_is_empty(d),
130 // "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
132 // sometimes the receive may be posted before the send
133 if(xbt_dynar_is_empty(d)){
134 TRACE_smpi_put_key(src, dst, key, n);
138 char *s = xbt_dynar_get_as (d, 0, char *);
139 snprintf (key, n, "%s", s);
140 xbt_dynar_remove_at (d, 0, NULL);
144 static xbt_dict_t process_category;
146 static void cleanup_extra_data (instr_extra_data extra){
148 if(extra->sendcounts!=NULL)
149 xbt_free(extra->sendcounts);
150 if(extra->recvcounts!=NULL)
151 xbt_free(extra->recvcounts);
156 void TRACE_internal_smpi_set_category (const char *category)
158 if (!TRACE_smpi_is_enabled()) return;
161 TRACE_category (category);
163 char processid[INSTR_DEFAULT_STR_SIZE];
164 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
165 if (xbt_dict_get_or_null (process_category, processid))
166 xbt_dict_remove (process_category, processid);
167 if (category != NULL)
168 xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
171 const char *TRACE_internal_smpi_get_category (void)
173 if (!TRACE_smpi_is_enabled()) return NULL;
175 char processid[INSTR_DEFAULT_STR_SIZE];
176 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
177 return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
180 void TRACE_smpi_alloc()
182 keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
183 process_category = xbt_dict_new_homogeneous(xbt_free_f);
186 void TRACE_smpi_release(void)
188 xbt_dict_free(&keys);
189 xbt_dict_free(&process_category);
192 void TRACE_smpi_init(int rank)
194 if (!TRACE_smpi_is_enabled()) return;
196 char str[INSTR_DEFAULT_STR_SIZE];
197 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
200 if (TRACE_smpi_is_grouped()){
201 father = PJ_container_get (SIMIX_host_self_get_name());
203 father = PJ_container_get_root ();
205 xbt_assert(father!=NULL,
206 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
207 PJ_container_new(str, INSTR_SMPI, father);
210 void TRACE_smpi_finalize(int rank)
212 if (!TRACE_smpi_is_enabled()) return;
214 char str[INSTR_DEFAULT_STR_SIZE];
215 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
216 PJ_container_remove_from_parent (container);
217 PJ_container_free (container);
220 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
222 if (!TRACE_smpi_is_enabled()) {
223 cleanup_extra_data(extra);
227 char str[INSTR_DEFAULT_STR_SIZE];
228 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
229 container_t container = PJ_container_get (str);
230 type_t type = PJ_type_get ("MPI_STATE", container->type);
231 const char *color = instr_find_color (operation);
232 val_t value = PJ_value_get_or_new (operation, color, type);
233 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
236 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
238 if (!TRACE_smpi_is_enabled()) return;
240 char str[INSTR_DEFAULT_STR_SIZE];
241 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
242 container_t container = PJ_container_get (str);
243 type_t type = PJ_type_get ("MPI_STATE", container->type);
245 new_pajePopState (SIMIX_get_clock(), container, type);
248 void TRACE_smpi_computing_init(int rank)
250 //first use, initialize the color in the trace
251 //TODO : check with lucas and Pierre how to generalize this approach
252 //to avoid unnecessary access to the color array
253 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
255 char str[INSTR_DEFAULT_STR_SIZE];
256 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
257 container_t container = PJ_container_get (str);
258 type_t type = PJ_type_get ("MPI_STATE", container->type);
259 const char *color = instr_find_color ("computing");
260 val_t value = PJ_value_get_or_new ("computing", color, type);
261 new_pajePushState (SIMIX_get_clock(), container, type, value);
264 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
266 //do not forget to set the color first, otherwise this will explode
267 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
268 cleanup_extra_data(extra);
272 char str[INSTR_DEFAULT_STR_SIZE];
273 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
274 container_t container = PJ_container_get (str);
275 type_t type = PJ_type_get ("MPI_STATE", container->type);
276 val_t value = PJ_value_get_or_new ("computing", NULL, type);
277 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
280 void TRACE_smpi_computing_out(int rank)
282 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
283 char str[INSTR_DEFAULT_STR_SIZE];
284 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
285 container_t container = PJ_container_get (str);
286 type_t type = PJ_type_get ("MPI_STATE", container->type);
287 new_pajePopState (SIMIX_get_clock(), container, type);
290 void TRACE_smpi_sleeping_init(int rank)
292 //first use, initialize the color in the trace
293 //TODO : check with lucas and Pierre how to generalize this approach
294 //to avoid unnecessary access to the color array
295 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping()) return;
297 char str[INSTR_DEFAULT_STR_SIZE];
298 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
299 container_t container = PJ_container_get (str);
300 type_t type = PJ_type_get ("MPI_STATE", container->type);
301 const char *color = instr_find_color ("sleeping");
302 val_t value = PJ_value_get_or_new ("sleeping", color, type);
303 new_pajePushState (SIMIX_get_clock(), container, type, value);
306 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
308 //do not forget to set the color first, otherwise this will explode
309 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
310 cleanup_extra_data(extra);
314 char str[INSTR_DEFAULT_STR_SIZE];
315 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
316 container_t container = PJ_container_get (str);
317 type_t type = PJ_type_get ("MPI_STATE", container->type);
318 val_t value = PJ_value_get_or_new ("sleeping", NULL, type);
319 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
322 void TRACE_smpi_sleeping_out(int rank)
324 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) return;
325 char str[INSTR_DEFAULT_STR_SIZE];
326 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
327 container_t container = PJ_container_get (str);
328 type_t type = PJ_type_get ("MPI_STATE", container->type);
329 new_pajePopState (SIMIX_get_clock(), container, type);
332 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
334 //do not forget to set the color first, otherwise this will explode
335 if (!TRACE_smpi_is_enabled()) {
336 cleanup_extra_data(extra);
340 char str[INSTR_DEFAULT_STR_SIZE];
341 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
342 container_t container = PJ_container_get (str);
343 type_t type = PJ_type_get ("MPI_STATE", container->type);
344 val_t value = PJ_value_get_or_new ("test", NULL, type);
345 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
348 void TRACE_smpi_testing_out(int rank)
350 if (!TRACE_smpi_is_enabled()) return;
351 char str[INSTR_DEFAULT_STR_SIZE];
352 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
353 container_t container = PJ_container_get (str);
354 type_t type = PJ_type_get ("MPI_STATE", container->type);
355 new_pajePopState (SIMIX_get_clock(), container, type);
358 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
360 if (!TRACE_smpi_is_enabled()) {
361 cleanup_extra_data(extra);
365 char str[INSTR_DEFAULT_STR_SIZE];
366 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
367 container_t container = PJ_container_get (str);
368 type_t type = PJ_type_get ("MPI_STATE", container->type);
369 const char *color = instr_find_color (operation);
370 val_t value = PJ_value_get_or_new (operation, color, type);
371 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
374 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
376 if (!TRACE_smpi_is_enabled()) return;
378 char str[INSTR_DEFAULT_STR_SIZE];
379 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
380 container_t container = PJ_container_get (str);
381 type_t type = PJ_type_get ("MPI_STATE", container->type);
383 new_pajePopState (SIMIX_get_clock(), container, type);
386 void TRACE_smpi_send(int rank, int src, int dst, int size)
388 if (!TRACE_smpi_is_enabled()) return;
390 char key[INSTR_DEFAULT_STR_SIZE] = {0};
391 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
393 char str[INSTR_DEFAULT_STR_SIZE];
394 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
395 container_t container = PJ_container_get (str);
396 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
398 new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
401 void TRACE_smpi_recv(int rank, int src, int dst)
403 if (!TRACE_smpi_is_enabled()) return;
405 char key[INSTR_DEFAULT_STR_SIZE] = {0};
406 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
408 char str[INSTR_DEFAULT_STR_SIZE];
409 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
410 container_t container = PJ_container_get (str);
411 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
413 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);