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. */
7 #include "instr/instr_private.h"
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 "scatterv", "0.52 0 0.52",
41 "scatter", "1 0.74 0.54",
46 static char *str_tolower (const char *str)
48 char *ret = xbt_strdup (str);
49 int i, n = strlen (ret);
50 for (i = 0; i < n; i++)
51 ret[i] = tolower (str[i]);
55 static const char *instr_find_color (const char *state)
57 char *target = str_tolower (state);
58 const char *ret = NULL;
61 while ((current = smpi_colors[i])){
62 if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
63 if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
71 static char *smpi_container(int rank, char *container, int n)
73 snprintf(container, n, "rank-%d", rank);
77 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
79 //get the dynar for src#dst
80 char aux[INSTR_DEFAULT_STR_SIZE];
81 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
82 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
84 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
85 xbt_dict_set(keys, aux, d, NULL);
88 static unsigned long long counter = 0;
91 MC_ignore_data_bss(&counter, sizeof(counter));
93 snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
96 char *a = (char*)xbt_strdup(key);
97 xbt_dynar_push_as(d, char *, a);
102 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
104 char aux[INSTR_DEFAULT_STR_SIZE];
105 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
106 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
108 xbt_assert(!xbt_dynar_is_empty(d),
109 "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
110 char *s = xbt_dynar_get_as (d, 0, char *);
111 snprintf (key, n, "%s", s);
112 xbt_dynar_remove_at (d, 0, NULL);
116 static xbt_dict_t process_category;
118 void TRACE_internal_smpi_set_category (const char *category)
120 if (!TRACE_smpi_is_enabled()) return;
123 TRACE_category (category);
125 char processid[INSTR_DEFAULT_STR_SIZE];
126 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
127 if (xbt_dict_get_or_null (process_category, processid))
128 xbt_dict_remove (process_category, processid);
129 if (category != NULL)
130 xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
133 const char *TRACE_internal_smpi_get_category (void)
135 if (!TRACE_smpi_is_enabled()) return NULL;
137 char processid[INSTR_DEFAULT_STR_SIZE];
138 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
139 return xbt_dict_get_or_null (process_category, processid);
142 void TRACE_smpi_alloc()
144 keys = xbt_dict_new_homogeneous(xbt_free);
145 process_category = xbt_dict_new_homogeneous(xbt_free);
148 void TRACE_smpi_release(void)
150 xbt_dict_free(&keys);
151 xbt_dict_free(&process_category);
154 void TRACE_smpi_init(int rank)
156 if (!TRACE_smpi_is_enabled()) return;
158 char str[INSTR_DEFAULT_STR_SIZE];
159 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
162 if (TRACE_smpi_is_grouped()){
163 father = PJ_container_get (SIMIX_host_self_get_name());
165 father = PJ_container_get_root ();
167 xbt_assert(father!=NULL,
168 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
169 PJ_container_new(str, INSTR_SMPI, father);
172 void TRACE_smpi_finalize(int rank)
174 if (!TRACE_smpi_is_enabled()) return;
176 char str[INSTR_DEFAULT_STR_SIZE];
177 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
178 PJ_container_remove_from_parent (container);
179 PJ_container_free (container);
182 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
184 if (!TRACE_smpi_is_enabled()) return;
186 char str[INSTR_DEFAULT_STR_SIZE];
187 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
188 container_t container = PJ_container_get (str);
189 type_t type = PJ_type_get ("MPI_STATE", container->type);
190 const char *color = instr_find_color (operation);
191 val_t value = PJ_value_get_or_new (operation, color, type);
192 new_pajePushState (SIMIX_get_clock(), container, type, value);
195 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
197 if (!TRACE_smpi_is_enabled()) return;
199 char str[INSTR_DEFAULT_STR_SIZE];
200 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
201 container_t container = PJ_container_get (str);
202 type_t type = PJ_type_get ("MPI_STATE", container->type);
204 new_pajePopState (SIMIX_get_clock(), container, type);
207 void TRACE_smpi_computing_init(int rank)
209 //first use, initialize the color in the trace
210 //TODO : check with lucas and Pierre how to generalize this approach
211 //to avoid unnecessary access to the color array
212 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
214 char str[INSTR_DEFAULT_STR_SIZE];
215 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
216 container_t container = PJ_container_get (str);
217 type_t type = PJ_type_get ("MPI_STATE", container->type);
218 const char *color = instr_find_color ("computing");
219 val_t value = PJ_value_get_or_new ("computing", color, type);
220 new_pajePushState (SIMIX_get_clock(), container, type, value);
223 void TRACE_smpi_computing_in(int rank)
225 //do not forget to set the color first, otherwise this will explode
226 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
228 char str[INSTR_DEFAULT_STR_SIZE];
229 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
230 container_t container = PJ_container_get (str);
231 type_t type = PJ_type_get ("MPI_STATE", container->type);
232 val_t value = PJ_value_get_or_new ("computing", NULL, type);
233 new_pajePushState (SIMIX_get_clock(), container, type, value);
236 void TRACE_smpi_computing_out(int rank)
238 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
239 char str[INSTR_DEFAULT_STR_SIZE];
240 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
241 container_t container = PJ_container_get (str);
242 type_t type = PJ_type_get ("MPI_STATE", container->type);
243 new_pajePopState (SIMIX_get_clock(), container, type);
246 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
248 if (!TRACE_smpi_is_enabled()) return;
251 char str[INSTR_DEFAULT_STR_SIZE];
252 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
253 container_t container = PJ_container_get (str);
254 type_t type = PJ_type_get ("MPI_STATE", container->type);
255 const char *color = instr_find_color (operation);
256 val_t value = PJ_value_get_or_new (operation, color, type);
257 new_pajePushState (SIMIX_get_clock(), container, type, value);
260 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
262 if (!TRACE_smpi_is_enabled()) return;
264 char str[INSTR_DEFAULT_STR_SIZE];
265 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
266 container_t container = PJ_container_get (str);
267 type_t type = PJ_type_get ("MPI_STATE", container->type);
269 new_pajePopState (SIMIX_get_clock(), container, type);
272 void TRACE_smpi_send(int rank, int src, int dst)
274 if (!TRACE_smpi_is_enabled()) return;
276 char key[INSTR_DEFAULT_STR_SIZE];
277 bzero (key, INSTR_DEFAULT_STR_SIZE);
278 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
280 char str[INSTR_DEFAULT_STR_SIZE];
281 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
282 container_t container = PJ_container_get (str);
283 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
285 new_pajeStartLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
288 void TRACE_smpi_recv(int rank, int src, int dst)
290 if (!TRACE_smpi_is_enabled()) return;
292 char key[INSTR_DEFAULT_STR_SIZE];
293 bzero (key, INSTR_DEFAULT_STR_SIZE);
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 */