Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'toufic' of github.com:Takishipp/simgrid
[simgrid.git] / src / smpi / 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 i, n = strlen (ret);
66   for (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 (!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 (!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 (!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 (SIMIX_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 (!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, int root, const char *operation, instr_extra_data extra)
237 {
238   if (!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   val_t value = PJ_value_get_or_new (operation, color, type);
249   new PushStateEvent (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
250 }
251
252 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
253 {
254   if (!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 (!TRACE_smpi_is_enabled() || !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   val_t value = PJ_value_get_or_new ("computing", color, type);
277   new PushStateEvent (SIMIX_get_clock(), container, type, value);
278 }
279
280 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
281 {
282   //do not forget to set the color first, otherwise this will explode
283   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
284       cleanup_extra_data(extra);
285       return;
286   }
287
288   char str[INSTR_DEFAULT_STR_SIZE];
289   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
290   container_t container = PJ_container_get (str);
291   type_t type = PJ_type_get ("MPI_STATE", container->type);
292   val_t value = PJ_value_get_or_new ("computing", nullptr, type);
293   new PushStateEvent  (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
294 }
295
296 void TRACE_smpi_computing_out(int rank)
297 {
298   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing())
299     return;
300   char str[INSTR_DEFAULT_STR_SIZE];
301   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
302   container_t container = PJ_container_get (str);
303   type_t type = PJ_type_get ("MPI_STATE", container->type);
304   new PopStateEvent (SIMIX_get_clock(), container, type);
305 }
306
307 void TRACE_smpi_sleeping_init(int rank)
308 {
309   //first use, initialize the color in the trace
310   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping())
311     return;
312
313   char str[INSTR_DEFAULT_STR_SIZE];
314   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
315   container_t container = PJ_container_get (str);
316   type_t type = PJ_type_get ("MPI_STATE", container->type);
317   const char *color = instr_find_color ("sleeping");
318   val_t value = PJ_value_get_or_new ("sleeping", color, type);
319   new PushStateEvent (SIMIX_get_clock(), container, type, value);
320 }
321
322 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
323 {
324   //do not forget to set the color first, otherwise this will explode
325   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
326       cleanup_extra_data(extra);
327       return;
328   }
329
330   char str[INSTR_DEFAULT_STR_SIZE];
331   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
332   container_t container = PJ_container_get (str);
333   type_t type = PJ_type_get ("MPI_STATE", container->type);
334   val_t value = PJ_value_get_or_new ("sleeping", nullptr, type);
335   new PushStateEvent  (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
336 }
337
338 void TRACE_smpi_sleeping_out(int rank)
339 {
340   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping())
341     return;
342   char str[INSTR_DEFAULT_STR_SIZE];
343   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
344   container_t container = PJ_container_get (str);
345   type_t type = PJ_type_get ("MPI_STATE", container->type);
346   new PopStateEvent (SIMIX_get_clock(), container, type);
347 }
348
349 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
350 {
351   //do not forget to set the color first, otherwise this will explode
352   if (!TRACE_smpi_is_enabled()) {
353       cleanup_extra_data(extra);
354       return;
355   }
356
357   char str[INSTR_DEFAULT_STR_SIZE];
358   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
359   container_t container = PJ_container_get (str);
360   type_t type = PJ_type_get ("MPI_STATE", container->type);
361   val_t value = PJ_value_get_or_new ("test", nullptr, type);
362   new PushStateEvent  (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
363 }
364
365 void TRACE_smpi_testing_out(int rank)
366 {
367   if (!TRACE_smpi_is_enabled())
368     return;
369   char str[INSTR_DEFAULT_STR_SIZE];
370   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
371   container_t container = PJ_container_get (str);
372   type_t type = PJ_type_get ("MPI_STATE", container->type);
373   new PopStateEvent (SIMIX_get_clock(), container, type);
374 }
375
376 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
377 {
378   if (!TRACE_smpi_is_enabled()) {
379       cleanup_extra_data(extra);
380       return;
381   }
382
383   char str[INSTR_DEFAULT_STR_SIZE];
384   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
385   container_t container = PJ_container_get (str);
386   type_t type = PJ_type_get ("MPI_STATE", container->type);
387   const char *color = instr_find_color (operation);
388   val_t value = PJ_value_get_or_new (operation, color, type);
389   new PushStateEvent (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
390 }
391
392 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
393 {
394   if (!TRACE_smpi_is_enabled())
395     return;
396
397   char str[INSTR_DEFAULT_STR_SIZE];
398   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
399   container_t container = PJ_container_get (str);
400   type_t type = PJ_type_get ("MPI_STATE", container->type);
401
402   new PopStateEvent (SIMIX_get_clock(), container, type);
403 }
404
405 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
406 {
407   if (!TRACE_smpi_is_enabled())
408     return;
409
410   char key[INSTR_DEFAULT_STR_SIZE] = {0};
411   TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,1);
412
413   char str[INSTR_DEFAULT_STR_SIZE];
414   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
415   container_t container = PJ_container_get (str);
416   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
417   XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
418   new StartLinkEvent (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
419 }
420
421 void TRACE_smpi_recv(int rank, int src, int dst, int tag)
422 {
423   if (!TRACE_smpi_is_enabled())
424     return;
425
426   char key[INSTR_DEFAULT_STR_SIZE] = {0};
427   TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,0);
428
429   char str[INSTR_DEFAULT_STR_SIZE];
430   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
431   container_t container = PJ_container_get (str);
432   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
433   XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
434   new EndLinkEvent (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
435 }