Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ae223f0160e8aa50230ab7cdf92b2c2afec07d08
[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.hpp"
8 #include <cctype>
9 #include <cstdarg>
10 #include <cwchar>
11 #include <deque>
12 #include <simgrid/sg_config.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
15
16 static std::unordered_map<char*, std::deque<std::string>*> 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     "test",     "0.52 0.52 0",
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     "exscan",          "1 0.54 0.25",
42     "scatterv",      "0.52 0 0.52",
43     "scatter",       "1 0.74 0.54",
44
45     "computing",     "0 1 1",
46     "sleeping",      "0 0.5 0.5",
47
48     "init",       "0 1 0",
49     "finalize",     "0 1 0",
50
51     "put",       "0.3 1 0",
52     "get",       "0 1 0.3",
53     "accumulate",       "1 0.3 0",
54     "win_fence",       "1 0 0.3",
55     "win_post",       "1 0 0.8",
56     "win_wait",       "1 0.8 0",
57     "win_start",       "0.8 0 1",
58     "win_complete",       "0.8 1 0",
59     nullptr, nullptr,
60 };
61
62 static char *str_tolower (const char *str)
63 {
64   char* ret = xbt_strdup(str);
65   int n     = strlen(ret);
66   for (int i = 0; i < n; i++)
67     ret[i] = tolower (str[i]);
68   return ret;
69 }
70
71 static const char *instr_find_color (const char *state)
72 {
73   char* target        = str_tolower(state);
74   const char* ret     = nullptr;
75   unsigned int i      = 0;
76   const char* current = smpi_colors[i];
77   while (current != nullptr) {
78     if (strcmp (state, current) == 0 //exact match
79         || strstr(target, current) != 0 ){//as substring
80          ret = smpi_colors[i+1];
81          break;
82     }
83     i+=2;
84     current = smpi_colors[i];
85   }
86   xbt_free(target);
87   return ret;
88 }
89
90 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
91 {
92   snprintf(container, n, "rank-%d", rank);
93   return container;
94 }
95
96 static char *TRACE_smpi_get_key(int src, int dst, int tag, char *key, int n, int send);
97
98 static char *TRACE_smpi_put_key(int src, int dst, int tag, char *key, int n, int send)
99 {
100   //get the dynar for src#dst
101   char aux[INSTR_DEFAULT_STR_SIZE];
102   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d#%d#%d", src, dst, tag, send);
103   auto it = keys.find(aux);
104   std::deque<std::string>* d;
105
106   if (it == keys.end()) {
107     d         = new std::deque<std::string>;
108     keys[aux] = d;
109   } else
110     d = it->second;
111
112   //generate the key
113   static unsigned long long counter = 0;
114   counter++;
115   snprintf(key, n, "%d_%d_%d_%llu", src, dst, tag, counter);
116
117   //push it
118   d->push_back(key);
119
120   return key;
121 }
122
123 static char *TRACE_smpi_get_key(int src, int dst, int tag, char *key, int n, int send)
124 {
125   char aux[INSTR_DEFAULT_STR_SIZE];
126   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d#%d#%d", src, dst, tag, send==1?0:1);
127   auto it = keys.find(aux);
128   if (it == keys.end()) {
129     // first posted
130     TRACE_smpi_put_key(src, dst, tag, key, n, send);
131   } else {
132     snprintf(key, n, "%s", it->second->front().c_str());
133     it->second->pop_front();
134   }
135   return key;
136 }
137
138 static std::unordered_map<smx_actor_t, std::string> process_category;
139
140 static void cleanup_extra_data (instr_extra_data extra){
141   if(extra!=nullptr){
142     if(extra->sendcounts!=nullptr)
143       xbt_free(extra->sendcounts);
144     if(extra->recvcounts!=nullptr)
145       xbt_free(extra->recvcounts);
146     xbt_free(extra);
147   }
148 }
149
150 void TRACE_internal_smpi_set_category (const char *category)
151 {
152   if (not TRACE_smpi_is_enabled())
153     return;
154
155   //declare category
156   TRACE_category (category);
157
158   if (category != nullptr)
159     process_category[SIMIX_process_self()] = category;
160 }
161
162 const char *TRACE_internal_smpi_get_category ()
163 {
164   if (not TRACE_smpi_is_enabled())
165     return nullptr;
166
167   auto it = process_category.find(SIMIX_process_self());
168   return (it == process_category.end()) ? nullptr : it->second.c_str();
169 }
170
171 void TRACE_smpi_alloc()
172 {
173   // for symmetry
174 }
175
176 void TRACE_smpi_release()
177 {
178   for (auto const& elm : keys)
179     delete elm.second;
180 }
181
182 void TRACE_smpi_init(int rank)
183 {
184   if (not TRACE_smpi_is_enabled())
185     return;
186
187   char str[INSTR_DEFAULT_STR_SIZE];
188   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
189
190   container_t father;
191   if (TRACE_smpi_is_grouped()){
192     father = PJ_container_get(sg_host_self_get_name());
193   }else{
194     father = PJ_container_get_root ();
195   }
196   xbt_assert(father!=nullptr,
197       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
198 #if HAVE_PAPI
199   container_t container =
200 #endif
201       new simgrid::instr::Container(str, simgrid::instr::INSTR_SMPI, father);
202 #if HAVE_PAPI
203   papi_counter_t counters = smpi_process()->papi_counters();
204
205   for (auto const& it : counters) {
206     /**
207      * Check whether this variable already exists or not. Otherwise, it will be created
208      * multiple times but only the last one would be used...
209      */
210     if (s_type::getOrNull(it.first.c_str(), container->type_) == nullptr) {
211       Type::variableNew(it.first.c_str(), nullptr, container->type_);
212     }
213   }
214 #endif
215 }
216
217 void TRACE_smpi_finalize(int rank)
218 {
219   if (not TRACE_smpi_is_enabled())
220     return;
221
222   char str[INSTR_DEFAULT_STR_SIZE];
223   container_t container = PJ_container_get(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
224   PJ_container_remove_from_parent (container);
225   delete container;
226 }
227
228 void TRACE_smpi_collective_in(int rank, const char *operation, instr_extra_data extra)
229 {
230   if (not TRACE_smpi_is_enabled()) {
231     cleanup_extra_data(extra);
232     return;
233   }
234
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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
239   const char *color = instr_find_color (operation);
240   simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate(operation, color, type);
241   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
242 }
243
244 void TRACE_smpi_collective_out(int rank, const char *operation)
245 {
246   if (not TRACE_smpi_is_enabled())
247     return;
248
249   char str[INSTR_DEFAULT_STR_SIZE];
250   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
251   container_t container = PJ_container_get (str);
252   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
253
254   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
255 }
256
257 void TRACE_smpi_computing_init(int rank)
258 {
259  //first use, initialize the color in the trace
260  if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
261    return;
262
263  char str[INSTR_DEFAULT_STR_SIZE];
264  smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
265  container_t container = PJ_container_get(str);
266  simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
267  const char* color     = instr_find_color("computing");
268  new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type,
269                                     simgrid::instr::Value::byNameOrCreate("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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
284   simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate("computing", "", type);
285   new simgrid::instr::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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
296   new simgrid::instr::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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
309   const char *color = instr_find_color ("sleeping");
310   simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate("sleeping", color, type);
311   new simgrid::instr::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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
326   simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate("sleeping", "", type);
327   new simgrid::instr::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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
338   new simgrid::instr::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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
353   simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate("test", "", type);
354   new simgrid::instr::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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
365   new simgrid::instr::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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
379   const char *color = instr_find_color (operation);
380   simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate(operation, color, type);
381   new simgrid::instr::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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
393
394   new simgrid::instr::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   simgrid::instr::Type* type = PJ_type_get_root()->getChild("MPI_LINK");
409   XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
410   new simgrid::instr::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   simgrid::instr::Type* type = PJ_type_get_root()->getChild("MPI_LINK");
425   XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
426   new simgrid::instr::EndLinkEvent(SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
427 }