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_->getChild("MPI_STATE");
239 const char *color = instr_find_color (operation);
240 simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate(operation, color, type);
241 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
244 void TRACE_smpi_collective_out(int rank, const char *operation)
246 if (not TRACE_smpi_is_enabled())
249 char str[INSTR_DEFAULT_STR_SIZE];
250 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
251 container_t container = simgrid::instr::Container::byName(str);
252 simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
254 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
257 void TRACE_smpi_computing_init(int rank)
259 //first use, initialize the color in the trace
260 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
263 char str[INSTR_DEFAULT_STR_SIZE];
264 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
265 container_t container = simgrid::instr::Container::byName(str);
266 simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
267 const char* color = instr_find_color("computing");
268 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type,
269 simgrid::instr::Value::byNameOrCreate("computing", color, type));
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_->getChild("MPI_STATE");
284 simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate("computing", "", type);
285 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
288 void TRACE_smpi_computing_out(int rank)
290 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_computing())
292 char str[INSTR_DEFAULT_STR_SIZE];
293 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
294 container_t container = simgrid::instr::Container::byName(str);
295 simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
296 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
299 void TRACE_smpi_sleeping_init(int rank)
301 //first use, initialize the color in the trace
302 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
305 char str[INSTR_DEFAULT_STR_SIZE];
306 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
307 container_t container = simgrid::instr::Container::byName(str);
308 simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
309 const char *color = instr_find_color ("sleeping");
310 simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate("sleeping", color, type);
311 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val);
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* type = container->type_->getChild("MPI_STATE");
326 simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate("sleeping", "", type);
327 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
330 void TRACE_smpi_sleeping_out(int rank)
332 if (not TRACE_smpi_is_enabled() || not TRACE_smpi_is_sleeping())
334 char str[INSTR_DEFAULT_STR_SIZE];
335 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
336 container_t container = simgrid::instr::Container::byName(str);
337 simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
338 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
341 void TRACE_smpi_testing_in(int rank, instr_extra_data extra)
343 //do not forget to set the color first, otherwise this will explode
344 if (not TRACE_smpi_is_enabled()) {
345 cleanup_extra_data(extra);
349 char str[INSTR_DEFAULT_STR_SIZE];
350 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
351 container_t container = simgrid::instr::Container::byName(str);
352 simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
353 simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate("test", "", type);
354 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
357 void TRACE_smpi_testing_out(int rank)
359 if (not TRACE_smpi_is_enabled())
361 char str[INSTR_DEFAULT_STR_SIZE];
362 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
363 container_t container = simgrid::instr::Container::byName(str);
364 simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
365 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
368 void TRACE_smpi_ptp_in(int rank, const char *operation, instr_extra_data extra)
370 if (not TRACE_smpi_is_enabled()) {
371 cleanup_extra_data(extra);
375 char str[INSTR_DEFAULT_STR_SIZE];
376 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
377 container_t container = simgrid::instr::Container::byName(str);
378 simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
379 const char *color = instr_find_color (operation);
380 simgrid::instr::Value* val = simgrid::instr::Value::byNameOrCreate(operation, color, type);
381 new simgrid::instr::PushStateEvent(SIMIX_get_clock(), container, type, val, static_cast<void*>(extra));
384 void TRACE_smpi_ptp_out(int rank, int dst, const char *operation)
386 if (not TRACE_smpi_is_enabled())
389 char str[INSTR_DEFAULT_STR_SIZE];
390 smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
391 container_t container = simgrid::instr::Container::byName(str);
392 simgrid::instr::Type* type = container->type_->getChild("MPI_STATE");
394 new simgrid::instr::PopStateEvent(SIMIX_get_clock(), container, type);
397 void TRACE_smpi_send(int rank, int src, int dst, int tag, int size)
399 if (not TRACE_smpi_is_enabled())
402 char key[INSTR_DEFAULT_STR_SIZE] = {0};
403 TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,1);
405 char str[INSTR_DEFAULT_STR_SIZE];
406 smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
407 container_t container = simgrid::instr::Container::byName(str);
408 simgrid::instr::Type* type = PJ_type_get_root()->getChild("MPI_LINK");
409 XBT_DEBUG("Send tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
410 new simgrid::instr::StartLinkEvent(SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key, size);
413 void TRACE_smpi_recv(int src, int dst, int tag)
415 if (not TRACE_smpi_is_enabled())
418 char key[INSTR_DEFAULT_STR_SIZE] = {0};
419 TRACE_smpi_get_key(src, dst, tag, key, INSTR_DEFAULT_STR_SIZE,0);
421 char str[INSTR_DEFAULT_STR_SIZE];
422 smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
423 container_t container = simgrid::instr::Container::byName(str);
424 simgrid::instr::Type* type = PJ_type_get_root()->getChild("MPI_LINK");
425 XBT_DEBUG("Recv tracing from %d to %d, tag %d, with key %s", src, dst, tag, key);
426 new simgrid::instr::EndLinkEvent(SIMIX_get_clock(), PJ_container_get_root(), type, container, "PTP", key);