Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
refactored smpi into multiple source files.
[simgrid.git] / src / smpi / smpi_global.c
1 #include <stdio.h>
2 #include "private.h"
3
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi,XBT_LOG_ROOT_CAT, "All SMPI categories (see \ref SMPI_API)");
5
6 SMPI_Global_t     smpi_global     = NULL;
7
8 void *smpi_request_new()
9 {
10         smpi_mpi_request_t *request = xbt_new(smpi_mpi_request_t, 1);
11
12         request->completed = 0;
13         request->simdata            = xbt_new(s_smpi_mpi_request_simdata_t, 1);
14         request->simdata->mutex     = SIMIX_mutex_init();
15         request->simdata->cond      = SIMIX_cond_init();
16
17         return request;
18 }
19
20 void smpi_request_free(void *pointer)
21 {
22
23         smpi_mpi_request_t *request = pointer;
24
25         if (NULL != request) {
26                 SIMIX_cond_destroy(request->simdata->cond);
27                 SIMIX_mutex_destroy(request->simdata->mutex);
28                 xbt_free(request->simdata);
29                 xbt_free(request);
30         }
31
32         return;
33 }
34
35 void smpi_request_reset(void *pointer)
36 {
37         return;
38 }
39
40
41 void *smpi_message_new()
42 {
43         return xbt_new(smpi_received_message_t, 1);
44 }
45
46 void smpi_message_free(void *pointer)
47 {
48         if (NULL != pointer) {
49                 xbt_free(pointer);
50         }
51
52         return;
53 }
54
55 void smpi_message_reset(void *pointer)
56 {
57         return;
58 }
59
60 void smpi_global_init()
61 {
62         int i;
63
64         int size = SIMIX_host_get_number();
65
66         smpi_global = xbt_new(s_SMPI_Global_t, 1);
67
68         // config variable
69         smpi_global->reference_speed                     = SMPI_DEFAULT_SPEED;
70
71         smpi_global->root_ready                          = 0;
72         smpi_global->ready_process_count                 = 0;
73
74         // start/stop
75         smpi_global->start_stop_mutex                    = SIMIX_mutex_init();
76         smpi_global->start_stop_cond                     = SIMIX_cond_init();
77
78         // processes
79         smpi_global->sender_processes                    = xbt_new(smx_process_t, size);
80         smpi_global->receiver_processes                  = xbt_new(smx_process_t, size);
81
82         // running hosts
83         smpi_global->running_hosts_count_mutex           = SIMIX_mutex_init();
84         smpi_global->running_hosts_count                 = 0;
85
86         // mallocators
87         smpi_global->request_mallocator                  = xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE,
88                                                              smpi_request_new, smpi_request_free, smpi_request_reset);
89         smpi_global->message_mallocator                  = xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE,
90                                                              smpi_message_new, smpi_message_free, smpi_message_reset);
91
92         // queues
93         smpi_global->pending_send_request_queues         = xbt_new(xbt_fifo_t,  size);
94         smpi_global->pending_send_request_queues_mutexes = xbt_new(smx_mutex_t, size);
95         smpi_global->pending_recv_request_queues         = xbt_new(xbt_fifo_t,  size);
96         smpi_global->pending_recv_request_queues_mutexes = xbt_new(smx_mutex_t, size);
97         smpi_global->received_message_queues             = xbt_new(xbt_fifo_t,  size);
98         smpi_global->received_message_queues_mutexes     = xbt_new(smx_mutex_t, size);
99
100         // timers
101         smpi_global->timers                              = xbt_new(xbt_os_timer_t, size);
102         smpi_global->timers_mutexes                      = xbt_new(smx_mutex_t, size);
103
104         for(i = 0; i < size; i++) {
105                 smpi_global->pending_send_request_queues[i]         = xbt_fifo_new();
106                 smpi_global->pending_send_request_queues_mutexes[i] = SIMIX_mutex_init();
107                 smpi_global->pending_recv_request_queues[i]         = xbt_fifo_new();
108                 smpi_global->pending_recv_request_queues_mutexes[i] = SIMIX_mutex_init();
109                 smpi_global->received_message_queues[i]             = xbt_fifo_new();
110                 smpi_global->received_message_queues_mutexes[i]     = SIMIX_mutex_init();
111                 smpi_global->timers[i]                              = xbt_os_timer_new();
112                 smpi_global->timers_mutexes[i]                      = SIMIX_mutex_init();
113         }
114
115 }
116
117 void smpi_global_destroy()
118 {
119         int i;
120
121         int size = SIMIX_host_get_number();
122
123         // start/stop
124         SIMIX_mutex_destroy(smpi_global->start_stop_mutex);
125         SIMIX_cond_destroy(smpi_global->start_stop_cond);
126
127         // processes
128         xbt_free(smpi_global->sender_processes);
129         xbt_free(smpi_global->receiver_processes);
130
131         // running hosts
132         SIMIX_mutex_destroy(smpi_global->running_hosts_count_mutex);
133
134         // mallocators
135         xbt_mallocator_free(smpi_global->request_mallocator);
136         xbt_mallocator_free(smpi_global->message_mallocator);
137
138         for(i = 0; i < size; i++) {
139                 xbt_fifo_free(smpi_global->pending_send_request_queues[i]);
140                 SIMIX_mutex_destroy(smpi_global->pending_send_request_queues_mutexes[i]);
141                 xbt_fifo_free(smpi_global->pending_recv_request_queues[i]);
142                 SIMIX_mutex_destroy(smpi_global->pending_recv_request_queues_mutexes[i]);
143                 xbt_fifo_free(smpi_global->received_message_queues[i]);
144                 SIMIX_mutex_destroy(smpi_global->received_message_queues_mutexes[i]);
145                 xbt_os_timer_free(smpi_global->timers[i]);
146                 SIMIX_mutex_destroy(smpi_global->timers_mutexes[i]);
147         }
148
149         xbt_free(smpi_global->pending_send_request_queues);
150         xbt_free(smpi_global->pending_send_request_queues_mutexes);
151         xbt_free(smpi_global->pending_recv_request_queues);
152         xbt_free(smpi_global->pending_recv_request_queues_mutexes);
153         xbt_free(smpi_global->received_message_queues);
154         xbt_free(smpi_global->received_message_queues_mutexes);
155         xbt_free(smpi_global->timers);
156         xbt_free(smpi_global->timers_mutexes);
157
158         xbt_free(smpi_global);
159 }
160
161 int smpi_run_simulation(int argc, char **argv)
162 {
163         xbt_fifo_item_t cond_item   = NULL;
164         smx_cond_t   cond           = NULL;
165         xbt_fifo_item_t action_item = NULL;
166         smx_action_t action         = NULL;
167
168         xbt_fifo_t   actions_failed = xbt_fifo_new();
169         xbt_fifo_t   actions_done   = xbt_fifo_new();
170
171         srand(SMPI_RAND_SEED);
172
173         SIMIX_global_init(&argc, argv);
174
175         SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
176         SIMIX_function_register("smpi_sender",         smpi_sender);
177         SIMIX_function_register("smpi_receiver",       smpi_receiver);
178
179         // FIXME: ought to verify these files...
180         SIMIX_create_environment(argv[1]);
181
182         // must initialize globals between creating environment and launching app....
183         smpi_global_init();
184
185         SIMIX_launch_application(argv[2]);
186
187         /* Prepare to display some more info when dying on Ctrl-C pressing */
188         // FIXME: doesn't work
189         //signal(SIGINT, inthandler);
190
191         /* Clean IO before the run */
192         fflush(stdout);
193         fflush(stderr);
194
195         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
196                 xbt_fifo_foreach(actions_failed, action_item, action, smx_action_t) {
197                         DEBUG1("** %s failed **", action->name);
198                         xbt_fifo_foreach(action->cond_list, cond_item, cond, smx_cond_t) {
199                                 SIMIX_cond_broadcast(cond);
200                         }
201                 }
202                 xbt_fifo_foreach(actions_done, action_item, action, smx_action_t) {
203                         DEBUG1("** %s done **",action->name);
204                         xbt_fifo_foreach(action->cond_list, cond_item, cond, smx_cond_t) {
205                                 SIMIX_cond_broadcast(cond);
206                         }
207                 }
208         }
209
210         // FIXME: cleanup incomplete
211         xbt_fifo_free(actions_failed);
212         xbt_fifo_free(actions_done);
213
214         INFO1("simulation time %g", SIMIX_get_clock());
215
216         smpi_global_destroy();
217
218         SIMIX_clean();
219
220         return 0;
221 }