Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in instr: use sensible identifier names
[simgrid.git] / src / smpi / internals / instr_smpi.cpp
1 /* Copyright (c) 2010, 2012-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8 #include "private.hpp"
9 #include <ctype.h>
10 #include <simgrid/sg_config.h>
11 #include <stdarg.h>
12 #include <wchar.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
15
16 static xbt_dict_t keys;
17
18 static const char *smpi_colors[] ={
19     "recv",     "1 0 0",
20     "irecv",    "1 0.52 0.52",
21     "send",     "0 0 1",
22     "isend",    "0.52 0.52 1",
23     "sendrecv", "0 1 1",
24     "wait",     "1 1 0",
25     "waitall",  "0.78 0.78 0",
26     "waitany",  "0.78 0.78 0.58",
27     "test",     "0.52 0.52 0",
28
29     "allgather",     "1 0 0",
30     "allgatherv",    "1 0.52 0.52",
31     "allreduce",     "1 0 1",
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",
36     "gather",        "1 1 0",
37     "gatherv",       "1 1 0.52",
38     "reduce",        "0 1 0",
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",
44
45     "computing",     "0 1 1",
46     "sleeping",      "0 0.5 0.5",
47
48     "init",       "0 1 0",
49     "finalize",     "0 1 0",
50
51     "put",       "0.3 1 0",
52     "get",       "0 1 0.3",
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",
59     nullptr, nullptr,
60 };
61
62 static char *str_tolower (const char *str)
63 {
64   char* ret = xbt_strdup(str);
65   int n     = strlen(ret);
66   for (int i = 0; i < n; i++)
67     ret[i] = tolower (str[i]);
68   return ret;
69 }
70
71 static const char *instr_find_color (const char *state)
72 {
73   char* target        = str_tolower(state);
74   const char* ret     = nullptr;
75   unsigned int i      = 0;
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];
81          break;
82     }
83     i+=2;
84     current = smpi_colors[i];
85   }
86   xbt_free(target);
87   return ret;
88 }
89
90 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
91 {
92   snprintf(container, n, "rank-%d", rank);
93   return container;
94 }
95
96 static char *TRACE_smpi_get_key(int src, int dst, int tag, char *key, int n, int send);
97
98 static char *TRACE_smpi_put_key(int src, int dst, int tag, char *key, int n, int send)
99 {
100   //get the dynar for src#dst
101   char aux[INSTR_DEFAULT_STR_SIZE];
102   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d#%d#%d", src, dst, tag, send);
103   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
104
105   if (d == nullptr) {
106     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
107     xbt_dict_set(keys, aux, d, nullptr);
108   }
109
110   //generate the key
111   static unsigned long long counter = 0;
112   counter++;
113   snprintf(key, n, "%d_%d_%d_%llu", src, dst, tag, counter);
114
115   //push it
116   char *a = static_cast<char*> (xbt_strdup(key));
117   xbt_dynar_push_as(d, char *, a);
118
119   return key;
120 }
121
122 static char *TRACE_smpi_get_key(int src, int dst, int tag, char *key, int n, int send)
123 {
124   char aux[INSTR_DEFAULT_STR_SIZE];
125   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d#%d#%d", src, dst, tag, send==1?0:1);
126   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
127
128   // first posted
129   if(xbt_dynar_is_empty(d)){
130       TRACE_smpi_put_key(src, dst, tag, key, n, send);
131       return key;
132   }
133
134   char *s = xbt_dynar_get_as (d, 0, char *);
135   snprintf (key, n, "%s", s);
136   xbt_dynar_remove_at (d, 0, nullptr);
137   return key;
138 }
139
140 static xbt_dict_t process_category;
141
142 static void cleanup_extra_data (instr_extra_data extra){
143   if(extra!=nullptr){
144     if(extra->sendcounts!=nullptr)
145       xbt_free(extra->sendcounts);
146     if(extra->recvcounts!=nullptr)
147       xbt_free(extra->recvcounts);
148     xbt_free(extra);
149   }
150 }
151
152 void TRACE_internal_smpi_set_category (const char *category)
153 {
154   if (not TRACE_smpi_is_enabled())
155     return;
156
157   //declare category
158   TRACE_category (category);
159
160   char processid[INSTR_DEFAULT_STR_SIZE];
161   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
162   if (xbt_dict_get_or_null (process_category, processid))
163     xbt_dict_remove (process_category, processid);
164   if (category != nullptr)
165     xbt_dict_set (process_category, processid, xbt_strdup(category), nullptr);
166 }
167
168 const char *TRACE_internal_smpi_get_category ()
169 {
170   if (not TRACE_smpi_is_enabled())
171     return nullptr;
172
173   char processid[INSTR_DEFAULT_STR_SIZE];
174   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
175   return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
176 }
177
178 void TRACE_smpi_alloc()
179 {
180   keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
181   process_category = xbt_dict_new_homogeneous(xbt_free_f);
182 }
183
184 void TRACE_smpi_release()
185 {
186   xbt_dict_free(&keys);
187   xbt_dict_free(&process_category);
188 }
189
190 void TRACE_smpi_init(int rank)
191 {
192   if (not TRACE_smpi_is_enabled())
193     return;
194
195   char str[INSTR_DEFAULT_STR_SIZE];
196   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
197
198   container_t father;
199   if (TRACE_smpi_is_grouped()){
200     father = PJ_container_get(sg_host_self_get_name());
201   }else{
202     father = PJ_container_get_root ();
203   }
204   xbt_assert(father!=nullptr,
205       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
206 #if HAVE_PAPI
207   container_t container =
208 #endif
209       PJ_container_new(str, INSTR_SMPI, father);
210 #if HAVE_PAPI
211   papi_counter_t counters = smpi_process()->papi_counters();
212
213   for (auto& it : counters) {
214     /**
215      * Check whether this variable already exists or not. Otherwise, it will be created
216      * multiple times but only the last one would be used...
217      */
218     if (PJ_type_get_or_null(it.first.c_str(), container->type) == nullptr) {
219       PJ_type_variable_new(it.first.c_str(), nullptr, container->type);
220     }
221   }
222 #endif
223 }
224
225 void TRACE_smpi_finalize(int rank)
226 {
227   if (not TRACE_smpi_is_enabled())
228     return;
229
230   char str[INSTR_DEFAULT_STR_SIZE];
231   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
232   PJ_container_remove_from_parent (container);
233   PJ_container_free (container);
234 }
235
236 void TRACE_smpi_collective_in(int rank, const char *operation, instr_extra_data extra)
237 {
238   if (not TRACE_smpi_is_enabled()) {
239     cleanup_extra_data(extra);
240     return;
241   }
242
243   char str[INSTR_DEFAULT_STR_SIZE];
244   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
245   container_t container = PJ_container_get (str);
246   type_t type = PJ_type_get ("MPI_STATE", container->type);
247   const char *color = instr_find_color (operation);
248   value* val            = value::get_or_new(operation, color, type);
249   new PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
250 }
251
252 void TRACE_smpi_collective_out(int rank, const char *operation)
253 {
254   if (not TRACE_smpi_is_enabled())
255     return;
256
257   char str[INSTR_DEFAULT_STR_SIZE];
258   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
259   container_t container = PJ_container_get (str);
260   type_t type = PJ_type_get ("MPI_STATE", container->type);
261
262   new PopStateEvent (SIMIX_get_clock(), container, type);
263 }
264
265 void TRACE_smpi_computing_init(int rank)
266 {
267  //first use, initialize the color in the trace
268  if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
269    return;
270
271  char str[INSTR_DEFAULT_STR_SIZE];
272  smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
273  container_t container = PJ_container_get(str);
274  type_t type           = PJ_type_get("MPI_STATE", container->type);
275  const char* color     = instr_find_color("computing");
276  new PushStateEvent(SIMIX_get_clock(), container, type, value::get_or_new("computing", color, type));
277 }
278
279 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
280 {
281   //do not forget to set the color first, otherwise this will explode
282   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing()) {
283     cleanup_extra_data(extra);
284     return;
285   }
286
287   char str[INSTR_DEFAULT_STR_SIZE];
288   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
289   container_t container = PJ_container_get (str);
290   type_t type = PJ_type_get ("MPI_STATE", container->type);
291   value* val            = value::get_or_new("computing", nullptr, type);
292   new PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
293 }
294
295 void TRACE_smpi_computing_out(int rank)
296 {
297   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
298     return;
299   char str[INSTR_DEFAULT_STR_SIZE];
300   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
301   container_t container = PJ_container_get (str);
302   type_t type = PJ_type_get ("MPI_STATE", container->type);
303   new PopStateEvent (SIMIX_get_clock(), container, type);
304 }
305
306 void TRACE_smpi_sleeping_init(int rank)
307 {
308   //first use, initialize the color in the trace
309   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
310     return;
311
312   char str[INSTR_DEFAULT_STR_SIZE];
313   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
314   container_t container = PJ_container_get (str);
315   type_t type = PJ_type_get ("MPI_STATE", container->type);
316   const char *color = instr_find_color ("sleeping");
317   value* val            = value::get_or_new("sleeping", color, type);
318   new PushStateEvent(SIMIX_get_clock(), container, type, val);
319 }
320
321 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
322 {
323   //do not forget to set the color first, otherwise this will explode
324   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping()) {
325     cleanup_extra_data(extra);
326     return;
327   }
328
329   char str[INSTR_DEFAULT_STR_SIZE];
330   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
331   container_t container = PJ_container_get (str);
332   type_t type = PJ_type_get ("MPI_STATE", container->type);
333   value* val            = value::get_or_new("sleeping", nullptr, type);
334   new PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
335 }
336
337 void TRACE_smpi_sleeping_out(int rank)
338 {
339   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
340     return;
341   char str[INSTR_DEFAULT_STR_SIZE];
342   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
343   container_t container = PJ_container_get (str);
344   type_t type = PJ_type_get ("MPI_STATE", container->type);
345   new PopStateEvent (SIMIX_get_clock(), container, type);
346 }
347
348 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
349 {
350   //do not forget to set the color first, otherwise this will explode
351   if (not TRACE_smpi_is_enabled()) {
352     cleanup_extra_data(extra);
353     return;
354   }
355
356   char str[INSTR_DEFAULT_STR_SIZE];
357   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
358   container_t container = PJ_container_get (str);
359   type_t type = PJ_type_get ("MPI_STATE", container->type);
360   value* val            = value::get_or_new("test", nullptr, type);
361   new PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
362 }
363
364 void TRACE_smpi_testing_out(int rank)
365 {
366   if (not TRACE_smpi_is_enabled())
367     return;
368   char str[INSTR_DEFAULT_STR_SIZE];
369   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
370   container_t container = PJ_container_get (str);
371   type_t type = PJ_type_get ("MPI_STATE", container->type);
372   new PopStateEvent (SIMIX_get_clock(), container, type);
373 }
374
375 void TRACE_smpi_ptp_in(int rank, const char *operation, instr_extra_data extra)
376 {
377   if (not TRACE_smpi_is_enabled()) {
378     cleanup_extra_data(extra);
379     return;
380   }
381
382   char str[INSTR_DEFAULT_STR_SIZE];
383   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
384   container_t container = PJ_container_get (str);
385   type_t type = PJ_type_get ("MPI_STATE", container->type);
386   const char *color = instr_find_color (operation);
387   value* val            = value::get_or_new(operation, color, type);
388   new PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
389 }
390
391 void TRACE_smpi_ptp_out(int rank, int dst, const char *operation)
392 {
393   if (not TRACE_smpi_is_enabled())
394     return;
395
396   char str[INSTR_DEFAULT_STR_SIZE];
397   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
398   container_t container = PJ_container_get (str);
399   type_t type = PJ_type_get ("MPI_STATE", container->type);
400
401   new PopStateEvent (SIMIX_get_clock(), container, type);
402 }
403
404 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
405 {
406   if (not TRACE_smpi_is_enabled())
407     return;
408
409   char key[INSTR_DEFAULT_STR_SIZE] = {0};
410   TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,1);
411
412   char str[INSTR_DEFAULT_STR_SIZE];
413   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
414   container_t container = PJ_container_get (str);
415   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
416   XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
417   new StartLinkEvent (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
418 }
419
420 void TRACE_smpi_recv(int src, int dst, int tag)
421 {
422   if (not TRACE_smpi_is_enabled())
423     return;
424
425   char key[INSTR_DEFAULT_STR_SIZE] = {0};
426   TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,0);
427
428   char str[INSTR_DEFAULT_STR_SIZE];
429   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
430   container_t container = PJ_container_get (str);
431   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
432   XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
433   new EndLinkEvent (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
434 }