1 /* Copyright (c) 2010. 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. */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
17 static xbt_dict_t keys;
19 static const char *smpi_colors[] ={
21 "irecv", "1 0.52 0.52",
23 "isend", "0.52 0.52 1",
26 "waitall", "0.78 0.78 0",
27 "waitany", "0.78 0.78 0.58",
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 "scatterv", "0.52 0 0.52",
42 "scatter", "1 0.74 0.54",
47 static char *str_tolower (const char *str)
49 char *ret = xbt_strdup (str);
50 int i, n = strlen (ret);
51 for (i = 0; i < n; i++)
52 ret[i] = tolower (str[i]);
56 static const char *instr_find_color (const char *state)
58 char *target = str_tolower (state);
59 const char *ret = NULL;
62 while ((current = smpi_colors[i])){
63 if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
64 if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
72 static char *smpi_container(int rank, char *container, int n)
74 snprintf(container, n, "rank-%d", rank);
78 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
80 //get the dynar for src#dst
81 char aux[INSTR_DEFAULT_STR_SIZE];
82 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
83 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
85 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
86 xbt_dict_set(keys, aux, d, NULL);
89 static unsigned long long counter = 0;
92 MC_ignore_data_bss(&counter, sizeof(counter));
94 snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
97 char *a = (char*)xbt_strdup(key);
98 xbt_dynar_push_as(d, char *, a);
103 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
105 char aux[INSTR_DEFAULT_STR_SIZE];
106 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
107 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
109 xbt_assert(!xbt_dynar_is_empty(d),
110 "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
111 char *s = xbt_dynar_get_as (d, 0, char *);
112 snprintf (key, n, "%s", s);
113 xbt_dynar_remove_at (d, 0, NULL);
117 static xbt_dict_t process_category;
119 void TRACE_internal_smpi_set_category (const char *category)
121 if (!TRACE_smpi_is_enabled()) return;
124 TRACE_category (category);
126 char processid[INSTR_DEFAULT_STR_SIZE];
127 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
128 if (xbt_dict_get_or_null (process_category, processid))
129 xbt_dict_remove (process_category, processid);
130 if (category != NULL)
131 xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
134 const char *TRACE_internal_smpi_get_category (void)
136 if (!TRACE_smpi_is_enabled()) return NULL;
138 char processid[INSTR_DEFAULT_STR_SIZE];
139 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
140 return xbt_dict_get_or_null (process_category, processid);
143 void TRACE_smpi_alloc()
145 keys = xbt_dict_new_homogeneous(xbt_free);
146 process_category = xbt_dict_new_homogeneous(xbt_free);
149 void TRACE_smpi_release(void)
151 xbt_dict_free(&keys);
152 xbt_dict_free(&process_category);
155 void TRACE_smpi_init(int rank)
157 if (!TRACE_smpi_is_enabled()) return;
159 char str[INSTR_DEFAULT_STR_SIZE];
160 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
163 if (TRACE_smpi_is_grouped()){
164 father = PJ_container_get (SIMIX_host_self_get_name());
166 father = PJ_container_get_root ();
168 xbt_assert(father!=NULL,
169 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
170 PJ_container_new(str, INSTR_SMPI, father);
173 void TRACE_smpi_finalize(int rank)
175 if (!TRACE_smpi_is_enabled()) return;
177 char str[INSTR_DEFAULT_STR_SIZE];
178 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
179 PJ_container_remove_from_parent (container);
180 PJ_container_free (container);
183 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
185 if (!TRACE_smpi_is_enabled()) return;
187 char str[INSTR_DEFAULT_STR_SIZE];
188 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
189 container_t container = PJ_container_get (str);
190 type_t type = PJ_type_get ("MPI_STATE", container->type);
191 const char *color = instr_find_color (operation);
192 val_t value = PJ_value_get_or_new (operation, color, type);
193 new_pajePushState (SIMIX_get_clock(), container, type, value);
196 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
198 if (!TRACE_smpi_is_enabled()) return;
200 char str[INSTR_DEFAULT_STR_SIZE];
201 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
202 container_t container = PJ_container_get (str);
203 type_t type = PJ_type_get ("MPI_STATE", container->type);
205 new_pajePopState (SIMIX_get_clock(), container, type);
208 void TRACE_smpi_computing_init(int rank)
210 //first use, initialize the color in the trace
211 //TODO : check with lucas and Pierre how to generalize this approach
212 //to avoid unnecessary access to the color array
213 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
215 char str[INSTR_DEFAULT_STR_SIZE];
216 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
217 container_t container = PJ_container_get (str);
218 type_t type = PJ_type_get ("MPI_STATE", container->type);
219 const char *color = instr_find_color ("computing");
220 val_t value = PJ_value_get_or_new ("computing", color, type);
221 new_pajePushState (SIMIX_get_clock(), container, type, value);
224 void TRACE_smpi_computing_in(int rank)
226 //do not forget to set the color first, otherwise this will explode
227 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
229 char str[INSTR_DEFAULT_STR_SIZE];
230 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
231 container_t container = PJ_container_get (str);
232 type_t type = PJ_type_get ("MPI_STATE", container->type);
233 val_t value = PJ_value_get_or_new ("computing", NULL, type);
234 new_pajePushState (SIMIX_get_clock(), container, type, value);
237 void TRACE_smpi_computing_out(int rank)
239 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) 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);
244 new_pajePopState (SIMIX_get_clock(), container, type);
247 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
249 if (!TRACE_smpi_is_enabled()) return;
252 char str[INSTR_DEFAULT_STR_SIZE];
253 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
254 container_t container = PJ_container_get (str);
255 type_t type = PJ_type_get ("MPI_STATE", container->type);
256 const char *color = instr_find_color (operation);
257 val_t value = PJ_value_get_or_new (operation, color, type);
258 new_pajePushState (SIMIX_get_clock(), container, type, value);
261 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
263 if (!TRACE_smpi_is_enabled()) return;
265 char str[INSTR_DEFAULT_STR_SIZE];
266 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
267 container_t container = PJ_container_get (str);
268 type_t type = PJ_type_get ("MPI_STATE", container->type);
270 new_pajePopState (SIMIX_get_clock(), container, type);
273 void TRACE_smpi_send(int rank, int src, int dst)
275 if (!TRACE_smpi_is_enabled()) return;
277 char key[INSTR_DEFAULT_STR_SIZE];
278 bzero (key, INSTR_DEFAULT_STR_SIZE);
279 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
281 char str[INSTR_DEFAULT_STR_SIZE];
282 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
283 container_t container = PJ_container_get (str);
284 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
286 new_pajeStartLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
289 void TRACE_smpi_recv(int rank, int src, int dst)
291 if (!TRACE_smpi_is_enabled()) return;
293 char key[INSTR_DEFAULT_STR_SIZE];
294 bzero (key, INSTR_DEFAULT_STR_SIZE);
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 */