Logo AND Algorithmique Numérique Distribuée

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