Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use simgrid function instead of MPI in collectives
[simgrid.git] / src / smpi / 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 "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     NULL, NULL,
45 };
46
47 static char *str_tolower (const char *str)
48 {
49   char *ret = xbt_strdup (str);
50   int i, n = strlen (ret);
51   for (i = 0; i < n; i++)
52     ret[i] = tolower (str[i]);
53   return ret;
54 }
55
56 static const char *instr_find_color (const char *state)
57 {
58   char *target = str_tolower (state);
59   const char *ret = NULL;
60   const char *current;
61   unsigned int i = 0;
62   while ((current = smpi_colors[i])){
63     if (strcmp (state, current) == 0){ ret = smpi_colors[i+1]; break; } //exact match
64     if (strstr(target, current)) { ret = smpi_colors[i+1]; break; }; //as substring
65     i+=2;
66   }
67   free (target);
68   return ret;
69 }
70
71
72 static char *smpi_container(int rank, char *container, int n)
73 {
74   snprintf(container, n, "rank-%d", rank);
75   return container;
76 }
77
78 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
79 {
80   //get the dynar for src#dst
81   char aux[INSTR_DEFAULT_STR_SIZE];
82   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
83   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
84   if (d == NULL) {
85     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
86     xbt_dict_set(keys, aux, d, NULL);
87   }
88   //generate the key
89   static unsigned long long counter = 0;
90   
91   if(MC_is_active())
92     MC_ignore_data_bss(&counter, sizeof(counter));
93
94   snprintf(key, n, "%d_%d_%llu", src, dst, counter++);
95
96   //push it
97   char *a = (char*)xbt_strdup(key);
98   xbt_dynar_push_as(d, char *, a);
99
100   return key;
101 }
102
103 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
104 {
105   char aux[INSTR_DEFAULT_STR_SIZE];
106   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
107   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
108
109   xbt_assert(!xbt_dynar_is_empty(d),
110       "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
111   char *s = xbt_dynar_get_as (d, 0, char *);
112   snprintf (key, n, "%s", s);
113   xbt_dynar_remove_at (d, 0, NULL);
114   return key;
115 }
116
117 static xbt_dict_t process_category;
118
119 void TRACE_internal_smpi_set_category (const char *category)
120 {
121   if (!TRACE_smpi_is_enabled()) return;
122
123   //declare category
124   TRACE_category (category);
125
126   char processid[INSTR_DEFAULT_STR_SIZE];
127   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
128   if (xbt_dict_get_or_null (process_category, processid))
129     xbt_dict_remove (process_category, processid);
130   if (category != NULL)
131     xbt_dict_set (process_category, processid, xbt_strdup(category), NULL);
132 }
133
134 const char *TRACE_internal_smpi_get_category (void)
135 {
136   if (!TRACE_smpi_is_enabled()) return NULL;
137
138   char processid[INSTR_DEFAULT_STR_SIZE];
139   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
140   return xbt_dict_get_or_null (process_category, processid);
141 }
142
143 void TRACE_smpi_alloc()
144 {
145   keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
146   process_category = xbt_dict_new_homogeneous(xbt_free);
147 }
148
149 void TRACE_smpi_release(void)
150 {
151   xbt_dict_free(&keys);
152   xbt_dict_free(&process_category);
153 }
154
155 void TRACE_smpi_init(int rank)
156 {
157   if (!TRACE_smpi_is_enabled()) return;
158
159   char str[INSTR_DEFAULT_STR_SIZE];
160   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
161
162   container_t father;
163   if (TRACE_smpi_is_grouped()){
164     father = PJ_container_get (SIMIX_host_self_get_name());
165   }else{
166     father = PJ_container_get_root ();
167   }
168   xbt_assert(father!=NULL,
169       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
170   PJ_container_new(str, INSTR_SMPI, father);
171 }
172
173 void TRACE_smpi_finalize(int rank)
174 {
175   if (!TRACE_smpi_is_enabled()) return;
176
177   char str[INSTR_DEFAULT_STR_SIZE];
178   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
179   PJ_container_remove_from_parent (container);
180   PJ_container_free (container);
181 }
182
183 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
184 {
185   if (!TRACE_smpi_is_enabled()) return;
186
187   char str[INSTR_DEFAULT_STR_SIZE];
188   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
189   container_t container = PJ_container_get (str);
190   type_t type = PJ_type_get ("MPI_STATE", container->type);
191   const char *color = instr_find_color (operation);
192   val_t value = PJ_value_get_or_new (operation, color, type);
193   new_pajePushState (SIMIX_get_clock(), container, type, value);
194 }
195
196 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
197 {
198   if (!TRACE_smpi_is_enabled()) return;
199
200   char str[INSTR_DEFAULT_STR_SIZE];
201   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
202   container_t container = PJ_container_get (str);
203   type_t type = PJ_type_get ("MPI_STATE", container->type);
204
205   new_pajePopState (SIMIX_get_clock(), container, type);
206 }
207
208 void TRACE_smpi_computing_init(int rank)
209 {
210  //first use, initialize the color in the trace
211  //TODO : check with lucas and Pierre how to generalize this approach
212   //to avoid unnecessary access to the color array
213   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) 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   const char *color = instr_find_color ("computing");
220   val_t value = PJ_value_get_or_new ("computing", color, type);
221   new_pajePushState (SIMIX_get_clock(), container, type, value);
222 }
223
224 void TRACE_smpi_computing_in(int rank)
225 {
226   //do not forget to set the color first, otherwise this will explode
227   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
228
229   char str[INSTR_DEFAULT_STR_SIZE];
230   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
231   container_t container = PJ_container_get (str);
232   type_t type = PJ_type_get ("MPI_STATE", container->type);
233   val_t value = PJ_value_get_or_new ("computing", NULL, type);
234   new_pajePushState (SIMIX_get_clock(), container, type, value);
235 }
236
237 void TRACE_smpi_computing_out(int rank)
238 {
239   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) return;
240   char str[INSTR_DEFAULT_STR_SIZE];
241   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
242   container_t container = PJ_container_get (str);
243   type_t type = PJ_type_get ("MPI_STATE", container->type);
244   new_pajePopState (SIMIX_get_clock(), container, type);
245 }
246
247 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
248 {
249   if (!TRACE_smpi_is_enabled()) return;
250
251
252   char str[INSTR_DEFAULT_STR_SIZE];
253   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
254   container_t container = PJ_container_get (str);
255   type_t type = PJ_type_get ("MPI_STATE", container->type);
256   const char *color = instr_find_color (operation);
257   val_t value = PJ_value_get_or_new (operation, color, type);
258   new_pajePushState (SIMIX_get_clock(), container, type, value);
259 }
260
261 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
262 {
263   if (!TRACE_smpi_is_enabled()) return;
264
265   char str[INSTR_DEFAULT_STR_SIZE];
266   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
267   container_t container = PJ_container_get (str);
268   type_t type = PJ_type_get ("MPI_STATE", container->type);
269
270   new_pajePopState (SIMIX_get_clock(), container, type);
271 }
272
273 void TRACE_smpi_send(int rank, int src, int dst)
274 {
275   if (!TRACE_smpi_is_enabled()) return;
276
277   char key[INSTR_DEFAULT_STR_SIZE];
278   bzero (key, INSTR_DEFAULT_STR_SIZE);
279   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
280
281   char str[INSTR_DEFAULT_STR_SIZE];
282   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
283   container_t container = PJ_container_get (str);
284   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
285
286   new_pajeStartLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
287 }
288
289 void TRACE_smpi_recv(int rank, int src, int dst)
290 {
291   if (!TRACE_smpi_is_enabled()) return;
292
293   char key[INSTR_DEFAULT_STR_SIZE];
294   bzero (key, INSTR_DEFAULT_STR_SIZE);
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 */