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 "exscan", "1 0.54 0.25",
42 "scatterv", "0.52 0 0.52",
43 "scatter", "1 0.74 0.54",
48 static char *str_tolower (const char *str)
50 char *ret = xbt_strdup (str);
51 int i, n = strlen (ret);
52 for (i = 0; i < n; i++)
53 ret[i] = tolower (str[i]);
57 static const char *instr_find_color (const char *state)
59 char *target = str_tolower (state);
60 const char *ret = NULL;
63 while ((current = smpi_colors[i])){
64 if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
65 if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
73 static char *smpi_container(int rank, char *container, int n)
75 snprintf(container, n, "rank-%d", rank);
79 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
81 //get the dynar for src#dst
82 char aux[INSTR_DEFAULT_STR_SIZE];
83 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
84 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
86 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
87 xbt_dict_set(keys, aux, d, NULL);
90 static unsigned long long counter = 0;
93 MC_ignore_data_bss(&counter, sizeof(counter));
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)
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_pajePushState (SIMIX_get_clock(), container, type, value);
197 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
199 if (!TRACE_smpi_is_enabled()) return;
201 char str[INSTR_DEFAULT_STR_SIZE];
202 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
203 container_t container = PJ_container_get (str);
204 type_t type = PJ_type_get ("MPI_STATE", container->type);
206 new_pajePopState (SIMIX_get_clock(), container, type);
209 void TRACE_smpi_computing_init(int rank)
211 //first use, initialize the color in the trace
212 //TODO : check with lucas and Pierre how to generalize this approach
213 //to avoid unnecessary access to the color array
214 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
216 char str[INSTR_DEFAULT_STR_SIZE];
217 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
218 container_t container = PJ_container_get (str);
219 type_t type = PJ_type_get ("MPI_STATE", container->type);
220 const char *color = instr_find_color ("computing");
221 val_t value = PJ_value_get_or_new ("computing", color, type);
222 new_pajePushState (SIMIX_get_clock(), container, type, value);
225 void TRACE_smpi_computing_in(int rank)
227 //do not forget to set the color first, otherwise this will explode
228 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
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 val_t value = PJ_value_get_or_new ("computing", NULL, type);
235 new_pajePushState (SIMIX_get_clock(), container, type, value);
238 void TRACE_smpi_computing_out(int rank)
240 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) 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);
245 new_pajePopState (SIMIX_get_clock(), container, type);
248 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
250 if (!TRACE_smpi_is_enabled()) return;
253 char str[INSTR_DEFAULT_STR_SIZE];
254 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
255 container_t container = PJ_container_get (str);
256 type_t type = PJ_type_get ("MPI_STATE", container->type);
257 const char *color = instr_find_color (operation);
258 val_t value = PJ_value_get_or_new (operation, color, type);
259 new_pajePushState (SIMIX_get_clock(), container, type, value);
262 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
264 if (!TRACE_smpi_is_enabled()) return;
266 char str[INSTR_DEFAULT_STR_SIZE];
267 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
268 container_t container = PJ_container_get (str);
269 type_t type = PJ_type_get ("MPI_STATE", container->type);
271 new_pajePopState (SIMIX_get_clock(), container, type);
274 void TRACE_smpi_send(int rank, int src, int dst)
276 if (!TRACE_smpi_is_enabled()) return;
278 char key[INSTR_DEFAULT_STR_SIZE];
279 bzero (key, INSTR_DEFAULT_STR_SIZE);
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_pajeStartLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
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];
295 bzero (key, INSTR_DEFAULT_STR_SIZE);
296 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
298 char str[INSTR_DEFAULT_STR_SIZE];
299 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
300 container_t container = PJ_container_get (str);
301 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
303 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
305 #endif /* HAVE_TRACING */