Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] do not rely on preprocessor constants to control what is traced (part 1)
[simgrid.git] / src / instr / instr_smpi.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "instr/instr_private.h"
8
9 #ifdef HAVE_TRACING
10
11 static xbt_dict_t keys;
12
13 static char *TRACE_smpi_container(int rank, char *container, int n)
14 {
15   snprintf(container, n, "rank-%d", rank);
16   return container;
17 }
18
19 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
20 {
21   //get the dynar for src#dst
22   char aux[INSTR_DEFAULT_STR_SIZE];
23   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
24   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
25   if (d == NULL) {
26     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
27     xbt_dict_set(keys, aux, d, xbt_free);
28   }
29   //generate the key
30   static unsigned long long counter = 0;
31   snprintf(key, n, "%d%d%lld", src, dst, counter++);
32
33   //push it
34   char *a = (char*)xbt_strdup(key);
35   xbt_dynar_push_as(d, char *, a);
36
37   return key;
38 }
39
40 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
41 {
42   char aux[INSTR_DEFAULT_STR_SIZE];
43   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
44   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
45
46   int length = xbt_dynar_length(d);
47   char *s = xbt_dynar_get_as (d, length-1, char *);
48   snprintf (key, n, "%s", s);
49   xbt_dynar_remove_at (d, length-1, NULL);
50   return key;
51 }
52
53 void TRACE_smpi_alloc()
54 {
55   keys = xbt_dict_new();
56 }
57
58 void TRACE_smpi_start(void)
59 {
60   if (TRACE_smpi_is_enabled()) {
61     TRACE_start();
62   }
63 }
64
65 void TRACE_smpi_release(void)
66 {
67   TRACE_surf_release();
68   if (TRACE_smpi_is_enabled()) {
69     TRACE_end();
70   }
71 }
72
73 void TRACE_smpi_init(int rank)
74 {
75   if (!TRACE_smpi_is_enabled())
76     return;
77
78   char str[INSTR_DEFAULT_STR_SIZE];
79   TRACE_smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
80   if (TRACE_smpi_is_grouped()){
81     pajeCreateContainer(SIMIX_get_clock(), str, "MPI_PROCESS",
82                       SIMIX_host_self_get_name(), str);
83   }else{
84     pajeCreateContainer(SIMIX_get_clock(), str, "MPI_PROCESS",
85                       "platform", str);
86   }
87 }
88
89 void TRACE_smpi_finalize(int rank)
90 {
91   if (!TRACE_smpi_is_enabled())
92     return;
93
94   char str[INSTR_DEFAULT_STR_SIZE];
95   pajeDestroyContainer(SIMIX_get_clock(), "MPI_PROCESS",
96                        TRACE_smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
97 }
98
99 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
100 {
101   if (!TRACE_smpi_is_enabled())
102     return;
103
104   char str[INSTR_DEFAULT_STR_SIZE];
105   pajePushState(SIMIX_get_clock(), "MPI_STATE",
106                 TRACE_smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE), operation);
107 }
108
109 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
110 {
111   if (!TRACE_smpi_is_enabled())
112     return;
113
114   char str[INSTR_DEFAULT_STR_SIZE];
115   pajePopState(SIMIX_get_clock(), "MPI_STATE",
116                TRACE_smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
117 }
118
119 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
120 {
121   if (!TRACE_smpi_is_enabled())
122     return;
123
124   char str[INSTR_DEFAULT_STR_SIZE];
125   pajePushState(SIMIX_get_clock(), "MPI_STATE",
126                 TRACE_smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE), operation);
127 }
128
129 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
130 {
131   if (!TRACE_smpi_is_enabled())
132     return;
133
134   char str[INSTR_DEFAULT_STR_SIZE];
135   pajePopState(SIMIX_get_clock(), "MPI_STATE",
136                TRACE_smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE));
137 }
138
139 void TRACE_smpi_send(int rank, int src, int dst)
140 {
141   if (!TRACE_smpi_is_enabled())
142     return;
143
144   char key[INSTR_DEFAULT_STR_SIZE], str[INSTR_DEFAULT_STR_SIZE];
145   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
146   pajeStartLink(SIMIX_get_clock(), "MPI_LINK", "0", "PTP",
147                 TRACE_smpi_container(src, str, INSTR_DEFAULT_STR_SIZE), key);
148 }
149
150 void TRACE_smpi_recv(int rank, int src, int dst)
151 {
152   if (!TRACE_smpi_is_enabled())
153     return;
154
155   char key[INSTR_DEFAULT_STR_SIZE], str[INSTR_DEFAULT_STR_SIZE];
156   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
157   pajeEndLink(SIMIX_get_clock(), "MPI_LINK", "0", "PTP",
158               TRACE_smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE), key);
159 }
160 #endif /* HAVE_TRACING */