Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
130bc2635a92c85cbc742e1adafb84765ae4a725
[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   xbt_assert(xbt_dynar_length(d)!=0,
49       "Trying to get a link key (for message reception) that has no corresponding send (%s).", __FUNCTION__);
50   char *s = xbt_dynar_get_as (d, 0, char *);
51   snprintf (key, n, "%s", s);
52   xbt_dynar_remove_at (d, 0, NULL);
53   return key;
54 }
55
56 static xbt_dict_t process_category;
57
58 void TRACE_internal_smpi_set_category (const char *category)
59 {
60   if (!TRACE_smpi_is_enabled()) return;
61
62   //declare category
63   TRACE_category (category);
64
65   char processid[INSTR_DEFAULT_STR_SIZE];
66   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
67   if (xbt_dict_get_or_null (process_category, processid))
68     xbt_dict_remove (process_category, processid);
69   if (category != NULL)
70     xbt_dict_set (process_category, processid, xbt_strdup(category), xbt_free);
71 }
72
73 const char *TRACE_internal_smpi_get_category (void)
74 {
75   if (!TRACE_smpi_is_enabled()) return NULL;
76
77   char processid[INSTR_DEFAULT_STR_SIZE];
78   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
79   return xbt_dict_get_or_null (process_category, processid);
80 }
81
82 void TRACE_smpi_alloc()
83 {
84   keys = xbt_dict_new();
85   process_category = xbt_dict_new();
86 }
87
88 void TRACE_smpi_start(void)
89 {
90   if (!TRACE_smpi_is_enabled()) return;
91
92   TRACE_start();
93 }
94
95 void TRACE_smpi_release(void)
96 {
97   if (!TRACE_smpi_is_enabled()) return;
98
99   TRACE_surf_release();
100   TRACE_end();
101 }
102
103 void TRACE_smpi_init(int rank)
104 {
105   if (!TRACE_smpi_is_enabled()) return;
106
107   char str[INSTR_DEFAULT_STR_SIZE];
108   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
109
110   container_t father;
111   if (TRACE_smpi_is_grouped()){
112     father = getContainer (SIMIX_host_self_get_name());
113   }else{
114     father = getContainer ("0");
115   }
116   xbt_assert(father!=NULL,
117       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
118   newContainer(str, INSTR_SMPI, father);
119 }
120
121 void TRACE_smpi_finalize(int rank)
122 {
123   if (!TRACE_smpi_is_enabled()) return;
124
125   char str[INSTR_DEFAULT_STR_SIZE];
126   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
127   destroyContainer(getContainer (str));
128 }
129
130 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
131 {
132   if (!TRACE_smpi_is_enabled()) return;
133
134   char str[INSTR_DEFAULT_STR_SIZE];
135   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
136   container_t container = getContainer (str);
137   type_t type = getType ("MPI_STATE", container->type);
138   val_t value = getValue (operation, "1 1 1", type);
139
140   new_pajePushState (SIMIX_get_clock(), container, type, value);
141 }
142
143 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
144 {
145   if (!TRACE_smpi_is_enabled()) return;
146
147   char str[INSTR_DEFAULT_STR_SIZE];
148   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
149   container_t container = getContainer (str);
150   type_t type = getType ("MPI_STATE", container->type);
151
152   new_pajePopState (SIMIX_get_clock(), container, type);
153 }
154
155 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
156 {
157   if (!TRACE_smpi_is_enabled()) return;
158
159   char str[INSTR_DEFAULT_STR_SIZE];
160   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
161   container_t container = getContainer (str);
162   type_t type = getType ("MPI_STATE", container->type);
163   val_t value = getValue (operation, "1 1 1", type);
164
165   new_pajePushState (SIMIX_get_clock(), container, type, value);
166 }
167
168 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
169 {
170   if (!TRACE_smpi_is_enabled()) return;
171
172   char str[INSTR_DEFAULT_STR_SIZE];
173   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
174   container_t container = getContainer (str);
175   type_t type = getType ("MPI_STATE", container->type);
176
177   new_pajePopState (SIMIX_get_clock(), container, type);
178 }
179
180 void TRACE_smpi_send(int rank, int src, int dst)
181 {
182   if (!TRACE_smpi_is_enabled()) return;
183
184   char key[INSTR_DEFAULT_STR_SIZE];
185   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
186
187   char str[INSTR_DEFAULT_STR_SIZE];
188   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
189   container_t container = getContainer (str);
190   type_t type = getType ("MPI_LINK", getRootType());
191
192   new_pajeStartLink (SIMIX_get_clock(), getRootContainer(), type, container, "PTP", key);
193 }
194
195 void TRACE_smpi_recv(int rank, int src, int dst)
196 {
197   if (!TRACE_smpi_is_enabled()) return;
198
199   char key[INSTR_DEFAULT_STR_SIZE];
200   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
201
202   char str[INSTR_DEFAULT_STR_SIZE];
203   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
204   container_t container = getContainer (str);
205   type_t type = getType ("MPI_LINK", getRootType());
206
207   new_pajeEndLink (SIMIX_get_clock(), getRootContainer(), type, container, "PTP", key);
208 }
209 #endif /* HAVE_TRACING */