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 std::string smpi_container_key(int rank)
153 return std::string("rank-") + std::to_string(rank);
156 XBT_PRIVATE container_t smpi_container(int rank)
158 return simgrid::instr::Container::by_name(smpi_container_key(rank));
161 static std::string TRACE_smpi_put_key(int src, int dst, int tag, int send)
163 // get the deque for src#dst
165 std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" + std::to_string(send);
166 auto it = keys.find(aux);
167 std::deque<std::string>* d;
169 if (it == keys.end()) {
170 d = new std::deque<std::string>;
176 static unsigned long long counter = 0;
179 std::to_string(src) + "_" + std::to_string(dst) + "_" + std::to_string(tag) + "_" + std::to_string(counter);
187 static std::string TRACE_smpi_get_key(int src, int dst, int tag, int send)
190 std::string aux = std::to_string(src) + "#" + std::to_string(dst) + "#" + std::to_string(tag) + "#" +
191 std::to_string(send == 1 ? 0 : 1);
192 auto it = keys.find(aux);
193 if (it == keys.end()) {
195 key = TRACE_smpi_put_key(src, dst, tag, send);
197 key = it->second->front();
198 it->second->pop_front();
203 static std::unordered_map<smx_actor_t, std::string> process_category;
205 void TRACE_internal_smpi_set_category (const char *category)
207 if (not TRACE_smpi_is_enabled())
211 TRACE_category (category);
213 if (category != nullptr)
214 process_category[SIMIX_process_self()] = category;
217 const char *TRACE_internal_smpi_get_category ()
219 if (not TRACE_smpi_is_enabled())
222 auto it = process_category.find(SIMIX_process_self());
223 return (it == process_category.end()) ? nullptr : it->second.c_str();
226 void TRACE_smpi_release()
228 for (auto const& elm : keys)
232 void TRACE_smpi_setup_container(int rank, sg_host_t host)
234 std::string str = smpi_container_key(rank);
237 if (TRACE_smpi_is_grouped()){
238 father = simgrid::instr::Container::by_name_or_null(host->get_name());
240 father = simgrid::instr::Container::get_root();
242 xbt_assert(father != nullptr, "Could not find a parent for mpi rank %s at function %s", str.c_str(), __func__);
243 father->create_child(str, "MPI"); // This container is of type MPI
246 void TRACE_smpi_init(int rank)
248 if (not TRACE_smpi_is_enabled())
251 TRACE_smpi_setup_container(rank, sg_host_self());
253 container_t container = simgrid::instr::Container::by_name(str);
254 papi_counter_t counters = smpi_process()->papi_counters();
256 for (auto const& it : counters) {
258 * Check whether this variable already exists or not. Otherwise, it will be created
259 * multiple times but only the last one would be used...
261 if (s_type::getOrNull(it.first.c_str(), container->type_) == nullptr) {
262 Type::variableNew(it.first.c_str(), "", container->type_);
268 void TRACE_smpi_finalize(int rank)
270 if (not TRACE_smpi_is_enabled())
273 smpi_container(rank)->remove_from_parent();
276 void TRACE_smpi_computing_init(int rank)
278 //first use, initialize the color in the trace
279 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
280 smpi_container(rank)->get_state("MPI_STATE")->add_entity_value("computing", instr_find_color("computing"));
283 void TRACE_smpi_computing_in(int rank, double amount)
285 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
287 ->get_state("MPI_STATE")
288 ->push_event("computing", new simgrid::instr::CpuTIData("compute", amount));
291 void TRACE_smpi_computing_out(int rank)
293 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_computing())
294 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
297 void TRACE_smpi_sleeping_in(int rank, double duration)
299 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_sleeping())
301 ->get_state("MPI_STATE")
302 ->push_event("sleeping", new simgrid::instr::CpuTIData("sleep", duration));
305 void TRACE_smpi_sleeping_out(int rank)
307 if (TRACE_smpi_is_enabled() && not TRACE_smpi_is_sleeping())
308 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
311 void TRACE_smpi_testing_in(int rank)
313 //do not forget to set the color first, otherwise this will explode
314 if (not TRACE_smpi_is_enabled())
317 simgrid::instr::StateType* state = smpi_container(rank)->get_state("MPI_STATE");
318 state->add_entity_value("test");
319 state->push_event("test", new simgrid::instr::NoOpTIData("test"));
322 void TRACE_smpi_testing_out(int rank)
324 if (TRACE_smpi_is_enabled())
325 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
328 void TRACE_smpi_comm_in(int rank, const char* operation, simgrid::instr::TIData* extra)
330 if (not TRACE_smpi_is_enabled()) {
335 simgrid::instr::StateType* state = smpi_container(rank)->get_state("MPI_STATE");
336 state->add_entity_value(operation, instr_find_color(operation));
337 state->push_event(operation, extra);
340 void TRACE_smpi_comm_out(int rank)
342 if (TRACE_smpi_is_enabled())
343 smpi_container(rank)->get_state("MPI_STATE")->pop_event();
346 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
348 if (not TRACE_smpi_is_enabled())
351 std::string key = TRACE_smpi_get_key(src, dst, tag, 1);
353 XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
354 simgrid::instr::Container::get_root()->get_link("MPI_LINK")->start_event(smpi_container(rank), "PTP", key, size);
357 void TRACE_smpi_recv(int src, int dst, int tag)
359 if (not TRACE_smpi_is_enabled())
362 std::string key = TRACE_smpi_get_key(src, dst, tag, 0);
364 XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key.c_str());
365 simgrid::instr::Container::get_root()->get_link("MPI_LINK")->end_event(smpi_container(dst), "PTP", key);
368 /**************** Functions to trace the migration of tasks. *****************/
369 void TRACE_smpi_send_process_data_in(int rank)
371 if (!TRACE_smpi_is_enabled()) return;
373 smpi_container(rank)->get_state("MIGRATE_STATE")->add_entity_value("migration", instr_find_color("migration"));
374 smpi_container(rank)->get_state("MIGRATE_STATE")->push_event("migration");
377 void TRACE_smpi_send_process_data_out(int rank)
379 if (!TRACE_smpi_is_enabled()) return;
381 /* Clean the process state. */
382 smpi_container(rank)->get_state("MIGRATE_STATE")->pop_event();
385 void TRACE_smpi_process_change_host(int rank, sg_host_t new_host)
387 if (!TRACE_smpi_is_enabled()) return;
389 /** The key is (most likely) used to match the events in the trace */
390 static long long int counter = 0;
391 std::string key = std::to_string(counter);
394 // start link (= tell the trace that this rank moves from A to B)
395 container_t cont = smpi_container(rank);
396 simgrid::instr::Container::get_root()->get_link("MIGRATE_LINK")->start_event(cont, "M", key);
398 // Destroy container of this rank on this host
399 cont->remove_from_parent();
401 // Setup container on new host
402 TRACE_smpi_setup_container(rank, new_host);
405 cont = smpi_container(rank); // This points to the newly created container
406 simgrid::instr::Container::get_root()->get_link("MIGRATE_LINK")->end_event(cont, "M", key);