Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
139274fab08d99f3080aad798af4a03d29310d5b
[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   TRACE_start();
91 }
92
93 void TRACE_smpi_release(void)
94 {
95   if (!TRACE_smpi_is_enabled()) return;
96
97   TRACE_surf_release();
98   TRACE_end();
99 }
100
101 void TRACE_smpi_init(int rank)
102 {
103   if (!TRACE_smpi_is_enabled()) return;
104
105   char str[INSTR_DEFAULT_STR_SIZE];
106   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
107
108   container_t father;
109   if (TRACE_smpi_is_grouped()){
110     father = getContainer (SIMIX_host_self_get_name());
111   }else{
112     father = getRootContainer ();
113   }
114   xbt_assert(father!=NULL,
115       "Could not find a parent for mpi rank %s at function %s", str, __FUNCTION__);
116   newContainer(str, INSTR_SMPI, father);
117 }
118
119 void TRACE_smpi_finalize(int rank)
120 {
121   if (!TRACE_smpi_is_enabled()) return;
122
123   char str[INSTR_DEFAULT_STR_SIZE];
124   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
125   destroyContainer(getContainer (str));
126 }
127
128 void TRACE_smpi_collective_in(int rank, int root, const char *operation)
129 {
130   if (!TRACE_smpi_is_enabled()) return;
131
132   char str[INSTR_DEFAULT_STR_SIZE];
133   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
134   container_t container = getContainer (str);
135   type_t type = getType ("MPI_STATE", container->type);
136   val_t value = getValue (operation, "1 1 1", type);
137
138   new_pajePushState (SIMIX_get_clock(), container, type, value);
139 }
140
141 void TRACE_smpi_collective_out(int rank, int root, const char *operation)
142 {
143   if (!TRACE_smpi_is_enabled()) return;
144
145   char str[INSTR_DEFAULT_STR_SIZE];
146   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
147   container_t container = getContainer (str);
148   type_t type = getType ("MPI_STATE", container->type);
149
150   new_pajePopState (SIMIX_get_clock(), container, type);
151 }
152
153 void TRACE_smpi_ptp_in(int rank, int src, int dst, const char *operation)
154 {
155   if (!TRACE_smpi_is_enabled()) return;
156
157   char str[INSTR_DEFAULT_STR_SIZE];
158   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
159   container_t container = getContainer (str);
160   type_t type = getType ("MPI_STATE", container->type);
161   val_t value = getValue (operation, "1 1 1", type);
162
163   new_pajePushState (SIMIX_get_clock(), container, type, value);
164 }
165
166 void TRACE_smpi_ptp_out(int rank, int src, int dst, const char *operation)
167 {
168   if (!TRACE_smpi_is_enabled()) return;
169
170   char str[INSTR_DEFAULT_STR_SIZE];
171   smpi_container(rank, str, INSTR_DEFAULT_STR_SIZE);
172   container_t container = getContainer (str);
173   type_t type = getType ("MPI_STATE", container->type);
174
175   new_pajePopState (SIMIX_get_clock(), container, type);
176 }
177
178 void TRACE_smpi_send(int rank, int src, int dst)
179 {
180   if (!TRACE_smpi_is_enabled()) return;
181
182   char key[INSTR_DEFAULT_STR_SIZE];
183   TRACE_smpi_put_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
184
185   char str[INSTR_DEFAULT_STR_SIZE];
186   smpi_container(src, str, INSTR_DEFAULT_STR_SIZE);
187   container_t container = getContainer (str);
188   type_t type = getType ("MPI_LINK", getRootType());
189
190   new_pajeStartLink (SIMIX_get_clock(), getRootContainer(), type, container, "PTP", key);
191 }
192
193 void TRACE_smpi_recv(int rank, int src, int dst)
194 {
195   if (!TRACE_smpi_is_enabled()) return;
196
197   char key[INSTR_DEFAULT_STR_SIZE];
198   TRACE_smpi_get_key(src, dst, key, INSTR_DEFAULT_STR_SIZE);
199
200   char str[INSTR_DEFAULT_STR_SIZE];
201   smpi_container(dst, str, INSTR_DEFAULT_STR_SIZE);
202   container_t container = getContainer (str);
203   type_t type = getType ("MPI_LINK", getRootType());
204
205   new_pajeEndLink (SIMIX_get_clock(), getRootContainer(), type, container, "PTP", key);
206 }
207 #endif /* HAVE_TRACING */