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",
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;
90 snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
93 char *a = (char*)xbt_strdup(key);
94 xbt_dynar_push_as(d, char *, a);
99 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
101 char aux[INSTR_DEFAULT_STR_SIZE];
102 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
103 xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
105 xbt_assert(!xbt_dynar_is_empty(d),
106 "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
107 char *s = xbt_dynar_get_as (d, 0, char *);
108 snprintf (key, n, "%s", s);
109 xbt_dynar_remove_at (d, 0, NULL);
113 static xbt_dict_t process_category;
115 void TRACE_internal_smpi_set_category (const char *category)
117 if (!TRACE_smpi_is_enabled()) return;
120 TRACE_category (category);
122 char processid[INSTR_DEFAULT_STR_SIZE];
123 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
124 if (xbt_dict_get_or_null (process_category, processid))
125 xbt_dict_remove (process_category, processid);
126 if (category != NULL)
127 xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
130 const char *TRACE_internal_smpi_get_category (void)
132 if (!TRACE_smpi_is_enabled()) return NULL;
134 char processid[INSTR_DEFAULT_STR_SIZE];
135 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
136 return xbt_dict_get_or_null (process_category, processid);
139 void TRACE_smpi_alloc()
141 keys = xbt_dict_new_homogeneous(xbt_free);
142 process_category = xbt_dict_new_homogeneous(xbt_free);
145 void TRACE_smpi_release(void)
147 xbt_dict_free(&keys);
148 xbt_dict_free(&process_category);
151 void TRACE_smpi_init(int rank)
153 if (!TRACE_smpi_is_enabled()) return;
155 char str[INSTR_DEFAULT_STR_SIZE];
156 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
159 if (TRACE_smpi_is_grouped()){
160 father = PJ_container_get (SIMIX_host_self_get_name());
162 father = PJ_container_get_root ();
164 xbt_assert(father!=NULL,
165 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
166 PJ_container_new(str, INSTR_SMPI, father);
169 void TRACE_smpi_finalize(int rank)
171 if (!TRACE_smpi_is_enabled()) return;
173 char str[INSTR_DEFAULT_STR_SIZE];
174 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
175 PJ_container_remove_from_parent (container);
176 PJ_container_free (container);
179 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
181 if (!TRACE_smpi_is_enabled()) return;
183 char str[INSTR_DEFAULT_STR_SIZE];
184 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
185 container_t container = PJ_container_get (str);
186 type_t type = PJ_type_get ("MPI_STATE", container->type);
187 const char *color = instr_find_color (operation);
188 val_t value = PJ_value_get_or_new (operation, color, type);
189 new_pajePushState (SIMIX_get_clock(), container, type, value);
192 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
194 if (!TRACE_smpi_is_enabled()) return;
196 char str[INSTR_DEFAULT_STR_SIZE];
197 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
198 container_t container = PJ_container_get (str);
199 type_t type = PJ_type_get ("MPI_STATE", container->type);
201 new_pajePopState (SIMIX_get_clock(), container, type);
204 void TRACE_smpi_computing_init(int rank)
206 //first use, initialize the color in the trace
207 //TODO : check with lucas and Pierre how to generalize this approach
208 //to avoid unnecessary access to the color array
209 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
211 char str[INSTR_DEFAULT_STR_SIZE];
212 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
213 container_t container = PJ_container_get (str);
214 type_t type = PJ_type_get ("MPI_STATE", container->type);
215 const char *color = instr_find_color ("computing");
216 val_t value = PJ_value_get_or_new ("computing", color, type);
217 new_pajePushState (SIMIX_get_clock(), container, type, value);
220 void TRACE_smpi_computing_in(int rank)
222 //do not forget to set the color first, otherwise this will explode
223 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
225 char str[INSTR_DEFAULT_STR_SIZE];
226 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
227 container_t container = PJ_container_get (str);
228 type_t type = PJ_type_get ("MPI_STATE", container->type);
229 val_t value = PJ_value_get_or_new ("computing", NULL, type);
230 new_pajePushState (SIMIX_get_clock(), container, type, value);
233 void TRACE_smpi_computing_out(int rank)
235 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
236 char str[INSTR_DEFAULT_STR_SIZE];
237 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
238 container_t container = PJ_container_get (str);
239 type_t type = PJ_type_get ("MPI_STATE", container->type);
240 new_pajePopState (SIMIX_get_clock(), container, type);
243 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
245 if (!TRACE_smpi_is_enabled()) return;
248 char str[INSTR_DEFAULT_STR_SIZE];
249 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
250 container_t container = PJ_container_get (str);
251 type_t type = PJ_type_get ("MPI_STATE", container->type);
252 const char *color = instr_find_color (operation);
253 val_t value = PJ_value_get_or_new (operation, color, type);
254 new_pajePushState (SIMIX_get_clock(), container, type, value);
257 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
259 if (!TRACE_smpi_is_enabled()) return;
261 char str[INSTR_DEFAULT_STR_SIZE];
262 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
263 container_t container = PJ_container_get (str);
264 type_t type = PJ_type_get ("MPI_STATE", container->type);
266 new_pajePopState (SIMIX_get_clock(), container, type);
269 void TRACE_smpi_send(int rank, int src, int dst)
271 if (!TRACE_smpi_is_enabled()) return;
273 char key[INSTR_DEFAULT_STR_SIZE];
274 bzero (key, INSTR_DEFAULT_STR_SIZE);
275 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
277 char str[INSTR_DEFAULT_STR_SIZE];
278 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
279 container_t container = PJ_container_get (str);
280 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
282 new_pajeStartLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
285 void TRACE_smpi_recv(int rank, int src, int dst)
287 if (!TRACE_smpi_is_enabled()) return;
289 char key[INSTR_DEFAULT_STR_SIZE];
290 bzero (key, INSTR_DEFAULT_STR_SIZE);
291 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
293 char str[INSTR_DEFAULT_STR_SIZE];
294 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
295 container_t container = PJ_container_get (str);
296 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
298 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
300 #endif /* HAVE_TRACING */