Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into async-wait
[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 const& 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       new simgrid::instr::Container(str, simgrid::instr::INSTR_SMPI, father);
203 #if HAVE_PAPI
204   papi_counter_t counters = smpi_process()->papi_counters();
205
206   for (auto const& 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   delete 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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
240   const char *color = instr_find_color (operation);
241   simgrid::instr::Value* val = simgrid::instr::Value::get_or_new(operation, color, type);
242   new simgrid::instr::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   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
254
255   new simgrid::instr::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  simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
268  const char* color     = instr_find_color("computing");
269  new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type,
270                                     simgrid::instr::Value::get_or_new("computing", color, type));
271 }
272
273 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
274 {
275   //do not forget to set the color first, otherwise this will explode
276   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing()) {
277     cleanup_extra_data(extra);
278     return;
279   }
280
281   char str[INSTR_DEFAULT_STR_SIZE];
282   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
283   container_t container = PJ_container_get (str);
284   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
285   simgrid::instr::Value* val = simgrid::instr::Value::get_or_new("computing", nullptr, type);
286   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
287 }
288
289 void TRACE_smpi_computing_out(int rank)
290 {
291   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
292     return;
293   char str[INSTR_DEFAULT_STR_SIZE];
294   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
295   container_t container = PJ_container_get (str);
296   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
297   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
298 }
299
300 void TRACE_smpi_sleeping_init(int rank)
301 {
302   //first use, initialize the color in the trace
303   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
304     return;
305
306   char str[INSTR_DEFAULT_STR_SIZE];
307   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
308   container_t container = PJ_container_get (str);
309   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
310   const char *color = instr_find_color ("sleeping");
311   simgrid::instr::Value* val = simgrid::instr::Value::get_or_new("sleeping", color, type);
312   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val);
313 }
314
315 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
316 {
317   //do not forget to set the color first, otherwise this will explode
318   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping()) {
319     cleanup_extra_data(extra);
320     return;
321   }
322
323   char str[INSTR_DEFAULT_STR_SIZE];
324   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
325   container_t container = PJ_container_get (str);
326   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
327   simgrid::instr::Value* val = simgrid::instr::Value::get_or_new("sleeping", nullptr, type);
328   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
329 }
330
331 void TRACE_smpi_sleeping_out(int rank)
332 {
333   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
334     return;
335   char str[INSTR_DEFAULT_STR_SIZE];
336   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
337   container_t container = PJ_container_get (str);
338   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
339   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
340 }
341
342 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
343 {
344   //do not forget to set the color first, otherwise this will explode
345   if (not TRACE_smpi_is_enabled()) {
346     cleanup_extra_data(extra);
347     return;
348   }
349
350   char str[INSTR_DEFAULT_STR_SIZE];
351   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
352   container_t container = PJ_container_get (str);
353   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
354   simgrid::instr::Value* val = simgrid::instr::Value::get_or_new("test", nullptr, type);
355   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
356 }
357
358 void TRACE_smpi_testing_out(int rank)
359 {
360   if (not TRACE_smpi_is_enabled())
361     return;
362   char str[INSTR_DEFAULT_STR_SIZE];
363   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
364   container_t container = PJ_container_get (str);
365   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
366   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
367 }
368
369 void TRACE_smpi_ptp_in(int rank, const char *operation, instr_extra_data extra)
370 {
371   if (not TRACE_smpi_is_enabled()) {
372     cleanup_extra_data(extra);
373     return;
374   }
375
376   char str[INSTR_DEFAULT_STR_SIZE];
377   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
378   container_t container = PJ_container_get (str);
379   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
380   const char *color = instr_find_color (operation);
381   simgrid::instr::Value* val = simgrid::instr::Value::get_or_new(operation, color, type);
382   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
383 }
384
385 void TRACE_smpi_ptp_out(int rank, int dst, const char *operation)
386 {
387   if (not TRACE_smpi_is_enabled())
388     return;
389
390   char str[INSTR_DEFAULT_STR_SIZE];
391   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
392   container_t container = PJ_container_get (str);
393   simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
394
395   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
396 }
397
398 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
399 {
400   if (not TRACE_smpi_is_enabled())
401     return;
402
403   char key[INSTR_DEFAULT_STR_SIZE] = {0};
404   TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,1);
405
406   char str[INSTR_DEFAULT_STR_SIZE];
407   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
408   container_t container = PJ_container_get (str);
409   simgrid::instr::Type* type = PJ_type_get_root()->getChild("MPI_LINK");
410   XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
411   new simgrid::instr::StartLinkEvent(SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
412 }
413
414 void TRACE_smpi_recv(int src, int dst, int tag)
415 {
416   if (not TRACE_smpi_is_enabled())
417     return;
418
419   char key[INSTR_DEFAULT_STR_SIZE] = {0};
420   TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,0);
421
422   char str[INSTR_DEFAULT_STR_SIZE];
423   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
424   container_t container = PJ_container_get (str);
425   simgrid::instr::Type* type = PJ_type_get_root()->getChild("MPI_LINK");
426   XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
427   new simgrid::instr::EndLinkEvent(SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);
428 }