1 /* Copyright (c) 2010, 2012-2015. 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. */
11 #include <simgrid/sg_config.h>
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
15 static xbt_dict_t keys;
17 static const char *smpi_colors[] ={
19 "irecv", "1 0.52 0.52",
21 "isend", "0.52 0.52 1",
24 "waitall", "0.78 0.78 0",
25 "waitany", "0.78 0.78 0.58",
26 "test", "0.52 0.52 0",
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",
45 "sleeping", "0 0.5 0.5",
52 "accumulate", "1 0.3 0",
53 "win_fence", "1 0 0.3",
54 "win_post", "1 0 0.8",
55 "win_wait", "1 0.8 0",
56 "win_start", "0.8 0 1",
57 "win_complete", "0.8 1 0",
61 static char *str_tolower (const char *str)
63 char *ret = xbt_strdup (str);
64 int i, n = strlen (ret);
65 for (i = 0; i < n; i++)
66 ret[i] = tolower (str[i]);
70 static const char *instr_find_color (const char *state)
72 char *target = str_tolower (state);
73 const char *ret = nullptr;
75 const char *current = smpi_colors[i];
76 while ((current != nullptr)){
77 if (strcmp (state, current) == 0 //exact match
78 || strstr(target, current) != 0 ){//as substring
79 ret = smpi_colors[i+1];
83 current = smpi_colors[i];
89 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
91 snprintf(container, n, "rank-%d", rank);
95 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
97 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
99 //get the dynar for src#dst
100 char aux[INSTR_DEFAULT_STR_SIZE];
101 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
102 xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
104 if(xbt_dynar_is_empty(d) == 0){
105 //receive was already pushed, perform a get instead
106 TRACE_smpi_get_key(src , dst, key ,n);
111 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
112 xbt_dict_set(keys, aux, d, nullptr);
116 static unsigned long long counter = 0;
118 snprintf(key, n, "%d_%d_%llu", src, dst, counter);
121 char *a = static_cast<char*> (xbt_strdup(key));
122 xbt_dynar_push_as(d, char *, a);
127 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
129 char aux[INSTR_DEFAULT_STR_SIZE];
130 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
131 xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
133 // sometimes the receive may be posted before the send
134 if(xbt_dynar_is_empty(d)){
135 TRACE_smpi_put_key(src, dst, key, n);
139 char *s = xbt_dynar_get_as (d, 0, char *);
140 snprintf (key, n, "%s", s);
141 xbt_dynar_remove_at (d, 0, nullptr);
145 static xbt_dict_t process_category;
147 static void cleanup_extra_data (instr_extra_data extra){
149 if(extra->sendcounts!=nullptr)
150 xbt_free(extra->sendcounts);
151 if(extra->recvcounts!=nullptr)
152 xbt_free(extra->recvcounts);
157 void TRACE_internal_smpi_set_category (const char *category)
159 if (!TRACE_smpi_is_enabled())
163 TRACE_category (category);
165 char processid[INSTR_DEFAULT_STR_SIZE];
166 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
167 if (xbt_dict_get_or_null (process_category, processid))
168 xbt_dict_remove (process_category, processid);
169 if (category != nullptr)
170 xbt_dict_set (process_category, processid, xbt_strdup(category), nullptr);
173 const char *TRACE_internal_smpi_get_category (void)
175 if (!TRACE_smpi_is_enabled())
178 char processid[INSTR_DEFAULT_STR_SIZE];
179 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
180 return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
183 void TRACE_smpi_alloc()
185 keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
186 process_category = xbt_dict_new_homogeneous(xbt_free_f);
189 void TRACE_smpi_release(void)
191 xbt_dict_free(&keys);
192 xbt_dict_free(&process_category);
195 void TRACE_smpi_init(int rank)
197 if (!TRACE_smpi_is_enabled())
200 char str[INSTR_DEFAULT_STR_SIZE];
201 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
204 if (TRACE_smpi_is_grouped()){
205 father = PJ_container_get (SIMIX_host_self_get_name());
207 father = PJ_container_get_root ();
209 xbt_assert(father!=nullptr,
210 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
211 PJ_container_new(str, INSTR_SMPI, father);
214 void TRACE_smpi_finalize(int rank)
216 if (!TRACE_smpi_is_enabled())
219 char str[INSTR_DEFAULT_STR_SIZE];
220 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
221 PJ_container_remove_from_parent (container);
222 PJ_container_free (container);
225 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
227 if (!TRACE_smpi_is_enabled()) {
228 cleanup_extra_data(extra);
232 char str[INSTR_DEFAULT_STR_SIZE];
233 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
234 container_t container = PJ_container_get (str);
235 type_t type = PJ_type_get ("MPI_STATE", container->type);
236 const char *color = instr_find_color (operation);
237 val_t value = PJ_value_get_or_new (operation, color, type);
238 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
241 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
243 if (!TRACE_smpi_is_enabled())
246 char str[INSTR_DEFAULT_STR_SIZE];
247 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
248 container_t container = PJ_container_get (str);
249 type_t type = PJ_type_get ("MPI_STATE", container->type);
251 new_pajePopState (SIMIX_get_clock(), container, type);
254 void TRACE_smpi_computing_init(int rank)
256 //first use, initialize the color in the trace
257 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing())
260 char str[INSTR_DEFAULT_STR_SIZE];
261 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
262 container_t container = PJ_container_get (str);
263 type_t type = PJ_type_get ("MPI_STATE", container->type);
264 const char *color = instr_find_color ("computing");
265 val_t value = PJ_value_get_or_new ("computing", color, type);
266 new_pajePushState (SIMIX_get_clock(), container, type, value);
269 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
271 //do not forget to set the color first, otherwise this will explode
272 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
273 cleanup_extra_data(extra);
277 char str[INSTR_DEFAULT_STR_SIZE];
278 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
279 container_t container = PJ_container_get (str);
280 type_t type = PJ_type_get ("MPI_STATE", container->type);
281 val_t value = PJ_value_get_or_new ("computing", nullptr, type);
282 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
285 void TRACE_smpi_computing_out(int rank)
287 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing())
289 char str[INSTR_DEFAULT_STR_SIZE];
290 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
291 container_t container = PJ_container_get (str);
292 type_t type = PJ_type_get ("MPI_STATE", container->type);
293 new_pajePopState (SIMIX_get_clock(), container, type);
296 void TRACE_smpi_sleeping_init(int rank)
298 //first use, initialize the color in the trace
299 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping())
302 char str[INSTR_DEFAULT_STR_SIZE];
303 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
304 container_t container = PJ_container_get (str);
305 type_t type = PJ_type_get ("MPI_STATE", container->type);
306 const char *color = instr_find_color ("sleeping");
307 val_t value = PJ_value_get_or_new ("sleeping", color, type);
308 new_pajePushState (SIMIX_get_clock(), container, type, value);
311 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
313 //do not forget to set the color first, otherwise this will explode
314 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
315 cleanup_extra_data(extra);
319 char str[INSTR_DEFAULT_STR_SIZE];
320 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
321 container_t container = PJ_container_get (str);
322 type_t type = PJ_type_get ("MPI_STATE", container->type);
323 val_t value = PJ_value_get_or_new ("sleeping", nullptr, type);
324 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
327 void TRACE_smpi_sleeping_out(int rank)
329 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping())
331 char str[INSTR_DEFAULT_STR_SIZE];
332 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
333 container_t container = PJ_container_get (str);
334 type_t type = PJ_type_get ("MPI_STATE", container->type);
335 new_pajePopState (SIMIX_get_clock(), container, type);
338 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
340 //do not forget to set the color first, otherwise this will explode
341 if (!TRACE_smpi_is_enabled()) {
342 cleanup_extra_data(extra);
346 char str[INSTR_DEFAULT_STR_SIZE];
347 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
348 container_t container = PJ_container_get (str);
349 type_t type = PJ_type_get ("MPI_STATE", container->type);
350 val_t value = PJ_value_get_or_new ("test", nullptr, type);
351 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
354 void TRACE_smpi_testing_out(int rank)
356 if (!TRACE_smpi_is_enabled())
358 char str[INSTR_DEFAULT_STR_SIZE];
359 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
360 container_t container = PJ_container_get (str);
361 type_t type = PJ_type_get ("MPI_STATE", container->type);
362 new_pajePopState (SIMIX_get_clock(), container, type);
365 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
367 if (!TRACE_smpi_is_enabled()) {
368 cleanup_extra_data(extra);
372 char str[INSTR_DEFAULT_STR_SIZE];
373 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
374 container_t container = PJ_container_get (str);
375 type_t type = PJ_type_get ("MPI_STATE", container->type);
376 const char *color = instr_find_color (operation);
377 val_t value = PJ_value_get_or_new (operation, color, type);
378 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
381 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
383 if (!TRACE_smpi_is_enabled())
386 char str[INSTR_DEFAULT_STR_SIZE];
387 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
388 container_t container = PJ_container_get (str);
389 type_t type = PJ_type_get ("MPI_STATE", container->type);
391 new_pajePopState (SIMIX_get_clock(), container, type);
394 void TRACE_smpi_send(int rank, int src, int dst, int size)
396 if (!TRACE_smpi_is_enabled())
399 char key[INSTR_DEFAULT_STR_SIZE] = {0};
400 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
402 char str[INSTR_DEFAULT_STR_SIZE];
403 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
404 container_t container = PJ_container_get (str);
405 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
407 new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
410 void TRACE_smpi_recv(int rank, int src, int dst)
412 if (!TRACE_smpi_is_enabled())
415 char key[INSTR_DEFAULT_STR_SIZE] = {0};
416 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
418 char str[INSTR_DEFAULT_STR_SIZE];
419 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
420 container_t container = PJ_container_get (str);
421 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
423 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);