Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc' into mc++
[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
14 #ifdef HAVE_TRACING
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
17
18 static xbt_dict_t keys;
19
20 static const char *smpi_colors[] ={
21     "recv",     "1 0 0",
22     "irecv",    "1 0.52 0.52",
23     "send",     "0 0 1",
24     "isend",    "0.52 0.52 1",
25     "sendrecv", "0 1 1",
26     "wait",     "1 1 0",
27     "waitall",  "0.78 0.78 0",
28     "waitany",  "0.78 0.78 0.58",
29
30     "allgather",     "1 0 0",
31     "allgatherv",    "1 0.52 0.52",
32     "allreduce",     "1 0 1",
33     "alltoall",      "0.52 0 1",
34     "alltoallv",     "0.78 0.52 1",
35     "barrier",       "0 0.78 0.78",
36     "bcast",         "0 0.78 0.39",
37     "gather",        "1 1 0",
38     "gatherv",       "1 1 0.52",
39     "reduce",        "0 1 0",
40     "reducescatter", "0.52 1 0.52",
41     "scan",          "1 0.58 0.23",
42     "exscan",          "1 0.54 0.25",
43     "scatterv",      "0.52 0 0.52",
44     "scatter",       "1 0.74 0.54",
45     "computing",     "0 1 1",
46     "init",       "0 1 0",
47     "finalize",     "0 1 0",
48     NULL, NULL,
49 };
50
51 static char *str_tolower (const char *str)
52 {
53   char *ret = xbt_strdup (str);
54   int i, n = strlen (ret);
55   for (i = 0; i < n; i++)
56     ret[i] = tolower (str[i]);
57   return ret;
58 }
59
60 static const char *instr_find_color (const char *state)
61 {
62   char *target = str_tolower (state);
63   const char *ret = NULL;
64   const char *current;
65   unsigned int i = 0;
66   while ((current = smpi_colors[i])){
67     if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
68     if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
69     i+=2;
70   }
71   free (target);
72   return ret;
73 }
74
75
76 static char *smpi_container(int rank, char *container, int n)
77 {
78   snprintf(container, n, "rank-%d", rank);
79   return container;
80 }
81
82 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
83
84
85 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
86 {
87   //get the dynar for src#dst
88   char aux[INSTR_DEFAULT_STR_SIZE];
89   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
90   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
91
92
93   if(!xbt_dynar_is_empty(d)){
94     //receive was already pushed, perform a get instead
95     TRACE_smpi_get_key(src , dst, key ,n);
96     return key;
97   }
98
99   if (d == NULL) {
100     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
101     xbt_dict_set(keys, aux, d, NULL);
102   }
103
104   //generate the key
105   static unsigned long long counter = 0;
106
107   snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
108
109   //push it
110   char *a = (char*)xbt_strdup(key);
111   xbt_dynar_push_as(d, char *, a);
112
113   return key;
114 }
115
116 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
117 {
118   char aux[INSTR_DEFAULT_STR_SIZE];
119   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
120   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
121
122  // xbt_assert(!xbt_dynar_is_empty(d),
123  //     "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
124
125   // sometimes the receive may be posted before the send
126   if(xbt_dynar_is_empty(d)){
127       TRACE_smpi_put_key(src, dst, key, n);
128       return key;
129   }
130
131   char *s = xbt_dynar_get_as (d, 0, char *);
132   snprintf (key, n, "%s", s);
133   xbt_dynar_remove_at (d, 0, NULL);
134   return key;
135 }
136
137 static xbt_dict_t process_category;
138
139 static void cleanup_extra_data (instr_extra_data extra){
140   if(extra!=NULL){
141     if(extra->sendcounts!=NULL)
142       xbt_free(extra->sendcounts);
143     if(extra->recvcounts!=NULL)
144       xbt_free(extra->recvcounts);
145     xbt_free(extra);
146   }
147 }
148
149 void TRACE_internal_smpi_set_category (const char *category)
150 {
151   if (!TRACE_smpi_is_enabled()) return;
152
153   //declare category
154   TRACE_category (category);
155
156   char processid[INSTR_DEFAULT_STR_SIZE];
157   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
158   if (xbt_dict_get_or_null (process_category, processid))
159     xbt_dict_remove (process_category, processid);
160   if (category != NULL)
161     xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
162 }
163
164 const char *TRACE_internal_smpi_get_category (void)
165 {
166   if (!TRACE_smpi_is_enabled()) return NULL;
167
168   char processid[INSTR_DEFAULT_STR_SIZE];
169   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
170   return xbt_dict_get_or_null (process_category, processid);
171 }
172
173 void TRACE_smpi_alloc()
174 {
175   keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
176   process_category = xbt_dict_new_homogeneous(xbt_free);
177 }
178
179 void TRACE_smpi_release(void)
180 {
181   xbt_dict_free(&keys);
182   xbt_dict_free(&process_category);
183 }
184
185 void TRACE_smpi_init(int rank)
186 {
187   if (!TRACE_smpi_is_enabled()) return;
188
189   char str[INSTR_DEFAULT_STR_SIZE];
190   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
191
192   container_t father;
193   if (TRACE_smpi_is_grouped()){
194     father = PJ_container_get (SIMIX_host_self_get_name());
195   }else{
196     father = PJ_container_get_root ();
197   }
198   xbt_assert(father!=NULL,
199       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
200   PJ_container_new(str, INSTR_SMPI, father);
201 }
202
203 void TRACE_smpi_finalize(int rank)
204 {
205   if (!TRACE_smpi_is_enabled()) return;
206
207   char str[INSTR_DEFAULT_STR_SIZE];
208   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
209   PJ_container_remove_from_parent (container);
210   PJ_container_free (container);
211 }
212
213 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
214 {
215   if (!TRACE_smpi_is_enabled()) {
216       cleanup_extra_data(extra);
217       return;
218   }
219
220   char str[INSTR_DEFAULT_STR_SIZE];
221   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
222   container_t container = PJ_container_get (str);
223   type_t type = PJ_type_get ("MPI_STATE", container->type);
224   const char *color = instr_find_color (operation);
225   val_t value = PJ_value_get_or_new (operation, color, type);
226    new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
227 }
228
229
230 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
231 {
232   if (!TRACE_smpi_is_enabled()) return;
233
234   char str[INSTR_DEFAULT_STR_SIZE];
235   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
236   container_t container = PJ_container_get (str);
237   type_t type = PJ_type_get ("MPI_STATE", container->type);
238
239   new_pajePopState (SIMIX_get_clock(), container, type);
240 }
241
242 void TRACE_smpi_computing_init(int rank)
243 {
244  //first use, initialize the color in the trace
245  //TODO : check with lucas and Pierre how to generalize this approach
246   //to avoid unnecessary access to the color array
247   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
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 ("computing");
254   val_t value = PJ_value_get_or_new ("computing", color, type);
255   new_pajePushState (SIMIX_get_clock(), container, type, value);
256 }
257
258 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
259 {
260   //do not forget to set the color first, otherwise this will explode
261   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
262       cleanup_extra_data(extra);
263       return;
264   }
265
266   char str[INSTR_DEFAULT_STR_SIZE];
267   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
268   container_t container = PJ_container_get (str);
269   type_t type = PJ_type_get ("MPI_STATE", container->type);
270   val_t value = PJ_value_get_or_new ("computing", NULL, type);
271   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, (void*)extra);
272 }
273
274 void TRACE_smpi_computing_out(int rank)
275 {
276   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
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   new_pajePopState (SIMIX_get_clock(), container, type);
282 }
283
284 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
285 {
286   if (!TRACE_smpi_is_enabled()) {
287       cleanup_extra_data(extra);
288       return;
289   }
290
291   char str[INSTR_DEFAULT_STR_SIZE];
292   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
293   container_t container = PJ_container_get (str);
294   type_t type = PJ_type_get ("MPI_STATE", container->type);
295   const char *color = instr_find_color (operation);
296   val_t value = PJ_value_get_or_new (operation, color, type);
297   new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
298 }
299
300 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
301 {
302   if (!TRACE_smpi_is_enabled()) return;
303
304   char str[INSTR_DEFAULT_STR_SIZE];
305   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
306   container_t container = PJ_container_get (str);
307   type_t type = PJ_type_get ("MPI_STATE", container->type);
308
309   new_pajePopState (SIMIX_get_clock(), container, type);
310 }
311
312 void TRACE_smpi_send(int rank, int src, int dst, int size)
313 {
314   if (!TRACE_smpi_is_enabled()) return;
315
316   char key[INSTR_DEFAULT_STR_SIZE] = {0};
317   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
318
319   char str[INSTR_DEFAULT_STR_SIZE];
320   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
321   container_t container = PJ_container_get (str);
322   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
323
324   new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
325 }
326
327 void TRACE_smpi_recv(int rank, int src, int dst)
328 {
329   if (!TRACE_smpi_is_enabled()) return;
330
331   char key[INSTR_DEFAULT_STR_SIZE] = {0};
332   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
333
334   char str[INSTR_DEFAULT_STR_SIZE];
335   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
336   container_t container = PJ_container_get (str);
337   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
338
339   new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
340 }
341 #endif /* HAVE_TRACING */