Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ba2db8b17beb45e89e4422f248981e905bb16212
[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         // processes
133         smpi_global->sender_processes                    = xbt_new(smx_process_t, size);
134         smpi_global->receiver_processes                  = xbt_new(smx_process_t, size);
135
136         // running hosts
137         smpi_global->running_hosts_count_mutex           = SIMIX_mutex_init();
138         smpi_global->running_hosts_count                 = 0;
139
140         // mallocators
141         smpi_global->request_mallocator                  = xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE,
142                                                              smpi_request_new, smpi_request_free, smpi_request_reset);
143         smpi_global->message_mallocator                  = xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE,
144                                                              smpi_message_new, smpi_message_free, smpi_message_reset);
145
146         // queues
147         smpi_global->pending_send_request_queues         = xbt_new(xbt_fifo_t,  size);
148         smpi_global->pending_send_request_queues_mutexes = xbt_new(smx_mutex_t, size);
149         smpi_global->pending_recv_request_queues         = xbt_new(xbt_fifo_t,  size);
150         smpi_global->pending_recv_request_queues_mutexes = xbt_new(smx_mutex_t, size);
151         smpi_global->received_message_queues             = xbt_new(xbt_fifo_t,  size);
152         smpi_global->received_message_queues_mutexes     = xbt_new(smx_mutex_t, size);
153
154         // timers
155         smpi_global->timers                              = xbt_new(xbt_os_timer_t, size);
156         smpi_global->timers_mutexes                      = xbt_new(smx_mutex_t, size);
157
158         for(i = 0; i < size; i++) {
159                 smpi_global->pending_send_request_queues[i]         = xbt_fifo_new();
160                 smpi_global->pending_send_request_queues_mutexes[i] = SIMIX_mutex_init();
161                 smpi_global->pending_recv_request_queues[i]         = xbt_fifo_new();
162                 smpi_global->pending_recv_request_queues_mutexes[i] = SIMIX_mutex_init();
163                 smpi_global->received_message_queues[i]             = xbt_fifo_new();
164                 smpi_global->received_message_queues_mutexes[i]     = SIMIX_mutex_init();
165                 smpi_global->timers[i]                              = xbt_os_timer_new();
166                 smpi_global->timers_mutexes[i]                      = SIMIX_mutex_init();
167         }
168
169 }
170
171 void smpi_global_destroy()
172 {
173         int i;
174
175         int size = SIMIX_host_get_number();
176
177         // start/stop
178         SIMIX_mutex_destroy(smpi_global->start_stop_mutex);
179         SIMIX_cond_destroy(smpi_global->start_stop_cond);
180
181         // processes
182         xbt_free(smpi_global->sender_processes);
183         xbt_free(smpi_global->receiver_processes);
184
185         // running hosts
186         SIMIX_mutex_destroy(smpi_global->running_hosts_count_mutex);
187
188         // mallocators
189         xbt_mallocator_free(smpi_global->request_mallocator);
190         xbt_mallocator_free(smpi_global->message_mallocator);
191
192         for(i = 0; i < size; i++) {
193                 xbt_fifo_free(smpi_global->pending_send_request_queues[i]);
194                 SIMIX_mutex_destroy(smpi_global->pending_send_request_queues_mutexes[i]);
195                 xbt_fifo_free(smpi_global->pending_recv_request_queues[i]);
196                 SIMIX_mutex_destroy(smpi_global->pending_recv_request_queues_mutexes[i]);
197                 xbt_fifo_free(smpi_global->received_message_queues[i]);
198                 SIMIX_mutex_destroy(smpi_global->received_message_queues_mutexes[i]);
199                 xbt_os_timer_free(smpi_global->timers[i]);
200                 SIMIX_mutex_destroy(smpi_global->timers_mutexes[i]);
201         }
202
203         xbt_free(smpi_global->pending_send_request_queues);
204         xbt_free(smpi_global->pending_send_request_queues_mutexes);
205         xbt_free(smpi_global->pending_recv_request_queues);
206         xbt_free(smpi_global->pending_recv_request_queues_mutexes);
207         xbt_free(smpi_global->received_message_queues);
208         xbt_free(smpi_global->received_message_queues_mutexes);
209         xbt_free(smpi_global->timers);
210         xbt_free(smpi_global->timers_mutexes);
211
212         xbt_free(smpi_global);
213
214         smpi_global = NULL;
215 }
216
217 int smpi_run_simulation(int argc, char **argv)
218 {
219         xbt_fifo_item_t cond_item   = NULL;
220         smx_cond_t   cond           = NULL;
221         xbt_fifo_item_t action_item = NULL;
222         smx_action_t action         = NULL;
223
224         xbt_fifo_t   actions_failed = xbt_fifo_new();
225         xbt_fifo_t   actions_done   = xbt_fifo_new();
226
227         srand(SMPI_RAND_SEED);
228
229         SIMIX_global_init(&argc, argv);
230
231         SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
232         SIMIX_function_register("smpi_sender",         smpi_sender);
233         SIMIX_function_register("smpi_receiver",       smpi_receiver);
234
235         // FIXME: ought to verify these files...
236         SIMIX_create_environment(argv[1]);
237
238         // must initialize globals between creating environment and launching app....
239         smpi_global_init();
240
241         SIMIX_launch_application(argv[2]);
242
243         /* Prepare to display some more info when dying on Ctrl-C pressing */
244         // FIXME: doesn't work
245         //signal(SIGINT, inthandler);
246
247         /* Clean IO before the run */
248         fflush(stdout);
249         fflush(stderr);
250
251         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
252                 xbt_fifo_foreach(actions_failed, action_item, action, smx_action_t) {
253                         DEBUG1("** %s failed **", action->name);
254                         xbt_fifo_foreach(action->cond_list, cond_item, cond, smx_cond_t) {
255                                 SIMIX_cond_broadcast(cond);
256                         }
257                 }
258                 xbt_fifo_foreach(actions_done, action_item, action, smx_action_t) {
259                         DEBUG1("** %s done **",action->name);
260                         xbt_fifo_foreach(action->cond_list, cond_item, cond, smx_cond_t) {
261                                 SIMIX_cond_broadcast(cond);
262                         }
263                 }
264         }
265
266         // FIXME: cleanup incomplete
267         xbt_fifo_free(actions_failed);
268         xbt_fifo_free(actions_done);
269
270         INFO1("simulation time %g", SIMIX_get_clock());
271
272         smpi_global_destroy();
273
274         SIMIX_clean();
275
276         return 0;
277 }