Logo AND Algorithmique Numérique Distribuée

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