Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] Introduce variable computation speeds.
[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 <wchar.h>
11 #include <stdarg.h>
12 #include <simgrid/sg_config.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     NULL, NULL,
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 = NULL;
75   const char *current;
76   unsigned int i = 0;
77   while ((current = smpi_colors[i])){
78     if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
79     if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
80     i+=2;
81   }
82   free (target);
83   return ret;
84 }
85
86 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
87 {
88   snprintf(container, n, "rank-%d", rank);
89   return container;
90 }
91
92 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
93
94 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
95 {
96   //get the dynar for src#dst
97   char aux[INSTR_DEFAULT_STR_SIZE];
98   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
99   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
100
101   if(!xbt_dynar_is_empty(d)){
102     //receive was already pushed, perform a get instead
103     TRACE_smpi_get_key(src , dst, key ,n);
104     return key;
105   }
106
107   if (d == NULL) {
108     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
109     xbt_dict_set(keys, aux, d, NULL);
110   }
111
112   //generate the key
113   static unsigned long long counter = 0;
114
115   snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
116
117   //push it
118   char *a = (char*)xbt_strdup(key);
119   xbt_dynar_push_as(d, char *, a);
120
121   return key;
122 }
123
124 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
125 {
126   char aux[INSTR_DEFAULT_STR_SIZE];
127   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
128   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
129
130  // xbt_assert(!xbt_dynar_is_empty(d),
131  //     "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
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 }