1 /* Copyright (c) 2010, 2012-2017. 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. */
12 #include <simgrid/sg_config.h>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
16 static std::unordered_map<char*, std::deque<std::string>*> keys;
18 static const char *smpi_colors[] ={
20 "irecv", "1 0.52 0.52",
22 "isend", "0.52 0.52 1",
25 "waitall", "0.78 0.78 0",
26 "waitany", "0.78 0.78 0.58",
27 "test", "0.52 0.52 0",
30 "allgatherv", "1 0.52 0.52",
32 "alltoall", "0.52 0 1",
33 "alltoallv", "0.78 0.52 1",
34 "barrier", "0 0.78 0.78",
35 "bcast", "0 0.78 0.39",
37 "gatherv", "1 1 0.52",
39 "reducescatter", "0.52 1 0.52",
40 "scan", "1 0.58 0.23",
41 "exscan", "1 0.54 0.25",
42 "scatterv", "0.52 0 0.52",
43 "scatter", "1 0.74 0.54",
46 "sleeping", "0 0.5 0.5",
53 "accumulate", "1 0.3 0",
54 "win_fence", "1 0 0.3",
55 "win_post", "1 0 0.8",
56 "win_wait", "1 0.8 0",
57 "win_start", "0.8 0 1",
58 "win_complete", "0.8 1 0",
62 static char *str_tolower (const char *str)
64 char* ret = xbt_strdup(str);
66 for (int i = 0; i < n; i++)
67 ret[i] = tolower (str[i]);
71 static const char *instr_find_color (const char *state)
73 char* target = str_tolower(state);
74 const char* ret = nullptr;
76 const char* current = smpi_colors[i];
77 while (current != nullptr) {
78 if (strcmp (state, current) == 0 //exact match
79 || strstr(target, current) != 0 ){//as substring
80 ret = smpi_colors[i+1];
84 current = smpi_colors[i];
90 XBT_PRIVATE char *smpi_container(int rank, char *container, int n)
92 snprintf(container, n, "rank-%d", rank);
96 static char *TRACE_smpi_get_key(int src, int dst, int tag, char *key, int n, int send);
98 static char *TRACE_smpi_put_key(int src, int dst, int tag, char *key, int n, int send)
100 //get the dynar for src#dst
101 char aux[INSTR_DEFAULT_STR_SIZE];
102 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d#%d#%d", src, dst, tag, send);
103 auto it = keys.find(aux);
104 std::deque<std::string>* d;
106 if (it == keys.end()) {
107 d = new std::deque<std::string>;
113 static unsigned long long counter = 0;
115 snprintf(key, n, "%d_%d_%d_%llu", src, dst, tag, counter);
123 static char *TRACE_smpi_get_key(int src, int dst, int tag, char *key, int n, int send)
125 char aux[INSTR_DEFAULT_STR_SIZE];
126 snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d#%d#%d", src, dst, tag, send==1?0:1);
127 auto it = keys.find(aux);
128 if (it == keys.end()) {
130 TRACE_smpi_put_key(src, dst, tag, key, n, send);
132 snprintf(key, n, "%s", it->second->front().c_str());
133 it->second->pop_front();
138 static std::unordered_map<smx_actor_t, std::string> process_category;
140 static void cleanup_extra_data (instr_extra_data extra){
142 if(extra->sendcounts!=nullptr)
143 xbt_free(extra->sendcounts);
144 if(extra->recvcounts!=nullptr)
145 xbt_free(extra->recvcounts);
150 void TRACE_internal_smpi_set_category (const char *category)
152 if (not TRACE_smpi_is_enabled())
156 TRACE_category (category);
158 if (category != nullptr)
159 process_category[SIMIX_process_self()] = category;
162 const char *TRACE_internal_smpi_get_category ()
164 if (not TRACE_smpi_is_enabled())
167 auto it = process_category.find(SIMIX_process_self());
168 return (it == process_category.end()) ? nullptr : it->second.c_str();
171 void TRACE_smpi_alloc()
176 void TRACE_smpi_release()
178 for (auto const& elm : keys)
182 void TRACE_smpi_init(int rank)
184 if (not TRACE_smpi_is_enabled())
187 char str[INSTR_DEFAULT_STR_SIZE];
188 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
191 if (TRACE_smpi_is_grouped()){
192 father = simgrid::instr::Container::byName(sg_host_self_get_name());
194 father = PJ_container_get_root ();
196 xbt_assert(father!=nullptr,
197 "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
199 container_t container =
201 new simgrid::instr::Container(str, simgrid::instr::INSTR_SMPI, father);
203 papi_counter_t counters = smpi_process()->papi_counters();
205 for (auto const& it : counters) {
207 * Check whether this variable already exists or not. Otherwise, it will be created
208 * multiple times but only the last one would be used...
210 if (s_type::getOrNull(it.first.c_str(), container->type_) == nullptr) {
211 Type::variableNew(it.first.c_str(), "", container->type_);
217 void TRACE_smpi_finalize(int rank)
219 if (not TRACE_smpi_is_enabled())
222 char str[INSTR_DEFAULT_STR_SIZE];
223 container_t container = simgrid::instr::Container::byName(smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
224 container->removeFromParent();
228 void TRACE_smpi_collective_in(int rank, const char *operation, instr_extra_data extra)
230 if (not TRACE_smpi_is_enabled()) {
231 cleanup_extra_data(extra);
235 char str[INSTR_DEFAULT_STR_SIZE];
236 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
237 container_t container = simgrid::instr::Container::byName(str);
238 simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
239 const char *color = instr_find_color (operation);
240 type->addEntityValue(operation, color);
241 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, type->getEntityValue(operation),
242 static_cast<void*>(extra));
245 void TRACE_smpi_collective_out(int rank, const char *operation)
247 if (not TRACE_smpi_is_enabled())
250 char str[INSTR_DEFAULT_STR_SIZE];
251 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
252 container_t container = simgrid::instr::Container::byName(str);
253 simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
255 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
258 void TRACE_smpi_computing_init(int rank)
260 //first use, initialize the color in the trace
261 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
264 char str[INSTR_DEFAULT_STR_SIZE];
265 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
266 container_t container = simgrid::instr::Container::byName(str);
267 simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
268 type->addEntityValue("computing", instr_find_color("computing"));
269 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, type->getEntityValue("computing"));
272 void TRACE_smpi_computing_in(int rank, instr_extra_data extra)
274 //do not forget to set the color first, otherwise this will explode
275 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing()) {
276 cleanup_extra_data(extra);
280 char str[INSTR_DEFAULT_STR_SIZE];
281 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
282 container_t container = simgrid::instr::Container::byName(str);
283 simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
284 type->addEntityValue("computing");
285 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, type->getEntityValue("computing"),
286 static_cast<void*>(extra));
289 void TRACE_smpi_computing_out(int rank)
291 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
293 char str[INSTR_DEFAULT_STR_SIZE];
294 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
295 container_t container = simgrid::instr::Container::byName(str);
296 simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
297 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
300 void TRACE_smpi_sleeping_init(int rank)
302 //first use, initialize the color in the trace
303 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
306 char str[INSTR_DEFAULT_STR_SIZE];
307 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
308 container_t container = simgrid::instr::Container::byName(str);
309 simgrid::instr::Type* state = container->type_->byName("MPI_STATE");
310 state->addEntityValue("sleeping", instr_find_color("sleeping"));
311 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, state, state->getEntityValue("sleeping"));
314 void TRACE_smpi_sleeping_in(int rank, instr_extra_data extra)
316 //do not forget to set the color first, otherwise this will explode
317 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping()) {
318 cleanup_extra_data(extra);
322 char str[INSTR_DEFAULT_STR_SIZE];
323 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
324 container_t container = simgrid::instr::Container::byName(str);
325 simgrid::instr::Type* state = container->type_->byName("MPI_STATE");
326 state->addEntityValue("sleeping");
327 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, state, state->getEntityValue("sleeping"),
328 static_cast<void*>(extra));
331 void TRACE_smpi_sleeping_out(int rank)
333 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
335 char str[INSTR_DEFAULT_STR_SIZE];
336 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
337 container_t container = simgrid::instr::Container::byName(str);
338 simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
339 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
342 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
344 //do not forget to set the color first, otherwise this will explode
345 if (not TRACE_smpi_is_enabled()) {
346 cleanup_extra_data(extra);
350 char str[INSTR_DEFAULT_STR_SIZE];
351 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
352 container_t container = simgrid::instr::Container::byName(str);
353 simgrid::instr::Type* state = container->type_->byName("MPI_STATE");
354 state->addEntityValue("test");
355 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, state, state->getEntityValue("test"),
356 static_cast<void*>(extra));
359 void TRACE_smpi_testing_out(int rank)
361 if (not TRACE_smpi_is_enabled())
363 char str[INSTR_DEFAULT_STR_SIZE];
364 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
365 container_t container = simgrid::instr::Container::byName(str);
366 simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
367 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
370 void TRACE_smpi_ptp_in(int rank, const char *operation, instr_extra_data extra)
372 if (not TRACE_smpi_is_enabled()) {
373 cleanup_extra_data(extra);
377 char str[INSTR_DEFAULT_STR_SIZE];
378 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
379 container_t container = simgrid::instr::Container::byName(str);
380 simgrid::instr::Type* state = container->type_->byName("MPI_STATE");
381 state->addEntityValue(operation, instr_find_color(operation));
382 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, state, state->getEntityValue(operation),
383 static_cast<void*>(extra));
386 void TRACE_smpi_ptp_out(int rank, int dst, const char *operation)
388 if (not TRACE_smpi_is_enabled())
391 char str[INSTR_DEFAULT_STR_SIZE];
392 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
393 container_t container = simgrid::instr::Container::byName(str);
394 simgrid::instr::Type* type = container->type_->byName("MPI_STATE");
396 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
399 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
401 if (not TRACE_smpi_is_enabled())
404 char key[INSTR_DEFAULT_STR_SIZE] = {0};
405 TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,1);
407 char str[INSTR_DEFAULT_STR_SIZE];
408 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
409 container_t container = simgrid::instr::Container::byName(str);
410 simgrid::instr::Type* type = simgrid::instr::Type::getRootType()->byName("MPI_LINK");
411 XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
412 new simgrid::instr::StartLinkEvent(SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
415 void TRACE_smpi_recv(int src, int dst, int tag)
417 if (not TRACE_smpi_is_enabled())
420 char key[INSTR_DEFAULT_STR_SIZE] = {0};
421 TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,0);
423 char str[INSTR_DEFAULT_STR_SIZE];
424 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
425 container_t container = simgrid::instr::Container::byName(str);
426 simgrid::instr::Type* type = simgrid::instr::Type::getRootType()->byName("MPI_LINK");
427 XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
428 new simgrid::instr::EndLinkEvent(SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);