Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[simgrid.git] / src / simix / smx_network.c
1 /* Copyright (c) 2009, 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 "private.h"
8 #include "xbt/log.h"
9 #include "xbt/dict.h"
10
11 /* Pimple to get an histogram of message sizes in the simulation */
12 xbt_dict_t msg_sizes = NULL;
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
15                                 "Logging specific to SIMIX (network)");
16
17 /******************************************************************************/
18 /*                           Rendez-Vous Points                               */
19 /******************************************************************************/ 
20
21 /**
22  *  \brief Creates a new rendez-vous point
23  *  \param name The name of the rendez-vous point
24  *  \return The created rendez-vous point
25  */
26 smx_rdv_t SIMIX_rdv_create(const char *name)
27 {
28   smx_rdv_t rdv = xbt_new0(s_smx_rvpoint_t, 1);
29   rdv->name = name ? xbt_strdup(name) : NULL;
30   rdv->read = SIMIX_mutex_init();
31   rdv->write = SIMIX_mutex_init();
32   rdv->comm_fifo = xbt_fifo_new();
33
34   return rdv;
35 }
36
37 /**
38  *  \brief Destroy a rendez-vous point
39  *  \param name The rendez-vous point to destroy
40  */
41 void SIMIX_rdv_destroy(smx_rdv_t rdv)
42 {
43   if(rdv->name)
44     xbt_free(rdv->name);
45   SIMIX_mutex_destroy(rdv->read);
46   SIMIX_mutex_destroy(rdv->write);
47   xbt_fifo_free(rdv->comm_fifo);
48   xbt_free(rdv);
49 }
50
51 /**
52  *  \brief Push a communication request into a rendez-vous point
53  *  \param rdv The rendez-vous point
54  *  \param comm The communication request
55  */
56 static inline void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
57 {
58   xbt_fifo_push(rdv->comm_fifo, comm);
59   comm->rdv = rdv;
60 }
61
62 /**
63  *  \brief Remove a communication request from a rendez-vous point
64  *  \param rdv The rendez-vous point
65  *  \param comm The communication request
66  */
67 static inline void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
68 {
69   xbt_fifo_remove(rdv->comm_fifo, comm);
70   comm->rdv = NULL;
71 }
72   
73 /**
74  *  \brief Checks if there is a communication request queued in a rendez-vous matching our needs
75  *  \param type The type of communication we are looking for (comm_send, comm_recv)
76  *  \return The communication request if found, NULL otherwise.
77  */
78 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type) {
79   smx_comm_t comm = (smx_comm_t)xbt_fifo_get_item_content(
80                                   xbt_fifo_get_first_item(rdv->comm_fifo));
81
82   if(comm && comm->type == type){
83     DEBUG0("Communication request found!");
84     xbt_fifo_shift(rdv->comm_fifo);
85     SIMIX_communication_use(comm);
86     comm->rdv = NULL;    
87     return comm;
88   }
89
90   DEBUG0("Communication request not found");
91   return NULL;
92 }
93
94 /**
95  *  \brief counts the number of communication requests of a given host pending
96  *         on a rendez-vous point
97  *  \param rdv The rendez-vous point
98  *  \param host The host to be counted
99  *  \return The number of comm request pending in the rdv
100  */
101 int 
102 SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
103 {
104   smx_comm_t comm = NULL;
105   xbt_fifo_item_t item = NULL;
106   int count = 0;
107
108   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
109     if (comm->src_proc->smx_host == host)
110       count++;
111   }
112
113   return count;
114 }
115
116 /**
117  *  \brief returns the communication at the head of the rendez-vous
118  *  \param rdv The rendez-vous point
119  *  \return The communication or NULL if empty
120  */
121 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
122 {
123   return (smx_comm_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
124 }
125
126 /** @brief adds some API-related data to the rendez-vous point */
127 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv,void *data) {
128   rdv->data=data;
129 }
130 /** @brief gets API-related data from the rendez-vous point */
131 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv) {
132   return rdv->data;
133 }
134
135 /******************************************************************************/
136 /*                           Communication Requests                           */
137 /******************************************************************************/ 
138
139 /**
140  *  \brief Creates a new communication request
141  *  \param type The type of communication (comm_send, comm_recv)
142  *  \return The new communication request
143  */  
144 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
145 {
146   /* alloc structures */
147   smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
148   comm->type = type;
149   comm->sem = SIMIX_sem_init(0);
150   comm->refcount = 1;
151   
152   return comm;
153 }
154
155 /**
156  *  \brief Destroy a communication request
157  *  \param comm The request to be destroyed
158  */
159 void SIMIX_communication_destroy(smx_comm_t comm)
160 {
161   comm->refcount--;
162   if(comm->refcount > 0)
163     return;
164
165   if(comm->sem){
166     SIMIX_sem_destroy(comm->sem);
167     comm->sem = NULL;
168   }
169   
170   if(comm->act){
171     SIMIX_action_destroy(comm->act);
172     comm->act = NULL;
173   }
174
175   if(comm->src_timeout){
176     SIMIX_action_destroy(comm->src_timeout);
177     comm->src_timeout = NULL;
178   }
179
180   if(comm->dst_timeout){
181       SIMIX_action_destroy(comm->dst_timeout);
182       comm->dst_timeout = NULL;
183     }
184
185   xbt_free(comm);
186 }
187
188 /**
189  *  \brief Increase the number of users of the communication.
190  *  \param comm The communication request
191  *  Each communication request can be used by more than one process, so it is
192  *  necessary to know number of them at destroy time, to avoid freeing stuff that
193  *  maybe is in use by others.
194  *  \
195  */
196 static inline void SIMIX_communication_use(smx_comm_t comm)
197 {
198   comm->refcount++;
199 }
200
201 /**
202  *  \brief Start the simulation of a communication request
203  *  \param comm The   comm->rdv = NULL;communication request
204  */
205 static inline void SIMIX_communication_start(smx_comm_t comm)
206 {
207   /* If both the sender and the receiver are already there, start the communication */
208   if(comm->src_proc && comm->dst_proc){
209     DEBUG1("Starting communication %p", comm);
210     comm->act = SIMIX_action_communicate(comm->src_proc->smx_host, 
211                                          comm->dst_proc->smx_host, NULL, 
212                                          comm->task_size, comm->rate);
213 #ifdef HAVE_TRACING
214     TRACE_smx_action_communicate (comm->act, comm->src_proc);
215 #endif
216
217     /* If any of the process is suspend, create the action but stop its execution,
218        it will be restarted when the sender process resume */
219     if(SIMIX_process_is_suspended(comm->src_proc) || 
220        SIMIX_process_is_suspended(comm->dst_proc)) {
221       SIMIX_action_suspend(comm->act);
222     }
223     
224     /* Add the communication as user data of the action */
225     comm->act->data = comm;
226
227     /* The semaphore will only get signaled once, but since the first unlocked guy will
228      * release_forever() the semaphore, that will unlock the second (and any other)
229      * communication partner */
230     SIMIX_register_action_to_semaphore(comm->act, comm->sem);
231   }
232 }
233
234 /**
235  *  \brief Waits for communication completion and performs error checking
236  *  \param comm The communication
237  *  \param timeout The max amount of time to wait for the communication to finish
238  *
239  *  Throws:
240  *   - host_error if peer failed
241  *   - timeout_error if communication reached the timeout specified
242  *   - network_error if network failed or peer issued a timeout
243  */
244 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
245 {
246   smx_action_t act_sleep = NULL;
247   int src_timeout = 0;
248   int dst_timeout = 0;
249
250   DEBUG1("Waiting for the completion of communication %p", comm);
251   
252   if (timeout >= 0) {
253     act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
254                 if(SIMIX_process_self()==comm->src_proc)
255                         comm->src_timeout = act_sleep;
256                 else
257                         comm->dst_timeout = act_sleep;
258     SIMIX_action_set_name(act_sleep,bprintf("Timeout for comm %p and wait on semaphore %p (max_duration:%f)", comm, comm->sem,timeout));
259     SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
260     SIMIX_process_self()->waiting_action = act_sleep;
261     SIMIX_sem_block_onto(comm->sem);
262     SIMIX_process_self()->waiting_action = NULL;
263     SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
264   } else {
265     SIMIX_sem_acquire(comm->sem);
266   }
267
268   /* Check for timeouts */
269   if ((src_timeout = ((comm->src_timeout) && (SIMIX_action_get_state(comm->src_timeout) == SURF_ACTION_DONE))) ||
270       (dst_timeout = ((comm->dst_timeout) && (SIMIX_action_get_state(comm->dst_timeout) == SURF_ACTION_DONE))) ) {
271                         /* Somebody did a timeout! */
272     if (src_timeout) DEBUG1("Communication timeout from the src! %p", comm);
273     if (dst_timeout) DEBUG1("Communication timeout from the dst! %p", comm);
274
275     if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
276       SIMIX_communication_cancel(comm);
277     else if (comm->rdv)
278       SIMIX_rdv_remove(comm->rdv, comm);
279
280     /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
281     SIMIX_sem_release_forever(comm->sem);
282     SIMIX_communication_destroy(comm);
283
284     THROW1(timeout_error, 0, "Communication timeouted because of %s",src_timeout?"the source":"the destination");
285   }
286
287   DEBUG1("Communication %p complete! Let's check for errors", comm);
288
289   /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
290   SIMIX_sem_release_forever(comm->sem);
291   
292   /* Check for errors other than timeouts (they are catched above) */
293   if(!SIMIX_host_get_state(SIMIX_host_self())){
294     if(comm->rdv)
295       SIMIX_rdv_remove(comm->rdv, comm);
296     SIMIX_communication_destroy(comm);
297     THROW0(host_error, 0, "Host failed");
298   } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
299     SIMIX_communication_destroy(comm);
300     THROW0(network_error, 0, "Link failure");
301   }
302   SIMIX_communication_destroy(comm);
303 }
304
305 /**
306  *  \brief Cancels a communication
307  *  \brief comm The communication to cancel
308  */
309 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
310 {
311   if (comm->act)
312     SIMIX_action_cancel(comm->act);
313 }
314
315 /**
316  *  \brief get the amount remaining from the communication
317  *  \param comm The communication
318  */
319 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
320 {
321   return SIMIX_action_get_remains(comm->act);
322 }  
323
324 /******************************************************************************/
325 /*                    SIMIX_network_copy_data callbacks                       */
326 /******************************************************************************/
327 static void (*SIMIX_network_copy_data_callback)(smx_comm_t, size_t) = &SIMIX_network_copy_pointer_callback;
328
329 void SIMIX_network_set_copy_data_callback(void (*callback)(smx_comm_t, size_t)) {
330   SIMIX_network_copy_data_callback = callback;
331 }
332
333 void SIMIX_network_copy_pointer_callback(smx_comm_t comm, size_t buff_size) {
334   xbt_assert1((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)",buff_size);
335   *(void**)(comm->dst_buff) = comm->src_buff;
336 }
337
338 void SIMIX_network_copy_buffer_callback(smx_comm_t comm, size_t buff_size) {
339   memcpy(comm->dst_buff, comm->src_buff, buff_size);
340 }
341
342 /**
343  *  \brief Copy the communication data from the sender's buffer to the receiver's one
344  *  \param comm The communication
345  */
346 void SIMIX_network_copy_data(smx_comm_t comm)
347 {
348   size_t buff_size = comm->src_buff_size;
349
350   DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
351       comm,
352       comm->src_proc->smx_host->name, comm->src_buff,
353       comm->dst_proc->smx_host->name, comm->dst_buff,
354       buff_size);
355
356   /* If there is no data to be copy then return */
357   if(!comm->src_buff || !comm->dst_buff)
358     return;
359   
360   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
361   if (comm->dst_buff_size)
362     buff_size = MIN(buff_size,*(comm->dst_buff_size));
363   
364   /* Update the receiver's buffer size to the copied amount */
365   if (comm->dst_buff_size)
366     *comm->dst_buff_size = buff_size;
367
368   if(buff_size == 0)
369     return;
370   (*SIMIX_network_copy_data_callback)(comm, buff_size);
371
372   /* pimple to display the message sizes */
373   {
374     if (msg_sizes == NULL)
375       msg_sizes = xbt_dict_new();
376     uintptr_t casted_size = comm->task_size;
377     uintptr_t amount = xbt_dicti_get(msg_sizes, casted_size);
378     amount++;
379
380     xbt_dicti_set(msg_sizes,casted_size, amount);
381   }
382 }
383 #include "xbt.h"
384 /* pimple to display the message sizes */
385 void SIMIX_message_sizes_output(const char *filename) {
386   FILE * out = fopen(filename,"w");
387   xbt_assert1(out,"Cannot open file %s",filename);
388   uintptr_t key,data;
389   xbt_dict_cursor_t cursor;
390   xbt_dict_foreach(msg_sizes,cursor,key,data) {
391     fprintf(out,"%zu %zu\n",key,data);
392   }
393   fclose(out);
394 }
395
396 /**
397  *  \brief Return the user data associated to the communication
398  *  \param comm The communication
399  *  \return the user data
400  */
401 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
402 {
403   return comm->data;
404 }
405
406 /******************************************************************************/
407 /*                        Synchronous Communication                           */
408 /******************************************************************************/
409 /**
410  *  \brief Put a send communication request in a rendez-vous point and waits for
411  *         its completion (blocking)
412  *  \param rdv The rendez-vous point
413  *  \param task_size The size of the communication action (for surf simulation)
414  *  \param rate The rate of the communication action (for surf)
415  *  \param timeout The timeout used for the waiting the completion 
416  *  \param src_buff The source buffer containing the message to be sent
417  *  \param src_buff_size The size of the source buffer
418  *  \param comm_ref The communication object used for the send  (useful if someone else wants to cancel this communication afterward)
419  *  \param data User data associated to the communication object
420  *  Throws:
421  *   - host_error if peer failed
422  *   - timeout_error if communication reached the timeout specified
423  *   - network_error if network failed or peer issued a timeout
424  */
425 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate,
426                         double timeout, void *src_buff, size_t src_buff_size,
427                         smx_comm_t *comm_ref, void *data)
428 {
429   *comm_ref = SIMIX_network_isend(rdv,task_size,rate,src_buff,src_buff_size,data);
430   SIMIX_network_wait(*comm_ref,timeout);
431 }
432
433 /**
434  *  \brief Put a receive communication request in a rendez-vous point and waits
435  *         for its completion (blocking)
436  *  \param rdv The rendez-vous point
437  *  \param timeout The timeout used for the waiting the completion 
438  *  \param dst_buff The destination buffer to copy the received message
439  *  \param src_buff_size The size of the destination buffer
440  *  \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
441  *  Throws:
442  *   - host_error if peer failed
443  *   - timeout_error if communication reached the timeout specified
444  *   - network_error if network failed or peer issued a timeout
445  */
446 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *dst_buff,
447                         size_t *dst_buff_size, smx_comm_t *comm_ref)
448 {
449   *comm_ref = SIMIX_network_irecv(rdv,dst_buff,dst_buff_size);
450   SIMIX_network_wait(*comm_ref,timeout);
451 }
452
453 /******************************************************************************/
454 /*                        Asynchronous Communication                          */
455 /******************************************************************************/
456 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size, double rate,
457     void *src_buff, size_t src_buff_size, void *data)
458 {
459   smx_comm_t comm;
460
461   /* Look for communication request matching our needs.
462      If it is not found then create it and push it into the rendez-vous point */
463   comm = SIMIX_rdv_get_request(rdv, comm_recv);
464
465   if(!comm){
466     comm = SIMIX_communication_new(comm_send);
467     SIMIX_rdv_push(rdv, comm);
468   }
469
470   /* Setup the communication request */
471   comm->src_proc = SIMIX_process_self();
472   comm->task_size = task_size;
473   comm->rate = rate;
474   comm->src_buff = src_buff;
475   comm->src_buff_size = src_buff_size;
476   comm->data = data;
477
478   SIMIX_communication_start(comm);
479   return comm;
480 }
481
482 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff, size_t *dst_buff_size) {
483   smx_comm_t comm;
484
485   /* Look for communication request matching our needs.
486      If it is not found then create it and push it into the rendez-vous point */
487   comm = SIMIX_rdv_get_request(rdv, comm_send);
488
489   if(!comm){
490     comm = SIMIX_communication_new(comm_recv);
491     SIMIX_rdv_push(rdv, comm);
492   }
493
494   /* Setup communication request */
495   comm->dst_proc = SIMIX_process_self();
496   comm->dst_buff = dst_buff;
497   comm->dst_buff_size = dst_buff_size;
498
499   SIMIX_communication_start(comm);
500   return comm;
501 }
502
503 /** @brief blocks until the communication terminates or the timeout occurs */
504 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout) {
505   /* Wait for communication completion */
506   SIMIX_communication_wait_for_completion(comm, timeout);
507 }
508
509 /** @Returns whether the (asynchronous) communication is done yet or not */
510 XBT_INLINE int SIMIX_network_test(smx_comm_t comm) {
511   return comm->sem?SIMIX_sem_would_block(comm->sem):0;
512 }
513
514 /** @brief wait for the completion of any communication of a set
515  *
516  *  @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
517  */
518 unsigned int SIMIX_network_waitany(xbt_dynar_t comms) {
519   xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t),NULL);
520   unsigned int cursor, found_comm=-1;
521   smx_comm_t comm,comm_finished=NULL;
522
523   xbt_dynar_foreach(comms,cursor,comm){
524     xbt_dynar_push(sems,&(comm->sem));
525   }
526
527   DEBUG1("Waiting for the completion of communication set %p", comms);
528
529   found_comm = SIMIX_sem_acquire_any(sems);
530   xbt_assert0(found_comm!=-1,"Cannot find which communication finished");
531   xbt_dynar_get_cpy(comms,found_comm,&comm_finished);
532
533   DEBUG1("Communication %p complete! Let's check for errors", comm_finished);
534
535   /* Make sure that everyone sleeping on that semaphore is awake,
536    * and that nobody will ever block on it */
537   SIMIX_sem_release_forever(comm_finished->sem);
538
539   /* Check for errors */
540   if(!SIMIX_host_get_state(SIMIX_host_self())){
541     if(comm_finished->rdv)
542       SIMIX_rdv_remove(comm_finished->rdv, comm_finished);
543     SIMIX_communication_destroy(comm_finished);
544     THROW0(host_error, 0, "Host failed");
545   } else if (SIMIX_action_get_state(comm_finished->act) == SURF_ACTION_FAILED){
546     SIMIX_communication_destroy(comm_finished);
547     THROW0(network_error, 0, "Link failure");
548   }
549   SIMIX_communication_destroy(comm_finished);
550
551   return found_comm;
552 }