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. */
10 #include <simgrid/sg_config.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",
27 "test", "0.52 0.52 0",
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",
46 "sleeping", "0 0.5 0.5",
53 "accumulate", "1 0.3 0",
54 "win_fence", "1 0 0.3",
55 "win_post", "1 0 0.8",
56 "win_wait", "1 0.8 0",
57 "win_start", "0.8 0 1",
58 "win_complete", "0.8 1 0",
62 static char *str_tolower (const char *str)
64 char *ret = xbt_strdup (str);
65 int i, n = strlen (ret);
66 for (i = 0; i < n; i++)
67 ret[i] = tolower (str[i]);
71 static const char *instr_find_color (const char *state)
73 char *target = str_tolower (state);
74 const char *ret = nullptr;
76 const char *current = smpi_colors[i];
77 while ((current != nullptr)){
78 if (strcmp (state, current) == 0 //exact match
79 || strstr(target, current) != 0 ){//as substring
80 ret = smpi_colors[i+1];
84 current = smpi_colors[i];
90 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
92 snprintf(container, n, "rank-%d", rank);
96 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
98 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
100 //get the dynar for src#dst
101 char aux[INSTR_DEFAULT_STR_SIZE];
102 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
103 xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
105 if(xbt_dynar_is_empty(d) == 0){
106 //receive was already pushed, perform a get instead
107 TRACE_smpi_get_key(src , dst, key ,n);
112 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
113 xbt_dict_set(keys, aux, d, nullptr);
117 static unsigned long long counter = 0;
119 snprintf(key, n, "%d_%d_%llu", src, dst, counter);
122 char *a = static_cast<char*> (xbt_strdup(key));
123 xbt_dynar_push_as(d, char *, a);
128 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
130 char aux[INSTR_DEFAULT_STR_SIZE];
131 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
132 xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
134 // sometimes the receive may be posted before the send
135 if(xbt_dynar_is_empty(d)){
136 TRACE_smpi_put_key(src, dst, key, n);
140 char *s = xbt_dynar_get_as (d, 0, char *);
141 snprintf (key, n, "%s", s);
142 xbt_dynar_remove_at (d, 0, nullptr);
146 static xbt_dict_t process_category;
148 static void cleanup_extra_data (instr_extra_data extra){
150 if(extra->sendcounts!=nullptr)
151 xbt_free(extra->sendcounts);
152 if(extra->recvcounts!=nullptr)
153 xbt_free(extra->recvcounts);
158 void TRACE_internal_smpi_set_category (const char *category)
160 if (!TRACE_smpi_is_enabled())
164 TRACE_category (category);
166 char processid[INSTR_DEFAULT_STR_SIZE];
167 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
168 if (xbt_dict_get_or_null (process_category, processid))
169 xbt_dict_remove (process_category, processid);
170 if (category != nullptr)
171 xbt_dict_set (process_category, processid, xbt_strdup(category), nullptr);
174 const char *TRACE_internal_smpi_get_category (void)
176 if (!TRACE_smpi_is_enabled())
179 char processid[INSTR_DEFAULT_STR_SIZE];
180 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
181 return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
184 void TRACE_smpi_alloc()
186 keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
187 process_category = xbt_dict_new_homogeneous(xbt_free_f);
190 void TRACE_smpi_release(void)
192 xbt_dict_free(&keys);
193 xbt_dict_free(&process_category);
196 void TRACE_smpi_init(int rank)
198 if (!TRACE_smpi_is_enabled())
201 char str[INSTR_DEFAULT_STR_SIZE];
202 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
205 if (TRACE_smpi_is_grouped()){
206 father = PJ_container_get (SIMIX_host_self_get_name());
208 father = PJ_container_get_root ();
210 xbt_assert(father!=nullptr,
211 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
213 container_t container =
215 PJ_container_new(str, INSTR_SMPI, father);
217 papi_counter_t counters = smpi_process_papi_counters();
219 for (auto& it : counters) {
221 * Check whether this variable already exists or not. Otherwise, it will be created
222 * multiple times but only the last one would be used...
224 if (PJ_type_get_or_null(it.first.c_str(), container->type) == nullptr) {
225 PJ_type_variable_new(it.first.c_str(), NULL, container->type);
231 void TRACE_smpi_finalize(int rank)
233 if (!TRACE_smpi_is_enabled())
236 char str[INSTR_DEFAULT_STR_SIZE];
237 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
238 PJ_container_remove_from_parent (container);
239 PJ_container_free (container);
242 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
244 if (!TRACE_smpi_is_enabled()) {
245 cleanup_extra_data(extra);
249 char str[INSTR_DEFAULT_STR_SIZE];
250 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
251 container_t container = PJ_container_get (str);
252 type_t type = PJ_type_get ("MPI_STATE", container->type);
253 const char *color = instr_find_color (operation);
254 val_t value = PJ_value_get_or_new (operation, color, type);
255 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
258 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
260 if (!TRACE_smpi_is_enabled())
263 char str[INSTR_DEFAULT_STR_SIZE];
264 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
265 container_t container = PJ_container_get (str);
266 type_t type = PJ_type_get ("MPI_STATE", container->type);
268 new_pajePopState (SIMIX_get_clock(), container, type);
271 void TRACE_smpi_computing_init(int rank)
273 //first use, initialize the color in the trace
274 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing())
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 const char *color = instr_find_color ("computing");
282 val_t value = PJ_value_get_or_new ("computing", color, type);
283 new_pajePushState (SIMIX_get_clock(), container, type, value);
286 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
288 //do not forget to set the color first, otherwise this will explode
289 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
290 cleanup_extra_data(extra);
294 char str[INSTR_DEFAULT_STR_SIZE];
295 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
296 container_t container = PJ_container_get (str);
297 type_t type = PJ_type_get ("MPI_STATE", container->type);
298 val_t value = PJ_value_get_or_new ("computing", nullptr, type);
299 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
302 void TRACE_smpi_computing_out(int rank)
304 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing())
306 char str[INSTR_DEFAULT_STR_SIZE];
307 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
308 container_t container = PJ_container_get (str);
309 type_t type = PJ_type_get ("MPI_STATE", container->type);
310 new_pajePopState (SIMIX_get_clock(), container, type);
313 void TRACE_smpi_sleeping_init(int rank)
315 //first use, initialize the color in the trace
316 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping())
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 const char *color = instr_find_color ("sleeping");
324 val_t value = PJ_value_get_or_new ("sleeping", color, type);
325 new_pajePushState (SIMIX_get_clock(), container, type, value);
328 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
330 //do not forget to set the color first, otherwise this will explode
331 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
332 cleanup_extra_data(extra);
336 char str[INSTR_DEFAULT_STR_SIZE];
337 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
338 container_t container = PJ_container_get (str);
339 type_t type = PJ_type_get ("MPI_STATE", container->type);
340 val_t value = PJ_value_get_or_new ("sleeping", nullptr, type);
341 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
344 void TRACE_smpi_sleeping_out(int rank)
346 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping())
348 char str[INSTR_DEFAULT_STR_SIZE];
349 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
350 container_t container = PJ_container_get (str);
351 type_t type = PJ_type_get ("MPI_STATE", container->type);
352 new_pajePopState (SIMIX_get_clock(), container, type);
355 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
357 //do not forget to set the color first, otherwise this will explode
358 if (!TRACE_smpi_is_enabled()) {
359 cleanup_extra_data(extra);
363 char str[INSTR_DEFAULT_STR_SIZE];
364 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
365 container_t container = PJ_container_get (str);
366 type_t type = PJ_type_get ("MPI_STATE", container->type);
367 val_t value = PJ_value_get_or_new ("test", nullptr, type);
368 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
371 void TRACE_smpi_testing_out(int rank)
373 if (!TRACE_smpi_is_enabled())
375 char str[INSTR_DEFAULT_STR_SIZE];
376 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
377 container_t container = PJ_container_get (str);
378 type_t type = PJ_type_get ("MPI_STATE", container->type);
379 new_pajePopState (SIMIX_get_clock(), container, type);
382 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
384 if (!TRACE_smpi_is_enabled()) {
385 cleanup_extra_data(extra);
389 char str[INSTR_DEFAULT_STR_SIZE];
390 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
391 container_t container = PJ_container_get (str);
392 type_t type = PJ_type_get ("MPI_STATE", container->type);
393 const char *color = instr_find_color (operation);
394 val_t value = PJ_value_get_or_new (operation, color, type);
395 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
398 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
400 if (!TRACE_smpi_is_enabled())
403 char str[INSTR_DEFAULT_STR_SIZE];
404 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
405 container_t container = PJ_container_get (str);
406 type_t type = PJ_type_get ("MPI_STATE", container->type);
408 new_pajePopState (SIMIX_get_clock(), container, type);
411 void TRACE_smpi_send(int rank, int src, int dst, int size)
413 if (!TRACE_smpi_is_enabled())
416 char key[INSTR_DEFAULT_STR_SIZE] = {0};
417 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
419 char str[INSTR_DEFAULT_STR_SIZE];
420 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
421 container_t container = PJ_container_get (str);
422 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
424 new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
427 void TRACE_smpi_recv(int rank, int src, int dst)
429 if (!TRACE_smpi_is_enabled())
432 char key[INSTR_DEFAULT_STR_SIZE] = {0};
433 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
435 char str[INSTR_DEFAULT_STR_SIZE];
436 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
437 container_t container = PJ_container_get (str);
438 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
440 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);