Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix bug in SMPI tracing, where messages were sometimes badly matched.
[simgrid.git] / src / smpi / instr_smpi.cpp
1 /* Copyright (c) 2010, 2012-2015. 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, char *key, int n);
97
98 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
99 {
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));
104
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);
108     return key;
109   }
110
111   if (d == nullptr) {
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);
115   }
116
117   //generate the key
118   static unsigned long long counter = 0;
119   counter++;
120   snprintf(key, n, "%d_%d_%llu", src, dst, counter);
121
122   //push it
123   char *a = static_cast<char*> (xbt_strdup(key));
124   xbt_dynar_push_as(d, char *, a);
125
126   return key;
127 }
128
129 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
130 {
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));
134
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);
138       return key;
139   }
140
141   char *s = xbt_dynar_get_as (d, 0, char *);
142   snprintf (key, n, "%s", s);
143   xbt_dynar_remove_at (d, 0, nullptr);
144   return key;
145 }
146
147 static xbt_dict_t process_category;
148
149 static void cleanup_extra_data (instr_extra_data extra){
150   if(extra!=nullptr){
151     if(extra->sendcounts!=nullptr)
152       xbt_free(extra->sendcounts);
153     if(extra->recvcounts!=nullptr)
154       xbt_free(extra->recvcounts);
155     xbt_free(extra);
156   }
157 }
158
159 void TRACE_internal_smpi_set_category (const char *category)
160 {
161   if (!TRACE_smpi_is_enabled())
162     return;
163
164   //declare category
165   TRACE_category (category);
166
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);
173 }
174
175 const char *TRACE_internal_smpi_get_category ()
176 {
177   if (!TRACE_smpi_is_enabled())
178     return nullptr;
179
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));
183 }
184
185 void TRACE_smpi_alloc()
186 {
187   keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
188   process_category = xbt_dict_new_homogeneous(xbt_free_f);
189 }
190
191 void TRACE_smpi_release()
192 {
193   xbt_dict_free(&keys);
194   xbt_dict_free(&process_category);
195 }
196
197 void TRACE_smpi_init(int rank)
198 {
199   if (!TRACE_smpi_is_enabled())
200     return;
201
202   char str[INSTR_DEFAULT_STR_SIZE];
203   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
204
205   container_t father;
206   if (TRACE_smpi_is_grouped()){
207     father = PJ_container_get (SIMIX_host_self_get_name());
208   }else{
209     father = PJ_container_get_root ();
210   }
211   xbt_assert(father!=nullptr,
212       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
213 #if HAVE_PAPI
214   container_t container =
215 #endif
216       PJ_container_new(str, INSTR_SMPI, father);
217 #if HAVE_PAPI
218   papi_counter_t counters = smpi_process_papi_counters();
219
220   for (auto& it : counters) {
221     /**
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...
224      */
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);
227     }
228   }
229 #endif
230 }
231
232 void TRACE_smpi_finalize(int rank)
233 {
234   if (!TRACE_smpi_is_enabled())
235     return;
236
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);
241 }
242
243 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
244 {
245   if (!TRACE_smpi_is_enabled()) {
246       cleanup_extra_data(extra);
247       return;
248   }
249
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));
257 }
258
259 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
260 {
261   if (!TRACE_smpi_is_enabled()) 
262     return;
263
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);
268
269   new_pajePopState (SIMIX_get_clock(), container, type);
270 }
271
272 void TRACE_smpi_computing_init(int rank)
273 {
274  //first use, initialize the color in the trace
275   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) 
276     return;
277
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);
285 }
286
287 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
288 {
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);
292       return;
293   }
294
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));
301 }
302
303 void TRACE_smpi_computing_out(int rank)
304 {
305   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing())
306     return;
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);
312 }
313
314 void TRACE_smpi_sleeping_init(int rank)
315 {
316   //first use, initialize the color in the trace
317   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping())
318     return;
319
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);
327 }
328
329 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
330 {
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);
334       return;
335   }
336
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));
343 }
344
345 void TRACE_smpi_sleeping_out(int rank)
346 {
347   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping())
348     return;
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);
354 }
355
356 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
357 {
358   //do not forget to set the color first, otherwise this will explode
359   if (!TRACE_smpi_is_enabled()) {
360       cleanup_extra_data(extra);
361       return;
362   }
363
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));
370 }
371
372 void TRACE_smpi_testing_out(int rank)
373 {
374   if (!TRACE_smpi_is_enabled())
375     return;
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);
381 }
382
383 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
384 {
385   if (!TRACE_smpi_is_enabled()) {
386       cleanup_extra_data(extra);
387       return;
388   }
389
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));
397 }
398
399 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
400 {
401   if (!TRACE_smpi_is_enabled())
402     return;
403
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);
408
409   new_pajePopState (SIMIX_get_clock(), container, type);
410 }
411
412 void TRACE_smpi_send(int rank, int src, int dst, int size)
413 {
414   if (!TRACE_smpi_is_enabled())
415     return;
416
417   char key[INSTR_DEFAULT_STR_SIZE] = {0};
418   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
419
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);
426 }
427
428 void TRACE_smpi_recv(int rank, int src, int dst)
429 {
430   if (!TRACE_smpi_is_enabled())
431     return;
432
433   char key[INSTR_DEFAULT_STR_SIZE] = {0};
434   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
435
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);
442 }