Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
84f567a41cf20fbf79b3d6ae57a1f043aba0ca02
[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_smpi_is_enabled()) return;
60
61   //declare category
62   TRACE_category (category);
63
64   char processid[INSTR_DEFAULT_STR_SIZE];
65   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
66   if (xbt_dict_get_or_null (process_category, processid))
67     xbt_dict_remove (process_category, processid);
68   if (category != NULL)
69     xbt_dict_set (process_category, processid, xbt_strdup(category), xbt_free);
70 }
71
72 const char *TRACE_internal_smpi_get_category (void)
73 {
74   if (!TRACE_smpi_is_enabled()) return NULL;
75
76   char processid[INSTR_DEFAULT_STR_SIZE];
77   snprintf (processid, INSTR_DEFAULT_STR_SIZE, "%p", SIMIX_process_self());
78   return xbt_dict_get_or_null (process_category, processid);
79 }
80
81 void TRACE_smpi_alloc()
82 {
83   keys = xbt_dict_new();
84   process_category = xbt_dict_new();
85 }
86
87 void TRACE_smpi_start(void)
88 {
89   if (TRACE_smpi_is_enabled()) {
90     TRACE_start();
91   }
92 }
93
94 void TRACE_smpi_release(void)
95 {
96   TRACE_surf_release();
97   if (TRACE_smpi_is_enabled()) {
98     TRACE_end();
99   }
100 }
101
102 void TRACE_smpi_init(int rank)
103 {
104   if (!TRACE_smpi_is_enabled())
105     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_assert2(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())
124     return;
125
126   char str[INSTR_DEFAULT_STR_SIZE];
127   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
128   destroyContainer(getContainer (str));
129 }
130
131 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
132 {
133   if (!TRACE_smpi_is_enabled())
134     return;
135
136   char str[INSTR_DEFAULT_STR_SIZE];
137   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
138   container_t container = getContainer (str);
139   type_t type = getType ("MPI_STATE");
140
141   pajePushState(SIMIX_get_clock(), type->id, container->id, operation);
142 }
143
144 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
145 {
146   if (!TRACE_smpi_is_enabled())
147     return;
148
149   char str[INSTR_DEFAULT_STR_SIZE];
150   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
151   container_t container = getContainer (str);
152   type_t type = getType ("MPI_STATE");
153
154   pajePopState(SIMIX_get_clock(), type->id, container->id);
155 }
156
157 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
158 {
159   if (!TRACE_smpi_is_enabled())
160     return;
161
162   char str[INSTR_DEFAULT_STR_SIZE];
163   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
164   container_t container = getContainer (str);
165   type_t type = getType ("MPI_STATE");
166
167   pajePushState(SIMIX_get_clock(), type->id, container->id, operation);
168 }
169
170 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
171 {
172   if (!TRACE_smpi_is_enabled())
173     return;
174
175   char str[INSTR_DEFAULT_STR_SIZE];
176   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
177   container_t container = getContainer (str);
178   type_t type = getType ("MPI_STATE");
179
180   pajePopState(SIMIX_get_clock(), type->id, container->id);
181 }
182
183 void TRACE_smpi_send(int rank, int src, int dst)
184 {
185   if (!TRACE_smpi_is_enabled())
186     return;
187
188   char key[INSTR_DEFAULT_STR_SIZE];
189   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
190
191   char str[INSTR_DEFAULT_STR_SIZE];
192   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
193   container_t container = getContainer (str);
194   type_t type = getType ("MPI_LINK");
195
196   pajeStartLink(SIMIX_get_clock(), type->id, type->father->id, "PTP", container->id, key);
197 }
198
199 void TRACE_smpi_recv(int rank, int src, int dst)
200 {
201   if (!TRACE_smpi_is_enabled())
202     return;
203
204   char key[INSTR_DEFAULT_STR_SIZE];
205   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
206
207   char str[INSTR_DEFAULT_STR_SIZE];
208   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
209   container_t container = getContainer (str);
210   type_t type = getType ("MPI_LINK");
211
212   pajeEndLink(SIMIX_get_clock(), type->id, type->father->id, "PTP", container->id, key);
213 }
214 #endif /* HAVE_TRACING */