Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill now unused xbt_heap.
[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 container_t smpi_container(int rank)
60 {
61   return simgrid::instr::Container::byName(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 void TRACE_internal_smpi_set_category (const char *category)
109 {
110   if (not TRACE_smpi_is_enabled())
111     return;
112
113   //declare category
114   TRACE_category (category);
115
116   if (category != nullptr)
117     process_category[SIMIX_process_self()] = category;
118 }
119
120 const char *TRACE_internal_smpi_get_category ()
121 {
122   if (not TRACE_smpi_is_enabled())
123     return nullptr;
124
125   auto it = process_category.find(SIMIX_process_self());
126   return (it == process_category.end()) ? nullptr : it->second.c_str();
127 }
128
129 void TRACE_smpi_alloc()
130 {
131   // for symmetry
132 }
133
134 void TRACE_smpi_release()
135 {
136   for (auto const& elm : keys)
137     delete elm.second;
138 }
139
140 void TRACE_smpi_init(int rank)
141 {
142   if (not TRACE_smpi_is_enabled())
143     return;
144
145   std::string str = std::string("rank-") + std::to_string(rank);
146
147   container_t father;
148   if (TRACE_smpi_is_grouped()){
149     father = simgrid::instr::Container::byNameOrNull(sg_host_self_get_name());
150   }else{
151     father = simgrid::instr::Container::getRoot();
152   }
153   xbt_assert(father != nullptr, "Could not find a parent for mpi rank %s at function %s", str.c_str(), __FUNCTION__);
154 #if HAVE_PAPI
155   container_t container =
156 #endif
157       new simgrid::instr::Container(str, "MPI", father);
158 #if HAVE_PAPI
159   papi_counter_t counters = smpi_process()->papi_counters();
160
161   for (auto const& it : counters) {
162     /**
163      * Check whether this variable already exists or not. Otherwise, it will be created
164      * multiple times but only the last one would be used...
165      */
166     if (s_type::getOrNull(it.first.c_str(), container->type_) == nullptr) {
167       Type::variableNew(it.first.c_str(), "", container->type_);
168     }
169   }
170 #endif
171 }
172
173 void TRACE_smpi_finalize(int rank)
174 {
175   if (not TRACE_smpi_is_enabled())
176     return;
177
178   container_t container = smpi_container(rank);
179   container->removeFromParent();
180   delete container;
181 }
182
183 void TRACE_smpi_computing_init(int rank)
184 {
185  //first use, initialize the color in the trace
186  if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
187    smpi_container(rank)->getState("MPI_STATE")->addEntityValue("computing", instr_find_color("computing"));
188 }
189
190 void TRACE_smpi_computing_in(int rank, double amount)
191 {
192   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
193     smpi_container(rank)
194         ->getState("MPI_STATE")
195         ->pushEvent("computing", new simgrid::instr::CpuTIData("compute", amount));
196 }
197
198 void TRACE_smpi_computing_out(int rank)
199 {
200   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
201     smpi_container(rank)->getState("MPI_STATE")->popEvent();
202 }
203
204 void TRACE_smpi_sleeping_init(int rank)
205 {
206   //first use, initialize the color in the trace
207   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_sleeping())
208     smpi_container(rank)->getState("MPI_STATE")->addEntityValue("sleeping", instr_find_color("sleeping"));
209 }
210
211 void TRACE_smpi_sleeping_in(int rank, double duration)
212 {
213   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_sleeping())
214     smpi_container(rank)
215         ->getState("MPI_STATE")
216         ->pushEvent("sleeping", new simgrid::instr::CpuTIData("sleep", duration));
217 }
218
219 void TRACE_smpi_sleeping_out(int rank)
220 {
221   if (TRACE_smpi_is_enabled() && not TRACE_smpi_is_sleeping())
222     smpi_container(rank)->getState("MPI_STATE")->popEvent();
223 }
224
225 void TRACE_smpi_testing_in(int rank)
226 {
227   //do not forget to set the color first, otherwise this will explode
228   if (not TRACE_smpi_is_enabled())
229     return;
230
231   simgrid::instr::StateType* state = smpi_container(rank)->getState("MPI_STATE");
232   state->addEntityValue("test");
233   state->pushEvent("test", new simgrid::instr::NoOpTIData("test"));
234 }
235
236 void TRACE_smpi_testing_out(int rank)
237 {
238   if (TRACE_smpi_is_enabled())
239     smpi_container(rank)->getState("MPI_STATE")->popEvent();
240 }
241
242 void TRACE_smpi_comm_in(int rank, const char* operation, simgrid::instr::TIData* extra)
243 {
244   if (not TRACE_smpi_is_enabled()) {
245     delete extra;
246     return;
247   }
248
249   simgrid::instr::StateType* state = smpi_container(rank)->getState("MPI_STATE");
250   state->addEntityValue(operation, instr_find_color(operation));
251   state->pushEvent(operation, extra);
252 }
253
254 void TRACE_smpi_comm_out(int rank)
255 {
256   if (TRACE_smpi_is_enabled())
257     smpi_container(rank)->getState("MPI_STATE")->popEvent();
258 }
259
260 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
261 {
262   if (not TRACE_smpi_is_enabled())
263     return;
264
265   std::string key = TRACE_smpi_get_key(src, dst, tag, 1);
266
267   XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
268   simgrid::instr::Container::getRoot()->getLink("MPI_LINK")->startEvent(smpi_container(rank), "PTP", key, size);
269 }
270
271 void TRACE_smpi_recv(int src, int dst, int tag)
272 {
273   if (not TRACE_smpi_is_enabled())
274     return;
275
276   std::string key = TRACE_smpi_get_key(src, dst, tag, 0);
277
278   XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
279   simgrid::instr::Container::getRoot()->getLink("MPI_LINK")->endEvent(smpi_container(dst), "PTP", key);
280 }