Logo AND Algorithmique Numérique Distribuée

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