Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] cosmetics
[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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_smpi, instr, "Tracing SMPI");
12
13 static xbt_dict_t keys;
14
15 static char *smpi_container(int rank, char *container, int n)
16 {
17   snprintf(container, n, "rank-%d", rank);
18   return container;
19 }
20
21 static char *TRACE_smpi_put_key(int src, int dst, char *key, int n)
22 {
23   //get the dynar for src#dst
24   char aux[INSTR_DEFAULT_STR_SIZE];
25   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
26   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
27   if (d == NULL) {
28     d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
29     xbt_dict_set(keys, aux, d, xbt_free);
30   }
31   //generate the key
32   static unsigned long long counter = 0;
33   snprintf(key, n, "%d%d%lld", src, dst, counter++);
34
35   //push it
36   char *a = (char*)xbt_strdup(key);
37   xbt_dynar_push_as(d, char *, a);
38
39   return key;
40 }
41
42 static char *TRACE_smpi_get_key(int src, int dst, char *key, int n)
43 {
44   char aux[INSTR_DEFAULT_STR_SIZE];
45   snprintf(aux, INSTR_DEFAULT_STR_SIZE, "%d#%d", src, dst);
46   xbt_dynar_t d = xbt_dict_get_or_null(keys, aux);
47
48   int length = xbt_dynar_length(d);
49   char *s = xbt_dynar_get_as (d, length-1, char *);
50   snprintf (key, n, "%s", s);
51   xbt_dynar_remove_at (d, length-1, NULL);
52   return key;
53 }
54
55 static xbt_dict_t process_category;
56
57 void TRACE_internal_smpi_set_category (const char *category)
58 {
59   if (!TRACE_is_active())
60     return;
61   if (!TRACE_smpi_is_enabled())
62     return;
63
64   //declare category
65   TRACE_category (category);
66
67   char processid[INSTR_DEFAULT_STR_SIZE];
68   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
69   if (xbt_dict_get_or_null (process_category, processid))
70     xbt_dict_remove (process_category, processid);
71   if (category != NULL)
72     xbt_dict_set (process_category, processid, xbt_strdup(category), xbt_free);
73 }
74
75 const char *TRACE_internal_smpi_get_category (void)
76 {
77   if (!TRACE_is_active())
78     return NULL;
79   if (!TRACE_smpi_is_enabled())
80     return NULL;
81
82   char processid[INSTR_DEFAULT_STR_SIZE];
83   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
84   return xbt_dict_get_or_null (process_category, processid);
85 }
86
87 void TRACE_smpi_alloc()
88 {
89   keys = xbt_dict_new();
90   process_category = xbt_dict_new();
91 }
92
93 void TRACE_smpi_start(void)
94 {
95   if (TRACE_is_enabled()) {
96     TRACE_start();
97   }
98 }
99
100 void TRACE_smpi_release(void)
101 {
102   if (TRACE_is_enabled()) {
103     TRACE_surf_release();
104     TRACE_end();
105   }
106 }
107
108 void TRACE_smpi_init(int rank)
109 {
110   if (!(TRACE_is_active() && TRACE_smpi_is_enabled()))
111     return;
112
113   char str[INSTR_DEFAULT_STR_SIZE];
114   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
115
116   container_t father;
117   if (TRACE_smpi_is_grouped()){
118     father = getContainer (SIMIX_host_self_get_name());
119   }else{
120     father = getContainer ("0");
121   }
122   xbt_assert2(father!=NULL,
123       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
124   newContainer(str, INSTR_SMPI, father);
125 }
126
127 void TRACE_smpi_finalize(int rank)
128 {
129   if (!(TRACE_is_active() && TRACE_smpi_is_enabled()))
130     return;
131
132   char str[INSTR_DEFAULT_STR_SIZE];
133   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
134   destroyContainer(getContainer (str));
135 }
136
137 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
138 {
139   if (!(TRACE_is_active() && TRACE_smpi_is_enabled()))
140     return;
141
142   char str[INSTR_DEFAULT_STR_SIZE];
143   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
144   container_t container = getContainer (str);
145   type_t type = getType ("MPI_STATE");
146
147   pajePushState(SIMIX_get_clock(), type->id, container->id, operation);
148 }
149
150 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
151 {
152   if (!(TRACE_is_active() && TRACE_smpi_is_enabled()))
153     return;
154
155   char str[INSTR_DEFAULT_STR_SIZE];
156   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
157   container_t container = getContainer (str);
158   type_t type = getType ("MPI_STATE");
159
160   pajePopState(SIMIX_get_clock(), type->id, container->id);
161 }
162
163 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
164 {
165   if (!(TRACE_is_active() && TRACE_smpi_is_enabled()))
166     return;
167
168   char str[INSTR_DEFAULT_STR_SIZE];
169   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
170   container_t container = getContainer (str);
171   type_t type = getType ("MPI_STATE");
172
173   pajePushState(SIMIX_get_clock(), type->id, container->id, operation);
174 }
175
176 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
177 {
178   if (!(TRACE_is_active() && TRACE_smpi_is_enabled()))
179     return;
180
181   char str[INSTR_DEFAULT_STR_SIZE];
182   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
183   container_t container = getContainer (str);
184   type_t type = getType ("MPI_STATE");
185
186   pajePopState(SIMIX_get_clock(), type->id, container->id);
187 }
188
189 void TRACE_smpi_send(int rank, int src, int dst)
190 {
191   if (!(TRACE_is_active() && TRACE_smpi_is_enabled()))
192     return;
193
194   char key[INSTR_DEFAULT_STR_SIZE];
195   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
196
197   char str[INSTR_DEFAULT_STR_SIZE];
198   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
199   container_t container = getContainer (str);
200   type_t type = getType ("MPI_LINK");
201
202   pajeStartLink(SIMIX_get_clock(), type->id, type->father->id, "PTP", container->id, key);
203 }
204
205 void TRACE_smpi_recv(int rank, int src, int dst)
206 {
207   if (!(TRACE_is_active() && TRACE_smpi_is_enabled()))
208     return;
209
210   char key[INSTR_DEFAULT_STR_SIZE];
211   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
212
213   char str[INSTR_DEFAULT_STR_SIZE];
214   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
215   container_t container = getContainer (str);
216   type_t type = getType ("MPI_LINK");
217
218   pajeEndLink(SIMIX_get_clock(), type->id, type->father->id, "PTP", container->id, key);
219 }
220 #endif /* HAVE_TRACING */