Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #256 from Flamefire/master
[simgrid.git] / src / smpi / internals / instr_smpi.cpp
1 /* Copyright (c) 2010, 2012-2018. 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     "rput",       "0.3 1 0",     "rget",           "0 1 0.3",        "raccumulate", "1 0.3 0",
37     "compare_and_swap",       "0.3 1 0",     "get_accumulate",           "0 1 0.3",        "rget_accumulate", "1 0.3 0",
38     "win_fence", "1 0 0.3",     "win_post",      "1 0 0.8",        "win_wait",   "1 0.8 0",
39     "win_start", "0.8 0 1",     "win_complete",  "0.8 1 0",        "win_lock", "1 0 0.3",     
40     "win_unlock", "1 0 0.3",     "win_lock_all",      "1 0 0.8",        "win_unlock_all",   "1 0.8 0",
41     "win_flush", "1 0 0.3",     "win_flush_local",      "1 0 0.8",        "win_flush_all",   "1 0.8 0",
42     "win_flush_local_all", "1 0 0.3", ""  , ""
43 };
44
45 static const char* instr_find_color(const char* state)
46 {
47   std::string target = std::string(state);
48   boost::algorithm::to_lower(target);
49   const char* ret     = nullptr;
50   unsigned int i      = 0;
51   const char* current = smpi_colors[i];
52   while (current != nullptr) {
53     if (target == current                          // exact match
54         || strstr(target.c_str(), current) != 0) { // as substring
55       ret = smpi_colors[i + 1];
56       break;
57     }
58     i+=2;
59     current = smpi_colors[i];
60   }
61   return ret;
62 }
63
64 XBT_PRIVATE container_t smpi_container(int rank)
65 {
66   return simgrid::instr::Container::byName(std::string("rank-") + std::to_string(rank));
67 }
68
69 static std::string TRACE_smpi_put_key(int src, int dst, int tag, int send)
70 {
71   // get the deque for src#dst
72   std::string aux =
73       std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" + std::to_string(send);
74   auto it = keys.find(aux);
75   std::deque<std::string>* d;
76
77   if (it == keys.end()) {
78     d         = new std::deque<std::string>;
79     keys[aux] = d;
80   } else
81     d = it->second;
82
83   //generate the key
84   static unsigned long long counter = 0;
85   counter++;
86   std::string key =
87       std::to_string(src) + "_" + std::to_string(dst) + "_" + std::to_string(tag) + "_" + std::to_string(counter);
88
89   //push it
90   d->push_back(key);
91
92   return key;
93 }
94
95 static std::string TRACE_smpi_get_key(int src, int dst, int tag, int send)
96 {
97   std::string key;
98   std::string aux = std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" +
99                     std::to_string(send == 1 ? 0 : 1);
100   auto it = keys.find(aux);
101   if (it == keys.end()) {
102     // first posted
103     key = TRACE_smpi_put_key(src, dst, tag, send);
104   } else {
105     key = it->second->front();
106     it->second->pop_front();
107   }
108   return key;
109 }
110
111 static std::unordered_map<smx_actor_t, std::string> process_category;
112
113 void TRACE_internal_smpi_set_category (const char *category)
114 {
115   if (not TRACE_smpi_is_enabled())
116     return;
117
118   //declare category
119   TRACE_category (category);
120
121   if (category != nullptr)
122     process_category[SIMIX_process_self()] = category;
123 }
124
125 const char *TRACE_internal_smpi_get_category ()
126 {
127   if (not TRACE_smpi_is_enabled())
128     return nullptr;
129
130   auto it = process_category.find(SIMIX_process_self());
131   return (it == process_category.end()) ? nullptr : it->second.c_str();
132 }
133
134 void TRACE_smpi_alloc()
135 {
136   // for symmetry
137 }
138
139 void TRACE_smpi_release()
140 {
141   for (auto const& elm : keys)
142     delete elm.second;
143 }
144
145 void TRACE_smpi_init(int rank)
146 {
147   if (not TRACE_smpi_is_enabled())
148     return;
149
150   std::string str = std::string("rank-") + std::to_string(rank);
151
152   container_t father;
153   if (TRACE_smpi_is_grouped()){
154     father = simgrid::instr::Container::byNameOrNull(sg_host_self_get_name());
155   }else{
156     father = simgrid::instr::Container::getRoot();
157   }
158   xbt_assert(father != nullptr, "Could not find a parent for mpi rank %s at function %s", str.c_str(), __FUNCTION__);
159 #if HAVE_PAPI
160   container_t container =
161 #endif
162       new simgrid::instr::Container(str, "MPI", father);
163 #if HAVE_PAPI
164   papi_counter_t counters = smpi_process()->papi_counters();
165
166   for (auto const& it : counters) {
167     /**
168      * Check whether this variable already exists or not. Otherwise, it will be created
169      * multiple times but only the last one would be used...
170      */
171     if (s_type::getOrNull(it.first.c_str(), container->type_) == nullptr) {
172       Type::variableNew(it.first.c_str(), "", container->type_);
173     }
174   }
175 #endif
176 }
177
178 void TRACE_smpi_finalize(int rank)
179 {
180   if (not TRACE_smpi_is_enabled())
181     return;
182
183   container_t container = smpi_container(rank);
184   container->removeFromParent();
185   delete container;
186 }
187
188 void TRACE_smpi_computing_init(int rank)
189 {
190  //first use, initialize the color in the trace
191  if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
192    smpi_container(rank)->getState("MPI_STATE")->addEntityValue("computing", instr_find_color("computing"));
193 }
194
195 void TRACE_smpi_computing_in(int rank, double amount)
196 {
197   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
198     smpi_container(rank)
199         ->getState("MPI_STATE")
200         ->pushEvent("computing", new simgrid::instr::CpuTIData("compute", amount));
201 }
202
203 void TRACE_smpi_computing_out(int rank)
204 {
205   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
206     smpi_container(rank)->getState("MPI_STATE")->popEvent();
207 }
208
209 void TRACE_smpi_sleeping_in(int rank, double duration)
210 {
211   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_sleeping())
212     smpi_container(rank)
213         ->getState("MPI_STATE")
214         ->pushEvent("sleeping", new simgrid::instr::CpuTIData("sleep", duration));
215 }
216
217 void TRACE_smpi_sleeping_out(int rank)
218 {
219   if (TRACE_smpi_is_enabled() && not TRACE_smpi_is_sleeping())
220     smpi_container(rank)->getState("MPI_STATE")->popEvent();
221 }
222
223 void TRACE_smpi_testing_in(int rank)
224 {
225   //do not forget to set the color first, otherwise this will explode
226   if (not TRACE_smpi_is_enabled())
227     return;
228
229   simgrid::instr::StateType* state = smpi_container(rank)->getState("MPI_STATE");
230   state->addEntityValue("test");
231   state->pushEvent("test", new simgrid::instr::NoOpTIData("test"));
232 }
233
234 void TRACE_smpi_testing_out(int rank)
235 {
236   if (TRACE_smpi_is_enabled())
237     smpi_container(rank)->getState("MPI_STATE")->popEvent();
238 }
239
240 void TRACE_smpi_comm_in(int rank, const char* operation, simgrid::instr::TIData* extra)
241 {
242   if (not TRACE_smpi_is_enabled()) {
243     delete extra;
244     return;
245   }
246
247   simgrid::instr::StateType* state = smpi_container(rank)->getState("MPI_STATE");
248   state->addEntityValue(operation, instr_find_color(operation));
249   state->pushEvent(operation, extra);
250 }
251
252 void TRACE_smpi_comm_out(int rank)
253 {
254   if (TRACE_smpi_is_enabled())
255     smpi_container(rank)->getState("MPI_STATE")->popEvent();
256 }
257
258 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
259 {
260   if (not TRACE_smpi_is_enabled())
261     return;
262
263   std::string key = TRACE_smpi_get_key(src, dst, tag, 1);
264
265   XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
266   simgrid::instr::Container::getRoot()->getLink("MPI_LINK")->startEvent(smpi_container(rank), "PTP", key, size);
267 }
268
269 void TRACE_smpi_recv(int src, int dst, int tag)
270 {
271   if (not TRACE_smpi_is_enabled())
272     return;
273
274   std::string key = TRACE_smpi_get_key(src, dst, tag, 0);
275
276   XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
277   simgrid::instr::Container::getRoot()->getLink("MPI_LINK")->endEvent(smpi_container(dst), "PTP", key);
278 }