Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b0c335d8304b9a44a047bf9206a9bc84f041a034
[simgrid.git] / src / smpi / smpi_global.c
1 #include <stdio.h>
2
3 #include "private.h"
4
5 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
6
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
8                                 "Logging specific to SMPI (kernel)");
9
10 smpi_global_t smpi_global = NULL;
11
12 void *smpi_request_new(void);
13
14 void *smpi_request_new()
15 {
16   smpi_mpi_request_t request = xbt_new(s_smpi_mpi_request_t, 1);
17
18   request->buf = NULL;
19   request->completed = 0;
20   request->mutex = SIMIX_mutex_init();
21   request->cond = SIMIX_cond_init();
22   request->data = NULL;
23   request->forward = 0;
24
25   return request;
26 }
27
28 void smpi_request_free(void *pointer);
29
30 void smpi_request_free(void *pointer)
31 {
32
33   smpi_mpi_request_t request = pointer;
34
35   SIMIX_cond_destroy(request->cond);
36   SIMIX_mutex_destroy(request->mutex);
37   xbt_free(request);
38
39   return;
40 }
41
42 void smpi_request_reset(void *pointer);
43
44 void smpi_request_reset(void *pointer)
45 {
46   smpi_mpi_request_t request = pointer;
47
48   request->buf = NULL;
49   request->completed = 0;
50   request->consumed = 0;
51   request->data = NULL;
52   request->forward = 0;
53
54   return;
55 }
56
57
58 void *smpi_message_new(void);
59
60 void *smpi_message_new()
61 {
62   smpi_received_message_t message = xbt_new(s_smpi_received_message_t, 1);
63   message->buf = NULL;
64   return message;
65 }
66
67 void smpi_message_free(void *pointer);
68
69 void smpi_message_free(void *pointer)
70 {
71   xbt_free(pointer);
72   return;
73 }
74
75 void smpi_message_reset(void *pointer);
76
77 void smpi_message_reset(void *pointer)
78 {
79   smpi_received_message_t message = pointer;
80   message->buf = NULL;
81   return;
82 }
83
84 int smpi_create_request(void *buf, int count, smpi_mpi_datatype_t datatype,
85                         int src, int dst, int tag,
86                         smpi_mpi_communicator_t comm,
87                         smpi_mpi_request_t * requestptr)
88 {
89   int retval = MPI_SUCCESS;
90
91   smpi_mpi_request_t request = NULL;
92
93             printf("in create-req():  MPI_ANY_SOURCE=%d,src=%d,comm->size=%d\n",MPI_ANY_SOURCE,src,comm->size);
94   // parameter checking prob belongs in smpi_mpi, but this is less repeat code
95   if (NULL == buf) {
96     retval = MPI_ERR_INTERN;
97   } else if (0 > count) {
98     retval = MPI_ERR_COUNT;
99   } else if (NULL == datatype) {
100     retval = MPI_ERR_TYPE;
101   } else if (MPI_ANY_SOURCE != src && (0 > src || comm->size <= src)) {
102     retval = MPI_ERR_RANK;
103   } else if (0 > dst || comm->size <= dst) {
104             printf("err MPI_ERR_RANK => MPI_ANY_SOURCE=%d,src=%d,dst=%d,comm->size=%d\n",MPI_ANY_SOURCE,src,dst,comm->size);
105     retval = MPI_ERR_RANK;
106   } else if (MPI_ANY_TAG != tag && 0 > tag) {
107     retval = MPI_ERR_TAG;
108   } else if (NULL == comm) {
109     retval = MPI_ERR_COMM;
110   } else if (NULL == requestptr) {
111     retval = MPI_ERR_ARG;
112   } else {
113     request = xbt_mallocator_get(smpi_global->request_mallocator);
114     request->comm = comm;
115     request->src = src;
116     request->dst = dst;
117     request->tag = tag;
118     request->buf = buf;
119     request->datatype = datatype;
120     request->count = count;
121
122     *requestptr = request;
123   }
124   return retval;
125 }
126 /* FIXME: understand what they do and put the prototypes in a header file (live in smpi_base.c) */
127 void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype * datatype);
128 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype * datatype);
129 void smpi_mpi_min_func(void *a, void *b, int *length, MPI_Datatype * datatype);
130 void smpi_mpi_max_func(void *a, void *b, int *length, MPI_Datatype * datatype);
131
132 void smpi_global_init()
133 {
134   int i;
135
136   /* Connect our log channels: that must be done manually under windows */
137 #ifdef XBT_LOG_CONNECT
138   XBT_LOG_CONNECT(smpi_base, smpi);
139   XBT_LOG_CONNECT(smpi_bench, smpi);
140   XBT_LOG_CONNECT(smpi_kernel, smpi);
141   XBT_LOG_CONNECT(smpi_mpi, smpi);
142   XBT_LOG_CONNECT(smpi_receiver, smpi);
143   XBT_LOG_CONNECT(smpi_sender, smpi);
144   XBT_LOG_CONNECT(smpi_util, smpi);
145 #endif
146
147   smpi_global = xbt_new(s_smpi_global_t, 1);
148
149   // mallocators
150   smpi_global->request_mallocator =
151     xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE, smpi_request_new,
152                        smpi_request_free, smpi_request_reset);
153   smpi_global->message_mallocator =
154     xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE, smpi_message_new,
155                        smpi_message_free, smpi_message_reset);
156
157   smpi_global->process_count = SIMIX_process_count();
158   DEBUG1("There is %d processes",smpi_global->process_count);
159
160   // sender/receiver processes
161   smpi_global->main_processes = xbt_new(smx_process_t, smpi_global->process_count);
162
163   // timers
164   smpi_global->timer = xbt_os_timer_new();
165   smpi_global->timer_mutex = SIMIX_mutex_init();
166   smpi_global->timer_cond = SIMIX_cond_init();
167
168   smpi_global->do_once_duration_nodes = NULL;
169   smpi_global->do_once_duration = NULL;
170   smpi_global->do_once_mutex = SIMIX_mutex_init();
171
172
173   smpi_mpi_global = xbt_new(s_smpi_mpi_global_t, 1);
174
175   // global communicator
176   smpi_mpi_global->mpi_comm_world = xbt_new(s_smpi_mpi_communicator_t, 1);
177   smpi_mpi_global->mpi_comm_world->size = smpi_global->process_count;
178   smpi_mpi_global->mpi_comm_world->barrier_count = 0;
179   smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
180   smpi_mpi_global->mpi_comm_world->barrier_cond = SIMIX_cond_init();
181   smpi_mpi_global->mpi_comm_world->rank_to_index_map =
182     xbt_new(int, smpi_global->process_count);
183   smpi_mpi_global->mpi_comm_world->index_to_rank_map =
184     xbt_new(int, smpi_global->process_count);
185   for (i = 0; i < smpi_global->process_count; i++) {
186     smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
187     smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
188   }
189
190   // mpi datatypes
191   smpi_mpi_global->mpi_byte = xbt_new(s_smpi_mpi_datatype_t, 1);
192   smpi_mpi_global->mpi_byte->size = (size_t) 1;
193   smpi_mpi_global->mpi_int = xbt_new(s_smpi_mpi_datatype_t, 1);
194   smpi_mpi_global->mpi_int->size = sizeof(int);
195   smpi_mpi_global->mpi_double = xbt_new(s_smpi_mpi_datatype_t, 1);
196   smpi_mpi_global->mpi_double->size = sizeof(double);
197
198   // mpi operations
199   smpi_mpi_global->mpi_land = xbt_new(s_smpi_mpi_op_t, 1);
200   smpi_mpi_global->mpi_land->func = smpi_mpi_land_func;
201   smpi_mpi_global->mpi_sum = xbt_new(s_smpi_mpi_op_t, 1);
202   smpi_mpi_global->mpi_sum->func = smpi_mpi_sum_func;
203   smpi_mpi_global->mpi_min = xbt_new(s_smpi_mpi_op_t, 1);
204   smpi_mpi_global->mpi_min->func = smpi_mpi_min_func;
205   smpi_mpi_global->mpi_max = xbt_new(s_smpi_mpi_op_t, 1);
206   smpi_mpi_global->mpi_min->func = smpi_mpi_max_func;
207
208 }
209
210 void smpi_global_destroy()
211 {
212   smpi_do_once_duration_node_t curr, next;
213
214   // processes
215   xbt_free(smpi_global->main_processes);
216
217   // mallocators
218   xbt_mallocator_free(smpi_global->request_mallocator);
219   xbt_mallocator_free(smpi_global->message_mallocator);
220
221   xbt_os_timer_free(smpi_global->timer);
222   SIMIX_mutex_destroy(smpi_global->timer_mutex);
223   SIMIX_cond_destroy(smpi_global->timer_cond);
224
225   for (curr = smpi_global->do_once_duration_nodes; NULL != curr; curr = next) {
226     next = curr->next;
227     xbt_free(curr->file);
228     xbt_free(curr);
229   }
230
231   SIMIX_mutex_destroy(smpi_global->do_once_mutex);
232
233   xbt_free(smpi_global);
234   smpi_global = NULL;
235
236   /* free smpi_mpi_global */
237   SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
238   SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
239   xbt_free(smpi_mpi_global->mpi_comm_world);
240
241   xbt_free(smpi_mpi_global->mpi_byte);
242   xbt_free(smpi_mpi_global->mpi_int);
243   xbt_free(smpi_mpi_global->mpi_double);
244
245   xbt_free(smpi_mpi_global->mpi_land);
246   xbt_free(smpi_mpi_global->mpi_sum);
247
248   xbt_free(smpi_mpi_global);
249
250 }
251
252 int smpi_process_index()
253 {
254   smpi_process_data_t pdata = (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
255   return pdata->index;
256 }
257
258 smx_mutex_t smpi_process_mutex()
259 {
260   smpi_process_data_t pdata = (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
261   return pdata->mutex;
262 }
263
264 smx_cond_t smpi_process_cond()
265 {
266   smpi_process_data_t pdata = (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
267   return pdata->cond;
268 }
269
270 static void smpi_cfg_cb_host_speed(const char *name, int pos) {
271         smpi_global->reference_speed = xbt_cfg_get_double_at(_surf_cfg_set,name,pos);
272 }
273
274 int smpi_run_simulation(int *argc, char **argv)
275 {
276   smx_cond_t cond = NULL;
277   smx_action_t action = NULL;
278
279   xbt_fifo_t actions_failed = xbt_fifo_new();
280   xbt_fifo_t actions_done = xbt_fifo_new();
281
282   srand(SMPI_RAND_SEED);
283
284   double default_reference_speed = 20000.0;
285   xbt_cfg_register(&_surf_cfg_set,"reference_speed","Power of the host running the simulation (in flop/s). Used to bench the operations.",
286                   xbt_cfgelm_double,&default_reference_speed,1,1,smpi_cfg_cb_host_speed,NULL);
287
288   int default_display_timing = 0;
289   xbt_cfg_register(&_surf_cfg_set,"display_timing","Boolean indicating whether we should display the timing after simulation.",
290                   xbt_cfgelm_int,&default_display_timing,1,1,NULL,NULL);
291
292   SIMIX_global_init(argc, argv);
293
294
295   // parse the platform file: get the host list
296   SIMIX_create_environment(argv[1]);
297
298   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
299   SIMIX_launch_application(argv[2]);
300
301   // must initialize globals between creating environment and launching app....
302   smpi_global_init();
303
304   /* Clean IO before the run */
305   fflush(stdout);
306   fflush(stderr);
307   SIMIX_init();
308
309   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
310     while ((action = xbt_fifo_pop(actions_failed))) {
311       DEBUG1("** %s failed **", action->name);
312       while ((cond = xbt_fifo_pop(action->cond_list))) {
313         SIMIX_cond_broadcast(cond);
314       }
315     }
316     while ((action = xbt_fifo_pop(actions_done))) {
317       DEBUG1("** %s done **", action->name);
318       while ((cond = xbt_fifo_pop(action->cond_list))) {
319         SIMIX_cond_broadcast(cond);
320       }
321     }
322   }
323
324   // FIXME: cleanup incomplete
325   xbt_fifo_free(actions_failed);
326   xbt_fifo_free(actions_done);
327
328
329   if (xbt_cfg_get_int(_surf_cfg_set,"display_timing"))
330           INFO1("simulation time %g", SIMIX_get_clock());
331
332   smpi_global_destroy();
333   SIMIX_clean();
334
335   return 0;
336 }