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. */
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",
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",
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;
91 snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
94 char *a = (char*)xbt_strdup(key);
95 xbt_dynar_push_as(d, char *, a);
100 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
102 char aux[INSTR_DEFAULT_STR_SIZE];
103 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
104 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
106 xbt_assert(!xbt_dynar_is_empty(d),
107 "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
108 char *s = xbt_dynar_get_as (d, 0, char *);
109 snprintf (key, n, "%s", s);
110 xbt_dynar_remove_at (d, 0, NULL);
114 static xbt_dict_t process_category;
116 void TRACE_internal_smpi_set_category (const char *category)
118 if (!TRACE_smpi_is_enabled()) return;
121 TRACE_category (category);
123 char processid[INSTR_DEFAULT_STR_SIZE];
124 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
125 if (xbt_dict_get_or_null (process_category, processid))
126 xbt_dict_remove (process_category, processid);
127 if (category != NULL)
128 xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
131 const char *TRACE_internal_smpi_get_category (void)
133 if (!TRACE_smpi_is_enabled()) return NULL;
135 char processid[INSTR_DEFAULT_STR_SIZE];
136 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
137 return xbt_dict_get_or_null (process_category, processid);
140 void TRACE_smpi_alloc()
142 keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
143 process_category = xbt_dict_new_homogeneous(xbt_free);
146 void TRACE_smpi_release(void)
148 xbt_dict_free(&keys);
149 xbt_dict_free(&process_category);
152 void TRACE_smpi_init(int rank)
154 if (!TRACE_smpi_is_enabled()) return;
156 char str[INSTR_DEFAULT_STR_SIZE];
157 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
160 if (TRACE_smpi_is_grouped()){
161 father = PJ_container_get (SIMIX_host_self_get_name());
163 father = PJ_container_get_root ();
165 xbt_assert(father!=NULL,
166 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
167 PJ_container_new(str, INSTR_SMPI, father);
170 void TRACE_smpi_finalize(int rank)
172 if (!TRACE_smpi_is_enabled()) return;
174 char str[INSTR_DEFAULT_STR_SIZE];
175 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
176 PJ_container_remove_from_parent (container);
177 PJ_container_free (container);
180 void TRACE_smpi_collective_in(int rank, int root, const char *operation, int size)
182 if (!TRACE_smpi_is_enabled()) return;
184 char str[INSTR_DEFAULT_STR_SIZE];
185 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
186 container_t container = PJ_container_get (str);
187 type_t type = PJ_type_get ("MPI_STATE", container->type);
188 const char *color = instr_find_color (operation);
189 val_t value = PJ_value_get_or_new (operation, color, type);
190 new_pajePushStateWithSize (SIMIX_get_clock(), container, type, value, size);
193 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
195 if (!TRACE_smpi_is_enabled()) return;
197 char str[INSTR_DEFAULT_STR_SIZE];
198 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
199 container_t container = PJ_container_get (str);
200 type_t type = PJ_type_get ("MPI_STATE", container->type);
202 new_pajePopState (SIMIX_get_clock(), container, type);
205 void TRACE_smpi_computing_init(int rank)
207 //first use, initialize the color in the trace
208 //TODO : check with lucas and Pierre how to generalize this approach
209 //to avoid unnecessary access to the color array
210 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
212 char str[INSTR_DEFAULT_STR_SIZE];
213 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
214 container_t container = PJ_container_get (str);
215 type_t mpi = PJ_type_get("MPI", PJ_container_get_root()->type);
216 type_t type = PJ_type_state_new ("COMPUTATION", mpi);
217 const char *color = instr_find_color ("computing");
218 val_t value = PJ_value_get_or_new ("computing", color, type);
219 new_pajePushState (SIMIX_get_clock(), container, type, value);
222 void TRACE_smpi_computing_in(int rank)
224 //do not forget to set the color first, otherwise this will explode
225 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
227 char str[INSTR_DEFAULT_STR_SIZE];
228 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
229 container_t container = PJ_container_get (str);
230 type_t type = PJ_type_get ("COMPUTATION", container->type);
231 val_t value = PJ_value_get_or_new ("computing", NULL, type);
232 new_pajePushState (SIMIX_get_clock(), container, type, value);
235 void TRACE_smpi_computing_out(int rank, double flops)
237 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
238 char str[INSTR_DEFAULT_STR_SIZE];
239 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
240 container_t container = PJ_container_get (str);
241 type_t type = PJ_type_get ("COMPUTATION", container->type);
242 xbt_dynar_t extra= xbt_dynar_new(sizeof(char*), NULL);
243 char* char_duration = bprintf("%g", flops);
244 xbt_dynar_push(extra, &char_duration);
245 new_pajePopStateWithExtra (SIMIX_get_clock(), container, type, extra);
248 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, int size)
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_pajePushStateWithSize (SIMIX_get_clock(), container, type, value, size);
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, int size)
276 if (!TRACE_smpi_is_enabled()) return;
278 char key[INSTR_DEFAULT_STR_SIZE] = {0};
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_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
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] = {0};
294 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
296 char str[INSTR_DEFAULT_STR_SIZE];
297 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
298 container_t container = PJ_container_get (str);
299 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
301 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
303 #endif /* HAVE_TRACING */