Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4c6054230fa9db8c1bc53926365a78b6e79ccdb4
[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, "%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     xbt_dict_set(keys, aux, d, nullptr);
114   }
115
116   //generate the key
117   static unsigned long long counter = 0;
118   counter++;
119   snprintf(key, n, "%d_%d_%llu", src, dst, counter);
120
121   //push it
122   char *a = static_cast<char*> (xbt_strdup(key));
123   xbt_dynar_push_as(d, char *, a);
124
125   return key;
126 }
127
128 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
129 {
130   char aux[INSTR_DEFAULT_STR_SIZE];
131   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
132   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
133
134   // sometimes the receive may be posted before the send
135   if(xbt_dynar_is_empty(d)){
136       TRACE_smpi_put_key(src, dst, key, n);
137       return key;
138   }
139
140   char *s = xbt_dynar_get_as (d, 0, char *);
141   snprintf (key, n, "%s", s);
142   xbt_dynar_remove_at (d, 0, nullptr);
143   return key;
144 }
145
146 static xbt_dict_t process_category;
147
148 static void cleanup_extra_data (instr_extra_data extra){
149   if(extra!=nullptr){
150     if(extra->sendcounts!=nullptr)
151       xbt_free(extra->sendcounts);
152     if(extra->recvcounts!=nullptr)
153       xbt_free(extra->recvcounts);
154     xbt_free(extra);
155   }
156 }
157
158 void TRACE_internal_smpi_set_category (const char *category)
159 {
160   if (!TRACE_smpi_is_enabled())
161     return;
162
163   //declare category
164   TRACE_category (category);
165
166   char processid[INSTR_DEFAULT_STR_SIZE];
167   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
168   if (xbt_dict_get_or_null (process_category, processid))
169     xbt_dict_remove (process_category, processid);
170   if (category != nullptr)
171     xbt_dict_set (process_category, processid, xbt_strdup(category), nullptr);
172 }
173
174 const char *TRACE_internal_smpi_get_category ()
175 {
176   if (!TRACE_smpi_is_enabled())
177     return nullptr;
178
179   char processid[INSTR_DEFAULT_STR_SIZE];
180   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
181   return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
182 }
183
184 void TRACE_smpi_alloc()
185 {
186   keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
187   process_category = xbt_dict_new_homogeneous(xbt_free_f);
188 }
189
190 void TRACE_smpi_release()
191 {
192   xbt_dict_free(&keys);
193   xbt_dict_free(&process_category);
194 }
195
196 void TRACE_smpi_init(int rank)
197 {
198   if (!TRACE_smpi_is_enabled())
199     return;
200
201   char str[INSTR_DEFAULT_STR_SIZE];
202   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
203
204   container_t father;
205   if (TRACE_smpi_is_grouped()){
206     father = PJ_container_get (SIMIX_host_self_get_name());
207   }else{
208     father = PJ_container_get_root ();
209   }
210   xbt_assert(father!=nullptr,
211       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
212 #if HAVE_PAPI
213   container_t container =
214 #endif
215       PJ_container_new(str, INSTR_SMPI, father);
216 #if HAVE_PAPI
217   papi_counter_t counters = smpi_process_papi_counters();
218
219   for (auto& it : counters) {
220     /**
221      * Check whether this variable already exists or not. Otherwise, it will be created
222      * multiple times but only the last one would be used...
223      */
224     if (PJ_type_get_or_null(it.first.c_str(), container->type) == nullptr) {
225       PJ_type_variable_new(it.first.c_str(), nullptr, container->type);
226     }
227   }
228 #endif
229 }
230
231 void TRACE_smpi_finalize(int rank)
232 {
233   if (!TRACE_smpi_is_enabled())
234     return;
235
236   char str[INSTR_DEFAULT_STR_SIZE];
237   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
238   PJ_container_remove_from_parent (container);
239   PJ_container_free (container);
240 }
241
242 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
243 {
244   if (!TRACE_smpi_is_enabled()) {
245       cleanup_extra_data(extra);
246       return;
247   }
248
249   char str[INSTR_DEFAULT_STR_SIZE];
250   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
251   container_t container = PJ_container_get (str);
252   type_t type = PJ_type_get ("MPI_STATE", container->type);
253   const char *color = instr_find_color (operation);
254   val_t value = PJ_value_get_or_new (operation, color, type);
255    new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
256 }
257
258 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
259 {
260   if (!TRACE_smpi_is_enabled()) 
261     return;
262
263   char str[INSTR_DEFAULT_STR_SIZE];
264   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
265   container_t container = PJ_container_get (str);
266   type_t type = PJ_type_get ("MPI_STATE", container->type);
267
268   new_pajePopState (SIMIX_get_clock(), container, type);
269 }
270
271 void TRACE_smpi_computing_init(int rank)
272 {
273  //first use, initialize the color in the trace
274   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) 
275     return;
276
277   char str[INSTR_DEFAULT_STR_SIZE];
278   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
279   container_t container = PJ_container_get (str);
280   type_t type = PJ_type_get ("MPI_STATE", container->type);
281   const char *color = instr_find_color ("computing");
282   val_t value = PJ_value_get_or_new ("computing", color, type);
283   new_pajePushState (SIMIX_get_clock(), container, type, value);
284 }
285
286 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
287 {
288   //do not forget to set the color first, otherwise this will explode
289   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
290       cleanup_extra_data(extra);
291       return;
292   }
293
294   char str[INSTR_DEFAULT_STR_SIZE];
295   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
296   container_t container = PJ_container_get (str);
297   type_t type = PJ_type_get ("MPI_STATE", container->type);
298   val_t value = PJ_value_get_or_new ("computing", nullptr, type);
299   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
300 }
301
302 void TRACE_smpi_computing_out(int rank)
303 {
304   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing())
305     return;
306   char str[INSTR_DEFAULT_STR_SIZE];
307   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
308   container_t container = PJ_container_get (str);
309   type_t type = PJ_type_get ("MPI_STATE", container->type);
310   new_pajePopState (SIMIX_get_clock(), container, type);
311 }
312
313 void TRACE_smpi_sleeping_init(int rank)
314 {
315   //first use, initialize the color in the trace
316   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping())
317     return;
318
319   char str[INSTR_DEFAULT_STR_SIZE];
320   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
321   container_t container = PJ_container_get (str);
322   type_t type = PJ_type_get ("MPI_STATE", container->type);
323   const char *color = instr_find_color ("sleeping");
324   val_t value = PJ_value_get_or_new ("sleeping", color, type);
325   new_pajePushState (SIMIX_get_clock(), container, type, value);
326 }
327
328 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
329 {
330   //do not forget to set the color first, otherwise this will explode
331   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
332       cleanup_extra_data(extra);
333       return;
334   }
335
336   char str[INSTR_DEFAULT_STR_SIZE];
337   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
338   container_t container = PJ_container_get (str);
339   type_t type = PJ_type_get ("MPI_STATE", container->type);
340   val_t value = PJ_value_get_or_new ("sleeping", nullptr, type);
341   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
342 }
343
344 void TRACE_smpi_sleeping_out(int rank)
345 {
346   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping())
347     return;
348   char str[INSTR_DEFAULT_STR_SIZE];
349   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
350   container_t container = PJ_container_get (str);
351   type_t type = PJ_type_get ("MPI_STATE", container->type);
352   new_pajePopState (SIMIX_get_clock(), container, type);
353 }
354
355 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
356 {
357   //do not forget to set the color first, otherwise this will explode
358   if (!TRACE_smpi_is_enabled()) {
359       cleanup_extra_data(extra);
360       return;
361   }
362
363   char str[INSTR_DEFAULT_STR_SIZE];
364   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
365   container_t container = PJ_container_get (str);
366   type_t type = PJ_type_get ("MPI_STATE", container->type);
367   val_t value = PJ_value_get_or_new ("test", nullptr, type);
368   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
369 }
370
371 void TRACE_smpi_testing_out(int rank)
372 {
373   if (!TRACE_smpi_is_enabled())
374     return;
375   char str[INSTR_DEFAULT_STR_SIZE];
376   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
377   container_t container = PJ_container_get (str);
378   type_t type = PJ_type_get ("MPI_STATE", container->type);
379   new_pajePopState (SIMIX_get_clock(), container, type);
380 }
381
382 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
383 {
384   if (!TRACE_smpi_is_enabled()) {
385       cleanup_extra_data(extra);
386       return;
387   }
388
389   char str[INSTR_DEFAULT_STR_SIZE];
390   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
391   container_t container = PJ_container_get (str);
392   type_t type = PJ_type_get ("MPI_STATE", container->type);
393   const char *color = instr_find_color (operation);
394   val_t value = PJ_value_get_or_new (operation, color, type);
395   new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
396 }
397
398 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
399 {
400   if (!TRACE_smpi_is_enabled())
401     return;
402
403   char str[INSTR_DEFAULT_STR_SIZE];
404   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
405   container_t container = PJ_container_get (str);
406   type_t type = PJ_type_get ("MPI_STATE", container->type);
407
408   new_pajePopState (SIMIX_get_clock(), container, type);
409 }
410
411 void TRACE_smpi_send(int rank, int src, int dst, int size)
412 {
413   if (!TRACE_smpi_is_enabled())
414     return;
415
416   char key[INSTR_DEFAULT_STR_SIZE] = {0};
417   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
418
419   char str[INSTR_DEFAULT_STR_SIZE];
420   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
421   container_t container = PJ_container_get (str);
422   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
423
424   new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
425 }
426
427 void TRACE_smpi_recv(int rank, int src, int dst)
428 {
429   if (!TRACE_smpi_is_enabled())
430     return;
431
432   char key[INSTR_DEFAULT_STR_SIZE] = {0};
433   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
434
435   char str[INSTR_DEFAULT_STR_SIZE];
436   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
437   container_t container = PJ_container_get (str);
438   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
439
440   new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
441 }