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>
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
86 static 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);
95 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
97 //get the dynar for src#dst
98 char aux[INSTR_DEFAULT_STR_SIZE];
99 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
100 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
103 if(!xbt_dynar_is_empty(d)){
104 //receive was already pushed, perform a get instead
105 TRACE_smpi_get_key(src , dst, key ,n);
110 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
111 xbt_dict_set(keys, aux, d, NULL);
115 static unsigned long long counter = 0;
117 snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
120 char *a = (char*)xbt_strdup(key);
121 xbt_dynar_push_as(d, char *, a);
126 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
128 char aux[INSTR_DEFAULT_STR_SIZE];
129 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
130 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
132 // xbt_assert(!xbt_dynar_is_empty(d),
133 // "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
135 // sometimes the receive may be posted before the send
136 if(xbt_dynar_is_empty(d)){
137 TRACE_smpi_put_key(src, dst, key, n);
141 char *s = xbt_dynar_get_as (d, 0, char *);
142 snprintf (key, n, "%s", s);
143 xbt_dynar_remove_at (d, 0, NULL);
147 static xbt_dict_t process_category;
149 static void cleanup_extra_data (instr_extra_data extra){
151 if(extra->sendcounts!=NULL)
152 xbt_free(extra->sendcounts);
153 if(extra->recvcounts!=NULL)
154 xbt_free(extra->recvcounts);
159 void TRACE_internal_smpi_set_category (const char *category)
161 if (!TRACE_smpi_is_enabled()) return;
164 TRACE_category (category);
166 char processid[INSTR_DEFAULT_STR_SIZE];
167 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
168 if (xbt_dict_get_or_null (process_category, processid))
169 xbt_dict_remove (process_category, processid);
170 if (category != NULL)
171 xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
174 const char *TRACE_internal_smpi_get_category (void)
176 if (!TRACE_smpi_is_enabled()) return NULL;
178 char processid[INSTR_DEFAULT_STR_SIZE];
179 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
180 return xbt_dict_get_or_null (process_category, processid);
183 void TRACE_smpi_alloc()
185 keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
186 process_category = xbt_dict_new_homogeneous(xbt_free_f);
189 void TRACE_smpi_release(void)
191 xbt_dict_free(&keys);
192 xbt_dict_free(&process_category);
195 void TRACE_smpi_init(int rank)
197 if (!TRACE_smpi_is_enabled()) return;
199 char str[INSTR_DEFAULT_STR_SIZE];
200 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
203 if (TRACE_smpi_is_grouped()){
204 father = PJ_container_get (SIMIX_host_self_get_name());
206 father = PJ_container_get_root ();
208 xbt_assert(father!=NULL,
209 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
210 PJ_container_new(str, INSTR_SMPI, father);
213 void TRACE_smpi_finalize(int rank)
215 if (!TRACE_smpi_is_enabled()) return;
217 char str[INSTR_DEFAULT_STR_SIZE];
218 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
219 PJ_container_remove_from_parent (container);
220 PJ_container_free (container);
223 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
225 if (!TRACE_smpi_is_enabled()) {
226 cleanup_extra_data(extra);
230 char str[INSTR_DEFAULT_STR_SIZE];
231 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
232 container_t container = PJ_container_get (str);
233 type_t type = PJ_type_get ("MPI_STATE", container->type);
234 const char *color = instr_find_color (operation);
235 val_t value = PJ_value_get_or_new (operation, color, type);
236 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
240 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
242 if (!TRACE_smpi_is_enabled()) return;
244 char str[INSTR_DEFAULT_STR_SIZE];
245 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
246 container_t container = PJ_container_get (str);
247 type_t type = PJ_type_get ("MPI_STATE", container->type);
249 new_pajePopState (SIMIX_get_clock(), container, type);
252 void TRACE_smpi_computing_init(int rank)
254 //first use, initialize the color in the trace
255 //TODO : check with lucas and Pierre how to generalize this approach
256 //to avoid unnecessary access to the color array
257 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
259 char str[INSTR_DEFAULT_STR_SIZE];
260 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
261 container_t container = PJ_container_get (str);
262 type_t type = PJ_type_get ("MPI_STATE", container->type);
263 const char *color = instr_find_color ("computing");
264 val_t value = PJ_value_get_or_new ("computing", color, type);
265 new_pajePushState (SIMIX_get_clock(), container, type, value);
268 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
270 //do not forget to set the color first, otherwise this will explode
271 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
272 cleanup_extra_data(extra);
276 char str[INSTR_DEFAULT_STR_SIZE];
277 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
278 container_t container = PJ_container_get (str);
279 type_t type = PJ_type_get ("MPI_STATE", container->type);
280 val_t value = PJ_value_get_or_new ("computing", NULL, type);
281 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
284 void TRACE_smpi_computing_out(int rank)
286 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
287 char str[INSTR_DEFAULT_STR_SIZE];
288 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
289 container_t container = PJ_container_get (str);
290 type_t type = PJ_type_get ("MPI_STATE", container->type);
291 new_pajePopState (SIMIX_get_clock(), container, type);
294 void TRACE_smpi_sleeping_init(int rank)
296 //first use, initialize the color in the trace
297 //TODO : check with lucas and Pierre how to generalize this approach
298 //to avoid unnecessary access to the color array
299 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping()) return;
301 char str[INSTR_DEFAULT_STR_SIZE];
302 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
303 container_t container = PJ_container_get (str);
304 type_t type = PJ_type_get ("MPI_STATE", container->type);
305 const char *color = instr_find_color ("sleeping");
306 val_t value = PJ_value_get_or_new ("sleeping", color, type);
307 new_pajePushState (SIMIX_get_clock(), container, type, value);
310 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
312 //do not forget to set the color first, otherwise this will explode
313 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
314 cleanup_extra_data(extra);
318 char str[INSTR_DEFAULT_STR_SIZE];
319 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
320 container_t container = PJ_container_get (str);
321 type_t type = PJ_type_get ("MPI_STATE", container->type);
322 val_t value = PJ_value_get_or_new ("sleeping", NULL, type);
323 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
326 void TRACE_smpi_sleeping_out(int rank)
328 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) return;
329 char str[INSTR_DEFAULT_STR_SIZE];
330 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
331 container_t container = PJ_container_get (str);
332 type_t type = PJ_type_get ("MPI_STATE", container->type);
333 new_pajePopState (SIMIX_get_clock(), container, type);
337 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
339 //do not forget to set the color first, otherwise this will explode
340 if (!TRACE_smpi_is_enabled()) {
341 cleanup_extra_data(extra);
345 char str[INSTR_DEFAULT_STR_SIZE];
346 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
347 container_t container = PJ_container_get (str);
348 type_t type = PJ_type_get ("MPI_STATE", container->type);
349 val_t value = PJ_value_get_or_new ("test", NULL, type);
350 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
353 void TRACE_smpi_testing_out(int rank)
355 if (!TRACE_smpi_is_enabled()) return;
356 char str[INSTR_DEFAULT_STR_SIZE];
357 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
358 container_t container = PJ_container_get (str);
359 type_t type = PJ_type_get ("MPI_STATE", container->type);
360 new_pajePopState (SIMIX_get_clock(), container, type);
363 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
365 if (!TRACE_smpi_is_enabled()) {
366 cleanup_extra_data(extra);
370 char str[INSTR_DEFAULT_STR_SIZE];
371 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
372 container_t container = PJ_container_get (str);
373 type_t type = PJ_type_get ("MPI_STATE", container->type);
374 const char *color = instr_find_color (operation);
375 val_t value = PJ_value_get_or_new (operation, color, type);
376 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
379 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
381 if (!TRACE_smpi_is_enabled()) return;
383 char str[INSTR_DEFAULT_STR_SIZE];
384 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
385 container_t container = PJ_container_get (str);
386 type_t type = PJ_type_get ("MPI_STATE", container->type);
388 new_pajePopState (SIMIX_get_clock(), container, type);
391 void TRACE_smpi_send(int rank, int src, int dst, int size)
393 if (!TRACE_smpi_is_enabled()) return;
395 char key[INSTR_DEFAULT_STR_SIZE] = {0};
396 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
398 char str[INSTR_DEFAULT_STR_SIZE];
399 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
400 container_t container = PJ_container_get (str);
401 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
403 new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
406 void TRACE_smpi_recv(int rank, int src, int dst)
408 if (!TRACE_smpi_is_enabled()) return;
410 char key[INSTR_DEFAULT_STR_SIZE] = {0};
411 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
413 char str[INSTR_DEFAULT_STR_SIZE];
414 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
415 container_t container = PJ_container_get (str);
416 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
418 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);