Logo AND Algorithmique Numérique Distribuée

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