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_computing_in(int rank, double amount)
276 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
278 ->get_state("MPI_STATE")
279 ->push_event("computing", new simgrid::instr::CpuTIData("compute", amount));
282 void TRACE_smpi_computing_out(int rank)
284 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
285 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
288 void TRACE_smpi_sleeping_in(int rank, double duration)
290 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_sleeping())
292 ->get_state("MPI_STATE")
293 ->push_event("sleeping", new simgrid::instr::CpuTIData("sleep", duration));
296 void TRACE_smpi_sleeping_out(int rank)
298 if (TRACE_smpi_is_enabled() && not TRACE_smpi_is_sleeping())
299 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
302 void TRACE_smpi_testing_in(int rank)
304 //do not forget to set the color first, otherwise this will explode
305 if (not TRACE_smpi_is_enabled())
308 simgrid::instr::StateType* state = smpi_container(rank)->get_state("MPI_STATE");
309 state->add_entity_value("test");
310 state->push_event("test", new simgrid::instr::NoOpTIData("test"));
313 void TRACE_smpi_testing_out(int rank)
315 if (TRACE_smpi_is_enabled())
316 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
319 void TRACE_smpi_comm_in(int rank, const char* operation, simgrid::instr::TIData* extra)
321 if (not TRACE_smpi_is_enabled()) {
326 simgrid::instr::StateType* state = smpi_container(rank)->get_state("MPI_STATE");
327 state->add_entity_value(operation, instr_find_color(operation));
328 state->push_event(operation, extra);
331 void TRACE_smpi_comm_out(int rank)
333 if (TRACE_smpi_is_enabled())
334 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
337 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
339 if (not TRACE_smpi_is_enabled())
342 std::string key = TRACE_smpi_get_key(src, dst, tag, 1);
344 XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
345 simgrid::instr::Container::get_root()->get_link("MPI_LINK")->start_event(smpi_container(rank), "PTP", key, size);
348 void TRACE_smpi_recv(int src, int dst, int tag)
350 if (not TRACE_smpi_is_enabled())
353 std::string key = TRACE_smpi_get_key(src, dst, tag, 0);
355 XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
356 simgrid::instr::Container::get_root()->get_link("MPI_LINK")->end_event(smpi_container(dst), "PTP", key);
359 /**************** Functions to trace the migration of tasks. *****************/
360 void TRACE_smpi_send_process_data_in(int rank)
362 if (!TRACE_smpi_is_enabled()) return;
364 smpi_container(rank)->get_state("MIGRATE_STATE")->add_entity_value("migration", instr_find_color("migration"));
365 smpi_container(rank)->get_state("MIGRATE_STATE")->push_event("migration");
368 void TRACE_smpi_send_process_data_out(int rank)
370 if (!TRACE_smpi_is_enabled()) return;
372 /* Clean the process state. */
373 smpi_container(rank)->get_state("MIGRATE_STATE")->pop_event();
376 void TRACE_smpi_process_change_host(int rank, sg_host_t new_host)
378 if (!TRACE_smpi_is_enabled()) return;
380 /** The key is (most likely) used to match the events in the trace */
381 static long long int counter = 0;
382 std::string key = std::to_string(counter);
385 // start link (= tell the trace that this rank moves from A to B)
386 container_t cont = smpi_container(rank);
387 simgrid::instr::Container::get_root()->get_link("MIGRATE_LINK")->start_event(cont, "M", key);
389 // Destroy container of this rank on this host
390 cont->remove_from_parent();
392 // Setup container on new host
393 TRACE_smpi_setup_container(rank, new_host);
396 cont = smpi_container(rank); // This points to the newly created container
397 simgrid::instr::Container::get_root()->get_link("MIGRATE_LINK")->end_event(cont, "M", key);