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>
9 #include <simgrid/s4u/Actor.hpp>
14 #include <simgrid/sg_config.hpp>
15 #include <simgrid/s4u/Host.hpp>
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
21 static std::unordered_map<std::string, std::deque<std::string>*> keys;
23 static const char* smpi_colors[] = {"recv",
127 "win_flush_local_all",
132 static const char* instr_find_color(const char* state)
134 std::string target = std::string(state);
135 boost::algorithm::to_lower(target);
136 const char* ret = nullptr;
138 const char* current = smpi_colors[i];
139 while (current != nullptr) {
140 if (target == current // exact match
141 || strstr(target.c_str(), current) != 0) { // as substring
142 ret = smpi_colors[i + 1];
146 current = smpi_colors[i];
151 XBT_PRIVATE container_t smpi_container(int rank)
153 return simgrid::instr::Container::by_name(std::string("rank-") + std::to_string(rank));
156 static std::string TRACE_smpi_put_key(int src, int dst, int tag, int send)
158 // get the deque for src#dst
160 std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" + std::to_string(send);
161 auto it = keys.find(aux);
162 std::deque<std::string>* d;
164 if (it == keys.end()) {
165 d = new std::deque<std::string>;
171 static unsigned long long counter = 0;
174 std::to_string(src) + "_" + std::to_string(dst) + "_" + std::to_string(tag) + "_" + std::to_string(counter);
182 static std::string TRACE_smpi_get_key(int src, int dst, int tag, int send)
185 std::string aux = std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" +
186 std::to_string(send == 1 ? 0 : 1);
187 auto it = keys.find(aux);
188 if (it == keys.end()) {
190 key = TRACE_smpi_put_key(src, dst, tag, send);
192 key = it->second->front();
193 it->second->pop_front();
198 static std::unordered_map<smx_actor_t, std::string> process_category;
200 void TRACE_internal_smpi_set_category (const char *category)
202 if (not TRACE_smpi_is_enabled())
206 TRACE_category (category);
208 if (category != nullptr)
209 process_category[SIMIX_process_self()] = category;
212 const char *TRACE_internal_smpi_get_category ()
214 if (not TRACE_smpi_is_enabled())
217 auto it = process_category.find(SIMIX_process_self());
218 return (it == process_category.end()) ? nullptr : it->second.c_str();
221 void TRACE_smpi_release()
223 for (auto const& elm : keys)
227 void TRACE_smpi_setup_container(int rank, sg_host_t host)
229 container_t father = simgrid::instr::Container::get_root();
230 if (TRACE_smpi_is_grouped()) {
231 father = simgrid::instr::Container::by_name_or_null(host->get_name());
232 xbt_assert(father != nullptr, "Could not find a parent for mpi rank 'rank-%d' at function %s", rank, __func__);
234 father->create_child(std::string("rank-") + std::to_string(rank), "MPI"); // This container is of type MPI
237 void TRACE_smpi_init(int rank)
239 if (not TRACE_smpi_is_enabled())
242 TRACE_smpi_setup_container(rank, sg_host_self());
244 container_t container = simgrid::instr::Container::by_name(str);
245 papi_counter_t counters = smpi_process()->papi_counters();
247 for (auto const& it : counters) {
249 * Check whether this variable already exists or not. Otherwise, it will be created
250 * multiple times but only the last one would be used...
252 if (s_type::getOrNull(it.first.c_str(), container->type_) == nullptr) {
253 Type::variableNew(it.first.c_str(), "", container->type_);
259 void TRACE_smpi_finalize(int rank)
261 if (not TRACE_smpi_is_enabled())
264 smpi_container(rank)->remove_from_parent();
267 void TRACE_smpi_computing_init(int rank)
269 //first use, initialize the color in the trace
270 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
271 smpi_container(rank)->get_state("MPI_STATE")->add_entity_value("computing", instr_find_color("computing"));
274 void TRACE_smpi_sleeping_init(int rank)
276 //first use, initialize the color in the trace
277 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_sleeping())
278 smpi_container(rank)->get_state("MPI_STATE")->add_entity_value("sleeping", instr_find_color("sleeping"));
281 void TRACE_smpi_computing_in(int rank, double amount)
283 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
285 ->get_state("MPI_STATE")
286 ->push_event("computing", new simgrid::instr::CpuTIData("compute", amount));
289 void TRACE_smpi_computing_out(int rank)
291 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
292 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
295 void TRACE_smpi_sleeping_in(int rank, double duration)
297 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_sleeping())
299 ->get_state("MPI_STATE")
300 ->push_event("sleeping", new simgrid::instr::CpuTIData("sleep", duration));
303 void TRACE_smpi_sleeping_out(int rank)
305 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_sleeping())
306 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
309 void TRACE_smpi_testing_in(int rank)
311 //do not forget to set the color first, otherwise this will explode
312 if (not TRACE_smpi_is_enabled())
315 simgrid::instr::StateType* state = smpi_container(rank)->get_state("MPI_STATE");
316 state->add_entity_value("test");
317 state->push_event("test", new simgrid::instr::NoOpTIData("test"));
320 void TRACE_smpi_testing_out(int rank)
322 if (TRACE_smpi_is_enabled())
323 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
326 void TRACE_smpi_comm_in(int rank, const char* operation, simgrid::instr::TIData* extra)
328 if (not TRACE_smpi_is_enabled()) {
333 simgrid::instr::StateType* state = smpi_container(rank)->get_state("MPI_STATE");
334 state->add_entity_value(operation, instr_find_color(operation));
335 state->push_event(operation, extra);
338 void TRACE_smpi_comm_out(int rank)
340 if (TRACE_smpi_is_enabled())
341 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
344 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
346 if (not TRACE_smpi_is_enabled())
349 std::string key = TRACE_smpi_get_key(src, dst, tag, 1);
351 XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
352 simgrid::instr::Container::get_root()->get_link("MPI_LINK")->start_event(smpi_container(rank), "PTP", key, size);
355 void TRACE_smpi_recv(int src, int dst, int tag)
357 if (not TRACE_smpi_is_enabled())
360 std::string key = TRACE_smpi_get_key(src, dst, tag, 0);
362 XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
363 simgrid::instr::Container::get_root()->get_link("MPI_LINK")->end_event(smpi_container(dst), "PTP", key);
366 /**************** Functions to trace the migration of tasks. *****************/
367 void TRACE_smpi_send_process_data_in(int rank)
369 if (not TRACE_smpi_is_enabled()) return;
371 smpi_container(rank)->get_state("MIGRATE_STATE")->add_entity_value("migration", instr_find_color("migration"));
372 smpi_container(rank)->get_state("MIGRATE_STATE")->push_event("migration");
375 void TRACE_smpi_send_process_data_out(int rank)
377 if (not TRACE_smpi_is_enabled()) return;
379 /* Clean the process state. */
380 smpi_container(rank)->get_state("MIGRATE_STATE")->pop_event();
383 void TRACE_smpi_process_change_host(int rank, sg_host_t new_host)
385 if (!TRACE_smpi_is_enabled()) return;
387 /** The key is (most likely) used to match the events in the trace */
388 static long long int counter = 0;
389 std::string key = std::to_string(counter);
392 // start link (= tell the trace that this rank moves from A to B)
393 container_t cont = smpi_container(rank);
394 simgrid::instr::Container::get_root()->get_link("MIGRATE_LINK")->start_event(cont, "M", key);
396 // Destroy container of this rank on this host
397 cont->remove_from_parent();
399 // Setup container on new host
400 TRACE_smpi_setup_container(rank, new_host);
403 cont = smpi_container(rank); // This points to the newly created container
404 simgrid::instr::Container::get_root()->get_link("MIGRATE_LINK")->end_event(cont, "M", key);