Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
removing references to mpi_comm_world from internal code
[simgrid.git] / src / smpi / smpi_global.c
1 #include <stdio.h>
2
3 #include "private.h"
4
5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi, XBT_LOG_ROOT_CAT, "All SMPI categories");
6
7 smpi_global_t     smpi_global     = NULL;
8
9 void *smpi_request_new(void);
10
11 void *smpi_request_new()
12 {
13         smpi_mpi_request_t request = xbt_new(s_smpi_mpi_request_t, 1);
14
15         request->completed = 0;
16         request->mutex     = SIMIX_mutex_init();
17         request->cond      = SIMIX_cond_init();
18
19         return request;
20 }
21
22 void smpi_request_free(void *pointer);
23
24 void smpi_request_free(void *pointer)
25 {
26
27         smpi_mpi_request_t request = pointer;
28
29         if (NULL != request) {
30                 SIMIX_cond_destroy(request->cond);
31                 SIMIX_mutex_destroy(request->mutex);
32                 xbt_free(request);
33         }
34
35         return;
36 }
37
38 void smpi_request_reset(void *pointer);
39
40 void smpi_request_reset(void *pointer)
41 {
42         smpi_mpi_request_t request = pointer;
43
44         request->completed = 0;
45
46         return;
47 }
48
49
50 void *smpi_message_new(void);
51
52 void *smpi_message_new()
53 {
54         return xbt_new(s_smpi_received_message_t, 1);
55 }
56
57 void smpi_message_free(void *pointer);
58
59 void smpi_message_free(void *pointer)
60 {
61         if (NULL != pointer) {
62                 xbt_free(pointer);
63         }
64
65         return;
66 }
67
68 void smpi_message_reset(void *pointer);
69
70 void smpi_message_reset(void *pointer)
71 {
72         return;
73 }
74
75 int smpi_create_request(void *buf, int count, smpi_mpi_datatype_t datatype,
76         int src, int dst, int tag, smpi_mpi_communicator_t comm, smpi_mpi_request_t *requestptr)
77 {
78         int retval = MPI_SUCCESS;
79
80         smpi_mpi_request_t request = NULL;
81
82         // FIXME: make sure requestptr is not null
83         if (NULL == buf) {
84                 retval = MPI_ERR_INTERN;
85         } else if (0 > count) {
86                 retval = MPI_ERR_COUNT;
87         } else if (NULL == datatype) {
88                 retval = MPI_ERR_TYPE;
89         } else if (MPI_ANY_SOURCE != src && (0 > src || comm->size <= src)) {
90                 retval = MPI_ERR_RANK;
91         } else if (0 > dst || comm->size <= dst) {
92                 retval = MPI_ERR_RANK;
93         } else if (MPI_ANY_TAG != tag && 0 > tag) {
94                 retval = MPI_ERR_TAG;
95         } else if (NULL == comm) {
96                 retval = MPI_ERR_COMM;
97         } else if (NULL == requestptr) {
98                 retval = MPI_ERR_INTERN;
99         } else {
100                 request           = xbt_mallocator_get(smpi_global->request_mallocator);
101                 request->comm     = comm;
102                 request->src      = src;
103                 request->dst      = dst;
104                 request->tag      = tag;
105                 request->buf      = buf;
106                 request->datatype = datatype;
107                 request->count    = count;
108
109                 *requestptr       = request;
110         }
111         return retval;
112 }
113
114 void smpi_global_init()
115 {
116         int i;
117
118         int size = SIMIX_host_get_number();
119
120         smpi_global                                      = xbt_new(s_smpi_global_t, 1);
121
122         // config variable
123         smpi_global->reference_speed                     = SMPI_DEFAULT_SPEED;
124
125         smpi_global->root_ready                          = 0;
126         smpi_global->ready_process_count                 = 0;
127
128         // start/stop
129         smpi_global->start_stop_mutex                    = SIMIX_mutex_init();
130         smpi_global->start_stop_cond                     = SIMIX_cond_init();
131
132         // host info blank until sim starts
133         // FIXME: is this okay?
134         smpi_global->hosts                               = NULL;
135         smpi_global->host_count                          = 0;
136
137         // running hosts
138         smpi_global->running_hosts_count_mutex           = SIMIX_mutex_init();
139         smpi_global->running_hosts_count                 = 0;
140
141         // mallocators
142         smpi_global->request_mallocator                  = xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE,
143                                                              smpi_request_new, smpi_request_free, smpi_request_reset);
144         smpi_global->message_mallocator                  = xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE,
145                                                              smpi_message_new, smpi_message_free, smpi_message_reset);
146
147         // queues
148         smpi_global->pending_send_request_queues         = xbt_new(xbt_fifo_t,  size);
149         smpi_global->pending_send_request_queues_mutexes = xbt_new(smx_mutex_t, size);
150         smpi_global->pending_recv_request_queues         = xbt_new(xbt_fifo_t,  size);
151         smpi_global->pending_recv_request_queues_mutexes = xbt_new(smx_mutex_t, size);
152         smpi_global->received_message_queues             = xbt_new(xbt_fifo_t,  size);
153         smpi_global->received_message_queues_mutexes     = xbt_new(smx_mutex_t, size);
154
155         // sender/receiver processes
156         smpi_global->sender_processes                    = xbt_new(smx_process_t, size);
157         smpi_global->receiver_processes                  = xbt_new(smx_process_t, size);
158
159         // timers
160         smpi_global->timers                              = xbt_new(xbt_os_timer_t, size);
161         smpi_global->timers_mutexes                      = xbt_new(smx_mutex_t, size);
162
163         for(i = 0; i < size; i++) {
164                 smpi_global->pending_send_request_queues[i]         = xbt_fifo_new();
165                 smpi_global->pending_send_request_queues_mutexes[i] = SIMIX_mutex_init();
166                 smpi_global->pending_recv_request_queues[i]         = xbt_fifo_new();
167                 smpi_global->pending_recv_request_queues_mutexes[i] = SIMIX_mutex_init();
168                 smpi_global->received_message_queues[i]             = xbt_fifo_new();
169                 smpi_global->received_message_queues_mutexes[i]     = SIMIX_mutex_init();
170                 smpi_global->timers[i]                              = xbt_os_timer_new();
171                 smpi_global->timers_mutexes[i]                      = SIMIX_mutex_init();
172         }
173
174 }
175
176 void smpi_global_destroy()
177 {
178         int i;
179
180         int size = SIMIX_host_get_number();
181
182         // start/stop
183         SIMIX_mutex_destroy(smpi_global->start_stop_mutex);
184         SIMIX_cond_destroy(smpi_global->start_stop_cond);
185
186         // processes
187         xbt_free(smpi_global->sender_processes);
188         xbt_free(smpi_global->receiver_processes);
189
190         // running hosts
191         SIMIX_mutex_destroy(smpi_global->running_hosts_count_mutex);
192
193         // mallocators
194         xbt_mallocator_free(smpi_global->request_mallocator);
195         xbt_mallocator_free(smpi_global->message_mallocator);
196
197         for(i = 0; i < size; i++) {
198                 xbt_fifo_free(smpi_global->pending_send_request_queues[i]);
199                 SIMIX_mutex_destroy(smpi_global->pending_send_request_queues_mutexes[i]);
200                 xbt_fifo_free(smpi_global->pending_recv_request_queues[i]);
201                 SIMIX_mutex_destroy(smpi_global->pending_recv_request_queues_mutexes[i]);
202                 xbt_fifo_free(smpi_global->received_message_queues[i]);
203                 SIMIX_mutex_destroy(smpi_global->received_message_queues_mutexes[i]);
204                 xbt_os_timer_free(smpi_global->timers[i]);
205                 SIMIX_mutex_destroy(smpi_global->timers_mutexes[i]);
206         }
207
208         xbt_free(smpi_global->pending_send_request_queues);
209         xbt_free(smpi_global->pending_send_request_queues_mutexes);
210         xbt_free(smpi_global->pending_recv_request_queues);
211         xbt_free(smpi_global->pending_recv_request_queues_mutexes);
212         xbt_free(smpi_global->received_message_queues);
213         xbt_free(smpi_global->received_message_queues_mutexes);
214         xbt_free(smpi_global->timers);
215         xbt_free(smpi_global->timers_mutexes);
216
217         xbt_free(smpi_global);
218
219         smpi_global = NULL;
220 }
221
222 // FIXME: smarter algorithm?
223 int smpi_host_index()
224 {
225         int i;
226         smx_host_t host = SIMIX_host_self();
227
228         for(i = smpi_global->host_count - 1; i > 0 && host != smpi_global->hosts[i]; i--);
229
230         return i;
231 }
232
233 int smpi_run_simulation(int argc, char **argv)
234 {
235         xbt_fifo_item_t cond_item   = NULL;
236         smx_cond_t   cond           = NULL;
237         xbt_fifo_item_t action_item = NULL;
238         smx_action_t action         = NULL;
239
240         xbt_fifo_t   actions_failed = xbt_fifo_new();
241         xbt_fifo_t   actions_done   = xbt_fifo_new();
242
243         srand(SMPI_RAND_SEED);
244
245         SIMIX_global_init(&argc, argv);
246
247         SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
248         SIMIX_function_register("smpi_sender",         smpi_sender);
249         SIMIX_function_register("smpi_receiver",       smpi_receiver);
250
251         // FIXME: ought to verify these files...
252         SIMIX_create_environment(argv[1]);
253
254         // must initialize globals between creating environment and launching app....
255         smpi_global_init();
256
257         SIMIX_launch_application(argv[2]);
258
259         /* Prepare to display some more info when dying on Ctrl-C pressing */
260         // FIXME: doesn't work
261         //signal(SIGINT, inthandler);
262
263         /* Clean IO before the run */
264         fflush(stdout);
265         fflush(stderr);
266
267         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
268                 xbt_fifo_foreach(actions_failed, action_item, action, smx_action_t) {
269                         DEBUG1("** %s failed **", action->name);
270                         xbt_fifo_foreach(action->cond_list, cond_item, cond, smx_cond_t) {
271                                 SIMIX_cond_broadcast(cond);
272                         }
273                 }
274                 xbt_fifo_foreach(actions_done, action_item, action, smx_action_t) {
275                         DEBUG1("** %s done **",action->name);
276                         xbt_fifo_foreach(action->cond_list, cond_item, cond, smx_cond_t) {
277                                 SIMIX_cond_broadcast(cond);
278                         }
279                 }
280         }
281
282         // FIXME: cleanup incomplete
283         xbt_fifo_free(actions_failed);
284         xbt_fifo_free(actions_done);
285
286         INFO1("simulation time %g", SIMIX_get_clock());
287
288         smpi_global_destroy();
289
290         SIMIX_clean();
291
292         return 0;
293 }