Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] Make smpi_container() available via private.h
[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   const char *current;
75   unsigned int i = 0;
76   while ((current = smpi_colors[i])){
77     if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
78     if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
79     i+=2;
80   }
81   free (target);
82   return ret;
83 }
84
85 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
86 {
87   snprintf(container, n, "rank-%d", rank);
88   return container;
89 }
90
91 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
92
93 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
94 {
95   //get the dynar for src#dst
96   char aux[INSTR_DEFAULT_STR_SIZE];
97   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
98   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
99
100   if(!xbt_dynar_is_empty(d)){
101     //receive was already pushed, perform a get instead
102     TRACE_smpi_get_key(src , dst, key ,n);
103     return key;
104   }
105
106   if (d == NULL) {
107     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
108     xbt_dict_set(keys, aux, d, NULL);
109   }
110
111   //generate the key
112   static unsigned long long counter = 0;
113
114   snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
115
116   //push it
117   char *a = (char*)xbt_strdup(key);
118   xbt_dynar_push_as(d, char *, a);
119
120   return key;
121 }
122
123 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
124 {
125   char aux[INSTR_DEFAULT_STR_SIZE];
126   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
127   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
128
129  // xbt_assert(!xbt_dynar_is_empty(d),
130  //     "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
131
132   // sometimes the receive may be posted before the send
133   if(xbt_dynar_is_empty(d)){
134       TRACE_smpi_put_key(src, dst, key, n);
135       return key;
136   }
137
138   char *s = xbt_dynar_get_as (d, 0, char *);
139   snprintf (key, n, "%s", s);
140   xbt_dynar_remove_at (d, 0, NULL);
141   return key;
142 }
143
144 static xbt_dict_t process_category;
145
146 static void cleanup_extra_data (instr_extra_data extra){
147   if(extra!=NULL){
148     if(extra->sendcounts!=NULL)
149       xbt_free(extra->sendcounts);
150     if(extra->recvcounts!=NULL)
151       xbt_free(extra->recvcounts);
152     xbt_free(extra);
153   }
154 }
155
156 void TRACE_internal_smpi_set_category (const char *category)
157 {
158   if (!TRACE_smpi_is_enabled()) return;
159
160   //declare category
161   TRACE_category (category);
162
163   char processid[INSTR_DEFAULT_STR_SIZE];
164   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
165   if (xbt_dict_get_or_null (process_category, processid))
166     xbt_dict_remove (process_category, processid);
167   if (category != NULL)
168     xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
169 }
170
171 const char *TRACE_internal_smpi_get_category (void)
172 {
173   if (!TRACE_smpi_is_enabled()) return NULL;
174
175   char processid[INSTR_DEFAULT_STR_SIZE];
176   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
177   return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
178 }
179
180 void TRACE_smpi_alloc()
181 {
182   keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
183   process_category = xbt_dict_new_homogeneous(xbt_free_f);
184 }
185
186 void TRACE_smpi_release(void)
187 {
188   xbt_dict_free(&keys);
189   xbt_dict_free(&process_category);
190 }
191
192 void TRACE_smpi_init(int rank)
193 {
194   if (!TRACE_smpi_is_enabled()) return;
195
196   char str[INSTR_DEFAULT_STR_SIZE];
197   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
198
199   container_t father;
200   if (TRACE_smpi_is_grouped()){
201     father = PJ_container_get (SIMIX_host_self_get_name());
202   }else{
203     father = PJ_container_get_root ();
204   }
205   xbt_assert(father!=NULL,
206       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
207   PJ_container_new(str, INSTR_SMPI, father);
208 }
209
210 void TRACE_smpi_finalize(int rank)
211 {
212   if (!TRACE_smpi_is_enabled()) return;
213
214   char str[INSTR_DEFAULT_STR_SIZE];
215   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
216   PJ_container_remove_from_parent (container);
217   PJ_container_free (container);
218 }
219
220 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
221 {
222   if (!TRACE_smpi_is_enabled()) {
223       cleanup_extra_data(extra);
224       return;
225   }
226
227   char str[INSTR_DEFAULT_STR_SIZE];
228   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
229   container_t container = PJ_container_get (str);
230   type_t type = PJ_type_get ("MPI_STATE", container->type);
231   const char *color = instr_find_color (operation);
232   val_t value = PJ_value_get_or_new (operation, color, type);
233    new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
234 }
235
236 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
237 {
238   if (!TRACE_smpi_is_enabled()) return;
239
240   char str[INSTR_DEFAULT_STR_SIZE];
241   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
242   container_t container = PJ_container_get (str);
243   type_t type = PJ_type_get ("MPI_STATE", container->type);
244
245   new_pajePopState (SIMIX_get_clock(), container, type);
246 }
247
248 void TRACE_smpi_computing_init(int rank)
249 {
250  //first use, initialize the color in the trace
251  //TODO : check with lucas and Pierre how to generalize this approach
252   //to avoid unnecessary access to the color array
253   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
254
255   char str[INSTR_DEFAULT_STR_SIZE];
256   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
257   container_t container = PJ_container_get (str);
258   type_t type = PJ_type_get ("MPI_STATE", container->type);
259   const char *color = instr_find_color ("computing");
260   val_t value = PJ_value_get_or_new ("computing", color, type);
261   new_pajePushState (SIMIX_get_clock(), container, type, value);
262 }
263
264 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
265 {
266   //do not forget to set the color first, otherwise this will explode
267   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
268       cleanup_extra_data(extra);
269       return;
270   }
271
272   char str[INSTR_DEFAULT_STR_SIZE];
273   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
274   container_t container = PJ_container_get (str);
275   type_t type = PJ_type_get ("MPI_STATE", container->type);
276   val_t value = PJ_value_get_or_new ("computing", NULL, type);
277   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, (void*)extra);
278 }
279
280 void TRACE_smpi_computing_out(int rank)
281 {
282   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
283   char str[INSTR_DEFAULT_STR_SIZE];
284   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
285   container_t container = PJ_container_get (str);
286   type_t type = PJ_type_get ("MPI_STATE", container->type);
287   new_pajePopState (SIMIX_get_clock(), container, type);
288 }
289
290 void TRACE_smpi_sleeping_init(int rank)
291 {
292  //first use, initialize the color in the trace
293  //TODO : check with lucas and Pierre how to generalize this approach
294   //to avoid unnecessary access to the color array
295   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping()) return;
296
297   char str[INSTR_DEFAULT_STR_SIZE];
298   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
299   container_t container = PJ_container_get (str);
300   type_t type = PJ_type_get ("MPI_STATE", container->type);
301   const char *color = instr_find_color ("sleeping");
302   val_t value = PJ_value_get_or_new ("sleeping", color, type);
303   new_pajePushState (SIMIX_get_clock(), container, type, value);
304 }
305
306 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
307 {
308   //do not forget to set the color first, otherwise this will explode
309   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
310       cleanup_extra_data(extra);
311       return;
312   }
313
314   char str[INSTR_DEFAULT_STR_SIZE];
315   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
316   container_t container = PJ_container_get (str);
317   type_t type = PJ_type_get ("MPI_STATE", container->type);
318   val_t value = PJ_value_get_or_new ("sleeping", NULL, type);
319   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, (void*)extra);
320 }
321
322 void TRACE_smpi_sleeping_out(int rank)
323 {
324   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) return;
325   char str[INSTR_DEFAULT_STR_SIZE];
326   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
327   container_t container = PJ_container_get (str);
328   type_t type = PJ_type_get ("MPI_STATE", container->type);
329   new_pajePopState (SIMIX_get_clock(), container, type);
330 }
331
332 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
333 {
334   //do not forget to set the color first, otherwise this will explode
335   if (!TRACE_smpi_is_enabled()) {
336       cleanup_extra_data(extra);
337       return;
338   }
339
340   char str[INSTR_DEFAULT_STR_SIZE];
341   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
342   container_t container = PJ_container_get (str);
343   type_t type = PJ_type_get ("MPI_STATE", container->type);
344   val_t value = PJ_value_get_or_new ("test", NULL, type);
345   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, (void*)extra);
346 }
347
348 void TRACE_smpi_testing_out(int rank)
349 {
350   if (!TRACE_smpi_is_enabled()) return;
351   char str[INSTR_DEFAULT_STR_SIZE];
352   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
353   container_t container = PJ_container_get (str);
354   type_t type = PJ_type_get ("MPI_STATE", container->type);
355   new_pajePopState (SIMIX_get_clock(), container, type);
356 }
357
358 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
359 {
360   if (!TRACE_smpi_is_enabled()) {
361       cleanup_extra_data(extra);
362       return;
363   }
364
365   char str[INSTR_DEFAULT_STR_SIZE];
366   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
367   container_t container = PJ_container_get (str);
368   type_t type = PJ_type_get ("MPI_STATE", container->type);
369   const char *color = instr_find_color (operation);
370   val_t value = PJ_value_get_or_new (operation, color, type);
371   new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
372 }
373
374 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
375 {
376   if (!TRACE_smpi_is_enabled()) return;
377
378   char str[INSTR_DEFAULT_STR_SIZE];
379   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
380   container_t container = PJ_container_get (str);
381   type_t type = PJ_type_get ("MPI_STATE", container->type);
382
383   new_pajePopState (SIMIX_get_clock(), container, type);
384 }
385
386 void TRACE_smpi_send(int rank, int src, int dst, int size)
387 {
388   if (!TRACE_smpi_is_enabled()) return;
389
390   char key[INSTR_DEFAULT_STR_SIZE] = {0};
391   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
392
393   char str[INSTR_DEFAULT_STR_SIZE];
394   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
395   container_t container = PJ_container_get (str);
396   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
397
398   new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
399 }
400
401 void TRACE_smpi_recv(int rank, int src, int dst)
402 {
403   if (!TRACE_smpi_is_enabled()) return;
404
405   char key[INSTR_DEFAULT_STR_SIZE] = {0};
406   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
407
408   char str[INSTR_DEFAULT_STR_SIZE];
409   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
410   container_t container = PJ_container_get (str);
411   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
412
413   new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
414 }