Logo AND Algorithmique Numérique Distribuée

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