Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more bash->sh changes
[simgrid.git] / src / smpi / instr_smpi.c
1 /* Copyright (c) 2010, 2012-2013. 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_put_key(int src, int dst, char *key, int n)
83 {
84   //get the dynar for src#dst
85   char aux[INSTR_DEFAULT_STR_SIZE];
86   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
87   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
88   if (d == NULL) {
89     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
90     xbt_dict_set(keys, aux, d, NULL);
91   }
92   //generate the key
93   static unsigned long long counter = 0;
94
95   snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
96
97   //push it
98   char *a = (char*)xbt_strdup(key);
99   xbt_dynar_push_as(d, char *, a);
100
101   return key;
102 }
103
104 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
105 {
106   char aux[INSTR_DEFAULT_STR_SIZE];
107   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
108   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
109
110   xbt_assert(!xbt_dynar_is_empty(d),
111       "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
112   char *s = xbt_dynar_get_as (d, 0, char *);
113   snprintf (key, n, "%s", s);
114   xbt_dynar_remove_at (d, 0, NULL);
115   return key;
116 }
117
118 static xbt_dict_t process_category;
119
120 static void cleanup_extra_data (instr_extra_data extra){
121   if(extra!=NULL){
122     if(extra->sendcounts!=NULL)
123       xbt_free(extra->sendcounts);
124     if(extra->recvcounts!=NULL)
125       xbt_free(extra->recvcounts);
126     xbt_free(extra);
127   }
128 }
129
130 void TRACE_internal_smpi_set_category (const char *category)
131 {
132   if (!TRACE_smpi_is_enabled()) return;
133
134   //declare category
135   TRACE_category (category);
136
137   char processid[INSTR_DEFAULT_STR_SIZE];
138   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
139   if (xbt_dict_get_or_null (process_category, processid))
140     xbt_dict_remove (process_category, processid);
141   if (category != NULL)
142     xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
143 }
144
145 const char *TRACE_internal_smpi_get_category (void)
146 {
147   if (!TRACE_smpi_is_enabled()) return NULL;
148
149   char processid[INSTR_DEFAULT_STR_SIZE];
150   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
151   return xbt_dict_get_or_null (process_category, processid);
152 }
153
154 void TRACE_smpi_alloc()
155 {
156   keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
157   process_category = xbt_dict_new_homogeneous(xbt_free);
158 }
159
160 void TRACE_smpi_release(void)
161 {
162   xbt_dict_free(&keys);
163   xbt_dict_free(&process_category);
164 }
165
166 void TRACE_smpi_init(int rank)
167 {
168   if (!TRACE_smpi_is_enabled()) return;
169
170   char str[INSTR_DEFAULT_STR_SIZE];
171   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
172
173   container_t father;
174   if (TRACE_smpi_is_grouped()){
175     father = PJ_container_get (SIMIX_host_self_get_name());
176   }else{
177     father = PJ_container_get_root ();
178   }
179   xbt_assert(father!=NULL,
180       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
181   PJ_container_new(str, INSTR_SMPI, father);
182 }
183
184 void TRACE_smpi_finalize(int rank)
185 {
186   if (!TRACE_smpi_is_enabled()) return;
187
188   char str[INSTR_DEFAULT_STR_SIZE];
189   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
190   PJ_container_remove_from_parent (container);
191   PJ_container_free (container);
192 }
193
194 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
195 {
196   if (!TRACE_smpi_is_enabled()) {
197       cleanup_extra_data(extra);
198       return;
199   }
200
201   char str[INSTR_DEFAULT_STR_SIZE];
202   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
203   container_t container = PJ_container_get (str);
204   type_t type = PJ_type_get ("MPI_STATE", container->type);
205   const char *color = instr_find_color (operation);
206   val_t value = PJ_value_get_or_new (operation, color, type);
207    new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
208 }
209
210
211 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
212 {
213   if (!TRACE_smpi_is_enabled()) return;
214
215   char str[INSTR_DEFAULT_STR_SIZE];
216   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
217   container_t container = PJ_container_get (str);
218   type_t type = PJ_type_get ("MPI_STATE", container->type);
219
220   new_pajePopState (SIMIX_get_clock(), container, type);
221 }
222
223 void TRACE_smpi_computing_init(int rank)
224 {
225  //first use, initialize the color in the trace
226  //TODO : check with lucas and Pierre how to generalize this approach
227   //to avoid unnecessary access to the color array
228   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
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 ("computing");
235   val_t value = PJ_value_get_or_new ("computing", color, type);
236   new_pajePushState (SIMIX_get_clock(), container, type, value);
237 }
238
239 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
240 {
241   //do not forget to set the color first, otherwise this will explode
242   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
243       cleanup_extra_data(extra);
244       return;
245   }
246
247   char str[INSTR_DEFAULT_STR_SIZE];
248   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
249   container_t container = PJ_container_get (str);
250   type_t type = PJ_type_get ("MPI_STATE", container->type);
251   val_t value = PJ_value_get_or_new ("computing", NULL, type);
252   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, (void*)extra);
253 }
254
255 void TRACE_smpi_computing_out(int rank)
256 {
257   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
258   char str[INSTR_DEFAULT_STR_SIZE];
259   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
260   container_t container = PJ_container_get (str);
261   type_t type = PJ_type_get ("MPI_STATE", container->type);
262   new_pajePopState (SIMIX_get_clock(), container, type);
263 }
264
265 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
266 {
267   if (!TRACE_smpi_is_enabled()) {
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   const char *color = instr_find_color (operation);
277   val_t value = PJ_value_get_or_new (operation, color, type);
278   new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
279 }
280
281 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
282 {
283   if (!TRACE_smpi_is_enabled()) return;
284
285   char str[INSTR_DEFAULT_STR_SIZE];
286   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
287   container_t container = PJ_container_get (str);
288   type_t type = PJ_type_get ("MPI_STATE", container->type);
289
290   new_pajePopState (SIMIX_get_clock(), container, type);
291 }
292
293 void TRACE_smpi_send(int rank, int src, int dst, int size)
294 {
295   if (!TRACE_smpi_is_enabled()) return;
296
297   char key[INSTR_DEFAULT_STR_SIZE] = {0};
298   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
299
300   char str[INSTR_DEFAULT_STR_SIZE];
301   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
302   container_t container = PJ_container_get (str);
303   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
304
305   new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
306 }
307
308 void TRACE_smpi_recv(int rank, int src, int dst)
309 {
310   if (!TRACE_smpi_is_enabled()) return;
311
312   char key[INSTR_DEFAULT_STR_SIZE] = {0};
313   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
314
315   char str[INSTR_DEFAULT_STR_SIZE];
316   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
317   container_t container = PJ_container_get (str);
318   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
319
320   new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
321 }
322 #endif /* HAVE_TRACING */