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, "r%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 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "s%d#%d", src, dst);
114 xbt_dict_set(keys, aux, d, nullptr);
118 static unsigned long long counter = 0;
120 snprintf(key, n, "%d_%d_%llu", src, dst, counter);
123 char *a = static_cast<char*> (xbt_strdup(key));
124 xbt_dynar_push_as(d, char *, a);
129 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
131 char aux[INSTR_DEFAULT_STR_SIZE];
132 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "s%d#%d", src, dst);
133 xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
135 // sometimes the receive may be posted before the send
136 if(xbt_dynar_is_empty(d)){
137 TRACE_smpi_put_key(src, dst, key, n);
141 char *s = xbt_dynar_get_as (d, 0, char *);
142 snprintf (key, n, "%s", s);
143 xbt_dynar_remove_at (d, 0, nullptr);
147 static xbt_dict_t process_category;
149 static void cleanup_extra_data (instr_extra_data extra){
151 if(extra->sendcounts!=nullptr)
152 xbt_free(extra->sendcounts);
153 if(extra->recvcounts!=nullptr)
154 xbt_free(extra->recvcounts);
159 void TRACE_internal_smpi_set_category (const char *category)
161 if (!TRACE_smpi_is_enabled())
165 TRACE_category (category);
167 char processid[INSTR_DEFAULT_STR_SIZE];
168 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
169 if (xbt_dict_get_or_null (process_category, processid))
170 xbt_dict_remove (process_category, processid);
171 if (category != nullptr)
172 xbt_dict_set (process_category, processid, xbt_strdup(category), nullptr);
175 const char *TRACE_internal_smpi_get_category ()
177 if (!TRACE_smpi_is_enabled())
180 char processid[INSTR_DEFAULT_STR_SIZE];
181 snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
182 return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
185 void TRACE_smpi_alloc()
187 keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
188 process_category = xbt_dict_new_homogeneous(xbt_free_f);
191 void TRACE_smpi_release()
193 xbt_dict_free(&keys);
194 xbt_dict_free(&process_category);
197 void TRACE_smpi_init(int rank)
199 if (!TRACE_smpi_is_enabled())
202 char str[INSTR_DEFAULT_STR_SIZE];
203 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
206 if (TRACE_smpi_is_grouped()){
207 father = PJ_container_get (SIMIX_host_self_get_name());
209 father = PJ_container_get_root ();
211 xbt_assert(father!=nullptr,
212 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
214 container_t container =
216 PJ_container_new(str, INSTR_SMPI, father);
218 papi_counter_t counters = smpi_process_papi_counters();
220 for (auto& it : counters) {
222 * Check whether this variable already exists or not. Otherwise, it will be created
223 * multiple times but only the last one would be used...
225 if (PJ_type_get_or_null(it.first.c_str(), container->type) == nullptr) {
226 PJ_type_variable_new(it.first.c_str(), nullptr, container->type);
232 void TRACE_smpi_finalize(int rank)
234 if (!TRACE_smpi_is_enabled())
237 char str[INSTR_DEFAULT_STR_SIZE];
238 container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
239 PJ_container_remove_from_parent (container);
240 PJ_container_free (container);
243 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
245 if (!TRACE_smpi_is_enabled()) {
246 cleanup_extra_data(extra);
250 char str[INSTR_DEFAULT_STR_SIZE];
251 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
252 container_t container = PJ_container_get (str);
253 type_t type = PJ_type_get ("MPI_STATE", container->type);
254 const char *color = instr_find_color (operation);
255 val_t value = PJ_value_get_or_new (operation, color, type);
256 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
259 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
261 if (!TRACE_smpi_is_enabled())
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_computing_init(int rank)
274 //first use, initialize the color in the trace
275 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing())
278 char str[INSTR_DEFAULT_STR_SIZE];
279 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
280 container_t container = PJ_container_get (str);
281 type_t type = PJ_type_get ("MPI_STATE", container->type);
282 const char *color = instr_find_color ("computing");
283 val_t value = PJ_value_get_or_new ("computing", color, type);
284 new_pajePushState (SIMIX_get_clock(), container, type, value);
287 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
289 //do not forget to set the color first, otherwise this will explode
290 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
291 cleanup_extra_data(extra);
295 char str[INSTR_DEFAULT_STR_SIZE];
296 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
297 container_t container = PJ_container_get (str);
298 type_t type = PJ_type_get ("MPI_STATE", container->type);
299 val_t value = PJ_value_get_or_new ("computing", nullptr, type);
300 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
303 void TRACE_smpi_computing_out(int rank)
305 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing())
307 char str[INSTR_DEFAULT_STR_SIZE];
308 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
309 container_t container = PJ_container_get (str);
310 type_t type = PJ_type_get ("MPI_STATE", container->type);
311 new_pajePopState (SIMIX_get_clock(), container, type);
314 void TRACE_smpi_sleeping_init(int rank)
316 //first use, initialize the color in the trace
317 if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping())
320 char str[INSTR_DEFAULT_STR_SIZE];
321 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
322 container_t container = PJ_container_get (str);
323 type_t type = PJ_type_get ("MPI_STATE", container->type);
324 const char *color = instr_find_color ("sleeping");
325 val_t value = PJ_value_get_or_new ("sleeping", color, type);
326 new_pajePushState (SIMIX_get_clock(), container, type, value);
329 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
331 //do not forget to set the color first, otherwise this will explode
332 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
333 cleanup_extra_data(extra);
337 char str[INSTR_DEFAULT_STR_SIZE];
338 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
339 container_t container = PJ_container_get (str);
340 type_t type = PJ_type_get ("MPI_STATE", container->type);
341 val_t value = PJ_value_get_or_new ("sleeping", nullptr, type);
342 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
345 void TRACE_smpi_sleeping_out(int rank)
347 if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping())
349 char str[INSTR_DEFAULT_STR_SIZE];
350 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
351 container_t container = PJ_container_get (str);
352 type_t type = PJ_type_get ("MPI_STATE", container->type);
353 new_pajePopState (SIMIX_get_clock(), container, type);
356 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
358 //do not forget to set the color first, otherwise this will explode
359 if (!TRACE_smpi_is_enabled()) {
360 cleanup_extra_data(extra);
364 char str[INSTR_DEFAULT_STR_SIZE];
365 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
366 container_t container = PJ_container_get (str);
367 type_t type = PJ_type_get ("MPI_STATE", container->type);
368 val_t value = PJ_value_get_or_new ("test", nullptr, type);
369 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
372 void TRACE_smpi_testing_out(int rank)
374 if (!TRACE_smpi_is_enabled())
376 char str[INSTR_DEFAULT_STR_SIZE];
377 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
378 container_t container = PJ_container_get (str);
379 type_t type = PJ_type_get ("MPI_STATE", container->type);
380 new_pajePopState (SIMIX_get_clock(), container, type);
383 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
385 if (!TRACE_smpi_is_enabled()) {
386 cleanup_extra_data(extra);
390 char str[INSTR_DEFAULT_STR_SIZE];
391 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
392 container_t container = PJ_container_get (str);
393 type_t type = PJ_type_get ("MPI_STATE", container->type);
394 const char *color = instr_find_color (operation);
395 val_t value = PJ_value_get_or_new (operation, color, type);
396 new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
399 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
401 if (!TRACE_smpi_is_enabled())
404 char str[INSTR_DEFAULT_STR_SIZE];
405 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
406 container_t container = PJ_container_get (str);
407 type_t type = PJ_type_get ("MPI_STATE", container->type);
409 new_pajePopState (SIMIX_get_clock(), container, type);
412 void TRACE_smpi_send(int rank, int src, int dst, int size)
414 if (!TRACE_smpi_is_enabled())
417 char key[INSTR_DEFAULT_STR_SIZE] = {0};
418 TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
420 char str[INSTR_DEFAULT_STR_SIZE];
421 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
422 container_t container = PJ_container_get (str);
423 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
424 XBT_DEBUG("Send tracing from %d to %d, with key %s", src, dst, key);
425 new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
428 void TRACE_smpi_recv(int rank, int src, int dst)
430 if (!TRACE_smpi_is_enabled())
433 char key[INSTR_DEFAULT_STR_SIZE] = {0};
434 TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
436 char str[INSTR_DEFAULT_STR_SIZE];
437 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
438 container_t container = PJ_container_get (str);
439 type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
440 XBT_DEBUG("Recv tracing from %d to %d, with key %s", src, dst, key);
441 new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);