Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Actually sonar did find an actual (tiny) bug. Requalify two teshes.
[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 <ctype.h>
9 #include <wchar.h>
10 #include <stdarg.h>
11 #include <simgrid/sg_config.h>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
14
15 static xbt_dict_t keys;
16
17 static const char *smpi_colors[] ={
18     "recv",     "1 0 0",
19     "irecv",    "1 0.52 0.52",
20     "send",     "0 0 1",
21     "isend",    "0.52 0.52 1",
22     "sendrecv", "0 1 1",
23     "wait",     "1 1 0",
24     "waitall",  "0.78 0.78 0",
25     "waitany",  "0.78 0.78 0.58",
26     "test",     "0.52 0.52 0",
27
28     "allgather",     "1 0 0",
29     "allgatherv",    "1 0.52 0.52",
30     "allreduce",     "1 0 1",
31     "alltoall",      "0.52 0 1",
32     "alltoallv",     "0.78 0.52 1",
33     "barrier",       "0 0.78 0.78",
34     "bcast",         "0 0.78 0.39",
35     "gather",        "1 1 0",
36     "gatherv",       "1 1 0.52",
37     "reduce",        "0 1 0",
38     "reducescatter", "0.52 1 0.52",
39     "scan",          "1 0.58 0.23",
40     "exscan",          "1 0.54 0.25",
41     "scatterv",      "0.52 0 0.52",
42     "scatter",       "1 0.74 0.54",
43
44     "computing",     "0 1 1",
45     "sleeping",      "0 0.5 0.5",
46
47     "init",       "0 1 0",
48     "finalize",     "0 1 0",
49
50     "put",       "0.3 1 0",
51     "get",       "0 1 0.3",
52     "accumulate",       "1 0.3 0",
53     "win_fence",       "1 0 0.3",
54     "win_post",       "1 0 0.8",
55     "win_wait",       "1 0.8 0",
56     "win_start",       "0.8 0 1",
57     "win_complete",       "0.8 1 0",
58     NULL, NULL,
59 };
60
61 static char *str_tolower (const char *str)
62 {
63   char *ret = xbt_strdup (str);
64   int i, n = strlen (ret);
65   for (i = 0; i < n; i++)
66     ret[i] = tolower (str[i]);
67   return ret;
68 }
69
70 static const char *instr_find_color (const char *state)
71 {
72   char *target = str_tolower (state);
73   const char *ret = NULL;
74   unsigned int i = 0;
75   const char *current = smpi_colors[i];
76   while ((current != NULL)){
77     if (strcmp (state, current) == 0 //exact match
78         || strstr(target, current) ){//as substring
79          ret = smpi_colors[i+1]; 
80          break; 
81     }
82     i+=2;
83     current = smpi_colors[i];
84   }
85   xbt_free(target);
86   return ret;
87 }
88
89 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
90 {
91   snprintf(container, n, "rank-%d", rank);
92   return container;
93 }
94
95 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
96
97 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
98 {
99   //get the dynar for src#dst
100   char aux[INSTR_DEFAULT_STR_SIZE];
101   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
102   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
103
104   if(xbt_dynar_is_empty(d) == 0){
105     //receive was already pushed, perform a get instead
106     TRACE_smpi_get_key(src , dst, key ,n);
107     return key;
108   }
109
110   if (d == NULL) {
111     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
112     xbt_dict_set(keys, aux, d, NULL);
113   }
114
115   //generate the key
116   static unsigned long long counter = 0;
117   counter++;
118   snprintf(key, n, "%d_%d_%llu", src, dst, counter);
119
120   //push it
121   char *a = static_cast<char*> (xbt_strdup(key));
122   xbt_dynar_push_as(d, char *, a);
123
124   return key;
125 }
126
127 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
128 {
129   char aux[INSTR_DEFAULT_STR_SIZE];
130   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
131   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
132
133   // sometimes the receive may be posted before the send
134   if(xbt_dynar_is_empty(d)){
135       TRACE_smpi_put_key(src, dst, key, n);
136       return key;
137   }
138
139   char *s = xbt_dynar_get_as (d, 0, char *);
140   snprintf (key, n, "%s", s);
141   xbt_dynar_remove_at (d, 0, NULL);
142   return key;
143 }
144
145 static xbt_dict_t process_category;
146
147 static void cleanup_extra_data (instr_extra_data extra){
148   if(extra!=NULL){
149     if(extra->sendcounts!=NULL)
150       xbt_free(extra->sendcounts);
151     if(extra->recvcounts!=NULL)
152       xbt_free(extra->recvcounts);
153     xbt_free(extra);
154   }
155 }
156
157 void TRACE_internal_smpi_set_category (const char *category)
158 {
159   if (!TRACE_smpi_is_enabled()) return;
160
161   //declare category
162   TRACE_category (category);
163
164   char processid[INSTR_DEFAULT_STR_SIZE];
165   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
166   if (xbt_dict_get_or_null (process_category, processid))
167     xbt_dict_remove (process_category, processid);
168   if (category != NULL)
169     xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
170 }
171
172 const char *TRACE_internal_smpi_get_category (void)
173 {
174   if (!TRACE_smpi_is_enabled()) return NULL;
175
176   char processid[INSTR_DEFAULT_STR_SIZE];
177   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
178   return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
179 }
180
181 void TRACE_smpi_alloc()
182 {
183   keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
184   process_category = xbt_dict_new_homogeneous(xbt_free_f);
185 }
186
187 void TRACE_smpi_release(void)
188 {
189   xbt_dict_free(&keys);
190   xbt_dict_free(&process_category);
191 }
192
193 void TRACE_smpi_init(int rank)
194 {
195   if (!TRACE_smpi_is_enabled()) return;
196
197   char str[INSTR_DEFAULT_STR_SIZE];
198   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
199
200   container_t father;
201   if (TRACE_smpi_is_grouped()){
202     father = PJ_container_get (SIMIX_host_self_get_name());
203   }else{
204     father = PJ_container_get_root ();
205   }
206   xbt_assert(father!=NULL,
207       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
208   PJ_container_new(str, INSTR_SMPI, father);
209 }
210
211 void TRACE_smpi_finalize(int rank)
212 {
213   if (!TRACE_smpi_is_enabled()) return;
214
215   char str[INSTR_DEFAULT_STR_SIZE];
216   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
217   PJ_container_remove_from_parent (container);
218   PJ_container_free (container);
219 }
220
221 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
222 {
223   if (!TRACE_smpi_is_enabled()) {
224       cleanup_extra_data(extra);
225       return;
226   }
227
228   char str[INSTR_DEFAULT_STR_SIZE];
229   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
230   container_t container = PJ_container_get (str);
231   type_t type = PJ_type_get ("MPI_STATE", container->type);
232   const char *color = instr_find_color (operation);
233   val_t value = PJ_value_get_or_new (operation, color, type);
234    new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
235 }
236
237 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
238 {
239   if (!TRACE_smpi_is_enabled()) return;
240
241   char str[INSTR_DEFAULT_STR_SIZE];
242   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
243   container_t container = PJ_container_get (str);
244   type_t type = PJ_type_get ("MPI_STATE", container->type);
245
246   new_pajePopState (SIMIX_get_clock(), container, type);
247 }
248
249 void TRACE_smpi_computing_init(int rank)
250 {
251  //first use, initialize the color in the trace
252  //TODO : check with lucas and Pierre how to generalize this approach
253   //to avoid unnecessary access to the color array
254   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
255
256   char str[INSTR_DEFAULT_STR_SIZE];
257   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
258   container_t container = PJ_container_get (str);
259   type_t type = PJ_type_get ("MPI_STATE", container->type);
260   const char *color = instr_find_color ("computing");
261   val_t value = PJ_value_get_or_new ("computing", color, type);
262   new_pajePushState (SIMIX_get_clock(), container, type, value);
263 }
264
265 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
266 {
267   //do not forget to set the color first, otherwise this will explode
268   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
269       cleanup_extra_data(extra);
270       return;
271   }
272
273   char str[INSTR_DEFAULT_STR_SIZE];
274   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
275   container_t container = PJ_container_get (str);
276   type_t type = PJ_type_get ("MPI_STATE", container->type);
277   val_t value = PJ_value_get_or_new ("computing", NULL, type);
278   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, (void*)extra);
279 }
280
281 void TRACE_smpi_computing_out(int rank)
282 {
283   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
284   char str[INSTR_DEFAULT_STR_SIZE];
285   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
286   container_t container = PJ_container_get (str);
287   type_t type = PJ_type_get ("MPI_STATE", container->type);
288   new_pajePopState (SIMIX_get_clock(), container, type);
289 }
290
291 void TRACE_smpi_sleeping_init(int rank)
292 {
293  //first use, initialize the color in the trace
294  //TODO : check with lucas and Pierre how to generalize this approach
295   //to avoid unnecessary access to the color array
296   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping()) return;
297
298   char str[INSTR_DEFAULT_STR_SIZE];
299   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
300   container_t container = PJ_container_get (str);
301   type_t type = PJ_type_get ("MPI_STATE", container->type);
302   const char *color = instr_find_color ("sleeping");
303   val_t value = PJ_value_get_or_new ("sleeping", color, type);
304   new_pajePushState (SIMIX_get_clock(), container, type, value);
305 }
306
307 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
308 {
309   //do not forget to set the color first, otherwise this will explode
310   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
311       cleanup_extra_data(extra);
312       return;
313   }
314
315   char str[INSTR_DEFAULT_STR_SIZE];
316   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
317   container_t container = PJ_container_get (str);
318   type_t type = PJ_type_get ("MPI_STATE", container->type);
319   val_t value = PJ_value_get_or_new ("sleeping", NULL, type);
320   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, (void*)extra);
321 }
322
323 void TRACE_smpi_sleeping_out(int rank)
324 {
325   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) return;
326   char str[INSTR_DEFAULT_STR_SIZE];
327   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
328   container_t container = PJ_container_get (str);
329   type_t type = PJ_type_get ("MPI_STATE", container->type);
330   new_pajePopState (SIMIX_get_clock(), container, type);
331 }
332
333 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
334 {
335   //do not forget to set the color first, otherwise this will explode
336   if (!TRACE_smpi_is_enabled()) {
337       cleanup_extra_data(extra);
338       return;
339   }
340
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   val_t value = PJ_value_get_or_new ("test", NULL, type);
346   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, (void*)extra);
347 }
348
349 void TRACE_smpi_testing_out(int rank)
350 {
351   if (!TRACE_smpi_is_enabled()) return;
352   char str[INSTR_DEFAULT_STR_SIZE];
353   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
354   container_t container = PJ_container_get (str);
355   type_t type = PJ_type_get ("MPI_STATE", container->type);
356   new_pajePopState (SIMIX_get_clock(), container, type);
357 }
358
359 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
360 {
361   if (!TRACE_smpi_is_enabled()) {
362       cleanup_extra_data(extra);
363       return;
364   }
365
366   char str[INSTR_DEFAULT_STR_SIZE];
367   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
368   container_t container = PJ_container_get (str);
369   type_t type = PJ_type_get ("MPI_STATE", container->type);
370   const char *color = instr_find_color (operation);
371   val_t value = PJ_value_get_or_new (operation, color, type);
372   new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
373 }
374
375 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
376 {
377   if (!TRACE_smpi_is_enabled()) return;
378
379   char str[INSTR_DEFAULT_STR_SIZE];
380   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
381   container_t container = PJ_container_get (str);
382   type_t type = PJ_type_get ("MPI_STATE", container->type);
383
384   new_pajePopState (SIMIX_get_clock(), container, type);
385 }
386
387 void TRACE_smpi_send(int rank, int src, int dst, int size)
388 {
389   if (!TRACE_smpi_is_enabled()) return;
390
391   char key[INSTR_DEFAULT_STR_SIZE] = {0};
392   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
393
394   char str[INSTR_DEFAULT_STR_SIZE];
395   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
396   container_t container = PJ_container_get (str);
397   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
398
399   new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
400 }
401
402 void TRACE_smpi_recv(int rank, int src, int dst)
403 {
404   if (!TRACE_smpi_is_enabled()) return;
405
406   char key[INSTR_DEFAULT_STR_SIZE] = {0};
407   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
408
409   char str[INSTR_DEFAULT_STR_SIZE];
410   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
411   container_t container = PJ_container_get (str);
412   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
413
414   new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
415 }