1 /* Copyright (c) 2010, 2012-2013. 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",
31 "allgatherv", "1 0.52 0.52",
33 "alltoall", "0.52 0 1",
34 "alltoallv", "0.78 0.52 1",
35 "barrier", "0 0.78 0.78",
36 "bcast", "0 0.78 0.39",
38 "gatherv", "1 1 0.52",
40 "reducescatter", "0.52 1 0.52",
41 "scan", "1 0.58 0.23",
42 "exscan", "1 0.54 0.25",
43 "scatterv", "0.52 0 0.52",
44 "scatter", "1 0.74 0.54",
51 static char *str_tolower (const char *str)
53 char *ret = xbt_strdup (str);
54 int i, n = strlen (ret);
55 for (i = 0; i < n; i++)
56 ret[i] = tolower (str[i]);
60 static const char *instr_find_color (const char *state)
62 char *target = str_tolower (state);
63 const char *ret = NULL;
66 while ((current = smpi_colors[i])){
67 if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
68 if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
76 static char *smpi_container(int rank, char *container, int n)
78 snprintf(container, n, "rank-%d", rank);
82 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
84 //get the dynar for src#dst
85 char aux[INSTR_DEFAULT_STR_SIZE];
86 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
87 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
89 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
90 xbt_dict_set(keys, aux, d, NULL);
93 static unsigned long long counter = 0;
95 snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
98 char *a = (char*)xbt_strdup(key);
99 xbt_dynar_push_as(d, char *, a);
104 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
106 char aux[INSTR_DEFAULT_STR_SIZE];
107 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
108 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
110 xbt_assert(!xbt_dynar_is_empty(d),
111 "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
112 char *s = xbt_dynar_get_as (d, 0, char *);
113 snprintf (key, n, "%s", s);
114 xbt_dynar_remove_at (d, 0, NULL);
118 static xbt_dict_t process_category;
120 void TRACE_internal_smpi_set_category (const char *category)
122 if (!TRACE_smpi_is_enabled()) return;
125 TRACE_category (category);
127 char processid[INSTR_DEFAULT_STR_SIZE];
128 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
129 if (xbt_dict_get_or_null (process_category, processid))
130 xbt_dict_remove (process_category, processid);
131 if (category != NULL)
132 xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
135 const char *TRACE_internal_smpi_get_category (void)
137 if (!TRACE_smpi_is_enabled()) return NULL;
139 char processid[INSTR_DEFAULT_STR_SIZE];
140 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
141 return xbt_dict_get_or_null (process_category, processid);
144 void TRACE_smpi_alloc()
146 keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
147 process_category = xbt_dict_new_homogeneous(xbt_free);
150 void TRACE_smpi_release(void)
152 xbt_dict_free(&keys);
153 xbt_dict_free(&process_category);
156 void TRACE_smpi_init(int rank)
158 if (!TRACE_smpi_is_enabled()) return;
160 char str[INSTR_DEFAULT_STR_SIZE];
161 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
164 if (TRACE_smpi_is_grouped()){
165 father = PJ_container_get (SIMIX_host_self_get_name());
167 father = PJ_container_get_root ();
169 xbt_assert(father!=NULL,
170 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
171 PJ_container_new(str, INSTR_SMPI, father);
174 void TRACE_smpi_finalize(int rank)
176 if (!TRACE_smpi_is_enabled()) return;
178 char str[INSTR_DEFAULT_STR_SIZE];
179 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
180 PJ_container_remove_from_parent (container);
181 PJ_container_free (container);
184 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
186 if (!TRACE_smpi_is_enabled()) return;
188 char str[INSTR_DEFAULT_STR_SIZE];
189 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
190 container_t container = PJ_container_get (str);
191 type_t type = PJ_type_get ("MPI_STATE", container->type);
192 const char *color = instr_find_color (operation);
193 val_t value = PJ_value_get_or_new (operation, color, type);
194 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
198 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
200 if (!TRACE_smpi_is_enabled()) return;
202 char str[INSTR_DEFAULT_STR_SIZE];
203 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
204 container_t container = PJ_container_get (str);
205 type_t type = PJ_type_get ("MPI_STATE", container->type);
207 new_pajePopState (SIMIX_get_clock(), container, type);
210 void TRACE_smpi_computing_init(int rank)
212 //first use, initialize the color in the trace
213 //TODO : check with lucas and Pierre how to generalize this approach
214 //to avoid unnecessary access to the color array
215 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
217 char str[INSTR_DEFAULT_STR_SIZE];
218 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
219 container_t container = PJ_container_get (str);
220 type_t type = PJ_type_get ("MPI_STATE", container->type);
221 const char *color = instr_find_color ("computing");
222 val_t value = PJ_value_get_or_new ("computing", color, type);
223 new_pajePushState (SIMIX_get_clock(), container, type, value);
226 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
228 //do not forget to set the color first, otherwise this will explode
229 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
231 char str[INSTR_DEFAULT_STR_SIZE];
232 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
233 container_t container = PJ_container_get (str);
234 type_t type = PJ_type_get ("MPI_STATE", container->type);
235 val_t value = PJ_value_get_or_new ("computing", NULL, type);
236 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
239 void TRACE_smpi_computing_out(int rank)
241 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
242 char str[INSTR_DEFAULT_STR_SIZE];
243 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
244 container_t container = PJ_container_get (str);
245 type_t type = PJ_type_get ("MPI_STATE", container->type);
246 new_pajePopState (SIMIX_get_clock(), container, type);
249 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
251 if (!TRACE_smpi_is_enabled()) return;
254 char str[INSTR_DEFAULT_STR_SIZE];
255 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
256 container_t container = PJ_container_get (str);
257 type_t type = PJ_type_get ("MPI_STATE", container->type);
258 const char *color = instr_find_color (operation);
259 val_t value = PJ_value_get_or_new (operation, color, type);
260 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
263 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
265 if (!TRACE_smpi_is_enabled()) return;
267 char str[INSTR_DEFAULT_STR_SIZE];
268 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
269 container_t container = PJ_container_get (str);
270 type_t type = PJ_type_get ("MPI_STATE", container->type);
272 new_pajePopState (SIMIX_get_clock(), container, type);
275 void TRACE_smpi_send(int rank, int src, int dst, int size)
277 if (!TRACE_smpi_is_enabled()) return;
279 char key[INSTR_DEFAULT_STR_SIZE] = {0};
280 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
282 char str[INSTR_DEFAULT_STR_SIZE];
283 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
284 container_t container = PJ_container_get (str);
285 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
287 new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
290 void TRACE_smpi_recv(int rank, int src, int dst)
292 if (!TRACE_smpi_is_enabled()) return;
294 char key[INSTR_DEFAULT_STR_SIZE] = {0};
295 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
297 char str[INSTR_DEFAULT_STR_SIZE];
298 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
299 container_t container = PJ_container_get (str);
300 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
302 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
304 #endif /* HAVE_TRACING */