1 /* Copyright (c) 2010-2018. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include <boost/algorithm/string.hpp>
13 #include <simgrid/sg_config.hpp>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
18 static std::unordered_map<std::string, std::deque<std::string>*> keys;
20 static const char* smpi_colors[] = {"recv",
122 "win_flush_local_all",
127 static const char* instr_find_color(const char* state)
129 std::string target = std::string(state);
130 boost::algorithm::to_lower(target);
131 const char* ret = nullptr;
133 const char* current = smpi_colors[i];
134 while (current != nullptr) {
135 if (target == current // exact match
136 || strstr(target.c_str(), current) != 0) { // as substring
137 ret = smpi_colors[i + 1];
141 current = smpi_colors[i];
146 XBT_PRIVATE container_t smpi_container(int rank)
148 return simgrid::instr::Container::byName(std::string("rank-") + std::to_string(rank));
151 static std::string TRACE_smpi_put_key(int src, int dst, int tag, int send)
153 // get the deque for src#dst
155 std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" + std::to_string(send);
156 auto it = keys.find(aux);
157 std::deque<std::string>* d;
159 if (it == keys.end()) {
160 d = new std::deque<std::string>;
166 static unsigned long long counter = 0;
169 std::to_string(src) + "_" + std::to_string(dst) + "_" + std::to_string(tag) + "_" + std::to_string(counter);
177 static std::string TRACE_smpi_get_key(int src, int dst, int tag, int send)
180 std::string aux = std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" +
181 std::to_string(send == 1 ? 0 : 1);
182 auto it = keys.find(aux);
183 if (it == keys.end()) {
185 key = TRACE_smpi_put_key(src, dst, tag, send);
187 key = it->second->front();
188 it->second->pop_front();
193 static std::unordered_map<smx_actor_t, std::string> process_category;
195 void TRACE_internal_smpi_set_category (const char *category)
197 if (not TRACE_smpi_is_enabled())
201 TRACE_category (category);
203 if (category != nullptr)
204 process_category[SIMIX_process_self()] = category;
207 const char *TRACE_internal_smpi_get_category ()
209 if (not TRACE_smpi_is_enabled())
212 auto it = process_category.find(SIMIX_process_self());
213 return (it == process_category.end()) ? nullptr : it->second.c_str();
216 void TRACE_smpi_alloc()
221 void TRACE_smpi_release()
223 for (auto const& elm : keys)
227 void TRACE_smpi_init(int rank)
229 if (not TRACE_smpi_is_enabled())
232 std::string str = std::string("rank-") + std::to_string(rank);
235 if (TRACE_smpi_is_grouped()){
236 father = simgrid::instr::Container::byNameOrNull(sg_host_self_get_name());
238 father = simgrid::instr::Container::getRoot();
240 xbt_assert(father != nullptr, "Could not find a parent for mpi rank %s at function %s", str.c_str(), __func__);
241 father->createChild(str, "MPI");
243 container_t container = simgrid::instr::Container::byName(str);
244 papi_counter_t counters = smpi_process()->papi_counters();
246 for (auto const& it : counters) {
248 * Check whether this variable already exists or not. Otherwise, it will be created
249 * multiple times but only the last one would be used...
251 if (s_type::getOrNull(it.first.c_str(), container->type_) == nullptr) {
252 Type::variableNew(it.first.c_str(), "", container->type_);
258 void TRACE_smpi_finalize(int rank)
260 if (not TRACE_smpi_is_enabled())
263 smpi_container(rank)->removeFromParent();
266 void TRACE_smpi_computing_init(int rank)
268 //first use, initialize the color in the trace
269 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
270 smpi_container(rank)->getState("MPI_STATE")->addEntityValue("computing", instr_find_color("computing"));
273 void TRACE_smpi_computing_in(int rank, double amount)
275 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
277 ->getState("MPI_STATE")
278 ->pushEvent("computing", new simgrid::instr::CpuTIData("compute", amount));
281 void TRACE_smpi_computing_out(int rank)
283 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
284 smpi_container(rank)->getState("MPI_STATE")->popEvent();
287 void TRACE_smpi_sleeping_in(int rank, double duration)
289 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_sleeping())
291 ->getState("MPI_STATE")
292 ->pushEvent("sleeping", new simgrid::instr::CpuTIData("sleep", duration));
295 void TRACE_smpi_sleeping_out(int rank)
297 if (TRACE_smpi_is_enabled() && not TRACE_smpi_is_sleeping())
298 smpi_container(rank)->getState("MPI_STATE")->popEvent();
301 void TRACE_smpi_testing_in(int rank)
303 //do not forget to set the color first, otherwise this will explode
304 if (not TRACE_smpi_is_enabled())
307 simgrid::instr::StateType* state = smpi_container(rank)->getState("MPI_STATE");
308 state->addEntityValue("test");
309 state->pushEvent("test", new simgrid::instr::NoOpTIData("test"));
312 void TRACE_smpi_testing_out(int rank)
314 if (TRACE_smpi_is_enabled())
315 smpi_container(rank)->getState("MPI_STATE")->popEvent();
318 void TRACE_smpi_comm_in(int rank, const char* operation, simgrid::instr::TIData* extra)
320 if (not TRACE_smpi_is_enabled()) {
325 simgrid::instr::StateType* state = smpi_container(rank)->getState("MPI_STATE");
326 state->addEntityValue(operation, instr_find_color(operation));
327 state->pushEvent(operation, extra);
330 void TRACE_smpi_comm_out(int rank)
332 if (TRACE_smpi_is_enabled())
333 smpi_container(rank)->getState("MPI_STATE")->popEvent();
336 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
338 if (not TRACE_smpi_is_enabled())
341 std::string key = TRACE_smpi_get_key(src, dst, tag, 1);
343 XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
344 simgrid::instr::Container::getRoot()->getLink("MPI_LINK")->startEvent(smpi_container(rank), "PTP", key, size);
347 void TRACE_smpi_recv(int src, int dst, int tag)
349 if (not TRACE_smpi_is_enabled())
352 std::string key = TRACE_smpi_get_key(src, dst, tag, 0);
354 XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
355 simgrid::instr::Container::getRoot()->getLink("MPI_LINK")->endEvent(smpi_container(dst), "PTP", key);