1 /* Copyright (c) 2010, 2012-2014. 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>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
18 static xbt_dict_t keys;
20 static const char *smpi_colors[] ={
22 "irecv", "1 0.52 0.52",
24 "isend", "0.52 0.52 1",
27 "waitall", "0.78 0.78 0",
28 "waitany", "0.78 0.78 0.58",
29 "test", "0.52 0.52 0",
32 "allgatherv", "1 0.52 0.52",
34 "alltoall", "0.52 0 1",
35 "alltoallv", "0.78 0.52 1",
36 "barrier", "0 0.78 0.78",
37 "bcast", "0 0.78 0.39",
39 "gatherv", "1 1 0.52",
41 "reducescatter", "0.52 1 0.52",
42 "scan", "1 0.58 0.23",
43 "exscan", "1 0.54 0.25",
44 "scatterv", "0.52 0 0.52",
45 "scatter", "1 0.74 0.54",
48 "sleeping", "0 0.5 0.5",
55 "accumulate", "1 0.3 0",
56 "win_fence", "1 0 0.3",
57 "win_post", "1 0 0.8",
58 "win_wait", "1 0.8 0",
59 "win_start", "0.8 0 1",
60 "win_complete", "0.8 1 0",
64 static char *str_tolower (const char *str)
66 char *ret = xbt_strdup (str);
67 int i, n = strlen (ret);
68 for (i = 0; i < n; i++)
69 ret[i] = tolower (str[i]);
73 static const char *instr_find_color (const char *state)
75 char *target = str_tolower (state);
76 const char *ret = NULL;
79 while ((current = smpi_colors[i])){
80 if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
81 if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
89 static char *smpi_container(int rank, char *container, int n)
91 snprintf(container, n, "rank-%d", rank);
95 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
98 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
100 //get the dynar for src#dst
101 char aux[INSTR_DEFAULT_STR_SIZE];
102 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
103 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
106 if(!xbt_dynar_is_empty(d)){
107 //receive was already pushed, perform a get instead
108 TRACE_smpi_get_key(src , dst, key ,n);
113 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
114 xbt_dict_set(keys, aux, d, NULL);
118 static unsigned long long counter = 0;
120 snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
123 char *a = (char*)xbt_strdup(key);
124 xbt_dynar_push_as(d, char *, a);
129 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
131 char aux[INSTR_DEFAULT_STR_SIZE];
132 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
133 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
135 // xbt_assert(!xbt_dynar_is_empty(d),
136 // "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
138 // sometimes the receive may be posted before the send
139 if(xbt_dynar_is_empty(d)){
140 TRACE_smpi_put_key(src, dst, key, n);
144 char *s = xbt_dynar_get_as (d, 0, char *);
145 snprintf (key, n, "%s", s);
146 xbt_dynar_remove_at (d, 0, NULL);
150 static xbt_dict_t process_category;
152 static void cleanup_extra_data (instr_extra_data extra){
154 if(extra->sendcounts!=NULL)
155 xbt_free(extra->sendcounts);
156 if(extra->recvcounts!=NULL)
157 xbt_free(extra->recvcounts);
162 void TRACE_internal_smpi_set_category (const char *category)
164 if (!TRACE_smpi_is_enabled()) return;
167 TRACE_category (category);
169 char processid[INSTR_DEFAULT_STR_SIZE];
170 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
171 if (xbt_dict_get_or_null (process_category, processid))
172 xbt_dict_remove (process_category, processid);
173 if (category != NULL)
174 xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
177 const char *TRACE_internal_smpi_get_category (void)
179 if (!TRACE_smpi_is_enabled()) return NULL;
181 char processid[INSTR_DEFAULT_STR_SIZE];
182 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
183 return xbt_dict_get_or_null (process_category, processid);
186 void TRACE_smpi_alloc()
188 keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
189 process_category = xbt_dict_new_homogeneous(xbt_free_f);
192 void TRACE_smpi_release(void)
194 xbt_dict_free(&keys);
195 xbt_dict_free(&process_category);
198 void TRACE_smpi_init(int rank)
200 if (!TRACE_smpi_is_enabled()) return;
202 char str[INSTR_DEFAULT_STR_SIZE];
203 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
206 if (TRACE_smpi_is_grouped()){
207 father = PJ_container_get (SIMIX_host_self_get_name());
209 father = PJ_container_get_root ();
211 xbt_assert(father!=NULL,
212 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
213 PJ_container_new(str, INSTR_SMPI, father);
216 void TRACE_smpi_finalize(int rank)
218 if (!TRACE_smpi_is_enabled()) return;
220 char str[INSTR_DEFAULT_STR_SIZE];
221 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
222 PJ_container_remove_from_parent (container);
223 PJ_container_free (container);
226 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
228 if (!TRACE_smpi_is_enabled()) {
229 cleanup_extra_data(extra);
233 char str[INSTR_DEFAULT_STR_SIZE];
234 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
235 container_t container = PJ_container_get (str);
236 type_t type = PJ_type_get ("MPI_STATE", container->type);
237 const char *color = instr_find_color (operation);
238 val_t value = PJ_value_get_or_new (operation, color, type);
239 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
243 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
245 if (!TRACE_smpi_is_enabled()) return;
247 char str[INSTR_DEFAULT_STR_SIZE];
248 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
249 container_t container = PJ_container_get (str);
250 type_t type = PJ_type_get ("MPI_STATE", container->type);
252 new_pajePopState (SIMIX_get_clock(), container, type);
255 void TRACE_smpi_computing_init(int rank)
257 //first use, initialize the color in the trace
258 //TODO : check with lucas and Pierre how to generalize this approach
259 //to avoid unnecessary access to the color array
260 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
262 char str[INSTR_DEFAULT_STR_SIZE];
263 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
264 container_t container = PJ_container_get (str);
265 type_t type = PJ_type_get ("MPI_STATE", container->type);
266 const char *color = instr_find_color ("computing");
267 val_t value = PJ_value_get_or_new ("computing", color, type);
268 new_pajePushState (SIMIX_get_clock(), container, type, value);
271 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
273 //do not forget to set the color first, otherwise this will explode
274 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
275 cleanup_extra_data(extra);
279 char str[INSTR_DEFAULT_STR_SIZE];
280 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
281 container_t container = PJ_container_get (str);
282 type_t type = PJ_type_get ("MPI_STATE", container->type);
283 val_t value = PJ_value_get_or_new ("computing", NULL, type);
284 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
287 void TRACE_smpi_computing_out(int rank)
289 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
290 char str[INSTR_DEFAULT_STR_SIZE];
291 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
292 container_t container = PJ_container_get (str);
293 type_t type = PJ_type_get ("MPI_STATE", container->type);
294 new_pajePopState (SIMIX_get_clock(), container, type);
297 void TRACE_smpi_sleeping_init(int rank)
299 //first use, initialize the color in the trace
300 //TODO : check with lucas and Pierre how to generalize this approach
301 //to avoid unnecessary access to the color array
302 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping()) return;
304 char str[INSTR_DEFAULT_STR_SIZE];
305 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
306 container_t container = PJ_container_get (str);
307 type_t type = PJ_type_get ("MPI_STATE", container->type);
308 const char *color = instr_find_color ("sleeping");
309 val_t value = PJ_value_get_or_new ("sleeping", color, type);
310 new_pajePushState (SIMIX_get_clock(), container, type, value);
313 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
315 //do not forget to set the color first, otherwise this will explode
316 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
317 cleanup_extra_data(extra);
321 char str[INSTR_DEFAULT_STR_SIZE];
322 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
323 container_t container = PJ_container_get (str);
324 type_t type = PJ_type_get ("MPI_STATE", container->type);
325 val_t value = PJ_value_get_or_new ("sleeping", NULL, type);
326 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
329 void TRACE_smpi_sleeping_out(int rank)
331 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) return;
332 char str[INSTR_DEFAULT_STR_SIZE];
333 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
334 container_t container = PJ_container_get (str);
335 type_t type = PJ_type_get ("MPI_STATE", container->type);
336 new_pajePopState (SIMIX_get_clock(), container, type);
340 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
342 //do not forget to set the color first, otherwise this will explode
343 if (!TRACE_smpi_is_enabled()) {
344 cleanup_extra_data(extra);
348 char str[INSTR_DEFAULT_STR_SIZE];
349 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
350 container_t container = PJ_container_get (str);
351 type_t type = PJ_type_get ("MPI_STATE", container->type);
352 val_t value = PJ_value_get_or_new ("test", NULL, type);
353 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
356 void TRACE_smpi_testing_out(int rank)
358 if (!TRACE_smpi_is_enabled()) return;
359 char str[INSTR_DEFAULT_STR_SIZE];
360 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
361 container_t container = PJ_container_get (str);
362 type_t type = PJ_type_get ("MPI_STATE", container->type);
363 new_pajePopState (SIMIX_get_clock(), container, type);
366 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
368 if (!TRACE_smpi_is_enabled()) {
369 cleanup_extra_data(extra);
373 char str[INSTR_DEFAULT_STR_SIZE];
374 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
375 container_t container = PJ_container_get (str);
376 type_t type = PJ_type_get ("MPI_STATE", container->type);
377 const char *color = instr_find_color (operation);
378 val_t value = PJ_value_get_or_new (operation, color, type);
379 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
382 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
384 if (!TRACE_smpi_is_enabled()) return;
386 char str[INSTR_DEFAULT_STR_SIZE];
387 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
388 container_t container = PJ_container_get (str);
389 type_t type = PJ_type_get ("MPI_STATE", container->type);
391 new_pajePopState (SIMIX_get_clock(), container, type);
394 void TRACE_smpi_send(int rank, int src, int dst, int size)
396 if (!TRACE_smpi_is_enabled()) return;
398 char key[INSTR_DEFAULT_STR_SIZE] = {0};
399 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
401 char str[INSTR_DEFAULT_STR_SIZE];
402 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
403 container_t container = PJ_container_get (str);
404 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
406 new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
409 void TRACE_smpi_recv(int rank, int src, int dst)
411 if (!TRACE_smpi_is_enabled()) return;
413 char key[INSTR_DEFAULT_STR_SIZE] = {0};
414 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
416 char str[INSTR_DEFAULT_STR_SIZE];
417 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
418 container_t container = PJ_container_get (str);
419 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
421 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
423 #endif /* HAVE_TRACING */