Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change size of sent messages for replay testing, and requalify tesh
[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 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_dynar_free_voidp);
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, instr_extra_data extra)
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_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
195 }
196
197
198 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
199 {
200   if (!TRACE_smpi_is_enabled()) return;
201
202   char str[INSTR_DEFAULT_STR_SIZE];
203   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
204   container_t container = PJ_container_get (str);
205   type_t type = PJ_type_get ("MPI_STATE", container->type);
206
207   new_pajePopState (SIMIX_get_clock(), container, type);
208 }
209
210 void TRACE_smpi_computing_init(int rank)
211 {
212  //first use, initialize the color in the trace
213  //TODO : check with lucas and Pierre how to generalize this approach
214   //to avoid unnecessary access to the color array
215   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) return;
216
217   char str[INSTR_DEFAULT_STR_SIZE];
218   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
219   container_t container = PJ_container_get (str);
220   type_t type = PJ_type_get ("MPI_STATE", container->type);
221   const char *color = instr_find_color ("computing");
222   val_t value = PJ_value_get_or_new ("computing", color, type);
223   new_pajePushState (SIMIX_get_clock(), container, type, value);
224 }
225
226 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
227 {
228   //do not forget to set the color first, otherwise this will explode
229   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
230
231   char str[INSTR_DEFAULT_STR_SIZE];
232   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
233   container_t container = PJ_container_get (str);
234   type_t type = PJ_type_get ("MPI_STATE", container->type);
235   val_t value = PJ_value_get_or_new ("computing", NULL, type);
236   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, (void*)extra);
237 }
238
239 void TRACE_smpi_computing_out(int rank)
240 {
241   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
242   char str[INSTR_DEFAULT_STR_SIZE];
243   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
244   container_t container = PJ_container_get (str);
245   type_t type = PJ_type_get ("MPI_STATE", container->type);
246   new_pajePopState (SIMIX_get_clock(), container, type);
247 }
248
249 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
250 {
251   if (!TRACE_smpi_is_enabled()) return;
252
253
254   char str[INSTR_DEFAULT_STR_SIZE];
255   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
256   container_t container = PJ_container_get (str);
257   type_t type = PJ_type_get ("MPI_STATE", container->type);
258   const char *color = instr_find_color (operation);
259   val_t value = PJ_value_get_or_new (operation, color, type);
260   new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, (void*)extra);
261 }
262
263 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
264 {
265   if (!TRACE_smpi_is_enabled()) return;
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
272   new_pajePopState (SIMIX_get_clock(), container, type);
273 }
274
275 void TRACE_smpi_send(int rank, int src, int dst, int size)
276 {
277   if (!TRACE_smpi_is_enabled()) return;
278
279   char key[INSTR_DEFAULT_STR_SIZE] = {0};
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_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
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] = {0};
295   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
296
297   char str[INSTR_DEFAULT_STR_SIZE];
298   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
299   container_t container = PJ_container_get (str);
300   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
301
302   new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
303 }
304 #endif /* HAVE_TRACING */