Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cd30016f02096be9cf44cc11f2211c1a24c8efa3
[simgrid.git] / src / instr / instr_smpi.c
1 /* Copyright (c) 2010. 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 "instr/instr_private.h"
8 #include "mc/mc.h"
9 #include <ctype.h>
10 #include <wchar.h>
11
12
13 #ifdef HAVE_TRACING
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
16
17 static xbt_dict_t keys;
18
19 static const char *smpi_colors[] ={
20     "recv",     "1 0 0",
21     "irecv",    "1 0.52 0.52",
22     "send",     "0 0 1",
23     "isend",    "0.52 0.52 1",
24     "sendrecv", "0 1 1",
25     "wait",     "1 1 0",
26     "waitall",  "0.78 0.78 0",
27     "waitany",  "0.78 0.78 0.58",
28
29     "allgather",     "1 0 0",
30     "allgatherv",    "1 0.52 0.52",
31     "allreduce",     "1 0 1",
32     "alltoall",      "0.52 0 1",
33     "alltoallv",     "0.78 0.52 1",
34     "barrier",       "0 0.78 0.78",
35     "bcast",         "0 0.78 0.39",
36     "gather",        "1 1 0",
37     "gatherv",       "1 1 0.52",
38     "reduce",        "0 1 0",
39     "reducescatter", "0.52 1 0.52",
40     "scan",          "1 0.58 0.23",
41     "scatterv",      "0.52 0 0.52",
42     "scatter",       "1 0.74 0.54",
43     "computing",     "0 1 1",
44
45     NULL, NULL,
46 };
47
48 static char *str_tolower (const char *str)
49 {
50   char *ret = xbt_strdup (str);
51   int i, n = strlen (ret);
52   for (i = 0; i < n; i++)
53     ret[i] = tolower (str[i]);
54   return ret;
55 }
56
57 static const char *instr_find_color (const char *state)
58 {
59   char *target = str_tolower (state);
60   const char *ret = NULL;
61   const char *current;
62   unsigned int i = 0;
63   while ((current = smpi_colors[i])){
64     if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
65     if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
66     i+=2;
67   }
68   free (target);
69   return ret;
70 }
71
72
73 static char *smpi_container(int rank, char *container, int n)
74 {
75   snprintf(container, n, "rank-%d", rank);
76   return container;
77 }
78
79 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
80 {
81   //get the dynar for src#dst
82   char aux[INSTR_DEFAULT_STR_SIZE];
83   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
84   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
85   if (d == NULL) {
86     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
87     xbt_dict_set(keys, aux, d, NULL);
88   }
89   //generate the key
90   static unsigned long long counter = 0;
91   
92   if(MC_is_active())
93     MC_ignore_data_bss(&counter, sizeof(counter));
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 void TRACE_internal_smpi_set_category (const char *category)
121 {
122   if (!TRACE_smpi_is_enabled()) return;
123
124   //declare category
125   TRACE_category (category);
126
127   char processid[INSTR_DEFAULT_STR_SIZE];
128   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
129   if (xbt_dict_get_or_null (process_category, processid))
130     xbt_dict_remove (process_category, processid);
131   if (category != NULL)
132     xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
133 }
134
135 const char *TRACE_internal_smpi_get_category (void)
136 {
137   if (!TRACE_smpi_is_enabled()) return NULL;
138
139   char processid[INSTR_DEFAULT_STR_SIZE];
140   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
141   return xbt_dict_get_or_null (process_category, processid);
142 }
143
144 void TRACE_smpi_alloc()
145 {
146   keys = xbt_dict_new_homogeneous(xbt_free);
147   process_category = xbt_dict_new_homogeneous(xbt_free);
148 }
149
150 void TRACE_smpi_release(void)
151 {
152   xbt_dict_free(&keys);
153   xbt_dict_free(&process_category);
154 }
155
156 void TRACE_smpi_init(int rank)
157 {
158   if (!TRACE_smpi_is_enabled()) return;
159
160   char str[INSTR_DEFAULT_STR_SIZE];
161   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
162
163   container_t father;
164   if (TRACE_smpi_is_grouped()){
165     father = PJ_container_get (SIMIX_host_self_get_name());
166   }else{
167     father = PJ_container_get_root ();
168   }
169   xbt_assert(father!=NULL,
170       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
171   PJ_container_new(str, INSTR_SMPI, father);
172 }
173
174 void TRACE_smpi_finalize(int rank)
175 {
176   if (!TRACE_smpi_is_enabled()) return;
177
178   char str[INSTR_DEFAULT_STR_SIZE];
179   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
180   PJ_container_remove_from_parent (container);
181   PJ_container_free (container);
182 }
183
184 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
185 {
186   if (!TRACE_smpi_is_enabled()) return;
187
188   char str[INSTR_DEFAULT_STR_SIZE];
189   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
190   container_t container = PJ_container_get (str);
191   type_t type = PJ_type_get ("MPI_STATE", container->type);
192   const char *color = instr_find_color (operation);
193   val_t value = PJ_value_get_or_new (operation, color, type);
194   new_pajePushState (SIMIX_get_clock(), container, type, value);
195 }
196
197 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
198 {
199   if (!TRACE_smpi_is_enabled()) return;
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
206   new_pajePopState (SIMIX_get_clock(), container, type);
207 }
208
209 void TRACE_smpi_computing_init(int rank)
210 {
211  //first use, initialize the color in the trace
212  //TODO : check with lucas and Pierre how to generalize this approach
213   //to avoid unnecessary access to the color array
214   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
215
216   char str[INSTR_DEFAULT_STR_SIZE];
217   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
218   container_t container = PJ_container_get (str);
219   type_t type = PJ_type_get ("MPI_STATE", container->type);
220   const char *color = instr_find_color ("computing");
221   val_t value = PJ_value_get_or_new ("computing", color, type);
222   new_pajePushState (SIMIX_get_clock(), container, type, value);
223 }
224
225 void TRACE_smpi_computing_in(int rank)
226 {
227   //do not forget to set the color first, otherwise this will explode
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   val_t value = PJ_value_get_or_new ("computing", NULL, type);
235   new_pajePushState (SIMIX_get_clock(), container, type, value);
236 }
237
238 void TRACE_smpi_computing_out(int rank)
239 {
240   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
241   char str[INSTR_DEFAULT_STR_SIZE];
242   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
243   container_t container = PJ_container_get (str);
244   type_t type = PJ_type_get ("MPI_STATE", container->type);
245   new_pajePopState (SIMIX_get_clock(), container, type);
246 }
247
248 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
249 {
250   if (!TRACE_smpi_is_enabled()) return;
251
252
253   char str[INSTR_DEFAULT_STR_SIZE];
254   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
255   container_t container = PJ_container_get (str);
256   type_t type = PJ_type_get ("MPI_STATE", container->type);
257   const char *color = instr_find_color (operation);
258   val_t value = PJ_value_get_or_new (operation, color, type);
259   new_pajePushState (SIMIX_get_clock(), container, type, value);
260 }
261
262 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
263 {
264   if (!TRACE_smpi_is_enabled()) return;
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
271   new_pajePopState (SIMIX_get_clock(), container, type);
272 }
273
274 void TRACE_smpi_send(int rank, int src, int dst)
275 {
276   if (!TRACE_smpi_is_enabled()) return;
277
278   char key[INSTR_DEFAULT_STR_SIZE];
279   bzero (key, INSTR_DEFAULT_STR_SIZE);
280   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
281
282   char str[INSTR_DEFAULT_STR_SIZE];
283   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
284   container_t container = PJ_container_get (str);
285   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
286
287   new_pajeStartLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
288 }
289
290 void TRACE_smpi_recv(int rank, int src, int dst)
291 {
292   if (!TRACE_smpi_is_enabled()) return;
293
294   char key[INSTR_DEFAULT_STR_SIZE];
295   bzero (key, INSTR_DEFAULT_STR_SIZE);
296   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
297
298   char str[INSTR_DEFAULT_STR_SIZE];
299   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
300   container_t container = PJ_container_get (str);
301   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
302
303   new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
304 }
305 #endif /* HAVE_TRACING */