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. */
12 #include <simgrid/sg_config.h>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
16 static xbt_dict_t keys;
18 static const char *smpi_colors[] ={
20 "irecv", "1 0.52 0.52",
22 "isend", "0.52 0.52 1",
25 "waitall", "0.78 0.78 0",
26 "waitany", "0.78 0.78 0.58",
27 "test", "0.52 0.52 0",
30 "allgatherv", "1 0.52 0.52",
32 "alltoall", "0.52 0 1",
33 "alltoallv", "0.78 0.52 1",
34 "barrier", "0 0.78 0.78",
35 "bcast", "0 0.78 0.39",
37 "gatherv", "1 1 0.52",
39 "reducescatter", "0.52 1 0.52",
40 "scan", "1 0.58 0.23",
41 "exscan", "1 0.54 0.25",
42 "scatterv", "0.52 0 0.52",
43 "scatter", "1 0.74 0.54",
46 "sleeping", "0 0.5 0.5",
53 "accumulate", "1 0.3 0",
54 "win_fence", "1 0 0.3",
55 "win_post", "1 0 0.8",
56 "win_wait", "1 0.8 0",
57 "win_start", "0.8 0 1",
58 "win_complete", "0.8 1 0",
62 static char *str_tolower (const char *str)
64 char *ret = xbt_strdup (str);
65 int i, n = strlen (ret);
66 for (i = 0; i < n; i++)
67 ret[i] = tolower (str[i]);
71 static const char *instr_find_color (const char *state)
73 char *target = str_tolower (state);
74 const char *ret = NULL;
77 while ((current = smpi_colors[i])){
78 if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
79 if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
86 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
88 snprintf(container, n, "rank-%d", rank);
92 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
94 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
96 //get the dynar for src#dst
97 char aux[INSTR_DEFAULT_STR_SIZE];
98 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
99 xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
101 if(!xbt_dynar_is_empty(d)){
102 //receive was already pushed, perform a get instead
103 TRACE_smpi_get_key(src , dst, key ,n);
108 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
109 xbt_dict_set(keys, aux, d, NULL);
113 static unsigned long long counter = 0;
115 snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
118 char *a = (char*)xbt_strdup(key);
119 xbt_dynar_push_as(d, char *, a);
124 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
126 char aux[INSTR_DEFAULT_STR_SIZE];
127 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
128 xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
130 // xbt_assert(!xbt_dynar_is_empty(d),
131 // "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
133 // sometimes the receive may be posted before the send
134 if(xbt_dynar_is_empty(d)){
135 TRACE_smpi_put_key(src, dst, key, n);
139 char *s = xbt_dynar_get_as (d, 0, char *);
140 snprintf (key, n, "%s", s);
141 xbt_dynar_remove_at (d, 0, NULL);
145 static xbt_dict_t process_category;
147 static void cleanup_extra_data (instr_extra_data extra){
149 if(extra->sendcounts!=NULL)
150 xbt_free(extra->sendcounts);
151 if(extra->recvcounts!=NULL)
152 xbt_free(extra->recvcounts);
157 void TRACE_internal_smpi_set_category (const char *category)
159 if (!TRACE_smpi_is_enabled()) return;
162 TRACE_category (category);
164 char processid[INSTR_DEFAULT_STR_SIZE];
165 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
166 if (xbt_dict_get_or_null (process_category, processid))
167 xbt_dict_remove (process_category, processid);
168 if (category != NULL)
169 xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
172 const char *TRACE_internal_smpi_get_category (void)
174 if (!TRACE_smpi_is_enabled()) return NULL;
176 char processid[INSTR_DEFAULT_STR_SIZE];
177 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
178 return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
181 void TRACE_smpi_alloc()
183 keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
184 process_category = xbt_dict_new_homogeneous(xbt_free_f);
187 void TRACE_smpi_release(void)
189 xbt_dict_free(&keys);
190 xbt_dict_free(&process_category);
193 void TRACE_smpi_init(int rank)
195 if (!TRACE_smpi_is_enabled()) return;
197 char str[INSTR_DEFAULT_STR_SIZE];
198 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
201 if (TRACE_smpi_is_grouped()){
202 father = PJ_container_get (SIMIX_host_self_get_name());
204 father = PJ_container_get_root ();
206 xbt_assert(father!=NULL,
207 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
208 PJ_container_new(str, INSTR_SMPI, father);
211 void TRACE_smpi_finalize(int rank)
213 if (!TRACE_smpi_is_enabled()) return;
215 char str[INSTR_DEFAULT_STR_SIZE];
216 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
217 PJ_container_remove_from_parent (container);
218 PJ_container_free (container);
221 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
223 if (!TRACE_smpi_is_enabled()) {
224 cleanup_extra_data(extra);
228 char str[INSTR_DEFAULT_STR_SIZE];
229 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
230 container_t container = PJ_container_get (str);
231 type_t type = PJ_type_get ("MPI_STATE", container->type);
232 const char *color = instr_find_color (operation);
233 val_t value = PJ_value_get_or_new (operation, color, type);
234 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
237 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
239 if (!TRACE_smpi_is_enabled()) return;
241 char str[INSTR_DEFAULT_STR_SIZE];
242 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
243 container_t container = PJ_container_get (str);
244 type_t type = PJ_type_get ("MPI_STATE", container->type);
246 new_pajePopState (SIMIX_get_clock(), container, type);
249 void TRACE_smpi_computing_init(int rank)
251 //first use, initialize the color in the trace
252 //TODO : check with lucas and Pierre how to generalize this approach
253 //to avoid unnecessary access to the color array
254 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
256 char str[INSTR_DEFAULT_STR_SIZE];
257 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
258 container_t container = PJ_container_get (str);
259 type_t type = PJ_type_get ("MPI_STATE", container->type);
260 const char *color = instr_find_color ("computing");
261 val_t value = PJ_value_get_or_new ("computing", color, type);
262 new_pajePushState (SIMIX_get_clock(), container, type, value);
265 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
267 //do not forget to set the color first, otherwise this will explode
268 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
269 cleanup_extra_data(extra);
273 char str[INSTR_DEFAULT_STR_SIZE];
274 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
275 container_t container = PJ_container_get (str);
276 type_t type = PJ_type_get ("MPI_STATE", container->type);
277 val_t value = PJ_value_get_or_new ("computing", NULL, type);
278 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
281 void TRACE_smpi_computing_out(int rank)
283 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
284 char str[INSTR_DEFAULT_STR_SIZE];
285 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
286 container_t container = PJ_container_get (str);
287 type_t type = PJ_type_get ("MPI_STATE", container->type);
288 new_pajePopState (SIMIX_get_clock(), container, type);
291 void TRACE_smpi_sleeping_init(int rank)
293 //first use, initialize the color in the trace
294 //TODO : check with lucas and Pierre how to generalize this approach
295 //to avoid unnecessary access to the color array
296 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping()) return;
298 char str[INSTR_DEFAULT_STR_SIZE];
299 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
300 container_t container = PJ_container_get (str);
301 type_t type = PJ_type_get ("MPI_STATE", container->type);
302 const char *color = instr_find_color ("sleeping");
303 val_t value = PJ_value_get_or_new ("sleeping", color, type);
304 new_pajePushState (SIMIX_get_clock(), container, type, value);
307 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
309 //do not forget to set the color first, otherwise this will explode
310 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
311 cleanup_extra_data(extra);
315 char str[INSTR_DEFAULT_STR_SIZE];
316 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
317 container_t container = PJ_container_get (str);
318 type_t type = PJ_type_get ("MPI_STATE", container->type);
319 val_t value = PJ_value_get_or_new ("sleeping", NULL, type);
320 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
323 void TRACE_smpi_sleeping_out(int rank)
325 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) return;
326 char str[INSTR_DEFAULT_STR_SIZE];
327 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
328 container_t container = PJ_container_get (str);
329 type_t type = PJ_type_get ("MPI_STATE", container->type);
330 new_pajePopState (SIMIX_get_clock(), container, type);
333 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
335 //do not forget to set the color first, otherwise this will explode
336 if (!TRACE_smpi_is_enabled()) {
337 cleanup_extra_data(extra);
341 char str[INSTR_DEFAULT_STR_SIZE];
342 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
343 container_t container = PJ_container_get (str);
344 type_t type = PJ_type_get ("MPI_STATE", container->type);
345 val_t value = PJ_value_get_or_new ("test", NULL, type);
346 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
349 void TRACE_smpi_testing_out(int rank)
351 if (!TRACE_smpi_is_enabled()) return;
352 char str[INSTR_DEFAULT_STR_SIZE];
353 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
354 container_t container = PJ_container_get (str);
355 type_t type = PJ_type_get ("MPI_STATE", container->type);
356 new_pajePopState (SIMIX_get_clock(), container, type);
359 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
361 if (!TRACE_smpi_is_enabled()) {
362 cleanup_extra_data(extra);
366 char str[INSTR_DEFAULT_STR_SIZE];
367 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
368 container_t container = PJ_container_get (str);
369 type_t type = PJ_type_get ("MPI_STATE", container->type);
370 const char *color = instr_find_color (operation);
371 val_t value = PJ_value_get_or_new (operation, color, type);
372 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
375 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
377 if (!TRACE_smpi_is_enabled()) return;
379 char str[INSTR_DEFAULT_STR_SIZE];
380 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
381 container_t container = PJ_container_get (str);
382 type_t type = PJ_type_get ("MPI_STATE", container->type);
384 new_pajePopState (SIMIX_get_clock(), container, type);
387 void TRACE_smpi_send(int rank, int src, int dst, int size)
389 if (!TRACE_smpi_is_enabled()) return;
391 char key[INSTR_DEFAULT_STR_SIZE] = {0};
392 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
394 char str[INSTR_DEFAULT_STR_SIZE];
395 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
396 container_t container = PJ_container_get (str);
397 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
399 new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
402 void TRACE_smpi_recv(int rank, int src, int dst)
404 if (!TRACE_smpi_is_enabled()) return;
406 char key[INSTR_DEFAULT_STR_SIZE] = {0};
407 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
409 char str[INSTR_DEFAULT_STR_SIZE];
410 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
411 container_t container = PJ_container_get (str);
412 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
414 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);