Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] Remove function declaration from smpi_bench.cpp
[simgrid.git] / src / smpi / instr_smpi.cpp
1 /* Copyright (c) 2010, 2012-2015. 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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
14
15 static xbt_dict_t keys;
16
17 static const char *smpi_colors[] ={
18     "recv",     "1 0 0",
19     "irecv",    "1 0.52 0.52",
20     "send",     "0 0 1",
21     "isend",    "0.52 0.52 1",
22     "sendrecv", "0 1 1",
23     "wait",     "1 1 0",
24     "waitall",  "0.78 0.78 0",
25     "waitany",  "0.78 0.78 0.58",
26     "test",     "0.52 0.52 0",
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
44     "computing",     "0 1 1",
45     "sleeping",      "0 0.5 0.5",
46
47     "init",       "0 1 0",
48     "finalize",     "0 1 0",
49
50     "put",       "0.3 1 0",
51     "get",       "0 1 0.3",
52     "accumulate",       "1 0.3 0",
53     "win_fence",       "1 0 0.3",
54     "win_post",       "1 0 0.8",
55     "win_wait",       "1 0.8 0",
56     "win_start",       "0.8 0 1",
57     "win_complete",       "0.8 1 0",
58     nullptr, nullptr,
59 };
60
61 static char *str_tolower (const char *str)
62 {
63   char *ret = xbt_strdup (str);
64   int i, n = strlen (ret);
65   for (i = 0; i < n; i++)
66     ret[i] = tolower (str[i]);
67   return ret;
68 }
69
70 static const char *instr_find_color (const char *state)
71 {
72   char *target = str_tolower (state);
73   const char *ret = nullptr;
74   unsigned int i = 0;
75   const char *current = smpi_colors[i];
76   while ((current != nullptr)){
77     if (strcmp (state, current) == 0 //exact match
78         || strstr(target, current) != 0 ){//as substring
79          ret = smpi_colors[i+1]; 
80          break; 
81     }
82     i+=2;
83     current = smpi_colors[i];
84   }
85   xbt_free(target);
86   return ret;
87 }
88
89 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
90 {
91   snprintf(container, n, "rank-%d", rank);
92   return container;
93 }
94
95 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n);
96
97 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
98 {
99   //get the dynar for src#dst
100   char aux[INSTR_DEFAULT_STR_SIZE];
101   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
102   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
103
104   if(xbt_dynar_is_empty(d) == 0){
105     //receive was already pushed, perform a get instead
106     TRACE_smpi_get_key(src , dst, key ,n);
107     return key;
108   }
109
110   if (d == nullptr) {
111     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
112     xbt_dict_set(keys, aux, d, nullptr);
113   }
114
115   //generate the key
116   static unsigned long long counter = 0;
117   counter++;
118   snprintf(key, n, "%d_%d_%llu", src, dst, counter);
119
120   //push it
121   char *a = static_cast<char*> (xbt_strdup(key));
122   xbt_dynar_push_as(d, char *, a);
123
124   return key;
125 }
126
127 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
128 {
129   char aux[INSTR_DEFAULT_STR_SIZE];
130   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
131   xbt_dynar_t d = static_cast<xbt_dynar_t>(xbt_dict_get_or_null(keys, aux));
132
133   // sometimes the receive may be posted before the send
134   if(xbt_dynar_is_empty(d)){
135       TRACE_smpi_put_key(src, dst, key, n);
136       return key;
137   }
138
139   char *s = xbt_dynar_get_as (d, 0, char *);
140   snprintf (key, n, "%s", s);
141   xbt_dynar_remove_at (d, 0, nullptr);
142   return key;
143 }
144
145 static xbt_dict_t process_category;
146
147 static void cleanup_extra_data (instr_extra_data extra){
148   if(extra!=nullptr){
149     if(extra->sendcounts!=nullptr)
150       xbt_free(extra->sendcounts);
151     if(extra->recvcounts!=nullptr)
152       xbt_free(extra->recvcounts);
153     xbt_free(extra);
154   }
155 }
156
157 void TRACE_internal_smpi_set_category (const char *category)
158 {
159   if (!TRACE_smpi_is_enabled())
160     return;
161
162   //declare category
163   TRACE_category (category);
164
165   char processid[INSTR_DEFAULT_STR_SIZE];
166   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
167   if (xbt_dict_get_or_null (process_category, processid))
168     xbt_dict_remove (process_category, processid);
169   if (category != nullptr)
170     xbt_dict_set (process_category, processid, xbt_strdup(category), nullptr);
171 }
172
173 const char *TRACE_internal_smpi_get_category (void)
174 {
175   if (!TRACE_smpi_is_enabled())
176     return nullptr;
177
178   char processid[INSTR_DEFAULT_STR_SIZE];
179   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
180   return static_cast<char*>(xbt_dict_get_or_null (process_category, processid));
181 }
182
183 void TRACE_smpi_alloc()
184 {
185   keys = xbt_dict_new_homogeneous(xbt_dynar_free_voidp);
186   process_category = xbt_dict_new_homogeneous(xbt_free_f);
187 }
188
189 void TRACE_smpi_release(void)
190 {
191   xbt_dict_free(&keys);
192   xbt_dict_free(&process_category);
193 }
194
195 void TRACE_smpi_init(int rank)
196 {
197   if (!TRACE_smpi_is_enabled())
198     return;
199
200   char str[INSTR_DEFAULT_STR_SIZE];
201   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
202
203   container_t father;
204   if (TRACE_smpi_is_grouped()){
205     father = PJ_container_get (SIMIX_host_self_get_name());
206   }else{
207     father = PJ_container_get_root ();
208   }
209   xbt_assert(father!=nullptr,
210       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
211   PJ_container_new(str, INSTR_SMPI, father);
212 }
213
214 void TRACE_smpi_finalize(int rank)
215 {
216   if (!TRACE_smpi_is_enabled())
217     return;
218
219   char str[INSTR_DEFAULT_STR_SIZE];
220   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
221   PJ_container_remove_from_parent (container);
222   PJ_container_free (container);
223 }
224
225 void TRACE_smpi_collective_in(int rank, int root, const char *operation, instr_extra_data extra)
226 {
227   if (!TRACE_smpi_is_enabled()) {
228       cleanup_extra_data(extra);
229       return;
230   }
231
232   char str[INSTR_DEFAULT_STR_SIZE];
233   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
234   container_t container = PJ_container_get (str);
235   type_t type = PJ_type_get ("MPI_STATE", container->type);
236   const char *color = instr_find_color (operation);
237   val_t value = PJ_value_get_or_new (operation, color, type);
238    new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
239 }
240
241 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
242 {
243   if (!TRACE_smpi_is_enabled()) 
244     return;
245
246   char str[INSTR_DEFAULT_STR_SIZE];
247   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
248   container_t container = PJ_container_get (str);
249   type_t type = PJ_type_get ("MPI_STATE", container->type);
250
251   new_pajePopState (SIMIX_get_clock(), container, type);
252 }
253
254 void TRACE_smpi_computing_init(int rank)
255 {
256  //first use, initialize the color in the trace
257   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_computing()) 
258     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   const char *color = instr_find_color ("computing");
265   val_t value = PJ_value_get_or_new ("computing", color, type);
266   new_pajePushState (SIMIX_get_clock(), container, type, value);
267 }
268
269 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
270 {
271   //do not forget to set the color first, otherwise this will explode
272   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing()) {
273       cleanup_extra_data(extra);
274       return;
275   }
276
277   char str[INSTR_DEFAULT_STR_SIZE];
278   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
279   container_t container = PJ_container_get (str);
280   type_t type = PJ_type_get ("MPI_STATE", container->type);
281   val_t value = PJ_value_get_or_new ("computing", nullptr, type);
282   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
283 }
284
285 void TRACE_smpi_computing_out(int rank)
286 {
287   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_computing())
288     return;
289   char str[INSTR_DEFAULT_STR_SIZE];
290   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
291   container_t container = PJ_container_get (str);
292   type_t type = PJ_type_get ("MPI_STATE", container->type);
293   new_pajePopState (SIMIX_get_clock(), container, type);
294 }
295
296 void TRACE_smpi_sleeping_init(int rank)
297 {
298   //first use, initialize the color in the trace
299   if (!TRACE_smpi_is_enabled() || !TRACE_smpi_is_sleeping())
300     return;
301
302   char str[INSTR_DEFAULT_STR_SIZE];
303   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
304   container_t container = PJ_container_get (str);
305   type_t type = PJ_type_get ("MPI_STATE", container->type);
306   const char *color = instr_find_color ("sleeping");
307   val_t value = PJ_value_get_or_new ("sleeping", color, type);
308   new_pajePushState (SIMIX_get_clock(), container, type, value);
309 }
310
311 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
312 {
313   //do not forget to set the color first, otherwise this will explode
314   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping()) {
315       cleanup_extra_data(extra);
316       return;
317   }
318
319   char str[INSTR_DEFAULT_STR_SIZE];
320   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
321   container_t container = PJ_container_get (str);
322   type_t type = PJ_type_get ("MPI_STATE", container->type);
323   val_t value = PJ_value_get_or_new ("sleeping", nullptr, type);
324   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
325 }
326
327 void TRACE_smpi_sleeping_out(int rank)
328 {
329   if (!TRACE_smpi_is_enabled()|| !TRACE_smpi_is_sleeping())
330     return;
331   char str[INSTR_DEFAULT_STR_SIZE];
332   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
333   container_t container = PJ_container_get (str);
334   type_t type = PJ_type_get ("MPI_STATE", container->type);
335   new_pajePopState (SIMIX_get_clock(), container, type);
336 }
337
338 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
339 {
340   //do not forget to set the color first, otherwise this will explode
341   if (!TRACE_smpi_is_enabled()) {
342       cleanup_extra_data(extra);
343       return;
344   }
345
346   char str[INSTR_DEFAULT_STR_SIZE];
347   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
348   container_t container = PJ_container_get (str);
349   type_t type = PJ_type_get ("MPI_STATE", container->type);
350   val_t value = PJ_value_get_or_new ("test", nullptr, type);
351   new_pajePushStateWithExtra  (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
352 }
353
354 void TRACE_smpi_testing_out(int rank)
355 {
356   if (!TRACE_smpi_is_enabled())
357     return;
358   char str[INSTR_DEFAULT_STR_SIZE];
359   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
360   container_t container = PJ_container_get (str);
361   type_t type = PJ_type_get ("MPI_STATE", container->type);
362   new_pajePopState (SIMIX_get_clock(), container, type);
363 }
364
365 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation, instr_extra_data extra)
366 {
367   if (!TRACE_smpi_is_enabled()) {
368       cleanup_extra_data(extra);
369       return;
370   }
371
372   char str[INSTR_DEFAULT_STR_SIZE];
373   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
374   container_t container = PJ_container_get (str);
375   type_t type = PJ_type_get ("MPI_STATE", container->type);
376   const char *color = instr_find_color (operation);
377   val_t value = PJ_value_get_or_new (operation, color, type);
378   new_pajePushStateWithExtra (SIMIX_get_clock(), container, type, value, static_cast<void*>(extra));
379 }
380
381 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
382 {
383   if (!TRACE_smpi_is_enabled())
384     return;
385
386   char str[INSTR_DEFAULT_STR_SIZE];
387   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
388   container_t container = PJ_container_get (str);
389   type_t type = PJ_type_get ("MPI_STATE", container->type);
390
391   new_pajePopState (SIMIX_get_clock(), container, type);
392 }
393
394 void TRACE_smpi_send(int rank, int src, int dst, int size)
395 {
396   if (!TRACE_smpi_is_enabled())
397     return;
398
399   char key[INSTR_DEFAULT_STR_SIZE] = {0};
400   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
401
402   char str[INSTR_DEFAULT_STR_SIZE];
403   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
404   container_t container = PJ_container_get (str);
405   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
406
407   new_pajeStartLinkWithSize (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
408 }
409
410 void TRACE_smpi_recv(int rank, int src, int dst)
411 {
412   if (!TRACE_smpi_is_enabled())
413     return;
414
415   char key[INSTR_DEFAULT_STR_SIZE] = {0};
416   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
417
418   char str[INSTR_DEFAULT_STR_SIZE];
419   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
420   container_t container = PJ_container_get (str);
421   type_t type = PJ_type_get ("MPI_LINK", PJ_type_get_root());
422
423   new_pajeEndLink (SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
424 }