Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename last PJ_ functions
[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 <boost/algorithm/string.hpp>
9 #include <cctype>
10 #include <cstdarg>
11 #include <cwchar>
12 #include <deque>
13 #include <simgrid/sg_config.h>
14 #include <string>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
17
18 static std::unordered_map<std::string, std::deque<std::string>*> keys;
19
20 static const char* smpi_colors[] = {
21     "recv",      "1 0 0",       "irecv",         "1 0.52 0.52",    "send",       "0 0 1",
22     "isend",     "0.52 0.52 1", "sendrecv",      "0 1 1",          "wait",       "1 1 0",
23     "waitall",   "0.78 0.78 0", "waitany",       "0.78 0.78 0.58", "test",       "0.52 0.52 0",
24
25     "allgather", "1 0 0",       "allgatherv",    "1 0.52 0.52",    "allreduce",  "1 0 1",
26     "alltoall",  "0.52 0 1",    "alltoallv",     "0.78 0.52 1",    "barrier",    "0 0.78 0.78",
27     "bcast",     "0 0.78 0.39", "gather",        "1 1 0",          "gatherv",    "1 1 0.52",
28     "reduce",    "0 1 0",       "reducescatter", "0.52 1 0.52",    "scan",       "1 0.58 0.23",
29     "exscan",    "1 0.54 0.25", "scatterv",      "0.52 0 0.52",    "scatter",    "1 0.74 0.54",
30
31     "computing", "0 1 1",       "sleeping",      "0 0.5 0.5",
32
33     "init",      "0 1 0",       "finalize",      "0 1 0",
34
35     "put",       "0.3 1 0",     "get",           "0 1 0.3",        "accumulate", "1 0.3 0",
36     "win_fence", "1 0 0.3",     "win_post",      "1 0 0.8",        "win_wait",   "1 0.8 0",
37     "win_start", "0.8 0 1",     "win_complete",  "0.8 1 0",        nullptr,      nullptr,
38 };
39
40 static const char* instr_find_color(const char* state)
41 {
42   std::string target = std::string(state);
43   boost::algorithm::to_lower(target);
44   const char* ret     = nullptr;
45   unsigned int i      = 0;
46   const char* current = smpi_colors[i];
47   while (current != nullptr) {
48     if (target == current                          // exact match
49         || strstr(target.c_str(), current) != 0) { // as substring
50       ret = smpi_colors[i + 1];
51       break;
52     }
53     i+=2;
54     current = smpi_colors[i];
55   }
56   return ret;
57 }
58
59 XBT_PRIVATE std::string smpi_container(int rank)
60 {
61   return std::string("rank-") + std::to_string(rank);
62 }
63
64 static std::string TRACE_smpi_put_key(int src, int dst, int tag, int send)
65 {
66   // get the deque for src#dst
67   std::string aux =
68       std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" + std::to_string(send);
69   auto it = keys.find(aux);
70   std::deque<std::string>* d;
71
72   if (it == keys.end()) {
73     d         = new std::deque<std::string>;
74     keys[aux] = d;
75   } else
76     d = it->second;
77
78   //generate the key
79   static unsigned long long counter = 0;
80   counter++;
81   std::string key =
82       std::to_string(src) + "_" + std::to_string(dst) + "_" + std::to_string(tag) + "_" + std::to_string(counter);
83
84   //push it
85   d->push_back(key);
86
87   return key;
88 }
89
90 static std::string TRACE_smpi_get_key(int src, int dst, int tag, int send)
91 {
92   std::string key;
93   std::string aux = std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" +
94                     std::to_string(send == 1 ? 0 : 1);
95   auto it = keys.find(aux);
96   if (it == keys.end()) {
97     // first posted
98     key = TRACE_smpi_put_key(src, dst, tag, send);
99   } else {
100     key = it->second->front();
101     it->second->pop_front();
102   }
103   return key;
104 }
105
106 static std::unordered_map<smx_actor_t, std::string> process_category;
107
108 static void cleanup_extra_data (instr_extra_data extra){
109   if(extra!=nullptr){
110     if(extra->sendcounts!=nullptr)
111       xbt_free(extra->sendcounts);
112     if(extra->recvcounts!=nullptr)
113       xbt_free(extra->recvcounts);
114     xbt_free(extra);
115   }
116 }
117
118 void TRACE_internal_smpi_set_category (const char *category)
119 {
120   if (not TRACE_smpi_is_enabled())
121     return;
122
123   //declare category
124   TRACE_category (category);
125
126   if (category != nullptr)
127     process_category[SIMIX_process_self()] = category;
128 }
129
130 const char *TRACE_internal_smpi_get_category ()
131 {
132   if (not TRACE_smpi_is_enabled())
133     return nullptr;
134
135   auto it = process_category.find(SIMIX_process_self());
136   return (it == process_category.end()) ? nullptr : it->second.c_str();
137 }
138
139 void TRACE_smpi_alloc()
140 {
141   // for symmetry
142 }
143
144 void TRACE_smpi_release()
145 {
146   for (auto const& elm : keys)
147     delete elm.second;
148 }
149
150 void TRACE_smpi_init(int rank)
151 {
152   if (not TRACE_smpi_is_enabled())
153     return;
154
155   std::string str = smpi_container(rank);
156
157   container_t father;
158   if (TRACE_smpi_is_grouped()){
159     father = simgrid::instr::Container::byNameOrNull(sg_host_self_get_name());
160   }else{
161     father = simgrid::instr::Container::getRootContainer();
162   }
163   xbt_assert(father != nullptr, "Could not find a parent for mpi rank %s at function %s", str.c_str(), __FUNCTION__);
164 #if HAVE_PAPI
165   container_t container =
166 #endif
167       new simgrid::instr::Container(str, "MPI", father);
168 #if HAVE_PAPI
169   papi_counter_t counters = smpi_process()->papi_counters();
170
171   for (auto const& it : counters) {
172     /**
173      * Check whether this variable already exists or not. Otherwise, it will be created
174      * multiple times but only the last one would be used...
175      */
176     if (s_type::getOrNull(it.first.c_str(), container->type_) == nullptr) {
177       Type::variableNew(it.first.c_str(), "", container->type_);
178     }
179   }
180 #endif
181 }
182
183 void TRACE_smpi_finalize(int rank)
184 {
185   if (not TRACE_smpi_is_enabled())
186     return;
187
188   container_t container = simgrid::instr::Container::byName(smpi_container(rank));
189   container->removeFromParent();
190   delete container;
191 }
192
193 void TRACE_smpi_collective_in(int rank, const char *operation, instr_extra_data extra)
194 {
195   if (not TRACE_smpi_is_enabled()) {
196     cleanup_extra_data(extra);
197     return;
198   }
199
200   container_t container      = simgrid::instr::Container::byName(smpi_container(rank));
201   simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
202   const char *color = instr_find_color (operation);
203   type->addEntityValue(operation, color);
204   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, type->getEntityValue(operation),
205                                      static_cast<void*>(extra));
206 }
207
208 void TRACE_smpi_collective_out(int rank, const char *operation)
209 {
210   if (not TRACE_smpi_is_enabled())
211     return;
212
213   container_t container      = simgrid::instr::Container::byName(smpi_container(rank));
214   simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
215
216   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
217 }
218
219 void TRACE_smpi_computing_init(int rank)
220 {
221  //first use, initialize the color in the trace
222  if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
223    return;
224
225  container_t container      = simgrid::instr::Container::byName(smpi_container(rank));
226  simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
227  type->addEntityValue("computing", instr_find_color("computing"));
228  new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, type->getEntityValue("computing"));
229 }
230
231 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
232 {
233   //do not forget to set the color first, otherwise this will explode
234   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing()) {
235     cleanup_extra_data(extra);
236     return;
237   }
238
239   container_t container      = simgrid::instr::Container::byName(smpi_container(rank));
240   simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
241   type->addEntityValue("computing");
242   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, type->getEntityValue("computing"),
243                                      static_cast<void*>(extra));
244 }
245
246 void TRACE_smpi_computing_out(int rank)
247 {
248   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
249     return;
250
251   container_t container      = simgrid::instr::Container::byName(smpi_container(rank));
252   simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
253   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
254 }
255
256 void TRACE_smpi_sleeping_init(int rank)
257 {
258   //first use, initialize the color in the trace
259   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
260     return;
261
262   container_t container       = simgrid::instr::Container::byName(smpi_container(rank));
263   simgrid::instr::Type* state = container->type_->byName("MPI_STATE");
264   state->addEntityValue("sleeping", instr_find_color("sleeping"));
265   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, state, state->getEntityValue("sleeping"));
266 }
267
268 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
269 {
270   //do not forget to set the color first, otherwise this will explode
271   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping()) {
272     cleanup_extra_data(extra);
273     return;
274   }
275
276   container_t container       = simgrid::instr::Container::byName(smpi_container(rank));
277   simgrid::instr::Type* state = container->type_->byName("MPI_STATE");
278   state->addEntityValue("sleeping");
279   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, state, state->getEntityValue("sleeping"),
280                                      static_cast<void*>(extra));
281 }
282
283 void TRACE_smpi_sleeping_out(int rank)
284 {
285   if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
286     return;
287
288   container_t container      = simgrid::instr::Container::byName(smpi_container(rank));
289   simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
290   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
291 }
292
293 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
294 {
295   //do not forget to set the color first, otherwise this will explode
296   if (not TRACE_smpi_is_enabled()) {
297     cleanup_extra_data(extra);
298     return;
299   }
300
301   container_t container       = simgrid::instr::Container::byName(smpi_container(rank));
302   simgrid::instr::Type* state = container->type_->byName("MPI_STATE");
303   state->addEntityValue("test");
304   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, state, state->getEntityValue("test"),
305                                      static_cast<void*>(extra));
306 }
307
308 void TRACE_smpi_testing_out(int rank)
309 {
310   if (not TRACE_smpi_is_enabled())
311     return;
312
313   container_t container      = simgrid::instr::Container::byName(smpi_container(rank));
314   simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
315   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
316 }
317
318 void TRACE_smpi_ptp_in(int rank, const char *operation, instr_extra_data extra)
319 {
320   if (not TRACE_smpi_is_enabled()) {
321     cleanup_extra_data(extra);
322     return;
323   }
324
325   container_t container       = simgrid::instr::Container::byName(smpi_container(rank));
326   simgrid::instr::Type* state = container->type_->byName("MPI_STATE");
327   state->addEntityValue(operation, instr_find_color(operation));
328   new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, state, state->getEntityValue(operation),
329                                      static_cast<void*>(extra));
330 }
331
332 void TRACE_smpi_ptp_out(int rank, int dst, const char *operation)
333 {
334   if (not TRACE_smpi_is_enabled())
335     return;
336
337   container_t container      = simgrid::instr::Container::byName(smpi_container(rank));
338   simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
339
340   new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
341 }
342
343 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
344 {
345   if (not TRACE_smpi_is_enabled())
346     return;
347
348   std::string key = TRACE_smpi_get_key(src, dst, tag, 1);
349
350   container_t container      = simgrid::instr::Container::byName(smpi_container(rank));
351   simgrid::instr::Type* type = simgrid::instr::Type::getRootType()->byName("MPI_LINK");
352   XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
353   new simgrid::instr::StartLinkEvent(SIMIX_get_clock(), simgrid::instr::Container::getRootContainer(), type, container,
354                                      "PTP", key, size);
355 }
356
357 void TRACE_smpi_recv(int src, int dst, int tag)
358 {
359   if (not TRACE_smpi_is_enabled())
360     return;
361
362   std::string key = TRACE_smpi_get_key(src, dst, tag, 0);
363
364   container_t container      = simgrid::instr::Container::byName(smpi_container(dst));
365   simgrid::instr::Type* type = simgrid::instr::Type::getRootType()->byName("MPI_LINK");
366   XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
367   new simgrid::instr::EndLinkEvent(SIMIX_get_clock(), simgrid::instr::Container::getRootContainer(), type, container,
368                                    "PTP", key);
369 }