Logo AND Algorithmique Numérique Distribuée

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